From 81a163782040697dc252aefc6d9b790099b73350 Mon Sep 17 00:00:00 2001 From: nedko Date: Fri, 4 Nov 2022 20:22:10 +0200 Subject: [PATCH] Fixed Battery Icon Print. Fixed Warnings --- code/main.c | 130 ++++++++++++++++++++++++++++------------------------ 1 file changed, 70 insertions(+), 60 deletions(-) diff --git a/code/main.c b/code/main.c index 14dfab3..8612990 100644 --- a/code/main.c +++ b/code/main.c @@ -31,6 +31,7 @@ #define BATTERY_TEMP_DIV 100 #define CHAR_SIZE 5 +#define BUFF_SIZE 16 #define OLED_ADDRESS (0x3C << 1) // #define OLED_ADDRESS (0x3D << 1) @@ -384,7 +385,7 @@ ISR(PCINT0_vect) // Check button inputs uint8_t btn_pin_state = PINA & 0x2F; uint8_t btn_event = EVENT_NONE; - uint8_t btn_state; + uint8_t btn_state = btn_state_current; uint8_t btn_idx; cli(); @@ -511,8 +512,13 @@ int main() read_battery(&battery_mAh, &battery_mV, &battery_temp); // Attempt to attach spool on initial startup - attach_detach_spool(); + // attach_detach_spool(); + display_enable(1); + print_mm(); + print_direction(); + print_battery_icon(); + while (1); state = STATE_IDLE; sei(); @@ -662,10 +668,10 @@ void display_set_area(uint8_t start_x, uint8_t end_x, uint8_t start_y, uint8_t e end_y = start_y; } - cmd[1] = start_x; - cmd[2] = end_x; - cmd[4] = start_y; - cmd[5] = end_y; + cmd_list[1] = start_x; + cmd_list[2] = end_x; + cmd_list[4] = start_y; + cmd_list[5] = end_y; for (i = 0; i < sizeof(cmd_list); ++i) { @@ -676,7 +682,7 @@ void display_set_area(uint8_t start_x, uint8_t end_x, uint8_t start_y, uint8_t e void display_init() { uint16_t i; - uint8_t buf[16]; + uint8_t buf[BUFF_SIZE]; const uint8_t cmd_list[] = { @@ -900,7 +906,8 @@ void print_direction() print_symbol(0xFF, OLED_X_SIZE - (CHAR_SIZE + 1) * 2 * 2, 0, 0); print_symbol(0xFF, OLED_X_SIZE - (CHAR_SIZE + 1) * 2, 0, 0); } - else + + if (0xFF != dir_highlight) { // Print '>' only when display is comming from clear if (0xFF == old_highlight) @@ -920,42 +927,15 @@ void print_direction() void print_clear_menu_area() { - uint16_t i; - uint16_t count; - uint8_t bytes; - uint8_t data[16]; - uint8_t cmd_list[6] = + uint8_t x; + uint8_t y; + + for (y = 1; y < (OLED_Y_SIZE / 8) / 2; ++y) { - 0x21, // Set Column Address - 0x00, // Start Address - 0x00, // End Address - 0x22, // Set Page Address - 0x02, // Start Page 2 - Skip Top Row Where Spool Data Is - 0x00 // End Page - }; - - // End X - Do Not Clear Battery Symbol - cmd_list[2] = OLED_X_SIZE - (CHAR_SIZE + 1) * 2 * 2 - 1; - - // End Y - Depends On OLED Size - cmd_list[5] = (OLED_Y_SIZE / 8) - 1; - - display_set_area(0, OLED_BATTERY_ICON_X1 - 1, 2, (OLED_Y_SIZE / 8) - 1); - - for (i = 0; i < sizeof(data); ++i) - { - data[i] = 0x00; - } - - count = (cmd_list[2] + 1) * (cmd_list[5] - cmd_list[4] + 1); - - i = 0; - while (i < count) - { - // If remaining bytes is smaller than buffer - send only required number - bytes = (count - i) < sizeof(data) ? count - i : sizeof(data); - display_send_data(data, bytes); - i += bytes; + for (x = 0; x < 8; ++x) + { + print_symbol(0xFF, x * (CHAR_SIZE + 1) * 2, y, 0); + } } } @@ -1006,7 +986,7 @@ void print_option(uint8_t option) void print_battery_icon() { static uint8_t old_percent = 0xFF; - uint8_t buf[16]; + uint8_t buf[BUFF_SIZE]; uint8_t i; uint8_t y; uint8_t percent; @@ -1014,16 +994,24 @@ void print_battery_icon() uint8_t bits; uint8_t mask; - for (i = 0; i < sizeof(buf), ++i) + for (i = 0; i < sizeof(buf); ++i) { buf[i] = 0xFF; } // Buffer safety -#if ((OLED_BATTERY_ICON_Y2 - OLED_BATTERY_ICON_Y1 + 1) * OLED_BATTERY_ICON_SIZE > sizeof(buf)) +#if ((OLED_BATTERY_ICON_Y2 - OLED_BATTERY_ICON_Y1 + 1) * OLED_BATTERY_ICON_SIZE > BUFF_SIZE) #error "THIS Y OVERFLOWS" #endif + // Set bitmask + bits = 0x00; + for (i = 0; i < OLED_BATTERY_ICON_SIZE; ++i) + { + bits >>= 1; + bits |= 0x80; + } + // Draw left/right borders only once if (0xFF == old_percent) { @@ -1033,9 +1021,39 @@ void print_battery_icon() display_send_data(buf, (OLED_BATTERY_ICON_Y2 - OLED_BATTERY_ICON_Y1 + 1) * OLED_BATTERY_ICON_SIZE); // Draw right border - display_set_area(OLED_BATTERY_ICON_X2 - 1, OLED_BATTERY_ICON_X2, + display_set_area(OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_SIZE + 1, OLED_BATTERY_ICON_X2, OLED_BATTERY_ICON_Y1, OLED_BATTERY_ICON_Y2); display_send_data(buf, (OLED_BATTERY_ICON_Y2 - OLED_BATTERY_ICON_Y1 + 1) * OLED_BATTERY_ICON_SIZE); + + for (i = 0; i < sizeof(buf); ++i) + { + buf[i] = (bits >> (8 - OLED_BATTERY_ICON_SIZE)); + } + + // Draw top extra border + display_set_area(OLED_BATTERY_ICON_X1 + OLED_BATTERY_ICON_SIZE, + OLED_BATTERY_ICON_X1 + OLED_BATTERY_ICON_SIZE * 2 - 1, + OLED_BATTERY_ICON_Y1, OLED_BATTERY_ICON_Y1); + display_send_data(buf, OLED_BATTERY_ICON_SIZE); + display_set_area(OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_SIZE * 2 + 1, + OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_SIZE, + OLED_BATTERY_ICON_Y1, OLED_BATTERY_ICON_Y1); + display_send_data(buf, OLED_BATTERY_ICON_SIZE); + + for (i = 0; i < sizeof(buf); ++i) + { + buf[i] = bits; + } + + // Draw bot extra border + display_set_area(OLED_BATTERY_ICON_X1 + OLED_BATTERY_ICON_SIZE, + OLED_BATTERY_ICON_X1 + OLED_BATTERY_ICON_SIZE * 2 - 1, + OLED_BATTERY_ICON_Y2, OLED_BATTERY_ICON_Y2); + display_send_data(buf, OLED_BATTERY_ICON_SIZE); + display_set_area(OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_SIZE * 2 + 1, + OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_SIZE, + OLED_BATTERY_ICON_Y2, OLED_BATTERY_ICON_Y2); + display_send_data(buf, OLED_BATTERY_ICON_SIZE); } // TODO: Calc battery percent @@ -1048,25 +1066,17 @@ void print_battery_icon() return; } - // Set bitmask - bits = 0x00; - for (i = 0; i < OLED_BATTERY_ICON_SIZE; ++i) - { - bits <<= 1; - bits += 1; - } - // Threshold is artificially bigger to not draw over border // On lower border it underflows threshold = 100 - ((OLED_BATTERY_ICON_PERCENT + 1) / 2) + OLED_BATTERY_ICON_PERCENT * 2; - for (y = OLED_BATTERY_ICON_Y1, y <= OLED_BATTERY_ICON_Y2; ++y) + for (y = OLED_BATTERY_ICON_Y1; y <= OLED_BATTERY_ICON_Y2; ++y) { mask = 0x00; for (i = 0; i < 8 / OLED_BATTERY_ICON_SIZE; ++i) { - mask <<= OLED_BATTERY_ICON_SIZE; + mask >>= OLED_BATTERY_ICON_SIZE; if (percent > threshold) { mask |= bits; @@ -1076,9 +1086,9 @@ void print_battery_icon() } // Add top border - if (0 == y) + if (OLED_BATTERY_ICON_Y1 == y) { - mask |= (bits << (8 - OLED_BATTERY_ICON_SIZE)); + mask |= (bits >> (8 - OLED_BATTERY_ICON_SIZE)); } // Add bottom border @@ -1102,7 +1112,7 @@ void print_battery_icon() } // Buffer safety -#if ((OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_X1 - OLED_BATTERY_ICON_SIZE * 4 + 1) > sizeof(buf)) +#if ((OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_X1 - OLED_BATTERY_ICON_SIZE * 4 + 1) > BUFF_SIZE) #error "THIS X OVERFLOWS" #endif