diff --git a/include/keypad.h b/include/keypad.h index 2e5fa9c..3dbebdf 100644 --- a/include/keypad.h +++ b/include/keypad.h @@ -23,34 +23,18 @@ #include -#define K0 (uint16_t)(1 << 0) -#define K1 (uint16_t)(1 << 1) -#define K2 (uint16_t)(1 << 2) -#define K3 (uint16_t)(1 << 3) -#define K4 (uint16_t)(1 << 4) -#define K5 (uint16_t)(1 << 5) -#define K6 (uint16_t)(1 << 6) -#define K7 (uint16_t)(1 << 7) -#define K8 (uint16_t)(1 << 8) -#define K9 (uint16_t)(1 << 9) -#define KS (uint16_t)(1 << 10) -#define KP (uint16_t)(1 << 11) - /** - * Initializes GPIO for the keypad. Must be called before any keypad reading. + * Initializes GPIO for the keypad. + * Starts a task to poll the buttons. Must be called before any keypad reading. */ void keypad_init(void); /** - * Reads the state of the keypad and returns it. - * @return the keypad's state + * Reads the last pressed key on the keypad. + * This driver keeps an 8 key buffer for key presses. + * @return the pressed key (as the character it represents), zero if no presses */ -uint16_t keypad_get(void); +int keypad_get(void); -/** - * Tests if the given key is currently pressed, returning non-zero if it is. - * @return non-zero if pressed - */ -uint8_t keypad_isdown(uint16_t); #endif // KEYPAD_H_ diff --git a/initrd/graph b/initrd/graph new file mode 100644 index 0000000..a218474 --- /dev/null +++ b/initrd/graph @@ -0,0 +1,67 @@ +# graph area +plotx = 50 +ploty = 50 +plotw = 380 +ploth = 220 +cx = plotx + plotw / 2 +cy = ploty + ploth / 2 + +# graph range +xmin = 0 - 10 +xmax = 10 +ymin = 0 - 10 +ymax = 10 + +xinc = plotw / (xmax - xmin) +yinc = ploth / (ymax - ymin) + +mlines = color(3, 3, 3) + +func(makegrid) { + rect(plotx, ploty, plotw, ploth, 0) + + x = plotx + while (x <= plotx + plotw) { + line(x, ploty, x, ploty + ploth, mlines) + x = x + xinc + } + + y = ploty + while (y <= ploty + ploth) { + line(plotx, y, plotx + plotw, y, mlines) + y = y + yinc + } + + line(cx, ploty, cx, ploty + ploth, 32767) + line(plotx, cy, plotx + plotw, cy, 32767) +} + +# +# BIG LOOP - ask for equ, graph it +# + +makegrid +clearcmd = "clear" +while (1) { + rect(0, 0, 480, 40, 0) + print("f(x) = ") + Fx = gets() + + if (Fx == clearcmd) { + makegrid + } else { + # do function + x = xmin + while (x < xmax) { + y = solve(Fx) + y = 0 - y + if ((y >= ymin) & (y <= ymax)) { + pixel(cx + x * xinc, cy + y * yinc, 511) + } + x = x + 1 / xinc + } + } + + ppos(0, 0) +} + diff --git a/initrd/init b/initrd/init index a218474..17eb48d 100644 --- a/initrd/init +++ b/initrd/init @@ -1,67 +1,8 @@ -# graph area -plotx = 50 -ploty = 50 -plotw = 380 -ploth = 220 -cx = plotx + plotw / 2 -cy = ploty + ploth / 2 - -# graph range -xmin = 0 - 10 -xmax = 10 -ymin = 0 - 10 -ymax = 10 - -xinc = plotw / (xmax - xmin) -yinc = ploth / (ymax - ymin) - -mlines = color(3, 3, 3) - -func(makegrid) { - rect(plotx, ploty, plotw, ploth, 0) - - x = plotx - while (x <= plotx + plotw) { - line(x, ploty, x, ploty + ploth, mlines) - x = x + xinc - } - - y = ploty - while (y <= ploty + ploth) { - line(plotx, y, plotx + plotw, y, mlines) - y = y + yinc - } - - line(cx, ploty, cx, ploty + ploth, 32767) - line(plotx, cy, plotx + plotw, cy, 32767) -} - -# -# BIG LOOP - ask for equ, graph it -# - -makegrid -clearcmd = "clear" +input = 0 while (1) { - rect(0, 0, 480, 40, 0) - print("f(x) = ") - Fx = gets() - - if (Fx == clearcmd) { - makegrid - } else { - # do function - x = xmin - while (x < xmax) { - y = solve(Fx) - y = 0 - y - if ((y >= ymin) & (y <= ymax)) { - pixel(cx + x * xinc, cy + y * yinc, 511) - } - x = x + 1 / xinc - } + while (input == 0) { + input = getkey } - - ppos(0, 0) + print(input) + input = 0 } - diff --git a/libinterp.a b/libinterp.a index 123d9f4..c135dfc 100644 Binary files a/libinterp.a and b/libinterp.a differ diff --git a/src/keypad.c b/src/keypad.c index fad774a..06cef51 100644 --- a/src/keypad.c +++ b/src/keypad.c @@ -18,66 +18,105 @@ * along with this program. If not, see . */ +#include #include #include +#include -#define PIN_0 GPIO_PORT(B, 2) -#define PIN_1 GPIO_PORT(B, 1) -#define PIN_2 GPIO_PORT(A, 11) -#define PIN_3 GPIO_PORT(C, 8) -#define PIN_4 GPIO_PORT(B, 15) -#define PIN_5 GPIO_PORT(B, 12) -#define PIN_6 GPIO_PORT(C, 6) -#define PIN_7 GPIO_PORT(B, 14) -#define PIN_8 GPIO_PORT(B, 11) -#define PIN_9 GPIO_PORT(C, 5) -#define PIN_S GPIO_PORT(B, 13) -#define PIN_P GPIO_PORT(A, 12) +#define ROW_0 GPIO_PORT(B, 15) +#define ROW_1 GPIO_PORT(B, 14) +#define ROW_2 GPIO_PORT(B, 13) +#define ROW_3 GPIO_PORT(C, 4) +#define COL_0 GPIO_PORT(B, 1) +#define COL_1 GPIO_PORT(B, 2) +#define COL_2 GPIO_PORT(B, 11) +#define COL_3 GPIO_PORT(B, 12) +#define COL_4 GPIO_PORT(A, 11) + +#define ROWS 4 +#define COLS 5 typedef struct { GPIO_TypeDef *port; uint16_t pin; - uint16_t keycode; -} key_t; +} port_t; + +static const port_t keypad_rows[ROWS] = { + { ROW_0 }, { ROW_1 }, { ROW_2 }, { ROW_3 } +}; + +static const port_t keypad_cols[COLS] = { + { COL_0 }, { COL_1 }, { COL_2 }, { COL_3 }, { COL_4 } +}; -static const key_t keypad_map[12] = { - { PIN_0, K0 }, - { PIN_1, K1 }, - { PIN_2, K2 }, - { PIN_3, K3 }, - { PIN_4, K4 }, - { PIN_5, K5 }, - { PIN_6, K6 }, - { PIN_7, K7 }, - { PIN_8, K8 }, - { PIN_9, K9 }, - { PIN_S, KS }, - { PIN_P, KP } +static const int keypad_map[ROWS][COLS] = { + { '7', '8', '9', 'x', '/' }, + { '4', '5', '6', 'y', '*' }, + { '3', '2', '1', 'z', '-' }, + { '.', '0', '\b', '\n', '+' } }; +#define BUFFER_SIZE 8 +static char keypad_buffer = 'A';//[BUFFER_SIZE]; +//static int keypad_buffer_pos = -1; + +void keypad_task(void) +{ + unsigned int col = 0; + while (1) { + gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 1); + for (unsigned int row = 0; row < ROWS; row++) { + if (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) { + //if (keypad_buffer_pos < BUFFER_SIZE) + keypad_buffer/*[++keypad_buffer_pos]*/ = keypad_map[row][col]; + while (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) + delay(1); + break; + } + } + gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 0); + col++; + if (col == COLS) + col = 0; + + delay(10); + } +} + void keypad_init(void) { - for (uint8_t i = 0; i < 12; i++) { - GPIO_TypeDef *p = keypad_map[i].port; - uint16_t pin = keypad_map[i].pin; + for (uint8_t i = 0; i < ROWS; i++) { + GPIO_TypeDef *p = keypad_rows[i].port; + uint16_t pin = keypad_rows[i].pin; gpio_mode(p, pin, OUTPUT); + gpio_speed(p, pin, VERYHIGH); gpio_dout(p, pin, 0); gpio_mode(p, pin, INPUT); - //gpio_pupd(p, pin, PULLDOWN); + gpio_pupd(p, pin, PULLDOWN); } -} -uint16_t keypad_get(void) -{ - uint16_t state = 0; - for (uint8_t i = 0; i < 12; i++) { - if (gpio_din(keypad_map[i].port, keypad_map[i].pin)) - state |= keypad_map[i].keycode; + for (uint8_t i = 0; i < COLS; i++) { + GPIO_TypeDef *p = keypad_cols[i].port; + uint16_t pin = keypad_cols[i].pin; + gpio_mode(p, pin, OUTPUT); + gpio_speed(p, pin, VERYHIGH); + gpio_dout(p, pin, 0); } - return state; + + task_start(keypad_task, 1024); } -uint8_t keypad_isdown(uint16_t keycode) +int keypad_get(void) { - return (keypad_get() & keycode); + //if (keypad_buffer_pos < 0) + // return 0; + + //int key = keypad_buffer[0]; + //for (int i = keypad_buffer_pos - 1; i > 0; i--) + // keypad_buffer[i - 1] = keypad_buffer[i]; + //keypad_buffer_pos--; + //return key; + int ret = keypad_buffer; + keypad_buffer = 0; + return ret; } diff --git a/src/main.c b/src/main.c index 9844199..1e9b58d 100644 --- a/src/main.c +++ b/src/main.c @@ -18,22 +18,22 @@ * along with this program. If not, see . */ -#include #include -#include -#include -#include -#include #include #include +#include +#include +#include #include +#include +#include #include +#include +#include #include +#include #include -#include -#include -#include -#include +#include extern uint8_t __bss_end__; extern char *itoa(int, char *, int); @@ -54,12 +54,9 @@ int main(void) clock_init(); heap_init(&__bss_end__); gpio_init(); - keypad_init(); serial_init(); random_init(); - - //extern void keypad_init(void); - //keypad_init(); + keypad_init(); gpio_mode(GPIOA, 5, OUTPUT); @@ -77,6 +74,7 @@ void kmain(void) dsp_init(); dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0)); dsp_cursoron(); + keypad_init(); task_start(task_interpreter, 4096); /*char buf[2]; diff --git a/src/stdlib.c b/src/stdlib.c index f5f9cc8..950f356 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -62,7 +62,6 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...) break; case 'f': itoa((int)va_arg(args, double), nbuf, 10); - continue; break; default: buf[off++] = format[i];