Fixed Battery Icon Print. Fixed Warnings

This commit is contained in:
nedko 2022-11-04 20:22:10 +02:00
parent fe846e4cce
commit 81a1637820

View File

@ -31,6 +31,7 @@
#define BATTERY_TEMP_DIV 100 #define BATTERY_TEMP_DIV 100
#define CHAR_SIZE 5 #define CHAR_SIZE 5
#define BUFF_SIZE 16
#define OLED_ADDRESS (0x3C << 1) #define OLED_ADDRESS (0x3C << 1)
// #define OLED_ADDRESS (0x3D << 1) // #define OLED_ADDRESS (0x3D << 1)
@ -384,7 +385,7 @@ ISR(PCINT0_vect)
// Check button inputs // Check button inputs
uint8_t btn_pin_state = PINA & 0x2F; uint8_t btn_pin_state = PINA & 0x2F;
uint8_t btn_event = EVENT_NONE; uint8_t btn_event = EVENT_NONE;
uint8_t btn_state; uint8_t btn_state = btn_state_current;
uint8_t btn_idx; uint8_t btn_idx;
cli(); cli();
@ -511,8 +512,13 @@ int main()
read_battery(&battery_mAh, &battery_mV, &battery_temp); read_battery(&battery_mAh, &battery_mV, &battery_temp);
// Attempt to attach spool on initial startup // 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; state = STATE_IDLE;
sei(); 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; end_y = start_y;
} }
cmd[1] = start_x; cmd_list[1] = start_x;
cmd[2] = end_x; cmd_list[2] = end_x;
cmd[4] = start_y; cmd_list[4] = start_y;
cmd[5] = end_y; cmd_list[5] = end_y;
for (i = 0; i < sizeof(cmd_list); ++i) 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() void display_init()
{ {
uint16_t i; uint16_t i;
uint8_t buf[16]; uint8_t buf[BUFF_SIZE];
const uint8_t cmd_list[] = 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 * 2, 0, 0);
print_symbol(0xFF, OLED_X_SIZE - (CHAR_SIZE + 1) * 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 // Print '>' only when display is comming from clear
if (0xFF == old_highlight) if (0xFF == old_highlight)
@ -920,42 +927,15 @@ void print_direction()
void print_clear_menu_area() void print_clear_menu_area()
{ {
uint16_t i; uint8_t x;
uint16_t count; uint8_t y;
uint8_t bytes;
uint8_t data[16]; for (y = 1; y < (OLED_Y_SIZE / 8) / 2; ++y)
uint8_t cmd_list[6] =
{ {
0x21, // Set Column Address for (x = 0; x < 8; ++x)
0x00, // Start Address {
0x00, // End Address print_symbol(0xFF, x * (CHAR_SIZE + 1) * 2, y, 0);
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;
} }
} }
@ -1006,7 +986,7 @@ void print_option(uint8_t option)
void print_battery_icon() void print_battery_icon()
{ {
static uint8_t old_percent = 0xFF; static uint8_t old_percent = 0xFF;
uint8_t buf[16]; uint8_t buf[BUFF_SIZE];
uint8_t i; uint8_t i;
uint8_t y; uint8_t y;
uint8_t percent; uint8_t percent;
@ -1014,16 +994,24 @@ void print_battery_icon()
uint8_t bits; uint8_t bits;
uint8_t mask; uint8_t mask;
for (i = 0; i < sizeof(buf), ++i) for (i = 0; i < sizeof(buf); ++i)
{ {
buf[i] = 0xFF; buf[i] = 0xFF;
} }
// Buffer safety // 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" #error "THIS Y OVERFLOWS"
#endif #endif
// Set bitmask
bits = 0x00;
for (i = 0; i < OLED_BATTERY_ICON_SIZE; ++i)
{
bits >>= 1;
bits |= 0x80;
}
// Draw left/right borders only once // Draw left/right borders only once
if (0xFF == old_percent) 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); display_send_data(buf, (OLED_BATTERY_ICON_Y2 - OLED_BATTERY_ICON_Y1 + 1) * OLED_BATTERY_ICON_SIZE);
// Draw right border // 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); 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); 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 // TODO: Calc battery percent
@ -1048,25 +1066,17 @@ void print_battery_icon()
return; 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 // Threshold is artificially bigger to not draw over border
// On lower border it underflows // On lower border it underflows
threshold = 100 - ((OLED_BATTERY_ICON_PERCENT + 1) / 2) + OLED_BATTERY_ICON_PERCENT * 2; 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; mask = 0x00;
for (i = 0; i < 8 / OLED_BATTERY_ICON_SIZE; ++i) for (i = 0; i < 8 / OLED_BATTERY_ICON_SIZE; ++i)
{ {
mask <<= OLED_BATTERY_ICON_SIZE; mask >>= OLED_BATTERY_ICON_SIZE;
if (percent > threshold) if (percent > threshold)
{ {
mask |= bits; mask |= bits;
@ -1076,9 +1086,9 @@ void print_battery_icon()
} }
// Add top border // 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 // Add bottom border
@ -1102,7 +1112,7 @@ void print_battery_icon()
} }
// Buffer safety // 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" #error "THIS X OVERFLOWS"
#endif #endif