From d75d23054107de6191e68c2502fa33e70e6ed846 Mon Sep 17 00:00:00 2001 From: finga Date: Sun, 12 Sep 2021 00:59:22 +0200 Subject: [PATCH] Implement contrast and backlight handling The default values for the contrast and backlight are set when writing to eeprom. Use the values stored in eeprom for contrast and backlight. Update the contrast and backlight when changing their values. --- firmware/src/main.c | 56 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/firmware/src/main.c b/firmware/src/main.c index 95fd530..b15be21 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -16,8 +16,8 @@ #define ENC_A (PINB & (1 << PB6)) #define ENC_B (PINB & (1 << PB7)) -uint8_t EEMEM eeprom_contrast = 0; -uint8_t EEMEM eeprom_backlight = 0; +uint8_t EEMEM eeprom_contrast = 8; +uint8_t EEMEM eeprom_backlight = 1; static const uint8_t sacred_chao[200] = { 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x03, 0xC3, 0xE3, 0x73, 0x37, 0x17, 0x07, 0x0F, 0x1E, 0x3C, 0xF0, 0x80, @@ -137,10 +137,29 @@ void lcd_init(void) { lcd_write(0x2F); // (5) Set Power Control: Booster, Regulator and Follower on lcd_write(0x27); // (8) Set VLCD Resistor Ratio: Set Contrast lcd_write(0x81); // (9) Set Electronic Volume: Set Contrast - lcd_write(0x10); // (9) Set Electronic Volume: Set Contrast + lcd_write(value_contrast); // (9) Set Electronic Volume: Set Contrast lcd_write(0xAF); // (12) Set Display Enable: Display on } +void lcd_update_contrast(void) { + SPI_PORT &= ~(1 << LCD_CD); + lcd_write(0x81); // (9) Set Electronic Volume: Set Contrast + lcd_write(value_contrast); // (9) Set Electronic Volume: Set Contrast + SPI_PORT |= (1 << LCD_CD); +} + +void lcd_update_backlight(void) { + switch (value_backlight) { + case 0: + DDRD &= (0 << PD5); + break; + default: + DDRD |= (1 << PD5); + OCR0B = value_backlight - 1; + break; + } +} + void lcd_fill(uint8_t data) { for (uint8_t i = 0; i < 8; i++) { SPI_PORT &= ~(1 << LCD_CD); @@ -517,10 +536,16 @@ static void update_setup(enum input event) { setup_state = contrast; break; case change_contrast: - value_contrast++; + if (value_contrast < 63) { + value_contrast++; + lcd_update_contrast(); + } break; case change_backlight: - value_backlight++; + if (value_backlight < 100) { + value_backlight++; + lcd_update_backlight(); + } break; } lcd_setup(); @@ -535,10 +560,16 @@ static void update_setup(enum input event) { setup_state = back; break; case change_contrast: - value_contrast--; + if (value_contrast > 0) { + value_contrast--; + lcd_update_contrast(); + } break; case change_backlight: - value_backlight--; + if (value_backlight > 0) { + value_backlight--; + lcd_update_backlight(); + } break; } lcd_setup(); @@ -578,11 +609,13 @@ static void update_setup(enum input event) { case change_contrast: setup_state = contrast; value_contrast = eeprom_read_byte(&eeprom_contrast); + lcd_update_contrast(); lcd_setup(); break; case change_backlight: setup_state = backlight; value_backlight = eeprom_read_byte(&eeprom_backlight); + lcd_update_backlight(); lcd_setup(); break; } @@ -688,13 +721,12 @@ int main(void) { value_contrast = eeprom_read_byte(&eeprom_contrast); value_backlight = eeprom_read_byte(&eeprom_backlight); - // FastPWM: 1.25kHz + // Init backlight: FastPWM: 1.25kHz // TODO: Try to get the backlit even more dim - TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1); - TCCR0B = (1 << CS01) | (1 << WGM02); // prescaler = 8; + TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1); + TCCR0B |= (1 << CS01) | (1 << WGM02); // prescaler = 8; OCR0A = 100; - OCR0B = 0; - DDRD |= (1 << PD5); + lcd_update_backlight(); // SPI setup spi_init();