Furbish code and comments

Add `TODO` comment to optimize waiting times, fix capitalization in
comments, add blank line and break up long lines. To improve code
semantics `lcd_home()` and `lcd_setup()` were renamed to `draw_home()`
and `draw_setup()` respectively. Improve ordering of function calls in
`main()`.
This commit is contained in:
finga 2021-09-23 22:52:54 +02:00
parent 2579ae3686
commit 3aa458c4bc

View file

@ -187,6 +187,7 @@ static void lcd_write(const uint8_t data) {
} }
static void lcd_init(void) { static void lcd_init(void) {
// TODO: Optimize waiting times
SPI_DDR |= (1 << LCD_CD) | (1 << LCD_RST); SPI_DDR |= (1 << LCD_CD) | (1 << LCD_RST);
_delay_ms(1); _delay_ms(1);
SPI_PORT |= (1 << LCD_RST); SPI_PORT |= (1 << LCD_RST);
@ -221,12 +222,12 @@ static void lcd_update_backlight(void) {
case 3: case 3:
case 4: case 4:
case 5: case 5:
TCCR0B = 0x0C; // prescaler = 256; TCCR0B = 0x0C; // Prescaler = 256;
DDRD |= (1 << PD5); DDRD |= (1 << PD5);
OCR0B = value_backlight - 1; OCR0B = value_backlight - 1;
break; break;
default: default:
TCCR0B = 0x0B; // prescaler = 64; TCCR0B = 0x0B; // Prescaler = 64;
DDRD |= (1 << PD5); DDRD |= (1 << PD5);
OCR0B = value_backlight - 4; OCR0B = value_backlight - 4;
break; break;
@ -301,6 +302,7 @@ static void lcd_write_integer_page(const uint8_t integer,
uint16_t comperator = 1; uint16_t comperator = 1;
for (; comperator <= integer; comperator *= 10, input_digits++); for (; comperator <= integer; comperator *= 10, input_digits++);
for (int8_t i = digits - input_digits; i > 0; i--) { for (int8_t i = digits - input_digits; i > 0; i--) {
lcd_write_kerning(2, invert); lcd_write_kerning(2, invert);
lcd_write_digit_page(0, page, invert); lcd_write_digit_page(0, page, invert);
@ -424,7 +426,7 @@ static void twi_write_register(const uint8_t address,
twi_stop(); twi_stop();
} }
static void lcd_home(void) { static void draw_home(void) {
lcd_fill(0x00); lcd_fill(0x00);
bool ch1_selected = false; bool ch1_selected = false;
@ -466,7 +468,7 @@ static void lcd_home(void) {
} }
} }
static void lcd_setup(void) { static void draw_setup(void) {
lcd_fill(0x00); lcd_fill(0x00);
bool contrast_selected = false; bool contrast_selected = false;
@ -505,7 +507,8 @@ static void lcd_setup(void) {
lcd_write_string_page("CONTRAST:\0", i, contrast_selected); lcd_write_string_page("CONTRAST:\0", i, contrast_selected);
lcd_write_kerning(2, contrast_selected); lcd_write_kerning(2, contrast_selected);
lcd_write_kerning(16, false); lcd_write_kerning(16, false);
lcd_write_integer_page(value_contrast, 2, i, change_contrast_selected); lcd_write_integer_page(value_contrast, 2, i,
change_contrast_selected);
lcd_write_kerning(2, change_contrast_selected); lcd_write_kerning(2, change_contrast_selected);
} }
@ -514,7 +517,8 @@ static void lcd_setup(void) {
lcd_write_string_page("BACKLIGHT:\0", i, backlight_selected); lcd_write_string_page("BACKLIGHT:\0", i, backlight_selected);
lcd_write_kerning(2, backlight_selected); lcd_write_kerning(2, backlight_selected);
lcd_write_kerning(5, false); lcd_write_kerning(5, false);
lcd_write_integer_page(value_backlight, 3, i, change_backlight_selected); lcd_write_integer_page(value_backlight, 3, i,
change_backlight_selected);
lcd_write_kerning(2, change_backlight_selected); lcd_write_kerning(2, change_backlight_selected);
} }
@ -528,7 +532,7 @@ static void lcd_setup(void) {
static void change_state(const enum state new_state) { static void change_state(const enum state new_state) {
switch (new_state) { switch (new_state) {
case home: case home:
lcd_home(); draw_home();
current_state = home; current_state = home;
break; break;
case ch1: case ch1:
@ -539,7 +543,7 @@ static void change_state(const enum state new_state) {
break; break;
case setup: case setup:
setup_state = contrast; setup_state = contrast;
lcd_setup(); draw_setup();
current_state = setup; current_state = setup;
break; break;
} }
@ -551,13 +555,13 @@ static void update_home(const enum input event) {
home_state++; home_state++;
if (home_state > setup) if (home_state > setup)
home_state = ch1; home_state = ch1;
lcd_home(); draw_home();
break; break;
case ccw: case ccw:
home_state--; home_state--;
if (home_state > setup) if (home_state > setup)
home_state = setup; home_state = setup;
lcd_home(); draw_home();
break; break;
case click: case click:
change_state(home_state); change_state(home_state);
@ -591,7 +595,7 @@ static void update_setup(const enum input event) {
} }
break; break;
} }
lcd_setup(); draw_setup();
break; break;
case ccw: case ccw:
switch (setup_state) { switch (setup_state) {
@ -615,17 +619,17 @@ static void update_setup(const enum input event) {
} }
break; break;
} }
lcd_setup(); draw_setup();
break; break;
case click: case click:
switch (setup_state) { switch (setup_state) {
case contrast: case contrast:
setup_state = change_contrast; setup_state = change_contrast;
lcd_setup(); draw_setup();
break; break;
case backlight: case backlight:
setup_state = change_backlight; setup_state = change_backlight;
lcd_setup(); draw_setup();
break; break;
case back: case back:
change_state(home); change_state(home);
@ -633,12 +637,12 @@ static void update_setup(const enum input event) {
case change_contrast: case change_contrast:
eeprom_update_byte(&eeprom_contrast, value_contrast); eeprom_update_byte(&eeprom_contrast, value_contrast);
setup_state = contrast; setup_state = contrast;
lcd_setup(); draw_setup();
break; break;
case change_backlight: case change_backlight:
eeprom_update_byte(&eeprom_backlight, value_backlight); eeprom_update_byte(&eeprom_backlight, value_backlight);
setup_state = backlight; setup_state = backlight;
lcd_setup(); draw_setup();
break; break;
} }
break; break;
@ -653,13 +657,13 @@ static void update_setup(const enum input event) {
setup_state = contrast; setup_state = contrast;
value_contrast = eeprom_read_byte(&eeprom_contrast); value_contrast = eeprom_read_byte(&eeprom_contrast);
lcd_update_contrast(); lcd_update_contrast();
lcd_setup(); draw_setup();
break; break;
case change_backlight: case change_backlight:
setup_state = backlight; setup_state = backlight;
value_backlight = eeprom_read_byte(&eeprom_backlight); value_backlight = eeprom_read_byte(&eeprom_backlight);
lcd_update_backlight(); lcd_update_backlight();
lcd_setup(); draw_setup();
break; break;
} }
break; break;
@ -726,7 +730,7 @@ ISR(PCINT0_vect) {
sei(); sei();
} }
// encoder button interrupt // Encoder button interrupt
ISR(PCINT1_vect) { ISR(PCINT1_vect) {
cli(); cli();
@ -762,13 +766,12 @@ int main(void) {
value_contrast = eeprom_read_byte(&eeprom_contrast); value_contrast = eeprom_read_byte(&eeprom_contrast);
value_backlight = eeprom_read_byte(&eeprom_backlight); value_backlight = eeprom_read_byte(&eeprom_backlight);
// Init backlight: FastPWM: 1.25kHz // Init backlight
// TODO: Try to get the backlit even more dim
TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1); TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
OCR0A = 255; OCR0A = 255;
lcd_update_backlight(); lcd_update_backlight();
// SPI setup // SPI and LCD init
spi_init(); spi_init();
lcd_init(); lcd_init();
@ -786,13 +789,12 @@ int main(void) {
OCR1A = 65535; OCR1A = 65535;
TIMSK1 |= (1 << OCIE1A); // Enable match compare A TIMSK1 |= (1 << OCIE1A); // Enable match compare A
// Show splash screen and load the menu // Show splash screen
lcd_splash(); lcd_splash();
_delay_ms(2000); _delay_ms(2000);
change_state(current_state);
// Enable interrupts // Load the menu
sei(); change_state(current_state);
// Set TWI bit rate to 200kHz // Set TWI bit rate to 200kHz
TWBR = 12; TWBR = 12;
@ -800,6 +802,9 @@ int main(void) {
(void) &twi_read_register; (void) &twi_read_register;
(void) &twi_write_register; (void) &twi_write_register;
// Enable interrupts
sei();
// Run... // Run...
for (;;); for (;;);
} }