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>
|
|
|
|
#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-08 14:04:07 +02:00
|
|
|
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,
|
|
|
|
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 uint8_t onders_org[96] = { 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};
|
2021-02-27 14:49:06 +01:00
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
struct symbol {
|
|
|
|
uint8_t length;
|
|
|
|
uint8_t symbol[];
|
|
|
|
};
|
|
|
|
|
2021-09-09 20:50:52 +02:00
|
|
|
static const struct symbol sym_a = { 6, { 0xC0, 0xF0, 0x3C, 0x3C, 0xF0, 0xC0,
|
|
|
|
0x3F, 0x3F, 0x06, 0x06, 0x3F, 0x3F } };
|
|
|
|
static const struct symbol sym_b = { 6, { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0x78,
|
|
|
|
0x3F, 0x3F, 0x31, 0x31, 0x3F, 0x1E } };
|
2021-09-08 14:53:29 +02:00
|
|
|
static const struct symbol sym_c = { 5, { 0xF8, 0xFC, 0x0C, 0x1C, 0x18,
|
|
|
|
0x1F, 0x3F, 0x30, 0x38, 0x18 } };
|
2021-09-09 20:50:52 +02:00
|
|
|
static const struct symbol sym_e = { 5, { 0xFC, 0xFC, 0x8C, 0x8C, 0x0C,
|
|
|
|
0x3F, 0x3F, 0x31, 0x31, 0x30 } };
|
|
|
|
static const struct symbol sym_g = { 6, { 0xF8, 0xFC, 0x0C, 0x0C, 0x3C, 0x38,
|
|
|
|
0x1F, 0x3F, 0x30, 0x33, 0x3F, 0x1F } };
|
2021-09-08 14:53:29 +02:00
|
|
|
static const struct symbol sym_h = { 6, { 0xFC, 0xFC, 0x80, 0x80, 0xFC, 0xFC,
|
|
|
|
0x3F, 0x3F, 0x01, 0x01, 0x3F, 0x3F } };
|
2021-09-09 20:50:52 +02:00
|
|
|
static const struct symbol sym_i = { 4, { 0x0C, 0xFC, 0xFC, 0x0C,
|
|
|
|
0x30, 0x3F, 0x3F, 0x30 } };
|
|
|
|
static const struct symbol sym_k = { 6, { 0xFC, 0xFC, 0xC0, 0xF0, 0x7C, 0x1C,
|
|
|
|
0x3F, 0x3F, 0x03, 0x0F, 0x3E, 0x38 } };
|
|
|
|
static const struct symbol sym_l = { 5, { 0xFC, 0xFC, 0x00, 0x00, 0x00,
|
|
|
|
0x3F, 0x3F, 0x30, 0x30, 0x30 } };
|
|
|
|
static const struct symbol sym_n = { 7, { 0xFC, 0xFC, 0xF0, 0xC0, 0x00, 0xFC, 0xFC,
|
|
|
|
0x3F, 0x3F, 0x00, 0x03, 0x0F, 0x3F, 0x3F } };
|
|
|
|
static const struct symbol sym_o = { 6, { 0xF8, 0xFC, 0x0C, 0x0C, 0xFC, 0xF8,
|
|
|
|
0x1F, 0x3F, 0x30, 0x30, 0x3F, 0x1F } };
|
|
|
|
static const struct symbol sym_p = { 6, { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x3F, 0x3F, 0x01, 0x01, 0x01, 0x00 } };
|
|
|
|
static const struct symbol sym_r = { 6, { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x3F, 0x3F, 0x01, 0x03, 0x3F, 0x3E } };
|
|
|
|
static const struct symbol sym_s = { 6, { 0xF8, 0xFC, 0x8C, 0x8C, 0x9C, 0x18,
|
|
|
|
0x18, 0x39, 0x31, 0x31, 0x3F, 0x1F } };
|
|
|
|
static const struct symbol sym_t = { 6, { 0x0C, 0x0C, 0xFC, 0xFC, 0x0C, 0x0C,
|
|
|
|
0x00, 0x00, 0x3F, 0x3F, 0x00, 0x00 } };
|
|
|
|
static const struct symbol sym_u = { 6, { 0xFC, 0xFC, 0x00, 0x00, 0xFC, 0xFC,
|
|
|
|
0x3F, 0x3F, 0x30, 0x30, 0x3F, 0x3F } };
|
2021-09-08 14:53:29 +02:00
|
|
|
|
|
|
|
static const struct symbol sym_0 = { 5, { 0xF8, 0xFC, 0x0C, 0xFC, 0xF8,
|
|
|
|
0x1F, 0x3F, 0x30, 0x3F, 0x1F } };
|
|
|
|
static const struct symbol sym_1 = { 5, { 0x30, 0x30, 0xFC, 0xFC, 0x00,
|
|
|
|
0x30, 0x30, 0x3F, 0x3F, 0x30 } };
|
|
|
|
static const struct symbol sym_2 = { 5, { 0x18, 0x1C, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x38, 0x3E, 0x3F, 0x33, 0x30 } };
|
|
|
|
static const struct symbol sym_3 = { 5, { 0x18, 0x9C, 0x8C, 0xFC, 0x78,
|
|
|
|
0x18, 0x39, 0x31, 0x3F, 0x1E } };
|
|
|
|
static const struct symbol sym_4 = { 5, { 0x80, 0xE0, 0x78, 0xFC, 0xFC,
|
|
|
|
0x07, 0x07, 0x06, 0x3F, 0x3F } };
|
|
|
|
static const struct symbol sym_5 = { 5, { 0xFC, 0xFC, 0x8C, 0x8C, 0x0C,
|
|
|
|
0x1C, 0x3D, 0x31, 0x3F, 0x1F } };
|
|
|
|
static const struct symbol sym_6 = { 5, { 0xF8, 0xFC, 0x8C, 0xBC, 0x38,
|
|
|
|
0x1F, 0x3F, 0x31, 0x3F, 0x1F } };
|
|
|
|
static const struct symbol sym_7 = { 5, { 0x0C, 0x0C, 0xEC, 0xFC, 0x1C,
|
|
|
|
0x00, 0x3E, 0x3F, 0x01, 0x00 } };
|
|
|
|
static const struct symbol sym_8 = { 5, { 0x78, 0xFC, 0x8C, 0xFC, 0x78,
|
|
|
|
0x1E, 0x3F, 0x31, 0x3F, 0x1E } };
|
|
|
|
static const struct symbol sym_9 = { 5, { 0xF8, 0xFC, 0x8C, 0xFC, 0xF8,
|
|
|
|
0x1C, 0x3D, 0x31, 0x3F, 0x1F } };
|
|
|
|
|
2021-09-09 20:50:52 +02:00
|
|
|
static const struct symbol sym_colon = { 2, { 0x30, 0x30,
|
|
|
|
0x0C, 0x0C } };
|
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
static const struct symbol sym_setup = { 19, { 0xF8, 0x98, 0xB8, 0x00, 0xF8, 0x98, 0x18, 0x00, 0x18, 0xF8, 0x18, 0x00, 0xF8, 0x00, 0xF8, 0x00, 0xF8, 0x98, 0xF8,
|
|
|
|
0x1D, 0x19, 0x1F, 0x00, 0x1F, 0x19, 0x18, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x18, 0x1F, 0x00, 0x1F, 0x01, 0x01 } };
|
|
|
|
|
2021-09-09 20:19:40 +02:00
|
|
|
enum input {cw, ccw, click, hold};
|
2021-09-08 14:53:29 +02:00
|
|
|
|
2021-09-09 20:40:50 +02:00
|
|
|
static volatile enum state {home, ch1, ch2, ch3, setup} current_state = home;
|
|
|
|
static volatile enum state home_state = ch1;
|
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-02-27 02:26:57 +01:00
|
|
|
void spi_init(void) {
|
|
|
|
SPI_DDR |= (1 << SPI_SCK) | (1 << SPI_MOSI) | (1 << SPI_SS);
|
|
|
|
SPI_PORT |= (1 << SPI_SS);
|
|
|
|
SPCR = (1 << SPE) | (1 << MSTR);
|
|
|
|
SPSR = (1 << SPI2X);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t spi_byte(uint8_t data) {
|
|
|
|
SPDR = data;
|
|
|
|
while(!(SPSR & (1 << SPIF)));
|
|
|
|
return SPDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lcd_write(uint8_t data) {
|
|
|
|
SPI_PORT &= ~(1 << SPI_SS);
|
|
|
|
spi_byte(data);
|
|
|
|
SPI_PORT |= (1 << SPI_SS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lcd_init(void) {
|
|
|
|
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
|
|
|
|
lcd_write(0x10); // (9) Set Electronic Volume: Set Contrast
|
|
|
|
lcd_write(0xAF); // (12) Set Display Enable: Display on
|
|
|
|
}
|
|
|
|
|
|
|
|
void lcd_fill(uint8_t data) {
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x00);
|
|
|
|
lcd_write(0x10);
|
2021-02-27 18:01:09 +01:00
|
|
|
lcd_write(0xB0 + i);
|
2021-02-27 02:26:57 +01:00
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lcd_write_symbol_page(const struct symbol* sym,
|
|
|
|
uint8_t page,
|
|
|
|
bool invert) {
|
|
|
|
for (uint8_t i = 0; i < sym->length; i++)
|
|
|
|
if (invert)
|
|
|
|
lcd_write(~sym->symbol[page * sym->length + i]);
|
|
|
|
else
|
|
|
|
lcd_write(sym->symbol[page * sym->length + i]);
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:10:46 +02:00
|
|
|
static void lcd_write_digit_page(uint8_t digit, uint8_t page,
|
|
|
|
bool invert) {
|
|
|
|
switch (digit) {
|
|
|
|
case 0:
|
|
|
|
lcd_write_symbol_page(&sym_0, page, invert);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
lcd_write_symbol_page(&sym_1, page, invert);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
lcd_write_symbol_page(&sym_2, page, invert);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
lcd_write_symbol_page(&sym_3, page, invert);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
lcd_write_symbol_page(&sym_4, page, invert);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
lcd_write_symbol_page(&sym_5, page, invert);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
lcd_write_symbol_page(&sym_6, page, invert);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
lcd_write_symbol_page(&sym_7, page, invert);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
lcd_write_symbol_page(&sym_8, page, invert);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
lcd_write_symbol_page(&sym_9, page, invert);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lcd_write_integer_page(uint8_t integer, uint8_t digits,
|
|
|
|
uint8_t page, bool invert) {
|
|
|
|
if (digits != 0 || integer != 0) {
|
|
|
|
uint8_t input_digits = 0;
|
|
|
|
uint16_t comperator = 1;
|
|
|
|
|
|
|
|
for (; comperator <= integer; comperator *= 10, input_digits++);
|
|
|
|
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-08 14:04:07 +02:00
|
|
|
void lcd_splash(void) {
|
2021-02-27 18:01:09 +01:00
|
|
|
lcd_fill(0x00);
|
2021-02-27 14:49:06 +01:00
|
|
|
|
2021-09-08 14:04:07 +02:00
|
|
|
for (uint8_t i = 0; i < 5; i++) {
|
2021-02-27 18:01:09 +01:00
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
2021-09-08 14:04:07 +02:00
|
|
|
lcd_write(0x0F);
|
2021-02-27 18:01:09 +01:00
|
|
|
lcd_write(0x11);
|
|
|
|
lcd_write(0xB1 + i);
|
2021-02-27 14:49:06 +01:00
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
2021-09-08 14:04:07 +02:00
|
|
|
for (uint8_t j = 0; j < 40; j++)
|
|
|
|
lcd_write(sacred_chao[i * 40 + j]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x0A);
|
|
|
|
lcd_write(0x11);
|
|
|
|
lcd_write(0xB6 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
2021-02-27 18:01:09 +01:00
|
|
|
for (uint8_t j = 0; j < 48; j++)
|
2021-09-08 14:04:07 +02:00
|
|
|
lcd_write(onders_org[i * 48 + j]);
|
2021-02-27 14:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
static void lcd_home(void) {
|
|
|
|
lcd_fill(0x00);
|
|
|
|
|
|
|
|
bool ch1_selected = false;
|
|
|
|
bool ch2_selected = false;
|
|
|
|
bool ch3_selected = false;
|
|
|
|
bool setup_selected = false;
|
|
|
|
|
2021-09-09 20:40:50 +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++) {
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x00);
|
|
|
|
lcd_write(0x10);
|
|
|
|
lcd_write(0xB0 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_kerning(2, ch1_selected);
|
|
|
|
lcd_write_symbol_page(&sym_c, i, ch1_selected);
|
|
|
|
lcd_write_kerning(2, ch1_selected);
|
|
|
|
lcd_write_symbol_page(&sym_h, i, ch1_selected);
|
|
|
|
lcd_write_kerning(2, ch1_selected);
|
|
|
|
lcd_write_symbol_page(&sym_1, i, ch1_selected);
|
|
|
|
lcd_write_kerning(2, ch1_selected);
|
|
|
|
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x00);
|
|
|
|
lcd_write(0x10);
|
|
|
|
lcd_write(0xB2 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_kerning(2, ch2_selected);
|
|
|
|
lcd_write_symbol_page(&sym_c, i, ch2_selected);
|
|
|
|
lcd_write_kerning(2, ch2_selected);
|
|
|
|
lcd_write_symbol_page(&sym_h, i, ch2_selected);
|
|
|
|
lcd_write_kerning(2, ch2_selected);
|
|
|
|
lcd_write_symbol_page(&sym_2, i, ch2_selected);
|
|
|
|
lcd_write_kerning(2, ch2_selected);
|
|
|
|
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x00);
|
|
|
|
lcd_write(0x10);
|
|
|
|
lcd_write(0xB4 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_kerning(2, ch3_selected);
|
|
|
|
lcd_write_symbol_page(&sym_c, i, ch3_selected);
|
|
|
|
lcd_write_kerning(2, ch3_selected);
|
|
|
|
lcd_write_symbol_page(&sym_h, i, ch3_selected);
|
|
|
|
lcd_write_kerning(2, ch3_selected);
|
|
|
|
lcd_write_symbol_page(&sym_3, i, ch3_selected);
|
|
|
|
lcd_write_kerning(2, ch3_selected);
|
|
|
|
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x00);
|
|
|
|
lcd_write(0x10);
|
|
|
|
lcd_write(0xB6 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_kerning(2, setup_selected);
|
|
|
|
lcd_write_symbol_page(&sym_setup, i, setup_selected);
|
|
|
|
lcd_write_kerning(3, setup_selected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 20:50:52 +02:00
|
|
|
static void lcd_setup(void) {
|
|
|
|
lcd_fill(0x00);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x00);
|
|
|
|
lcd_write(0x10);
|
|
|
|
lcd_write(0xB0 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_kerning(32, true);
|
|
|
|
lcd_write_symbol_page(&sym_s, i, true);
|
|
|
|
lcd_write_kerning(2, true);
|
|
|
|
lcd_write_symbol_page(&sym_e, i, true);
|
|
|
|
lcd_write_kerning(2, true);
|
|
|
|
lcd_write_symbol_page(&sym_t, i, true);
|
|
|
|
lcd_write_kerning(2, true);
|
|
|
|
lcd_write_symbol_page(&sym_u, i, true);
|
|
|
|
lcd_write_kerning(2, true);
|
|
|
|
lcd_write_symbol_page(&sym_p, i, true);
|
|
|
|
lcd_write_kerning(33, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x02);
|
|
|
|
lcd_write(0x10);
|
|
|
|
lcd_write(0xB2 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_symbol_page(&sym_c, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_o, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_n, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_t, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_r, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_a, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_s, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_t, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_colon, i, false);
|
2021-09-11 00:10:46 +02:00
|
|
|
lcd_write_kerning(11, false);
|
|
|
|
lcd_write_integer_page(123, 1, i, false);
|
2021-09-09 20:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x02);
|
|
|
|
lcd_write(0x10);
|
|
|
|
lcd_write(0xB4 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_symbol_page(&sym_b, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_a, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_c, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_k, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_l, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_i, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_g, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_h, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_t, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_colon, i, false);
|
2021-09-11 00:10:46 +02:00
|
|
|
lcd_write_kerning(7, false);
|
|
|
|
lcd_write_integer_page(123, 3, i, false);
|
2021-09-09 20:50:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
SPI_PORT &= ~(1 << LCD_CD);
|
|
|
|
lcd_write(0x03);
|
|
|
|
lcd_write(0x12);
|
|
|
|
lcd_write(0xB6 + i);
|
|
|
|
SPI_PORT |= (1 << LCD_CD);
|
|
|
|
|
|
|
|
lcd_write_symbol_page(&sym_b, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_a, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_c, i, false);
|
|
|
|
lcd_write_kerning(2, false);
|
|
|
|
lcd_write_symbol_page(&sym_k, i, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 20:40:50 +02:00
|
|
|
static void change_state(enum state new_state) {
|
|
|
|
switch(new_state) {
|
|
|
|
case home:
|
|
|
|
lcd_home();
|
|
|
|
current_state = home;
|
|
|
|
break;
|
|
|
|
case ch1:
|
|
|
|
break;
|
|
|
|
case ch2:
|
|
|
|
break;
|
|
|
|
case ch3:
|
|
|
|
break;
|
|
|
|
case setup:
|
2021-09-09 20:50:52 +02:00
|
|
|
lcd_setup();
|
|
|
|
current_state = setup;
|
2021-09-09 20:40:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
static void update_home(enum input event) {
|
|
|
|
switch (event) {
|
|
|
|
case cw:
|
2021-09-09 20:40:50 +02:00
|
|
|
home_state++;
|
|
|
|
if (home_state > setup)
|
|
|
|
home_state = ch1;
|
2021-09-08 14:53:29 +02:00
|
|
|
lcd_home();
|
|
|
|
break;
|
|
|
|
case ccw:
|
2021-09-09 20:40:50 +02:00
|
|
|
home_state--;
|
|
|
|
if (home_state > setup)
|
|
|
|
home_state = setup;
|
2021-09-08 14:53:29 +02:00
|
|
|
lcd_home();
|
|
|
|
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-09 20:50:52 +02:00
|
|
|
static void update_setup(enum input event) {
|
|
|
|
switch (event) {
|
|
|
|
case cw:
|
|
|
|
break;
|
|
|
|
case ccw:
|
|
|
|
break;
|
|
|
|
case click:
|
|
|
|
break;
|
|
|
|
case hold:
|
|
|
|
change_state(home);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
static void update_state(enum input event) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
switch(enc) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-09 20:19:40 +02:00
|
|
|
// TODO: proper dechattering
|
2021-03-01 16:46:40 +01:00
|
|
|
_delay_us(100);
|
|
|
|
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
2021-09-09 20:19:40 +02:00
|
|
|
// encoder button interrupt
|
|
|
|
ISR(PCINT1_vect) {
|
|
|
|
cli();
|
|
|
|
|
|
|
|
if (PINC & (1 << PC0)) { // release
|
|
|
|
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
|
|
|
|
if (TCNT1 != 0) {
|
|
|
|
update_state(click); // Switch to selected state
|
|
|
|
TCNT1 = 0;
|
|
|
|
}
|
|
|
|
} else { // press
|
|
|
|
if (!(TCCR1B & (1 << CS10))) {
|
|
|
|
TCCR1B |= (1 << CS11) | (1 << CS10); // Enable Timer/Counter1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: proper debouncing
|
|
|
|
_delay_us(70);
|
|
|
|
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer/Counter1 compare match A
|
|
|
|
ISR(TIMER1_COMPA_vect) {
|
|
|
|
cli();
|
|
|
|
|
|
|
|
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
|
|
|
|
TCNT1 = 0;
|
|
|
|
update_state(hold);
|
|
|
|
|
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
2021-02-18 22:00:24 +01:00
|
|
|
int main(void) {
|
|
|
|
// FastPWM: 1.25kHz
|
2021-03-01 16:46:40 +01:00
|
|
|
// TODO: Try to get the backlit even more dim
|
2021-02-18 22:00:24 +01:00
|
|
|
TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
|
|
|
|
TCCR0B = (1 << CS01) | (1 << WGM02); // prescaler = 8;
|
|
|
|
OCR0A = 100;
|
2021-03-01 16:46:40 +01:00
|
|
|
OCR0B = 0;
|
2021-02-18 22:00:24 +01:00
|
|
|
DDRD |= (1 << PD5);
|
|
|
|
|
2021-03-01 16:46:40 +01:00
|
|
|
// SPI setup
|
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);
|
|
|
|
|
|
|
|
// Timer1 setup to recognize held button
|
|
|
|
OCR1A = 8192;
|
|
|
|
TIMSK1 |= (1 << OCIE1A); // Enable match compare A
|
|
|
|
|
2021-09-08 14:53:29 +02:00
|
|
|
// Show splash screen and load the menu
|
2021-03-01 16:46:40 +01:00
|
|
|
lcd_splash();
|
2021-09-08 14:53:29 +02:00
|
|
|
_delay_ms(250);
|
|
|
|
change_state(current_state);
|
2021-02-18 22:00:24 +01:00
|
|
|
|
2021-03-01 16:46:40 +01:00
|
|
|
// Enable interrupts
|
|
|
|
sei();
|
|
|
|
|
|
|
|
// Run...
|
|
|
|
for (;;);
|
2021-02-18 22:00:24 +01:00
|
|
|
}
|