Get content from sensor.

This commit is contained in:
unlockable
2023-09-07 14:27:43 +08:00
parent 23e8cc8333
commit a0b1c3c055

View File

@@ -118,13 +118,12 @@ public:
} }
static void read_data_from_sensor_interrupt() { static void read_data_from_sensor_interrupt() {
// Serial.println("Updated!");
while (Serial1.available()) { while (Serial1.available()) {
WitSerialDataIn(Serial.read()); WitSerialDataIn(Serial1.read());
} }
if (SensorReader::s_cDataUpdate) { if (SensorReader::s_cDataUpdate) {
if (SensorReader::s_cDataUpdate & ANGLE_UPDATE) { if (SensorReader::s_cDataUpdate & ANGLE_UPDATE) {
Serial.println("Updated!");
SensorReader::angle.x = sReg[Roll] / 32768.0f * 180.0f; SensorReader::angle.x = sReg[Roll] / 32768.0f * 180.0f;
SensorReader::angle.y = sReg[Roll + 1] / 32768.0f * 180.0f; SensorReader::angle.y = sReg[Roll + 1] / 32768.0f * 180.0f;
SensorReader::angle.z = SensorReader::angle.z =
@@ -146,10 +145,50 @@ class Cube {
private: private:
static int layer_count; static int layer_count;
static int brightness_count; static int brightness_count;
static bool blinking_LED_status;
public: public:
// Every int represents a row of 8 LED status. // Every int represents a row of 8 LED status.
static int LED_status[8][8]; static uint16_t LED_status[8][8];
static uint16_t LED_brightness[8][8];
static uint8_t LED_blinking_status[8][8];
static void set_blinking(int x, int y, int z) {
// 1 = enable blinking
if (x >= 8 || x < 0 || y >= 8 || y < 0 || z >= 8 || z < 0) {
return;
}
LED_blinking_status[z][x] = LED_blinking_status[z][x] | (1 << y);
}
static void unset_blinking(int x, int y, int z) {
if (x >= 8 || x < 0 || y >= 8 || y < 0 || z >= 8 || z < 0) {
return;
}
LED_blinking_status[z][x] = LED_blinking_status[z][x] & (~1 << y);
LED_status[z][x] = (LED_status[z][x] & (~(3 << (y * 2)))) |
(LED_brightness[z][x] & (3 << (y * 2)));
}
static void do_blinking() {
blinking_LED_status ^= 1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
if (LED_blinking_status[i][j] >> k & 1) {
if (blinking_LED_status) {
LED_status[i][j] = LED_brightness[i][j];
}
else {
LED_status[i][j] =
LED_status[i][j] & (~(3 << (k * 2)));
}
}
}
}
}
}
static void display() { static void display() {
// Serial.println("Here"); // Serial.println("Here");
@@ -183,19 +222,53 @@ public:
brightness %= 4; brightness %= 4;
LED_status[z][x] = LED_status[z][x] =
(LED_status[z][x] & (~(3 << (y * 2)))) | (brightness << (y * 2)); (LED_status[z][x] & (~(3 << (y * 2)))) | (brightness << (y * 2));
LED_brightness[z][x] = (LED_brightness[z][x] & (~(3 << (y * 2)))) |
(brightness << (y * 2));
} }
static int get_status(int x, int y, int z) { static int get_status(int x, int y, int z) {
return LED_status[z][x] >> (y * 2) & 3; return LED_brightness[z][x] >> (y * 2) & 3;
} }
static void clear() { static void clear() {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
LED_brightness[i][j] = 0;
LED_status[i][j] = 0; LED_status[i][j] = 0;
} }
} }
} }
static void draw_line(int x, int y, int z, int length, int direction,
int brightness) {
if (x >= 8 || x < 0 || y >= 8 || y < 0 || z >= 8 || z < 0) {
return;
}
if (direction >= 3 || direction < 0 || length <= 0 || brightness >= 4 ||
brightness < 0) {
return;
}
// 0: x
// 1: y
// 2: z
switch (direction) {
case 0:
for (int i = 0; i < length; i++) {
set_status(x + i, y, z, brightness);
}
break;
case 1:
for (int i = 0; i < length; i++) {
set_status(x, y + i, z, brightness);
}
break;
case 2:
for (int i = 0; i < length; i++) {
set_status(x, y, z + i, brightness);
}
break;
}
}
}; };
enum Operators { enum Operators {
@@ -555,7 +628,10 @@ public:
int Cube::layer_count = 0; int Cube::layer_count = 0;
int Cube::brightness_count = 0; int Cube::brightness_count = 0;
int Cube::LED_status[8][8] = {0}; uint16_t Cube::LED_status[8][8] = {0};
uint16_t Cube::LED_brightness[8][8] = {0};
uint8_t Cube::LED_blinking_status[8][8] = {0};
bool Cube::blinking_LED_status = false;
double *Symbol::x_value_ptr, *Symbol::y_value_ptr; double *Symbol::x_value_ptr, *Symbol::y_value_ptr;
@@ -585,6 +661,41 @@ void setup() {
} }
Timer1.attachInterrupt(Cube::display); Timer1.attachInterrupt(Cube::display);
cli();
TCCR4A = 0; // set entire TCCR1A register to 0
TCCR4B = 0; // same for TCCR1B
TCNT4 = 0; // initialize counter value to 0
// set compare match register for 1hz increments
OCR4A = 7812 / 1; // = (16*10^6) / (1*1024) - 1 (must be <65536)
// 15625 = 1 sec
// turn on CTC mode
TCCR4B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR4B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK4 |= (1 << OCIE4A);
TCCR5A = 0;
TCCR5B = 0;
TCNT5 = 0;
OCR5A = 10000 / 1;
TCCR5B |= (1 << WGM12);
TCCR5B |= (1 << CS12) | (1 << CS10);
TIMSK5 |= (1 << OCIE5A);
sei();
Cube::set_status(1, 1, 1, 3);
Cube::set_status(1, 1, 2, 3);
Cube::set_blinking(1, 1, 2);
}
ISR(TIMER4_COMPA_vect) {
Cube::do_blinking();
}
ISR(TIMER5_COMPA_vect) {
SensorReader::read_data_from_sensor_interrupt();
} }
void loop() { void loop() {
@@ -598,7 +709,7 @@ void loop() {
Serial.print("z: "); Serial.print("z: ");
Serial.println(angle.z); Serial.println(angle.z);
delay(500); delay(1500);
// String input = ""; // String input = "";
// Serial.println("Input expression"); // Serial.println("Input expression");