Compare commits

...

3 commits

Author SHA1 Message Date
a5d88e879c Furbish code and comment
Remove unneeded brackets and improve comment.
2021-09-12 01:05:33 +02:00
14dd301657 Fix kerning in setup screen for backlight
This fixes the inverting of the first two pixel on the setup screen
when the backlight setting is selected.
2021-09-12 01:04:10 +02:00
d75d230541 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.
2021-09-12 00:59:22 +02:00

View file

@ -16,8 +16,8 @@
#define ENC_A (PINB & (1 << PB6)) #define ENC_A (PINB & (1 << PB6))
#define ENC_B (PINB & (1 << PB7)) #define ENC_B (PINB & (1 << PB7))
uint8_t EEMEM eeprom_contrast = 0; uint8_t EEMEM eeprom_contrast = 8;
uint8_t EEMEM eeprom_backlight = 0; 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, 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, 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(0x2F); // (5) Set Power Control: Booster, Regulator and Follower on
lcd_write(0x27); // (8) Set VLCD Resistor Ratio: Set Contrast lcd_write(0x27); // (8) Set VLCD Resistor Ratio: Set Contrast
lcd_write(0x81); // (9) Set Electronic Volume: 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 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) { void lcd_fill(uint8_t data) {
for (uint8_t i = 0; i < 8; i++) { for (uint8_t i = 0; i < 8; i++) {
SPI_PORT &= ~(1 << LCD_CD); SPI_PORT &= ~(1 << LCD_CD);
@ -407,18 +426,19 @@ static void lcd_setup(void) {
lcd_write_kerning(2, contrast_selected); lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_colon, i, contrast_selected); lcd_write_symbol_page(&sym_colon, i, contrast_selected);
lcd_write_kerning(2, contrast_selected); lcd_write_kerning(2, contrast_selected);
lcd_write_kerning(9, false); lcd_write_kerning(16, false);
lcd_write_integer_page(value_contrast, 3, 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);
} }
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i = 0; i < 2; i++) {
SPI_PORT &= ~(1 << LCD_CD); SPI_PORT &= ~(1 << LCD_CD);
lcd_write(0x02); lcd_write(0x00);
lcd_write(0x10); lcd_write(0x10);
lcd_write(0xB4 + i); lcd_write(0xB4 + i);
SPI_PORT |= (1 << LCD_CD); SPI_PORT |= (1 << LCD_CD);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_b, i, backlight_selected); lcd_write_symbol_page(&sym_b, i, backlight_selected);
lcd_write_kerning(2, backlight_selected); lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_a, i, backlight_selected); lcd_write_symbol_page(&sym_a, i, backlight_selected);
@ -517,10 +537,16 @@ static void update_setup(enum input event) {
setup_state = contrast; setup_state = contrast;
break; break;
case change_contrast: case change_contrast:
value_contrast++; if (value_contrast < 63) {
value_contrast++;
lcd_update_contrast();
}
break; break;
case change_backlight: case change_backlight:
value_backlight++; if (value_backlight < 100) {
value_backlight++;
lcd_update_backlight();
}
break; break;
} }
lcd_setup(); lcd_setup();
@ -535,10 +561,16 @@ static void update_setup(enum input event) {
setup_state = back; setup_state = back;
break; break;
case change_contrast: case change_contrast:
value_contrast--; if (value_contrast > 0) {
value_contrast--;
lcd_update_contrast();
}
break; break;
case change_backlight: case change_backlight:
value_backlight--; if (value_backlight > 0) {
value_backlight--;
lcd_update_backlight();
}
break; break;
} }
lcd_setup(); lcd_setup();
@ -578,11 +610,13 @@ static void update_setup(enum input event) {
case change_contrast: case change_contrast:
setup_state = contrast; setup_state = contrast;
value_contrast = eeprom_read_byte(&eeprom_contrast); value_contrast = eeprom_read_byte(&eeprom_contrast);
lcd_update_contrast();
lcd_setup(); lcd_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_setup(); lcd_setup();
break; break;
} }
@ -661,9 +695,8 @@ ISR(PCINT1_vect) {
TCNT1 = 0; TCNT1 = 0;
} }
} else { // press } else { // press
if (!(TCCR1B & (1 << CS10))) { if (!(TCCR1B & (1 << CS10)))
TCCR1B |= (1 << CS11) | (1 << CS10); // Enable Timer/Counter1 TCCR1B |= (1 << CS11) | (1 << CS10); // Enable Timer/Counter1
}
} }
// TODO: proper debouncing // TODO: proper debouncing
@ -672,7 +705,7 @@ ISR(PCINT1_vect) {
sei(); sei();
} }
// Timer/Counter1 compare match A // Timer/Counter1 compare match A for back event
ISR(TIMER1_COMPA_vect) { ISR(TIMER1_COMPA_vect) {
cli(); cli();
@ -688,13 +721,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);
// FastPWM: 1.25kHz // Init backlight: FastPWM: 1.25kHz
// TODO: Try to get the backlit even more dim // TODO: Try to get the backlit even more dim
TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1); TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
TCCR0B = (1 << CS01) | (1 << WGM02); // prescaler = 8; TCCR0B |= (1 << CS01) | (1 << WGM02); // prescaler = 8;
OCR0A = 100; OCR0A = 100;
OCR0B = 0; lcd_update_backlight();
DDRD |= (1 << PD5);
// SPI setup // SPI setup
spi_init(); spi_init();