Removed dead code. Fixed ms volatility to be local. Misc fixes
This commit is contained in:
parent
1cd774cc87
commit
d0125ef276
322
code/main.c
322
code/main.c
@ -42,13 +42,6 @@
|
||||
#define OLED_X_SIZE 128
|
||||
#define OLED_Y_SIZE 64
|
||||
|
||||
#define OLED_BATTERY_ICON_X1 (OLED_X_SIZE - (CHAR_SIZE + 1) * 2 * 2)
|
||||
#define OLED_BATTERY_ICON_X2 (OLED_X_SIZE - 1)
|
||||
#define OLED_BATTERY_ICON_Y1 2
|
||||
#define OLED_BATTERY_ICON_Y2 ((OLED_Y_SIZE / 8) - 1)
|
||||
#define OLED_BATTERY_ICON_SIZE 2
|
||||
#define OLED_BATTERY_ICON_PERCENT 5
|
||||
|
||||
// Time(ms) to keep the display on before sleep
|
||||
#define DISPLAY_DELAY 10000
|
||||
|
||||
@ -110,7 +103,7 @@ uint8_t event_count = 0;
|
||||
uint8_t *event_read = events;
|
||||
uint8_t *event_write = events;
|
||||
|
||||
volatile uint32_t ms = 0;
|
||||
uint32_t ms = 0;
|
||||
|
||||
#ifdef ROT_REVERSE
|
||||
const uint8_t PROGMEM rot_table[16] =
|
||||
@ -166,31 +159,46 @@ const uint8_t PROGMEM unfold_table[16] =
|
||||
0xAA
|
||||
};
|
||||
|
||||
#define CHAR_0 0
|
||||
#define CHAR_A 10
|
||||
#define CHAR_B 11
|
||||
#define CHAR_C 12
|
||||
#define CHAR_D 13
|
||||
#define CHAR_V 14
|
||||
#define CHAR_a 15
|
||||
#define CHAR_c 16
|
||||
#define CHAR_d 17
|
||||
#define CHAR_e 18
|
||||
#define CHAR_h 19
|
||||
#define CHAR_j 20
|
||||
#define CHAR_m 21
|
||||
#define CHAR_r 22
|
||||
#define CHAR_s 23
|
||||
#define CHAR_t 24
|
||||
#define CHAR_u 25
|
||||
#define CHAR_y 26
|
||||
#define CHAR_dot 27
|
||||
#define CHAR_lt 28
|
||||
#define CHAR_gt 29
|
||||
#define CHAR_deg 30
|
||||
enum charmap
|
||||
{
|
||||
CHAR_0 = 0,
|
||||
CHAR_1,
|
||||
CHAR_2,
|
||||
CHAR_3,
|
||||
CHAR_4,
|
||||
CHAR_5,
|
||||
CHAR_6,
|
||||
CHAR_7,
|
||||
CHAR_8,
|
||||
CHAR_9,
|
||||
CHAR_A,
|
||||
CHAR_B,
|
||||
CHAR_C,
|
||||
CHAR_D,
|
||||
CHAR_V,
|
||||
CHAR_a,
|
||||
CHAR_c,
|
||||
CHAR_d,
|
||||
CHAR_e,
|
||||
CHAR_h,
|
||||
CHAR_j,
|
||||
CHAR_m,
|
||||
CHAR_r,
|
||||
CHAR_s,
|
||||
CHAR_t,
|
||||
CHAR_u,
|
||||
CHAR_y,
|
||||
// CHAR_plus,
|
||||
CHAR_minus,
|
||||
CHAR_dot,
|
||||
// CHAR_lt,
|
||||
CHAR_gt,
|
||||
CHAR_deg,
|
||||
CHAR_MAX
|
||||
};
|
||||
|
||||
// Bottom -> Top (In Byte); Left -> Right (In Row)
|
||||
const uint8_t PROGMEM symbols[31 * CHAR_SIZE] =
|
||||
const uint8_t PROGMEM symbols[CHAR_MAX * CHAR_SIZE] =
|
||||
{
|
||||
0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
|
||||
0x00, 0x42, 0x7F, 0x40, 0x00, // 1
|
||||
@ -219,8 +227,10 @@ const uint8_t PROGMEM symbols[31 * CHAR_SIZE] =
|
||||
0x04, 0x3F, 0x44, 0x40, 0x20, // t
|
||||
0x3C, 0x40, 0x40, 0x20, 0x7C, // u
|
||||
0x0C, 0x50, 0x50, 0x50, 0x3C, // y
|
||||
// 0x10, 0x10, 0x7C, 0x10, 0x10, // plus
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, // minus
|
||||
0x00, 0x60, 0x60, 0x00, 0x00, // .
|
||||
0x00, 0x08, 0x14, 0x22, 0x41, // <
|
||||
// 0x00, 0x08, 0x14, 0x22, 0x41, // <
|
||||
0x41, 0x22, 0x14, 0x08, 0x00, // >
|
||||
0x0E, 0x11, 0x11, 0x0E, 0x00 // deg
|
||||
};
|
||||
@ -312,8 +322,8 @@ void print_menu(uint8_t force_draw);
|
||||
// Prints the option in the correct place
|
||||
void print_option(uint8_t option);
|
||||
|
||||
// Prints the battery icon depending on battery percentage
|
||||
void print_battery_icon();
|
||||
// Prints the battery percentage
|
||||
void print_battery_percent();
|
||||
|
||||
// Prints battery statistics in menu area
|
||||
void print_battery_stat(uint8_t force_draw);
|
||||
@ -551,7 +561,7 @@ int main()
|
||||
{
|
||||
print_mm();
|
||||
print_direction();
|
||||
print_battery_icon();
|
||||
print_battery_percent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -675,6 +685,7 @@ void display_set_area(uint8_t start_x, uint8_t end_x, uint8_t start_y, uint8_t e
|
||||
end_y = start_y;
|
||||
}
|
||||
#endif
|
||||
|
||||
cmd_list[1] = start_x;
|
||||
cmd_list[2] = end_x;
|
||||
cmd_list[4] = start_y;
|
||||
@ -894,23 +905,6 @@ void print_mm()
|
||||
print_symbol(symbol, x, y, 0);
|
||||
}
|
||||
|
||||
#if 0
|
||||
// Print 'mm' (only after clear)
|
||||
if ((0xFF == old_highlight) && (0xFF != count_highlight))
|
||||
{
|
||||
print_symbol(CHAR_m, x, y, 0);
|
||||
x += (CHAR_SIZE + 1) * 2;
|
||||
print_symbol(CHAR_m, x, y, 0);
|
||||
}
|
||||
|
||||
// Clear 'mm'
|
||||
if (0xFF == count_highlight)
|
||||
{
|
||||
print_symbol(0xFF, x, y, 0);
|
||||
x += (CHAR_SIZE + 1) * 2;
|
||||
print_symbol(0xFF, x, y, 0);
|
||||
}
|
||||
#endif
|
||||
old_highlight = count_highlight;
|
||||
}
|
||||
|
||||
@ -1018,9 +1012,8 @@ void print_option(uint8_t option)
|
||||
}
|
||||
}
|
||||
|
||||
void print_battery_icon()
|
||||
void print_battery_percent()
|
||||
{
|
||||
// Battery Percent
|
||||
#if 0
|
||||
static uint8_t old_symbols[3] = {0xFF, 0xFF, 0xFF};
|
||||
uint32_t percent = BATTERY_CAPACITY_MAH;
|
||||
@ -1029,214 +1022,65 @@ void print_battery_icon()
|
||||
uint8_t is_first = 1;
|
||||
|
||||
// Calculate percent
|
||||
percent -= battery_mAh;
|
||||
// battery_mAh is always negative
|
||||
percent += battery_mAh;
|
||||
percent *= 100;
|
||||
percent /= BATTERY_CAPACITY_MAH;
|
||||
|
||||
if (percent > 100)
|
||||
{
|
||||
percent = 100;
|
||||
}
|
||||
|
||||
// Print percent top to bot
|
||||
for (i = 3; i > 0; --i)
|
||||
{
|
||||
symbol = extract_digit(percent, i);
|
||||
if ((0 == symbol) && (is_first) && (i != 1))
|
||||
if ((is_first) && (0 == symbol) && (i != 1))
|
||||
{
|
||||
symbol = 0xFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
is_first = 0;
|
||||
}
|
||||
|
||||
// 3,2,1 -> 1,2,3
|
||||
if (symbol != old_symbols[3 - i + 1])
|
||||
// 3,2,1 -> 0,1,2 / 1,2,3
|
||||
if (symbol != old_symbols[3 - i])
|
||||
{
|
||||
print_symbol(symbol, OLED_X_SIZE - (CHAR_SIZE + 1) * 2, 3 - i + 1, 0);
|
||||
old_symbols[3 - i + 1] = symbol;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// EEPROM IDX
|
||||
#if 0
|
||||
uint32_t idx = (uint32_t) eeprom_idx;
|
||||
|
||||
if (eeprom_idx >= 0)
|
||||
{
|
||||
print_symbol(extract_digit(idx, 2), OLED_X_SIZE - (CHAR_SIZE + 1) * 2, 1, 0);
|
||||
print_symbol(extract_digit(idx, 1), OLED_X_SIZE - (CHAR_SIZE + 1) * 2, 2, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Graphic Icon
|
||||
#if 0
|
||||
static uint8_t old_percent = 0xFF;
|
||||
uint8_t buf[BUFF_SIZE];
|
||||
uint8_t i;
|
||||
uint8_t y;
|
||||
uint8_t percent;
|
||||
uint8_t threshold;
|
||||
uint8_t bits;
|
||||
uint8_t mask;
|
||||
|
||||
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 > 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)
|
||||
{
|
||||
// Draw left border
|
||||
display_set_area(OLED_BATTERY_ICON_X1, OLED_BATTERY_ICON_X1 + OLED_BATTERY_ICON_SIZE - 1,
|
||||
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);
|
||||
i2c_stop();
|
||||
|
||||
// Draw right border
|
||||
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);
|
||||
i2c_stop();
|
||||
|
||||
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);
|
||||
i2c_stop();
|
||||
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);
|
||||
i2c_stop();
|
||||
|
||||
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);
|
||||
i2c_stop();
|
||||
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);
|
||||
i2c_stop();
|
||||
}
|
||||
|
||||
// TODO: Calc battery percent
|
||||
percent = 50;
|
||||
|
||||
// Do not draw if not needed
|
||||
if ((old_percent / OLED_BATTERY_ICON_PERCENT) == (percent / OLED_BATTERY_ICON_PERCENT))
|
||||
{
|
||||
old_percent = percent;
|
||||
return;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
mask = 0x00;
|
||||
|
||||
for (i = 0; i < 8 / OLED_BATTERY_ICON_SIZE; ++i)
|
||||
{
|
||||
mask >>= OLED_BATTERY_ICON_SIZE;
|
||||
if (percent > threshold)
|
||||
{
|
||||
mask |= bits;
|
||||
}
|
||||
|
||||
threshold -= OLED_BATTERY_ICON_PERCENT;
|
||||
}
|
||||
|
||||
// Add top border
|
||||
if (OLED_BATTERY_ICON_Y1 == y)
|
||||
{
|
||||
mask |= (bits >> (8 - OLED_BATTERY_ICON_SIZE));
|
||||
}
|
||||
|
||||
// Add bottom border
|
||||
if (OLED_BATTERY_ICON_Y2 == y)
|
||||
{
|
||||
mask |= bits;
|
||||
}
|
||||
|
||||
// Fill buffer
|
||||
for (i = 0; i < sizeof(buf); ++i)
|
||||
{
|
||||
buf[i] = mask;
|
||||
}
|
||||
|
||||
// Diplay percent
|
||||
display_set_area(OLED_BATTERY_ICON_X1 + OLED_BATTERY_ICON_SIZE * 2,
|
||||
OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_SIZE * 2,
|
||||
y, y);
|
||||
display_send_data(buf, OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_X1 -
|
||||
OLED_BATTERY_ICON_SIZE * 4 + 1);
|
||||
i2c_stop();
|
||||
}
|
||||
|
||||
// Buffer safety
|
||||
#if ((OLED_BATTERY_ICON_X2 - OLED_BATTERY_ICON_X1 - OLED_BATTERY_ICON_SIZE * 4 + 1) > BUFF_SIZE)
|
||||
#error "THIS X OVERFLOWS"
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void print_battery_stat(uint8_t force_draw)
|
||||
{
|
||||
uint8_t symbols[MENU_STRLEN] = {0xFF, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF};
|
||||
int16_t old_mAh = 0xFFFF;
|
||||
int16_t old_mV = 0xFFFF;
|
||||
int16_t old_temp = 0xFFFF;
|
||||
uint8_t symbols[MENU_STRLEN] = {0xFF, 0, 0, 0, 0, CHAR_m, CHAR_A, CHAR_h};
|
||||
static int16_t old_mAh = 0xFFFF;
|
||||
static int16_t old_mV = 0xFFFF;
|
||||
static int16_t old_temp = 0xFFFF;
|
||||
|
||||
if ((old_mAh != battery_mAh) || force_draw)
|
||||
if (force_draw || (old_mAh != battery_mAh))
|
||||
{
|
||||
symbols[5] = CHAR_m;
|
||||
symbols[6] = CHAR_A;
|
||||
symbols[7] = CHAR_h;
|
||||
|
||||
print_battery_single_stat(1, battery_mAh, symbols, 5);
|
||||
print_battery_single_stat(1, BATTERY_CAPACITY_MAH + battery_mAh, symbols, 5);
|
||||
old_mAh = battery_mAh;
|
||||
}
|
||||
|
||||
if ((old_mV != battery_mV) || force_draw)
|
||||
symbols[6] = CHAR_V;
|
||||
symbols[7] = 0xFF;
|
||||
if (force_draw || (old_mV != battery_mV))
|
||||
{
|
||||
symbols[5] = CHAR_m;
|
||||
symbols[6] = CHAR_V;
|
||||
symbols[7] = 0xFF;
|
||||
|
||||
print_battery_single_stat(2, battery_mV, symbols, 5);
|
||||
old_mV = battery_mV;
|
||||
}
|
||||
|
||||
if ((old_temp != battery_temp) || force_draw)
|
||||
symbols[5] = CHAR_deg;
|
||||
symbols[6] = CHAR_C;
|
||||
if (force_draw || (old_temp != battery_temp))
|
||||
{
|
||||
symbols[5] = CHAR_deg;
|
||||
symbols[6] = CHAR_C;
|
||||
symbols[7] = 0xFF;
|
||||
|
||||
print_battery_single_stat(3, battery_temp, symbols, 3);
|
||||
old_temp = battery_temp;
|
||||
}
|
||||
@ -1251,12 +1095,12 @@ void print_battery_single_stat(uint8_t y, int16_t bat_stat, uint8_t *symbols, ui
|
||||
// Show +/-
|
||||
if (bat_stat < 0)
|
||||
{
|
||||
symbols[0] = CHAR_lt;
|
||||
symbols[0] = CHAR_minus;
|
||||
stat = -stat;
|
||||
}
|
||||
else
|
||||
{
|
||||
symbols[0] = CHAR_gt;
|
||||
symbols[0] = 0xFF;
|
||||
}
|
||||
|
||||
// Extract 4 digits
|
||||
@ -1361,6 +1205,8 @@ int8_t write_eeprom_val(int8_t idx, uint32_t value, uint8_t direction)
|
||||
int8_t err;
|
||||
uint8_t buf[4];
|
||||
uint32_t write_complete_when;
|
||||
// This prevents infinite loop when waiting for write to complete
|
||||
volatile uint32_t *ms_ptr = &ms;
|
||||
|
||||
if (idx < 0)
|
||||
{
|
||||
@ -1382,8 +1228,8 @@ int8_t write_eeprom_val(int8_t idx, uint32_t value, uint8_t direction)
|
||||
}
|
||||
|
||||
// Wait for write to complete
|
||||
write_complete_when = ms + 20;
|
||||
while (ms < write_complete_when);
|
||||
write_complete_when = (*ms_ptr) + 20;
|
||||
while ((*ms_ptr) < write_complete_when);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1439,6 +1285,14 @@ int8_t read_battery(int16_t *mAh, int16_t *mV, int16_t *temp)
|
||||
// Transform into mAh
|
||||
val *= BATTERY_CHARGE_MULT;
|
||||
val /= BATTERY_CHARGE_DIV;
|
||||
|
||||
// Battery percent is calculated via negative mAh
|
||||
if (val > 0)
|
||||
{
|
||||
reset_battery();
|
||||
val = 0;
|
||||
}
|
||||
|
||||
(*mAh) = val;
|
||||
|
||||
// Read mV bits
|
||||
@ -1927,7 +1781,7 @@ void process_battery()
|
||||
{
|
||||
// Button Clicked - Update Battery Info
|
||||
read_battery(&battery_mAh, &battery_mV, &battery_temp);
|
||||
print_battery_icon();
|
||||
print_battery_percent();
|
||||
print_battery_stat(0);
|
||||
}
|
||||
long_press_when_ms = 0xFFFFFFFF;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user