]> code.bitgloo.com Git - clyne/calculator.git/commitdiff
keypad driver progress
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 26 Mar 2018 16:29:10 +0000 (12:29 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 26 Mar 2018 16:29:10 +0000 (12:29 -0400)
include/keypad.h
initrd/graph [new file with mode: 0644]
initrd/init
libinterp.a
src/keypad.c
src/main.c
src/stdlib.c

index 2e5fa9cacc316b01c88cc0468843cf0cdcacc7d5..3dbebdf8ccc392f8a27c50145bf30596e109c0b4 100644 (file)
 
 #include <stdint.h>
 
-#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 (file)
index 0000000..a218474
--- /dev/null
@@ -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)
+}
+
index a218474d5565414f97ca51e1ad627e56f3e344c4..17eb48d2a17a9d308a3184cc9309a17c13abe77f 100644 (file)
@@ -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
 }
-
index 123d9f418c141931bf4de8feec44f22e84b421e5..c135dfcada4726243988951d049d3826890b2386 100644 (file)
Binary files a/libinterp.a and b/libinterp.a differ
index fad774a895c99907be3cb3fe01affdfa44ea55b5..06cef5196f6acd8fc12987600165c83ac048ac52 100644 (file)
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#include <clock.h>
 #include <keypad.h>
 #include <gpio.h>
+#include <task.h>
 
-#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;
 }
index 98441996a8b8b32729460b0c0c1797cb893b43fa..1e9b58d529be4cd6c9741cbddf4e25b638548bb3 100644 (file)
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.\r
  */\r
 \r
-#include <stm32l476xx.h>\r
 #include <clock.h>\r
-#include <heap.h>\r
-#include <task.h>\r
-#include <gpio.h>\r
-#include <lcd.h>\r
 #include <display.h>\r
 #include <display_draw.h>\r
+#include <flash.h>\r
+#include <gpio.h>\r
+#include <heap.h>\r
 #include <initrd.h>\r
+#include <keypad.h>\r
+#include <lcd.h>\r
 #include <parser.h>\r
+#include <random.h>\r
+#include <script.h>\r
 #include <serial.h>\r
+#include <stm32l476xx.h>\r
 #include <string.h>\r
-#include <script.h>\r
-#include <random.h>\r
-#include <keypad.h>\r
-#include <flash.h>\r
+#include <task.h>\r
 \r
 extern uint8_t __bss_end__;\r
 extern char *itoa(int, char *, int);\r
@@ -54,12 +54,9 @@ int main(void)
        clock_init();\r
        heap_init(&__bss_end__);\r
        gpio_init();\r
-       keypad_init();\r
        serial_init();\r
        random_init();\r
-\r
-       //extern void keypad_init(void);\r
-       //keypad_init();\r
+       keypad_init();\r
 \r
        gpio_mode(GPIOA, 5, OUTPUT);\r
 \r
@@ -77,6 +74,7 @@ void kmain(void)
        dsp_init();\r
        dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0));\r
        dsp_cursoron();\r
+       keypad_init();\r
        task_start(task_interpreter, 4096);\r
 \r
        /*char buf[2];\r
index f5f9cc8e34d7eda887b1c120635a995578f81db5..950f3567cee60dc04dc947200a22c6193789cd4c 100644 (file)
@@ -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];