Created
February 10, 2021 07:21
-
-
Save yurafuca/bc5830d131fd009a75f68257fa7eb539 to your computer and use it in GitHub Desktop.
max 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "sys/alt_stdio.h" | |
| #include "system.h" | |
| #include "altera_avalon_pio_regs.h" | |
| #include "sys/alt_sys_wrappers.h" | |
| #include "altera_avalon_uart_regs.h" | |
| #include "sys/alt_irq.h" | |
| #define write(reg, v) IOWR_ALTERA_AVALON_PIO_DATA(reg, v) | |
| #define read(reg) IORD_ALTERA_AVALON_PIO_DATA(reg) | |
| #define send(v) IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE, v) | |
| #define sleep(msec) usleep(1000 * msec) | |
| typedef enum { false, true } bool; | |
| typedef enum { none, left, right } rotation; | |
| int numbers[] = { | |
| 0b00111111, // 0 | |
| 0b00000110, // 1 | |
| 0b01011011, // 2 | |
| 0b01001111, // 3 | |
| 0b01100110, // 4 | |
| 0b01101101, // 5 | |
| 0b01111101, // 6 | |
| 0b00100111, // 7 | |
| 0b01111111, // 8 | |
| 0b01101111 // 9 | |
| }; | |
| int alphabets[] = { | |
| 0b01110111, // A | |
| 0b01111100, // B | |
| 0b00111001, // C | |
| 0b01011110, // D | |
| 0b01111001, // E | |
| 0b01110001, // F | |
| 0b00111101, // G | |
| 0b01110110, // H | |
| 0b00110000, // I | |
| 0b00011110, // J | |
| 0b01111010, // K | |
| 0b00111000, // L | |
| 0b00110111, // M | |
| 0b01010100, // N | |
| 0b00111111, // O | |
| 0b01110011, // P | |
| 0b01100111, // Q | |
| 0b01010000, // R | |
| 0b01101101, // S | |
| 0b01111000, // T | |
| 0b00011100, // U | |
| 0b00111110, // V | |
| 0b01001001, // W | |
| 0b01110110, // X | |
| 0b01100110, // Y | |
| 0b01001000, // Z | |
| 0b10000000, // . | |
| 0b10000010, // ! | |
| }; | |
| int q[] = { 0, 0, 0, 0 }; | |
| void q_insert(int v) { | |
| q[3] = q[2]; | |
| q[2] = q[1]; | |
| q[1] = q[0]; | |
| q[0] = v; | |
| } | |
| rotation q_rotation() { | |
| if (q[3] == 2 && q[2] == 0 && q[1] == 1 && q[0] == 3) { | |
| return right; | |
| } | |
| if (q[3] == 1 && q[2] == 0 && q[1] == 2 && q[0] == 3) { | |
| return left; | |
| } | |
| return none; | |
| } | |
| void led(int left, int right, bool is_left) { | |
| if (is_left) { | |
| write(COM_BASE, 0b10); | |
| write(D_BASE, left); | |
| } else { | |
| write(COM_BASE, 0b01); | |
| write(D_BASE, right); | |
| } | |
| } | |
| int main() | |
| { | |
| write(D_BASE, 0); | |
| write(COM_BASE, 0b11); | |
| char count = 0; | |
| bool is_left = true; | |
| char enc_last = 0; | |
| bool psw_last = read(PSW_BASE); | |
| bool tsw_last = read(TSW_BASE); | |
| while (1) { | |
| char enc = read(ENC_BASE); | |
| if (enc != enc_last) { | |
| q_insert(enc); | |
| switch (q_rotation()) { | |
| case none: | |
| break; | |
| case left: | |
| count -= 1; | |
| break; | |
| case right: | |
| count += 1; | |
| break; | |
| } | |
| } | |
| if (count < 0) { | |
| count = 27; | |
| } | |
| if (count > 27) { | |
| count = 0; | |
| } | |
| led(0, alphabets[count], is_left); | |
| // led(numbers[count / 10], numbers[count % 10], is_left); | |
| // led(0, numbers[enc], is_left); | |
| bool psw = read(PSW_BASE); | |
| if (psw != psw_last && psw == true) { | |
| if (count == 26) { | |
| send('.'); | |
| } else if (count == 27) { | |
| send('!'); | |
| } else { | |
| send(count + 'A'); | |
| } | |
| } | |
| bool tsw = read(TSW_BASE); | |
| if (tsw != tsw_last) { | |
| send(13); // CR | |
| send(10); // LF | |
| } | |
| enc_last = enc; | |
| psw_last = psw; | |
| tsw_last = tsw; | |
| is_left = !is_left; | |
| sleep(0.1); | |
| } | |
| return (0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment