From 8e11f269ce352df26af7c44d283131494dcd70fa Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 11 Apr 2018 20:49:32 -0400 Subject: [PATCH] status bar, arrow keys, ins/del --- include/display_draw.h | 5 +++++ include/keypad.h | 5 +++++ initrd/init | 2 +- src/display_draw.c | 12 ++++++++++-- src/keypad.c | 29 +++++++++++++++++------------ src/main.c | 30 ++++++++++++++++++++++++++++++ src/script.c | 41 +++++++++++++++++++++++++++++++++++++---- 7 files changed, 105 insertions(+), 19 deletions(-) diff --git a/include/display_draw.h b/include/display_draw.h index 8db3e2c..1057a1c 100644 --- a/include/display_draw.h +++ b/include/display_draw.h @@ -63,6 +63,11 @@ void dsp_rect(int x, int y, int w, int h, uint16_t color); */ void dsp_cpos(int x, int y); +/* + * Shifts the cursor the given amount of characters. + */ +void dsp_spos(int x, int y); + /** * Sets the pixel offset of the text cursor. * @param x x-pixel offset from (0, 0) diff --git a/include/keypad.h b/include/keypad.h index 5263a0d..d55b867 100644 --- a/include/keypad.h +++ b/include/keypad.h @@ -23,6 +23,11 @@ #include +#define K_UP 0x18 +#define K_DOWN 0x19 +#define K_LEFT 0x1B +#define K_RIGHT 0x1A + /** * Initializes GPIO for the keypad. * Starts a task to poll the buttons. Must be called before any keypad reading. diff --git a/initrd/init b/initrd/init index 6c5acac..68ef07d 100644 --- a/initrd/init +++ b/initrd/init @@ -1,5 +1,5 @@ while (1) { - rect(0, 0, 480, 320, 0) + rect(0, 0, 480, 300, 0) ppos(0, 0) print("Free mem: ") diff --git a/src/display_draw.c b/src/display_draw.c index f02b8a0..97ee386 100644 --- a/src/display_draw.c +++ b/src/display_draw.c @@ -28,7 +28,7 @@ #define C_WIDTH 12 #define C_HEIGHT 16 #define S_WIDTH 40 -#define S_HEIGHT 20 +#define S_HEIGHT 18 volatile uint8_t lock = 0; #define LOCK while (lock) { delay(5); } task_hold(1); lock = 1 @@ -66,7 +66,7 @@ void dsp_putchar(int c) curx = 0; if (++cury == S_HEIGHT) { UNLOCK; - dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); + dsp_rect(0, 0, C_WIDTH * S_WIDTH, C_HEIGHT * S_HEIGHT, 0); cury = 0; } UNLOCK; @@ -120,6 +120,14 @@ void dsp_cpos(int x, int y) cury = y; } +void dsp_spos(int x, int y) +{ + if ((int)curx + x >= 0) + curx += x; + if ((int)cury + y >= 0) + cury += y; +} + void dsp_coff(int x, int y) { curxo = x; diff --git a/src/keypad.c b/src/keypad.c index 58bc508..b00cf41 100644 --- a/src/keypad.c +++ b/src/keypad.c @@ -55,23 +55,24 @@ static const port_t keypad_cols[COLS] = { #define K_2ND 0x000000FF #define K_HOLD 0x000001FF +#define K_INS 0x000002FF static const char keypad_map[ROWS * COLS * 4] = { - "\x7F\0\0\0" ">\0\0\0" ">=\0\0" "==\0\0" "=\0\0\0" - "x\0\0\0" "<\0\0\0" "<=\0\0" "!=\0\0" "%\0\0\0" - "7\0\0\0" "8\0\0\0" "9\0\0\0" "(\0\0\0" ")\0\0\0" - "4\0\0\0" "5\0\0\0" "6\0\0\0" "/\0\0\0" "*\0\0\0" - "1\0\0\0" "2\0\0\0" "3\0\0\0" "-\0\0\0" "+\0\0\0" - ".\0\0\0" "0\0\0\0" "\xFF\0\0\0" "\b\0\0\0" "\n\0\0\0" + "\x7F\0\0\0" "\xFF\0\0\0" "\xFF\x02\0\0" "\x19\0\0\0" "\x18\0\0\0" + "x\0\0\0" "\0\0\0\0" "\0\0\0\0" "\x1B\0\0\0" "\x1A\0\0\0" + "7\0\0\0" "8\0\0\0" "9\0\0\0" "(\0\0\0" ")\0\0\0" + "4\0\0\0" "5\0\0\0" "6\0\0\0" "/\0\0\0" "*\0\0\0" + "1\0\0\0" "2\0\0\0" "3\0\0\0" "-\0\0\0" "+\0\0\0" + ".\0\0\0" "0\0\0\0" "=\0\0\0" "\b\0\0\0" "\n\0\0\0" }; static const char keypad_map_2nd[ROWS * COLS * 4] = { - "a\0\0\0" "b\0\0\0" "c\0\0\0" "d\0\0\0" "e\0\0\0" - "f\0\0\0" "g\0\0\0" "h\0\0\0" "i\0\0\0" "j\0\0\0" - "k\0\0\0" "l\0\0\0" "m\0\0\0" "n\0\0\0" "o\0\0\0" - "p\0\0\0" "q\0\0\0" "r\0\0\0" "s\0\0\0" "t\0\0\0" - "u\0\0\0" "v\0\0\0" "w\0\0\0" "x\0\0\0" "y\0\0\0" - "z\0\0\0" "\0\0\0\0" "\0\0\0\0" "\x7F\0\0\0" "\xFF\x01\0\0" + "a\0\0\0" "b\0\0\0" "c\0\0\0" "d\0\0\0" "e\0\0\0" + "f\0\0\0" "g\0\0\0" "h\0\0\0" "i\0\0\0" "j\0\0\0" + "k\0\0\0" "l\0\0\0" "m\0\0\0" "n\0\0\0" "o\0\0\0" + "p\0\0\0" "q\0\0\0" "r\0\0\0" "s\0\0\0" "t\0\0\0" + "u\0\0\0" "v\0\0\0" "w\0\0\0" "x\0\0\0" "y\0\0\0" + "z\0\0\0" "\0\0\0\0" "\0\0\0\0" "\0\0\0\0" "\xFF\x01\0\0" }; #define KEY(r, c, i) map[r * COLS * 4 + c * 4 + i] @@ -81,6 +82,8 @@ static const char keypad_map_2nd[ROWS * COLS * 4] = { static char keypad_buffer[BUFFER_SIZE]; static int keypad_buffer_pos = -1; +int keypad_insert = 0; + void keypad_task(void) { unsigned int col = 0; @@ -99,6 +102,8 @@ void keypad_task(void) if ((hold ^= 1) == 0) use2nd = 0; } + } else if (KEYCODE(row, col) == K_INS) { + keypad_insert ^= 1; } else if (keypad_buffer_pos < BUFFER_SIZE) { if (use2nd != 0 && hold == 0) use2nd = 0; diff --git a/src/main.c b/src/main.c index 4d6f140..a86db6a 100644 --- a/src/main.c +++ b/src/main.c @@ -40,6 +40,7 @@ extern char *itoa(int, char *, int); void kmain(void); void task_interpreter(void); +void task_status(void); int main(void) { @@ -77,6 +78,7 @@ void kmain(void) keypad_start(); task_start(task_interpreter, 4096); + task_start(task_status, 512); while (1) { gpio_dout(GPIOA, 5, 1); @@ -125,6 +127,34 @@ fail: return 0; } + + +void task_status(void) +{ + extern int keypad_insert; + + int lastInsert = -1; + + int16_t bg = dsp_color(0x3F, 0x3F, 0x3F); + int16_t red = dsp_color(0xFF, 0, 0); + int16_t green = dsp_color(0, 0xFF, 0); + + dsp_rect(0, 300, 480, 20, bg); + + while (1) { + if (lastInsert != keypad_insert) { + lastInsert = keypad_insert; + dsp_rect(0, 300, 480, 20, bg); + if (lastInsert > 0) + dsp_rect(4, 304, 12, 12, green); + else + dsp_rect(4, 304, 12, 12, red); + } + + delay(500); + } +} + void task_interpreter(void) { instance *it = load_program("init"); diff --git a/src/script.c b/src/script.c index 61ff598..51a3bb7 100644 --- a/src/script.c +++ b/src/script.c @@ -168,6 +168,7 @@ int script_gets(instance *it) char *s = malloc(64); char c[2] = {0, 0}; int index = 0; + int furthest = 0; do { do { @@ -178,9 +179,32 @@ int script_gets(instance *it) if (c[0] == 0x7F) { it->lnidx = 998; break; + } else if (c[0] == K_LEFT) { + if (index > 0) { + dsp_spos(-1, 0); + index--; + } + continue; + } else if (c[0] == K_RIGHT) { + if (index < furthest) { + dsp_spos(1, 0); + index++; + } + continue; + } else if (c[0] == K_UP || c[0] == K_DOWN) + continue; + + if (c[0] == '\n') { + s[furthest] = '\n'; + break; + } + + extern int keypad_insert; + if (keypad_insert != 0 && index < furthest) { + for (int i = furthest; i >= index; i--) + s[i] = s[i - 1]; } - //c[0] = serial_get(); s[index] = c[0]; if (c[0] == '\b' || c[0] == 127) { index--; @@ -188,11 +212,20 @@ int script_gets(instance *it) dsp_puts("\b"); index--; } + } else if (keypad_insert != 0) { + dsp_spos(-index, 0); + s[furthest + 1] = '\0'; + dsp_puts(s); + dsp_spos(-(furthest - index), 0); + furthest++; } else if (c[0] != '\n'/*'\r'*/) { dsp_puts(c); } - } while (s[index] != '\n'/*'\r'*/ && index++ < 63); - s[index] = '\0'; + + if (++index > furthest) + furthest = index; + } while (furthest < 63); + s[furthest] = '\0'; variable *r = make_vars(0, s); ipush(it, (uint32_t)r); @@ -274,7 +307,7 @@ int script_program(instance *it) int initrdOffset = (int)igetarg(it, 0)->value.f; char *name = initrd_getname(initrdOffset); - dsp_rect(0, 0, 480, 320, 0); + dsp_rect(0, 0, 480, 300, 0); dsp_cpos(0, 0); dsp_coff(0, 0);