Fixed most functionality

This commit is contained in:
nedko 2022-11-05 03:27:13 +02:00
parent b5cfd4d4ce
commit 19d0ddfb91
2 changed files with 66 additions and 28 deletions

1
TODO
View File

@ -1 +1,2 @@
Fix EEPROM detection (and maybe read / write)
Battery icon - how to calc percentage?

View File

@ -49,7 +49,7 @@
#define OLED_BATTERY_ICON_PERCENT 5
// Time(ms) to keep the display on before sleep
#define DISPLAY_DELAY 3000
#define DISPLAY_DELAY 10000
// Time(ms) to assume long button press
#define LONG_PRESS 500
@ -226,10 +226,10 @@ const uint8_t PROGMEM symbols[31 * CHAR_SIZE] =
const char PROGMEM menu_strings[(MENU_COUNT + 1) * MENU_STRLEN] =
{
CHAR_A, CHAR_t, CHAR_t, CHAR_a, CHAR_c, CHAR_h, 0xFF, 0xFF,
CHAR_A, CHAR_d, CHAR_j, CHAR_u, CHAR_s, CHAR_t, 0xFF, 0xFF,
CHAR_B, CHAR_a, CHAR_t, CHAR_t, CHAR_e, CHAR_r, CHAR_y, 0xFF,
CHAR_D, CHAR_e, CHAR_t, CHAR_a, CHAR_c, CHAR_h, 0xFF, 0xFF
0xFF, CHAR_A, CHAR_t, CHAR_t, CHAR_a, CHAR_c, CHAR_h, 0xFF,
0xFF, CHAR_A, CHAR_d, CHAR_j, CHAR_u, CHAR_s, CHAR_t, 0xFF,
0xFF, CHAR_B, CHAR_a, CHAR_t, CHAR_t, CHAR_e, CHAR_r, CHAR_y,
0xFF, CHAR_D, CHAR_e, CHAR_t, CHAR_a, CHAR_c, CHAR_h, 0xFF
};
// 1 / (4 * ROT_PULSE_COUNT) * (2 * pi * ROT_WHEEL_RAD)
@ -372,7 +372,7 @@ void set_adjust_spool();
void set_battery_state();
// Updates the ms counter
ISR(TIM1_OVF_vect)
ISR(TIM1_CAPT_vect)
{
cli();
ms += 10;
@ -425,11 +425,6 @@ ISR(PCINT0_vect)
case 0x23:
btn_state = BTN_DOWN14;
break;
// This should never happen -> Button is broken
default:
btn_state = btn_state_current;
break;
}
btn_idx = btn_state_current * 6 + btn_state;
@ -467,6 +462,14 @@ ISR(PCINT0_vect)
}
}
// Actually put the event in the queue
if (btn_event != EVENT_NONE)
{
*event_write = btn_event;
++event_count;
PTR_INC(event_write);
}
old_btn_pin_state = btn_pin_state;
btn_state_current = btn_state;
if ((btn_state != BTN_UP5) && (btn_state != BTN_DOWN5))
@ -557,6 +560,12 @@ int main()
void simple_init()
{
// Pull-ups on button
PORTA = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 5);
// Pull-ups on rotary encoder
PORTB = (1 << 0) | (1 << 1);
// No Prescaler - 1 MHz
// Set Mode to CTC with ICR1
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
@ -565,8 +574,8 @@ void simple_init()
// Interrupt every 10ms
ICR1 = 10000;
// Enable Interrupt on overflow
TIMSK1 = (1 << TOIE1);
// Enable Interrupt on capture
TIMSK1 = (1 << ICIE1);
// Interrupt on Up/Down/Click pins
PCMSK0 = (1 << PCINT0) | (1 << PCINT1) | (1 << PCINT2) | (1 << PCINT3) | (1 << PCINT5);
@ -637,6 +646,7 @@ void display_set_area(uint8_t start_x, uint8_t end_x, uint8_t start_y, uint8_t e
};
uint8_t i;
#if 0
if (start_x >= OLED_X_SIZE)
{
start_x = OLED_X_SIZE - 1;
@ -666,7 +676,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;
@ -941,9 +951,10 @@ void print_clear_menu_area()
void print_menu(uint8_t force_draw)
{
static uint8_t old_option = 0xFF;
static uint8_t old_spool = 0;
uint8_t i;
if (force_draw || (old_option != menu_option))
if (force_draw || (old_option != menu_option) || (old_option == 0 && (old_spool != spool_attached)))
{
for (i = 0; i < MENU_COUNT; ++i)
{
@ -953,19 +964,22 @@ void print_menu(uint8_t force_draw)
}
}
}
old_option = menu_option;
old_spool = spool_attached;
}
void print_option(uint8_t option)
{
uint8_t invert = 0;
uint8_t x = 0;
uint8_t select = 0xFF;
uint8_t x = (CHAR_SIZE + 1) * 2;
uint8_t y = 1 + option;
uint8_t i;
// Whether to invert or not
if (option == menu_option)
{
invert = 1;
select = CHAR_gt;
}
// Whether to display "Attach" or "Detach"
@ -974,16 +988,29 @@ void print_option(uint8_t option)
option += MENU_COUNT;
}
for (i = 0; i < MENU_STRLEN; ++i)
print_symbol(select, 0, y ,0);
for (i = 1; i < MENU_STRLEN; ++i)
{
// Get the symbol index from the menu_strings
print_symbol(pgm_read_byte(&(menu_strings[option * MENU_STRLEN + i])), x, y, invert);
print_symbol(pgm_read_byte(&(menu_strings[option * MENU_STRLEN + i])), x, y, 0);
x += (CHAR_SIZE + 1) * 2;
}
}
void print_battery_icon()
{
#if 1
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
#if 0
static uint8_t old_percent = 0xFF;
uint8_t buf[BUFF_SIZE];
uint8_t i;
@ -1114,7 +1141,7 @@ void print_battery_icon()
#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)
@ -1130,7 +1157,7 @@ void print_battery_stat(uint8_t force_draw)
symbols[6] = CHAR_A;
symbols[7] = CHAR_h;
print_battery_single_stat(1, battery_mAh, symbols, 0xFF);
print_battery_single_stat(1, battery_mAh, symbols, 5);
old_mAh = battery_mAh;
}
@ -1140,7 +1167,7 @@ void print_battery_stat(uint8_t force_draw)
symbols[6] = CHAR_V;
symbols[7] = 0xFF;
print_battery_single_stat(1, battery_mV, symbols, 0xFF);
print_battery_single_stat(2, battery_mV, symbols, 5);
old_mV = battery_mV;
}
@ -1150,7 +1177,7 @@ void print_battery_stat(uint8_t force_draw)
symbols[6] = CHAR_C;
symbols[7] = 0xFF;
print_battery_single_stat(1, battery_temp, symbols, 3);
print_battery_single_stat(3, battery_temp, symbols, 3);
old_temp = battery_temp;
}
}
@ -1210,8 +1237,8 @@ int16_t find_eeprom_idx()
uint8_t err;
int16_t idx = 0;
uint8_t found = 0;
uint8_t dir;
uint32_t value = 0xFFFFFFFF;
uint8_t dir = 0xFF;
uint32_t value = 0x00FFFFFF;
// FF is the erased byte value
for (idx = 0; idx < EEPROM_SIZE / EEPROM_IDX_SIZE; ++idx)
@ -1222,7 +1249,8 @@ int16_t find_eeprom_idx()
return -1;
}
if (value != 0xFFFFFFFF)
// if ((value != 0x00FFFFFF) && (dir != 0xFF))
if (value != 0x00FFFFFF)
{
found = 1;
break;
@ -1682,6 +1710,11 @@ void process_menu()
menu_option %= MENU_COUNT;
break;
}
if (state != STATE_MENU)
{
break;
}
}
// Hold button to exit menu
@ -1792,13 +1825,16 @@ void process_adjust()
write_eeprom_val(eeprom_idx, 0xFFFFFFFF, 0xFF);
++eeprom_idx;
eeprom_idx %= EEPROM_SIZE / EEPROM_IDX_SIZE;
write_eeprom_val(eeprom_idx, (uint32_t) count_value, rot_dir_is_A);
write_eeprom_val(eeprom_idx, count_value, rot_dir_is_A);
// Go back to idle
state = STATE_IDLE;
long_press_when_ms = 0xFFFFFFFF;
sleep_when_ms = ms + DISPLAY_DELAY;
spool_counting = 1;
count_highlight = 0;
dir_highlight = 0;
}
}
@ -1924,5 +1960,6 @@ void set_battery_state()
{
print_clear_menu_area();
state = STATE_BATTERY;
read_battery(&battery_mAh, &battery_mV, &battery_temp);
print_battery_stat(1);
}