Cleanup and let gcc optimise the cases where row == 0 and row != 0
[uccvend-snackrom.git] / ROM2 / keypad.c
index e87715d..5bd5e2e 100644 (file)
@@ -1,44 +1,83 @@
+#include "chime.h"
 #include "vend.h"
+#include "keypad.h"
+#include "display_basic.h"
 
-u8 last_key; /* the last key registered */
-u8 curr_key; /* the key currently being held down */
+volatile u8 last_key; /* the last key registered */
+volatile bool new_key;
 
 /* first 8 from the first row, then 3 from the second row */
 /* keys are 1-9, 0, reset */
-const u8 keymap0[8] = {8, 7, 6, 5, 4, 3, 2, 1};
-const u8 keymap1[3] = {11, 10, 9};
+const u8 keymap0[8] = {KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8};
+const u8 keymap1[3] = {KEY_9, KEY_0, KEY_RESET};
+
+#define        NO_KEY  8
+
+/* reads the keypad and returns the bit number that was turned on in the shift
+ * register (from 0..7). If no bits were turned on, it returns 8 (aka NO_KEY)
+ */
+extern inline u8 keypad_read_row(const u8 row) {
+       u8 i, num;
 
-extern inline int keypad_read_row(int row) {
-       int i, num;
        if (row)
-               _io_ports[M6811_PORTD] |= PORTD_KEYPAD_ROW;
+               bset((void*)&_io_ports[M6811_PORTD], PORTD_KEYPAD_ROW);
        else
-               _io_ports[M6811_PORTD] &= ~PORTD_KEYPAD_ROW;
+               bclr((void*)&_io_ports[M6811_PORTD], PORTD_KEYPAD_ROW);
+
+       spi_enable();
+
+       bset_misc_output(A3800_KEYPAD_STROBE);
+       bclr_misc_output(A3800_KEYPAD_STROBE);
 
        bclr_misc_output(A3800_DISPLAY_WRITE);  /* disable the display clock */
 
-       _io_ports[M6811_SPDR] = 0;
-       while(!(_io_ports[M6811_SPDR]&M6811_SPIE)); /* wait for completion */
+       _io_ports[M6811_SPDR] = 0x55; /* doesn't matter what we send. */
+       while(!(_io_ports[M6811_SPSR]&M6811_SPIF)); /* wait for completion */
+
+       /* SPDR read to clear SPIF flag is performed below: */
+       i = _io_ports[M6811_SPDR];
+
+       num = 0;
+       while (((i & 0x80) == 0) && (num < 8)) {
+               i = i << 1;
+               num++;
+       }
 
-       for (i = _io_ports[M6811_SPDR], num = 0;
-               (i&0x1) == 0;
-               i=i>>1, num++);
+       spi_disable();
 
        return num;
 }
 
-/* returns a key 0..9 or 11 for reset */
+/* sets last_key to 1..10 or 11 for reset */
 void keypad_read() {
        /* FIXME: need to do debouncing of some sort? */
-       int key;
+       u8 key;
        key = keypad_read_row(0);
-       if (!key) {
+       if (NO_KEY == key) {
                key = keypad_read_row(1);
-               curr_key = keymap0[key];
-       }
-       curr_key = keymap1[key];
+               if (key <= 2)
+                       key = keymap1[key];
+               else
+                       key = 0;
+       } else
+               key = keymap0[key];
 
-       if (curr_key != last_key) {
-               last_key = curr_key;
+       if (key != last_key) {
+               last_key = key;
+               if (key != 0) {
+                       new_key = 1;
+                       chime_start();
+               }
        }
 }
+
+bool keypad_pressed() {
+       if (!new_key) return 0;
+       new_key = 0;
+       return 1;
+}
+
+void keypad_init() {
+       last_key = 0;
+       new_key = 0;
+}

UCC git Repository :: git.ucc.asn.au