Cleanup gcc compile warnings
[uccvend-snackrom.git] / ROM2 / keypad.c
index 5ab9b45..cc6093d 100644 (file)
@@ -1,19 +1,64 @@
+#include "chime.h"
 #include "vend.h"
+#include "keypad.h"
+
+u8 last_key; /* the last key registered */
+bool new_key = 0;
+
+/* first 8 from the first row, then 3 from the second row */
+/* keys are 1-9, 0, reset */
+const u8 keymap0[8] = {KEY_8, KEY_7, KEY_6, KEY_5, KEY_4, KEY_3, KEY_2, KEY_1};
+const u8 keymap1[3] = {KEY_RESET, KEY_0, KEY_9};
+
+#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(u8 row) {
+       u8 i, num;
+       if (row)
+               bset((void*)&_io_ports[M6811_PORTD], PORTD_KEYPAD_ROW);
+       else
+               bclr((void*)&_io_ports[M6811_PORTD], PORTD_KEYPAD_ROW);
 
-int keypad_read_row() {
        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 */
 
-       return _io_ports[M6811_SPDR];
+       for (i = _io_ports[M6811_SPDR], num = 0;
+               ((i&0x1) == 0) && (num < 7);
+               i=i>>1, num++);
+
+       return num;
 }
 
-/* row is 0 or 1 */
-void keypad_select_row(int row) {
-       if (row)
-               _io_ports[M6811_PORTD] |= PORTD_KEYPAD_ROW;
-       else
-               _io_ports[M6811_PORTD] &= ~PORTD_KEYPAD_ROW;
+/* returns a key 1..10 or 11 for reset */
+void keypad_read() {
+       /* FIXME: need to do debouncing of some sort? */
+       u8 key;
+       key = keypad_read_row(0);
+       if (NO_KEY == key) {
+               key = keypad_read_row(1);
+               if (key <= 2)
+                       key = keymap1[key];
+               else
+                       key = 0;
+       } else
+               key = keymap0[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;
+}

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