2021-09-11 15:44:11 +02:00
|
|
|
#include <avr/eeprom.h>
|
2021-02-18 22:00:24 +01:00
|
|
|
#include <avr/io.h>
|
2021-03-01 16:46:40 +01:00
|
|
|
#include <avr/interrupt.h>
|
2021-02-18 22:00:24 +01:00
|
|
|
#include <util/delay.h>
|
2021-09-16 22:02:00 +02:00
|
|
|
#include <util/twi.h>
|
2021-09-14 21:23:38 +02:00
|
|
|
#include <assert.h>
|
2021-02-18 22:00:24 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2021-02-27 02:26:57 +01:00
|
|
|
#define SPI_PORT PORTB
|
|
|
|
#define SPI_DDR DDRB
|
|
|
|
#define SPI_SCK PB5
|
|
|
|
#define SPI_MOSI PB3
|
|
|
|
#define SPI_SS PB2
|
|
|
|
|
2021-02-27 18:01:09 +01:00
|
|
|
#define LCD_CD PB0
|
2021-02-27 02:26:57 +01:00
|
|
|
#define LCD_RST PB1
|
|
|
|
|
2021-03-01 16:46:40 +01:00
|
|
|
#define ENC_A (PINB & (1 << PB6))
|
|
|
|
#define ENC_B (PINB & (1 << PB7))
|
|
|
|
|
2021-09-16 22:02:00 +02:00
|
|
|
#define TWI_PORT PORTC
|
|
|
|
#define TWI_SDA PC4
|
|
|
|
#define TWI_SCL PC5
|
|
|
|
|
2021-09-24 12:25:47 +02:00
|
|
|
#define SI5351_ADDRESS 0x60
|
2021-09-16 22:02:00 +02:00
|
|
|
|
2021-09-12 22:01:05 +02:00
|
|
|
#define SYM_ENTRY(SYM) { SYM, sizeof(SYM) / 2 }
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static uint8_t EEMEM eeprom_contrast = 8;
|
|
|
|
static uint8_t EEMEM eeprom_backlight = 1;
|
2021-09-11 15:44:11 +02:00
|
|
|
|
2021-09-12 22:01:05 +02:00
|
|
|
static const __flash uint8_t sacred_chao[] = { 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,
|
|
|
|
0xFF, 0xFF, 0xFF, 0xE7, 0xC3, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0x30, 0x00, 0x00, 0xFF,
|
|
|
|
0x01, 0x0F, 0x3F, 0x4F, 0x9F, 0x3F, 0x3E, 0x3C, 0x7C, 0x7C, 0x7C, 0x7C, 0x3E, 0x3E, 0x3E, 0x1F, 0x1F, 0x0F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x07, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x07, 0x07, 0x03, 0x81, 0x40, 0x30, 0x0E, 0x01,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x08, 0x10, 0x20, 0x20, 0x40, 0x40, 0x40, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x40, 0x40, 0x40, 0x20, 0x20, 0x10, 0x08, 0x08, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
|
|
|
static const __flash uint8_t onders_org[] = { 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x60, 0xF8, 0x00, 0x00, 0xE0, 0xA0, 0xE0, 0x00, 0x00, 0xE0, 0x20, 0x60, 0x00, 0x00, 0xE0, 0xA0, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x20, 0x60, 0x00, 0x00, 0xE0, 0x60, 0xE0,
|
|
|
|
0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x0F};
|
|
|
|
|
|
|
|
static const __flash uint8_t sym_0[] = { 0xF8, 0xFC, 0x0C, 0xFC, 0xF8,
|
|
|
|
0x1F, 0x3F, 0x30, 0x3F, 0x1F };
|
|
|
|
static const __flash uint8_t sym_1[] = { 0x30, 0x30, 0xFC, 0xFC, 0x00,
|
|
|
|
0x30, 0x30, 0x3F, 0x3F, 0x30 };
|
|
|
|
static const __flash uint8_t sym_2[] = { 0x18, 0x1C, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x38, 0x3E, 0x3F, 0x33, 0x30 };
|
|
|
|
static const __flash uint8_t sym_3[] = { 0x18, 0x9C, 0x8C, 0xFC, 0x78,
|
|
|
|
0x18, 0x39, 0x31, 0x3F, 0x1E };
|
|
|
|
static const __flash uint8_t sym_4[] = { 0x80, 0xE0, 0x78, 0xFC, 0xFC,
|
|
|
|
0x07, 0x07, 0x06, 0x3F, 0x3F };
|
|
|
|
static const __flash uint8_t sym_5[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0x0C,
|
|
|
|
0x1C, 0x3D, 0x31, 0x3F, 0x1F };
|
|
|
|
static const __flash uint8_t sym_6[] = { 0xF8, 0xFC, 0x8C, 0xBC, 0x38,
|
|
|
|
0x1F, 0x3F, 0x31, 0x3F, 0x1F };
|
|
|
|
static const __flash uint8_t sym_7[] = { 0x0C, 0x0C, 0xEC, 0xFC, 0x1C,
|
|
|
|
0x00, 0x3E, 0x3F, 0x01, 0x00 };
|
|
|
|
static const __flash uint8_t sym_8[] = { 0x78, 0xFC, 0x8C, 0xFC, 0x78,
|
|
|
|
0x1E, 0x3F, 0x31, 0x3F, 0x1E };
|
|
|
|
static const __flash uint8_t sym_9[] = { 0xF8, 0xFC, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x1C, 0x3D, 0x31, 0x3F, 0x1F };
|
|
|
|
|
|
|
|
static const __flash uint8_t sym_colon[] = { 0x30, 0x30,
|
|
|
|
0x0C, 0x0C };
|
|
|
|
|
|
|
|
static const __flash uint8_t sym_a[] = { 0xC0, 0xF0, 0x3C, 0x3C, 0xF0, 0xC0,
|
|
|
|
0x3F, 0x3F, 0x06, 0x06, 0x3F, 0x3F };
|
|
|
|
static const __flash uint8_t sym_b[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0x78,
|
|
|
|
0x3F, 0x3F, 0x31, 0x31, 0x3F, 0x1E };
|
|
|
|
static const __flash uint8_t sym_c[] = { 0xF8, 0xFC, 0x0C, 0x1C, 0x18,
|
|
|
|
0x1F, 0x3F, 0x30, 0x38, 0x18 };
|
|
|
|
static const __flash uint8_t sym_e[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0x0C,
|
|
|
|
0x3F, 0x3F, 0x31, 0x31, 0x30 };
|
|
|
|
static const __flash uint8_t sym_g[] = { 0xF8, 0xFC, 0x0C, 0x0C, 0x3C, 0x38,
|
|
|
|
0x1F, 0x3F, 0x30, 0x33, 0x3F, 0x1F };
|
|
|
|
static const __flash uint8_t sym_h[] = { 0xFC, 0xFC, 0x80, 0x80, 0xFC, 0xFC,
|
|
|
|
0x3F, 0x3F, 0x01, 0x01, 0x3F, 0x3F };
|
|
|
|
static const __flash uint8_t sym_i[] = { 0x0C, 0xFC, 0xFC, 0x0C,
|
|
|
|
0x30, 0x3F, 0x3F, 0x30 };
|
|
|
|
static const __flash uint8_t sym_k[] = { 0xFC, 0xFC, 0xC0, 0xF0, 0x7C, 0x1C,
|
|
|
|
0x3F, 0x3F, 0x03, 0x0F, 0x3E, 0x38 };
|
|
|
|
static const __flash uint8_t sym_l[] = { 0xFC, 0xFC, 0x00, 0x00, 0x00,
|
|
|
|
0x3F, 0x3F, 0x30, 0x30, 0x30 };
|
|
|
|
static const __flash uint8_t sym_n[] = { 0xFC, 0xFC, 0xF0, 0xC0, 0x00, 0xFC, 0xFC,
|
|
|
|
0x3F, 0x3F, 0x00, 0x03, 0x0F, 0x3F, 0x3F };
|
|
|
|
static const __flash uint8_t sym_o[] = { 0xF8, 0xFC, 0x0C, 0x0C, 0xFC, 0xF8,
|
|
|
|
0x1F, 0x3F, 0x30, 0x30, 0x3F, 0x1F };
|
|
|
|
static const __flash uint8_t sym_p[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x3F, 0x3F, 0x01, 0x01, 0x01, 0x00 };
|
|
|
|
static const __flash uint8_t sym_r[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x3F, 0x3F, 0x01, 0x03, 0x3F, 0x3E };
|
|
|
|
static const __flash uint8_t sym_s[] = { 0xF8, 0xFC, 0x8C, 0x8C, 0x9C, 0x18,
|
|
|
|
0x18, 0x39, 0x31, 0x31, 0x3F, 0x1F };
|
|
|
|
static const __flash uint8_t sym_t[] = { 0x0C, 0x0C, 0xFC, 0xFC, 0x0C, 0x0C,
|
|
|
|
0x00, 0x00, 0x3F, 0x3F, 0x00, 0x00 };
|
|
|
|
static const __flash uint8_t sym_u[] = { 0xFC, 0xFC, 0x00, 0x00, 0xFC, 0xFC,
|
|
|
|
0x3F, 0x3F, 0x30, 0x30, 0x3F, 0x3F };
|
|
|
|
|
2021-09-23 22:45:46 +02:00
|
|
|
static const __flash uint8_t sym_underscore[] = { 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x20, 0x20, 0x20, 0x20, 0x20 };
|
|
|
|
|
2021-09-21 19:43:22 +02:00
|
|
|
static const __flash uint8_t sym_invalid[] = { 0x80, 0xE0, 0x98, 0xCC, 0x4C, 0x18, 0xE0, 0x80,
|
|
|
|
0x01, 0x07, 0x1F, 0x24, 0x25, 0x1F, 0x07, 0x01};
|
2021-02-27 14:49:06 +01:00
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
struct symbol {
|
2021-09-12 22:01:05 +02:00
|
|
|
const __flash uint8_t* data;
|
|
|
|
const uint8_t length;
|
2021-09-08 14:53:29 +02:00
|
|
|
};
|
|
|
|
|
2021-09-12 22:01:05 +02:00
|
|
|
static const struct symbol symbol_table[] = { SYM_ENTRY(sym_0),
|
|
|
|
SYM_ENTRY(sym_1),
|
|
|
|
SYM_ENTRY(sym_2),
|
|
|
|
SYM_ENTRY(sym_3),
|
|
|
|
SYM_ENTRY(sym_4),
|
|
|
|
SYM_ENTRY(sym_5),
|
|
|
|
SYM_ENTRY(sym_6),
|
|
|
|
SYM_ENTRY(sym_7),
|
|
|
|
SYM_ENTRY(sym_8),
|
|
|
|
SYM_ENTRY(sym_9),
|
|
|
|
SYM_ENTRY(sym_colon),
|
2021-09-21 19:43:22 +02:00
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
2021-09-12 22:01:05 +02:00
|
|
|
SYM_ENTRY(sym_a),
|
|
|
|
SYM_ENTRY(sym_b),
|
|
|
|
SYM_ENTRY(sym_c),
|
2021-09-21 19:43:22 +02:00
|
|
|
SYM_ENTRY(sym_invalid),
|
2021-09-12 22:01:05 +02:00
|
|
|
SYM_ENTRY(sym_e),
|
2021-09-21 19:43:22 +02:00
|
|
|
SYM_ENTRY(sym_invalid),
|
2021-09-12 22:01:05 +02:00
|
|
|
SYM_ENTRY(sym_g),
|
|
|
|
SYM_ENTRY(sym_h),
|
|
|
|
SYM_ENTRY(sym_i),
|
2021-09-21 19:43:22 +02:00
|
|
|
SYM_ENTRY(sym_invalid),
|
2021-09-12 22:01:05 +02:00
|
|
|
SYM_ENTRY(sym_k),
|
|
|
|
SYM_ENTRY(sym_l),
|
2021-09-21 19:43:22 +02:00
|
|
|
SYM_ENTRY(sym_invalid),
|
2021-09-12 22:01:05 +02:00
|
|
|
SYM_ENTRY(sym_n),
|
|
|
|
SYM_ENTRY(sym_o),
|
|
|
|
SYM_ENTRY(sym_p),
|
2021-09-21 19:43:22 +02:00
|
|
|
SYM_ENTRY(sym_invalid),
|
2021-09-12 22:01:05 +02:00
|
|
|
SYM_ENTRY(sym_r),
|
|
|
|
SYM_ENTRY(sym_s),
|
|
|
|
SYM_ENTRY(sym_t),
|
2021-09-23 22:45:46 +02:00
|
|
|
SYM_ENTRY(sym_u),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_invalid),
|
|
|
|
SYM_ENTRY(sym_underscore),
|
|
|
|
};
|
2021-09-08 14:53:29 +02:00
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
enum input { cw, ccw, click, hold };
|
2021-09-08 14:53:29 +02:00
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static volatile enum state { home, ch1, ch2, ch3, setup } current_state = home;
|
2021-09-09 20:40:50 +02:00
|
|
|
static volatile enum state home_state = ch1;
|
2021-09-12 15:37:01 +02:00
|
|
|
static volatile enum setup_state { contrast, backlight, back, change_contrast, change_backlight } setup_state = contrast;
|
2021-09-08 14:53:29 +02:00
|
|
|
|
2021-09-09 20:40:50 +02:00
|
|
|
static volatile uint8_t enc = 0;
|
2021-03-01 16:46:40 +01:00
|
|
|
|
2021-09-11 15:36:02 +02:00
|
|
|
static volatile uint8_t value_contrast;
|
|
|
|
static volatile uint8_t value_backlight;
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void spi_init(void) {
|
2021-02-27 02:26:57 +01:00
|
|
|
SPI_DDR |= (1 << SPI_SCK) | (1 << SPI_MOSI) | (1 << SPI_SS);
|
|
|
|
SPI_PORT |= (1 << SPI_SS);
|
|
|
|
SPCR = (1 << SPE) | (1 << MSTR);
|
|
|
|
SPSR = (1 << SPI2X);
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static uint8_t spi_byte(const uint8_t data) {
|
2021-09-11 00:21:01 +02:00
|
|
|
SPDR = data;
|
|
|
|
while(!(SPSR & (1 << SPIF)));
|
|
|
|
return SPDR;
|
|
|
|
}
|
2021-02-27 02:26:57 +01:00
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void lcd_write(const uint8_t data) {
|
2021-02-27 02:26:57 +01:00
|
|
|
SPI_PORT &= ~(1 << SPI_SS);
|
|
|
|
spi_byte(data);
|
|
|
|
SPI_PORT |= (1 << SPI_SS);
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void lcd_init(void) {
|
2021-09-23 22:52:54 +02:00
|
|
|
// TODO: Optimize waiting times
|
2021-02-27 02:26:57 +01:00
|
|
|
SPI_DDR |= (1 << LCD_CD) | (1 << LCD_RST);
|
|
|
|
_delay_ms(1);
|
|
|
|
SPI_PORT |= (1 << LCD_RST);
|
|
|
|
_delay_ms(5);
|
|
|
|
|
|
|
|
lcd_write(0x40); // (6) Set Scroll Line: Display start line 0
|
|
|
|
lcd_write(0xA1); // (13) Set SEG direction: SEG reverse
|
|
|
|
lcd_write(0xC0); // (14) Set COM direction: Normal COM0 - COM63
|
|
|
|
lcd_write(0xA6); // (11) Set Inverse Display: Display inverse off
|
|
|
|
lcd_write(0xA2); // (17) Set LCD Bias Ratio: Set Bias 1/9 (Duty 1/65)
|
|
|
|
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
|
2021-09-12 00:59:22 +02:00
|
|
|
lcd_write(value_contrast); // (9) Set Electronic Volume: Set Contrast
|
2021-02-27 02:26:57 +01:00
|
|
|
lcd_write(0xAF); // (12) Set Display Enable: Display on
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void lcd_update_contrast(void) {
|
2021-09-12 00:59:22 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void lcd_update_backlight(void) {
|
2021-09-12 00:59:22 +02:00
|
|
|
switch (value_backlight) {
|
|
|
|
case 0:
|
|
|
|
DDRD &= (0 << PD5);
|
|
|
|
break;
|
2021-09-21 18:40:49 +02:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
2021-09-23 22:52:54 +02:00
|
|
|
TCCR0B = 0x0C; // Prescaler = 256;
|
2021-09-12 00:59:22 +02:00
|
|
|
DDRD |= (1 << PD5);
|
|
|
|
OCR0B = value_backlight - 1;
|
|
|
|
break;
|
2021-09-21 18:40:49 +02:00
|
|
|
default:
|
2021-09-23 22:52:54 +02:00
|
|
|
TCCR0B = 0x0B; // Prescaler = 64;
|
2021-09-21 18:40:49 +02:00
|
|
|
DDRD |= (1 << PD5);
|
|
|
|
OCR0B = value_backlight - 4;
|
|
|
|
break;
|
2021-09-12 00:59:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 21:23:38 +02:00
|
|
|
static void lcd_move_cursor(const uint8_t column, const uint8_t row) {
|
|
|
|
assert(row < 16);
|
|
|
|
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x00 + (0x0F & column));
|
|
|
|
lcd_write(0x10 + (column >> 4));
|
|
|
|
lcd_write(0xB0 + row);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void lcd_fill(const uint8_t data) {
|
2021-02-27 02:26:57 +01:00
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(0, i);
|
2021-02-27 02:26:57 +01:00
|
|
|
|
2021-02-27 18:01:09 +01:00
|
|
|
for (uint8_t j = 0; j < 102; j++)
|
2021-02-27 02:26:57 +01:00
|
|
|
lcd_write(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
static void lcd_write_kerning(const uint8_t length,
|
|
|
|
const bool invert) {
|
|
|
|
for (uint8_t i = 0; i < length; i++)
|
|
|
|
if (invert)
|
|
|
|
lcd_write(0xFF);
|
|
|
|
else
|
|
|
|
lcd_write(0x00);
|
|
|
|
}
|
|
|
|
|
2021-09-12 22:01:05 +02:00
|
|
|
static void lcd_write_symbol_page(const struct symbol* symbol,
|
2021-09-12 15:37:01 +02:00
|
|
|
const uint8_t page,
|
|
|
|
const bool invert) {
|
2021-09-12 22:01:05 +02:00
|
|
|
for (uint8_t i = 0; i < symbol->length; i++)
|
2021-09-08 14:53:29 +02:00
|
|
|
if (invert)
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write(~symbol->data[page * symbol->length + i]);
|
2021-09-08 14:53:29 +02:00
|
|
|
else
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write(symbol->data[page * symbol->length + i]);
|
2021-09-08 14:53:29 +02:00
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void lcd_write_digit_page(const uint8_t digit,
|
|
|
|
const uint8_t page,
|
|
|
|
const bool invert) {
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_symbol_page(&symbol_table[digit], page, invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lcd_write_character_page(const char character,
|
|
|
|
const uint8_t page,
|
|
|
|
const bool invert) {
|
|
|
|
lcd_write_symbol_page(&symbol_table[character - 48], page, invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lcd_write_string_page(const char* string,
|
|
|
|
const uint8_t page,
|
|
|
|
const bool invert) {
|
2021-09-19 22:14:00 +02:00
|
|
|
for (uint8_t i = 0; string[i] != 0; i++) {
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_kerning(2, invert);
|
|
|
|
lcd_write_character_page(string[i], page, invert);
|
2021-09-11 00:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void lcd_write_integer_page(const uint8_t integer,
|
|
|
|
const uint8_t digits,
|
|
|
|
const uint8_t page,
|
|
|
|
const bool invert) {
|
2021-09-11 00:10:46 +02:00
|
|
|
if (digits != 0 || integer != 0) {
|
|
|
|
uint8_t input_digits = 0;
|
|
|
|
uint16_t comperator = 1;
|
|
|
|
|
|
|
|
for (; comperator <= integer; comperator *= 10, input_digits++);
|
2021-09-23 22:52:54 +02:00
|
|
|
|
2021-09-11 00:10:46 +02:00
|
|
|
for (int8_t i = digits - input_digits; i > 0; i--) {
|
|
|
|
lcd_write_kerning(2, invert);
|
|
|
|
lcd_write_digit_page(0, page, invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; comperator >= 10; comperator /= 10) {
|
|
|
|
lcd_write_kerning(2, invert);
|
|
|
|
lcd_write_digit_page((integer % comperator) / (comperator / 10),
|
|
|
|
page, invert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 12:27:53 +02:00
|
|
|
static void twi_error(bool inverted) {
|
|
|
|
if (inverted)
|
|
|
|
lcd_fill(0xFF);
|
|
|
|
else
|
|
|
|
lcd_fill(0x00);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
2021-09-23 22:47:17 +02:00
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
lcd_move_cursor(0, i);
|
2021-09-24 12:27:53 +02:00
|
|
|
lcd_write_string_page("TW_STATUS:\0", i, inverted);
|
|
|
|
lcd_write_kerning(2, inverted);
|
|
|
|
lcd_write_integer_page(TW_STATUS, 3, i, inverted);
|
2021-09-23 22:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
lcd_move_cursor(0, i + 2);
|
2021-09-24 12:27:53 +02:00
|
|
|
lcd_write_string_page("TW_DATA:\0", i, inverted);
|
|
|
|
lcd_write_kerning(16, inverted);
|
|
|
|
lcd_write_integer_page(TWDR, 3, i, inverted);
|
2021-09-23 22:47:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:02:00 +02:00
|
|
|
for (;;);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void twi_start(void) {
|
|
|
|
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); // Start transmission
|
|
|
|
while (!(TWCR & (1 << TWINT))); // Wait until start is transmitted
|
|
|
|
|
|
|
|
if (TW_STATUS != TW_START) // Check status
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t twi_transmit(const uint8_t data) {
|
|
|
|
TWDR = data;
|
|
|
|
TWCR = (1 << TWINT) | (1 << TWEN); // Transmit data
|
|
|
|
while (!(TWCR & (1 << TWINT))); // Wait until data is transmitted
|
|
|
|
|
|
|
|
return TW_STATUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t twi_receive(void) {
|
|
|
|
TWCR = (1 << TWINT) | (1 << TWEN); // Receive data
|
|
|
|
while (!(TWCR & (1 << TWINT))); // Wait until data is received
|
|
|
|
|
|
|
|
return TW_STATUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void twi_stop(void) {
|
|
|
|
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t twi_read_register(const uint8_t address,
|
|
|
|
const uint8_t reg) {
|
|
|
|
twi_start();
|
|
|
|
|
2021-09-24 12:25:47 +02:00
|
|
|
if (twi_transmit(address << 1) != TW_MT_SLA_ACK)
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
|
|
|
if (twi_transmit(reg) != TW_MT_DATA_ACK)
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
|
|
|
twi_stop();
|
|
|
|
|
|
|
|
_delay_us(2);
|
|
|
|
|
|
|
|
twi_start();
|
|
|
|
|
2021-09-24 12:25:47 +02:00
|
|
|
if (twi_transmit((address << 1) + 1) != TW_MR_SLA_ACK)
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
|
|
|
if (twi_receive() != TW_MR_DATA_NACK)
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
|
|
|
twi_stop();
|
|
|
|
|
|
|
|
return TWDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void twi_write_register(const uint8_t address,
|
|
|
|
const uint8_t reg,
|
|
|
|
const uint8_t data) {
|
|
|
|
twi_start();
|
|
|
|
|
2021-09-24 12:25:47 +02:00
|
|
|
if (twi_transmit(address << 1) != TW_MT_SLA_ACK)
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
|
|
|
if (twi_transmit(reg) != TW_MT_DATA_ACK)
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
|
|
|
if (twi_transmit(data) != TW_MT_DATA_ACK)
|
2021-09-24 12:27:53 +02:00
|
|
|
twi_error(true);
|
2021-09-16 22:02:00 +02:00
|
|
|
|
|
|
|
twi_stop();
|
|
|
|
}
|
|
|
|
|
2021-09-24 12:29:35 +02:00
|
|
|
static void draw_splash(void) {
|
|
|
|
lcd_fill(0x00);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 5; i++) {
|
|
|
|
lcd_move_cursor(31, 1 + i);
|
|
|
|
|
|
|
|
for (uint8_t j = 0; j < 40; j++)
|
|
|
|
lcd_write(sacred_chao[i * 40 + j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
lcd_move_cursor(26, 6 + i);
|
|
|
|
|
|
|
|
for (uint8_t j = 0; j < 48; j++)
|
|
|
|
lcd_write(onders_org[i * 48 + j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
static void draw_home(void) {
|
2021-09-08 14:53:29 +02:00
|
|
|
lcd_fill(0x00);
|
|
|
|
|
|
|
|
bool ch1_selected = false;
|
|
|
|
bool ch2_selected = false;
|
|
|
|
bool ch3_selected = false;
|
|
|
|
bool setup_selected = false;
|
|
|
|
|
2021-09-11 00:21:01 +02:00
|
|
|
switch (home_state) {
|
2021-09-08 14:53:29 +02:00
|
|
|
case ch1:
|
|
|
|
ch1_selected = true;
|
|
|
|
break;
|
|
|
|
case ch2:
|
|
|
|
ch2_selected = true;
|
|
|
|
break;
|
|
|
|
case ch3:
|
|
|
|
ch3_selected = true;
|
|
|
|
break;
|
|
|
|
default: // setup
|
|
|
|
setup_selected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(0, i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_string_page("CH1\0", i, ch1_selected);
|
2021-09-08 14:53:29 +02:00
|
|
|
lcd_write_kerning(2, ch1_selected);
|
|
|
|
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(0, 2 + i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_string_page("CH2\0", i, ch2_selected);
|
2021-09-08 14:53:29 +02:00
|
|
|
lcd_write_kerning(2, ch2_selected);
|
|
|
|
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(0, 4 + i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_string_page("CH3\0", i, ch3_selected);
|
2021-09-08 14:53:29 +02:00
|
|
|
lcd_write_kerning(2, ch3_selected);
|
|
|
|
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(33, 6 + i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_string_page("SETUP\0", i, setup_selected);
|
2021-09-08 14:53:29 +02:00
|
|
|
lcd_write_kerning(2, setup_selected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
static void draw_setup(void) {
|
2021-09-09 20:50:52 +02:00
|
|
|
lcd_fill(0x00);
|
|
|
|
|
2021-09-11 11:13:15 +02:00
|
|
|
bool contrast_selected = false;
|
|
|
|
bool backlight_selected = false;
|
|
|
|
bool back_selected = false;
|
2021-09-11 15:36:02 +02:00
|
|
|
bool change_contrast_selected = false;
|
|
|
|
bool change_backlight_selected = false;
|
2021-09-11 11:13:15 +02:00
|
|
|
|
|
|
|
switch (setup_state) {
|
|
|
|
case contrast:
|
|
|
|
contrast_selected = true;
|
|
|
|
break;
|
|
|
|
case backlight:
|
|
|
|
backlight_selected = true;
|
|
|
|
break;
|
2021-09-11 15:36:02 +02:00
|
|
|
case back:
|
2021-09-11 11:13:15 +02:00
|
|
|
back_selected = true;
|
|
|
|
break;
|
2021-09-11 15:36:02 +02:00
|
|
|
case change_contrast:
|
|
|
|
change_contrast_selected = true;
|
|
|
|
break;
|
|
|
|
case change_backlight:
|
|
|
|
change_backlight_selected = true;
|
|
|
|
break;
|
2021-09-11 11:13:15 +02:00
|
|
|
}
|
|
|
|
|
2021-09-09 20:50:52 +02:00
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(0, i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_kerning(30, true);
|
|
|
|
lcd_write_string_page("SETUP\0", i, true);
|
2021-09-09 20:50:52 +02:00
|
|
|
lcd_write_kerning(33, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(0, 2 + i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_string_page("CONTRAST:\0", i, contrast_selected);
|
2021-09-11 15:36:02 +02:00
|
|
|
lcd_write_kerning(2, contrast_selected);
|
2021-09-12 01:04:10 +02:00
|
|
|
lcd_write_kerning(16, false);
|
2021-09-23 22:52:54 +02:00
|
|
|
lcd_write_integer_page(value_contrast, 2, i,
|
|
|
|
change_contrast_selected);
|
2021-09-11 15:36:02 +02:00
|
|
|
lcd_write_kerning(2, change_contrast_selected);
|
2021-09-09 20:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(0, 4 + i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_string_page("BACKLIGHT:\0", i, backlight_selected);
|
2021-09-11 15:36:02 +02:00
|
|
|
lcd_write_kerning(2, backlight_selected);
|
|
|
|
lcd_write_kerning(5, false);
|
2021-09-23 22:52:54 +02:00
|
|
|
lcd_write_integer_page(value_backlight, 3, i,
|
|
|
|
change_backlight_selected);
|
2021-09-11 15:36:02 +02:00
|
|
|
lcd_write_kerning(2, change_backlight_selected);
|
2021-09-09 20:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
2021-09-14 21:23:38 +02:00
|
|
|
lcd_move_cursor(33, 6 + i);
|
2021-09-12 22:01:05 +02:00
|
|
|
lcd_write_string_page("BACK:\0", i, back_selected);
|
2021-09-11 11:13:15 +02:00
|
|
|
lcd_write_kerning(2, back_selected);
|
2021-09-09 20:50:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void change_state(const enum state new_state) {
|
2021-09-11 00:21:01 +02:00
|
|
|
switch (new_state) {
|
2021-09-09 20:40:50 +02:00
|
|
|
case home:
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_home();
|
2021-09-09 20:40:50 +02:00
|
|
|
current_state = home;
|
|
|
|
break;
|
|
|
|
case ch1:
|
|
|
|
break;
|
|
|
|
case ch2:
|
|
|
|
break;
|
|
|
|
case ch3:
|
|
|
|
break;
|
|
|
|
case setup:
|
2021-09-11 11:13:15 +02:00
|
|
|
setup_state = contrast;
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-09 20:50:52 +02:00
|
|
|
current_state = setup;
|
2021-09-09 20:40:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void update_home(const enum input event) {
|
2021-09-08 14:53:29 +02:00
|
|
|
switch (event) {
|
|
|
|
case cw:
|
2021-09-09 20:40:50 +02:00
|
|
|
home_state++;
|
|
|
|
if (home_state > setup)
|
|
|
|
home_state = ch1;
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_home();
|
2021-09-08 14:53:29 +02:00
|
|
|
break;
|
|
|
|
case ccw:
|
2021-09-09 20:40:50 +02:00
|
|
|
home_state--;
|
|
|
|
if (home_state > setup)
|
|
|
|
home_state = setup;
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_home();
|
2021-09-08 14:53:29 +02:00
|
|
|
break;
|
2021-09-09 20:19:40 +02:00
|
|
|
case click:
|
2021-09-09 20:40:50 +02:00
|
|
|
change_state(home_state);
|
2021-09-09 20:19:40 +02:00
|
|
|
break;
|
|
|
|
case hold:
|
|
|
|
break;
|
2021-09-08 14:53:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void update_setup(const enum input event) {
|
2021-09-09 20:50:52 +02:00
|
|
|
switch (event) {
|
|
|
|
case cw:
|
2021-09-11 15:36:02 +02:00
|
|
|
switch (setup_state) {
|
|
|
|
case contrast:
|
|
|
|
case backlight:
|
|
|
|
case back:
|
|
|
|
setup_state++;
|
|
|
|
if (setup_state > back)
|
|
|
|
setup_state = contrast;
|
|
|
|
break;
|
|
|
|
case change_contrast:
|
2021-09-12 00:59:22 +02:00
|
|
|
if (value_contrast < 63) {
|
|
|
|
value_contrast++;
|
|
|
|
lcd_update_contrast();
|
|
|
|
}
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
|
|
|
case change_backlight:
|
2021-09-12 00:59:22 +02:00
|
|
|
if (value_backlight < 100) {
|
|
|
|
value_backlight++;
|
|
|
|
lcd_update_backlight();
|
|
|
|
}
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-09 20:50:52 +02:00
|
|
|
break;
|
|
|
|
case ccw:
|
2021-09-11 15:36:02 +02:00
|
|
|
switch (setup_state) {
|
|
|
|
case contrast:
|
|
|
|
case backlight:
|
|
|
|
case back:
|
|
|
|
setup_state--;
|
|
|
|
if (setup_state > back)
|
|
|
|
setup_state = back;
|
|
|
|
break;
|
|
|
|
case change_contrast:
|
2021-09-12 00:59:22 +02:00
|
|
|
if (value_contrast > 0) {
|
|
|
|
value_contrast--;
|
|
|
|
lcd_update_contrast();
|
|
|
|
}
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
|
|
|
case change_backlight:
|
2021-09-12 00:59:22 +02:00
|
|
|
if (value_backlight > 0) {
|
|
|
|
value_backlight--;
|
|
|
|
lcd_update_backlight();
|
|
|
|
}
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-09 20:50:52 +02:00
|
|
|
break;
|
|
|
|
case click:
|
2021-09-11 11:13:15 +02:00
|
|
|
switch (setup_state) {
|
|
|
|
case contrast:
|
2021-09-11 15:36:02 +02:00
|
|
|
setup_state = change_contrast;
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-11 11:13:15 +02:00
|
|
|
break;
|
|
|
|
case backlight:
|
2021-09-11 15:36:02 +02:00
|
|
|
setup_state = change_backlight;
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-11 11:13:15 +02:00
|
|
|
break;
|
|
|
|
case back:
|
|
|
|
change_state(home);
|
|
|
|
break;
|
2021-09-11 15:36:02 +02:00
|
|
|
case change_contrast:
|
2021-09-11 15:44:11 +02:00
|
|
|
eeprom_update_byte(&eeprom_contrast, value_contrast);
|
2021-09-11 15:36:02 +02:00
|
|
|
setup_state = contrast;
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
|
|
|
case change_backlight:
|
2021-09-11 15:44:11 +02:00
|
|
|
eeprom_update_byte(&eeprom_backlight, value_backlight);
|
2021-09-11 15:36:02 +02:00
|
|
|
setup_state = backlight;
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
2021-09-11 11:13:15 +02:00
|
|
|
}
|
2021-09-09 20:50:52 +02:00
|
|
|
break;
|
|
|
|
case hold:
|
2021-09-11 15:36:02 +02:00
|
|
|
switch (setup_state) {
|
|
|
|
case contrast:
|
|
|
|
case backlight:
|
|
|
|
case back:
|
|
|
|
change_state(home);
|
|
|
|
break;
|
|
|
|
case change_contrast:
|
|
|
|
setup_state = contrast;
|
2021-09-11 15:44:11 +02:00
|
|
|
value_contrast = eeprom_read_byte(&eeprom_contrast);
|
2021-09-12 00:59:22 +02:00
|
|
|
lcd_update_contrast();
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
|
|
|
case change_backlight:
|
|
|
|
setup_state = backlight;
|
2021-09-11 15:44:11 +02:00
|
|
|
value_backlight = eeprom_read_byte(&eeprom_backlight);
|
2021-09-12 00:59:22 +02:00
|
|
|
lcd_update_backlight();
|
2021-09-23 22:52:54 +02:00
|
|
|
draw_setup();
|
2021-09-11 15:36:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-09-09 20:50:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:37:01 +02:00
|
|
|
static void update_state(const enum input event) {
|
2021-09-08 14:53:29 +02:00
|
|
|
switch (current_state) {
|
|
|
|
case home:
|
|
|
|
update_home(event);
|
|
|
|
break;
|
2021-09-09 20:40:50 +02:00
|
|
|
case ch1:
|
|
|
|
break;
|
|
|
|
case ch2:
|
|
|
|
break;
|
|
|
|
case ch3:
|
|
|
|
break;
|
|
|
|
case setup:
|
2021-09-09 20:50:52 +02:00
|
|
|
update_setup(event);
|
2021-09-08 14:53:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 16:46:40 +01:00
|
|
|
// Encoder rotation interrupt
|
|
|
|
ISR(PCINT0_vect) {
|
|
|
|
cli();
|
|
|
|
|
2021-09-23 22:57:03 +02:00
|
|
|
// Dechatter
|
|
|
|
_delay_ms(3);
|
|
|
|
|
2021-09-11 00:21:01 +02:00
|
|
|
switch (enc) {
|
2021-03-01 16:46:40 +01:00
|
|
|
case 0:
|
|
|
|
if (ENC_A && !ENC_B)
|
|
|
|
enc = 1;
|
|
|
|
else if (!ENC_A && ENC_B)
|
|
|
|
enc = 3;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (ENC_A && ENC_B) {
|
|
|
|
enc = 2;
|
2021-09-08 14:53:29 +02:00
|
|
|
update_state(cw);
|
2021-03-01 16:46:40 +01:00
|
|
|
} else if (!ENC_A && !ENC_B) {
|
|
|
|
enc = 0;
|
2021-09-08 14:53:29 +02:00
|
|
|
update_state(ccw);
|
2021-03-01 16:46:40 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (!ENC_A && ENC_B)
|
|
|
|
enc = 3;
|
|
|
|
else if (ENC_A && !ENC_B)
|
|
|
|
enc = 1;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (!ENC_A && !ENC_B) {
|
|
|
|
enc = 0;
|
2021-09-08 14:53:29 +02:00
|
|
|
update_state(cw);
|
2021-03-01 16:46:40 +01:00
|
|
|
} else if (ENC_A && ENC_B) {
|
|
|
|
enc = 2;
|
2021-09-08 14:53:29 +02:00
|
|
|
update_state(ccw);
|
2021-03-01 16:46:40 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
// Encoder button interrupt
|
2021-09-09 20:19:40 +02:00
|
|
|
ISR(PCINT1_vect) {
|
|
|
|
cli();
|
|
|
|
|
2021-09-23 22:57:03 +02:00
|
|
|
// Debounce
|
|
|
|
_delay_ms(2);
|
|
|
|
|
2021-09-21 18:40:49 +02:00
|
|
|
if (PINC & (1 << PC0)) { // Release
|
|
|
|
if (TCCR1B & (1 << CS10)) // If release before hold
|
2021-09-09 20:19:40 +02:00
|
|
|
update_state(click); // Switch to selected state
|
2021-09-21 18:40:49 +02:00
|
|
|
|
|
|
|
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
|
|
|
|
} else { // Press
|
|
|
|
TCNT1 = 0;
|
2021-09-12 01:05:33 +02:00
|
|
|
if (!(TCCR1B & (1 << CS10)))
|
2021-09-09 20:19:40 +02:00
|
|
|
TCCR1B |= (1 << CS11) | (1 << CS10); // Enable Timer/Counter1
|
|
|
|
}
|
|
|
|
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
2021-09-12 01:05:33 +02:00
|
|
|
// Timer/Counter1 compare match A for back event
|
2021-09-09 20:19:40 +02:00
|
|
|
ISR(TIMER1_COMPA_vect) {
|
|
|
|
cli();
|
|
|
|
|
|
|
|
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
|
|
|
|
update_state(hold);
|
|
|
|
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
2021-02-18 22:00:24 +01:00
|
|
|
int main(void) {
|
2021-09-11 15:44:11 +02:00
|
|
|
// Load contrast and backlight values from EEPROM
|
|
|
|
value_contrast = eeprom_read_byte(&eeprom_contrast);
|
|
|
|
value_backlight = eeprom_read_byte(&eeprom_backlight);
|
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
// Init backlight
|
2021-09-12 00:59:22 +02:00
|
|
|
TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
|
2021-09-21 18:40:49 +02:00
|
|
|
OCR0A = 255;
|
2021-09-12 00:59:22 +02:00
|
|
|
lcd_update_backlight();
|
2021-02-18 22:00:24 +01:00
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
// SPI and LCD init
|
2021-02-27 02:26:57 +01:00
|
|
|
spi_init();
|
|
|
|
lcd_init();
|
|
|
|
|
2021-03-01 16:46:40 +01:00
|
|
|
// Encoder setup
|
|
|
|
PORTB |= (1 << PB6) | (1 << PB7);
|
|
|
|
PCICR |= (1 << PCIE0);
|
|
|
|
PCMSK0 |= (1 << PCINT6) | (1 << PCINT7);
|
2021-02-18 22:00:24 +01:00
|
|
|
|
2021-09-09 20:19:40 +02:00
|
|
|
// Encoder switch setup
|
|
|
|
PORTC |= (1 << PC0);
|
|
|
|
PCICR |= (1 << PCIE1);
|
|
|
|
PCMSK1 |= (1 << PCINT8);
|
|
|
|
|
2021-09-21 18:40:49 +02:00
|
|
|
// Timer/Counter1 setup to recognize held button
|
|
|
|
OCR1A = 65535;
|
2021-09-09 20:19:40 +02:00
|
|
|
TIMSK1 |= (1 << OCIE1A); // Enable match compare A
|
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
// Show splash screen
|
2021-09-24 12:29:35 +02:00
|
|
|
draw_splash();
|
2021-09-21 18:40:49 +02:00
|
|
|
_delay_ms(2000);
|
2021-02-18 22:00:24 +01:00
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
// Load the menu
|
|
|
|
change_state(current_state);
|
2021-03-01 16:46:40 +01:00
|
|
|
|
2021-09-16 22:02:00 +02:00
|
|
|
// Set TWI bit rate to 200kHz
|
|
|
|
TWBR = 12;
|
|
|
|
|
|
|
|
(void) &twi_read_register;
|
|
|
|
(void) &twi_write_register;
|
|
|
|
|
2021-09-23 22:52:54 +02:00
|
|
|
// Enable interrupts
|
|
|
|
sei();
|
|
|
|
|
2021-03-01 16:46:40 +01:00
|
|
|
// Run...
|
|
|
|
for (;;);
|
2021-02-18 22:00:24 +01:00
|
|
|
}
|