Acorn Electron keyboard scanner, turns an old Acorn Electron into a USB keyboard.

Dependencies:   USBDevice mbed

Revision:
0:9fd3dad2dc25
Child:
1:84cd616cc684
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Apr 30 21:26:27 2014 +0000
@@ -0,0 +1,112 @@
+#include "mbed.h"
+#include "USBKeyboard.h"
+#include "hid_keys.h"
+
+#define MAX_ROWS 5
+#define MAX_COLS 14
+#define REPORT_LEN 9
+#define REPORT_ID_KEYBOARD 1
+
+const uint8_t hid_keys[MAX_ROWS * MAX_COLS] = 
+{
+    KEY_PERIOD,     KEY_L,          KEY_O,      KEY_9,      KEY_BACKSPACE,
+    KEY_SLASH,      KEY_SEMICOLON,  KEY_P,      KEY_0,      KEY_NONE,
+    KEY_NONE,       KEY_EQUALS,     KEY_UP_ARROW,   KEY_MINUS, KEY_NONE,
+    KEY_DELETE,     KEY_ENTER,      KEY_DOWN_ARROW, KEY_LEFT_ARROW, KEY_NONE,
+    KEY_SPACE,      KEY_NONE,       KEY_OPEN_SQUARE, KEY_RIGHT_ARROW, KEY_NONE,
+    KEY_COMMA,      KEY_K,          KEY_I,      KEY_8,      KEY_NONE,
+    KEY_M,          KEY_J,          KEY_U,      KEY_7,      KEY_NONE,
+    KEY_N,          KEY_H,          KEY_Y,      KEY_6,      KEY_NONE,
+    KEY_B,          KEY_G,          KEY_T,      KEY_5,      KEY_NONE,
+    KEY_V,          KEY_F,          KEY_R,      KEY_4,      KEY_NONE,
+    KEY_C,          KEY_D,          KEY_E,      KEY_3,      KEY_NONE,
+    KEY_X,          KEY_S,          KEY_W,      KEY_2,      KEY_NONE,
+    KEY_Z,          KEY_A,          KEY_Q,      KEY_1,      KEY_NONE,
+    KEY_LEFT_SHIFT, KEY_LEFT_CTRL,  KEY_TAB,    KEY_ESC,    KEY_NONE,
+};
+
+
+BusOut leds(LED1, LED2, LED3);
+
+BusOut scanCols(
+    PTC10,PTC8, 
+    PTC6, PTA5,   
+    PTC5, PTA4,  
+    PTC4, PTA12,
+    PTC3, PTD4,   
+    PTC0, PTA2,  
+    PTC7, PTA1  
+    );
+
+BusIn inRows(
+    PTB10,
+    PTB11,
+    PTE2,
+    PTE3,
+    PTE5 );
+
+DigitalOut extLed(PTE4);
+
+USBKeyboard kbd;
+
+static int scanColumn(int col)
+{
+    int rowBits;
+    // Drive output low to scan
+    scanCols.write(0x3FFF ^ (1 << col));
+    leds.write(col >> 1);
+    wait(0.01);
+    rowBits = inRows.read();
+    scanCols.write(0x3FFF);
+    // Inputs also active-low
+    return rowBits ^ 0x1F;
+}
+    
+int main()
+{
+    // Setup
+    inRows.mode(PullUp);
+    extLed = 1;
+
+    // Run loop
+    while(1)
+    {
+        int col, ocount;
+        HID_REPORT report;
+        
+        report.data[0] = REPORT_ID_KEYBOARD;
+        report.data[1] = 0; // modifiers
+        report.data[2] = 0;
+        ocount = 3;
+        
+        for (col=0; col < MAX_COLS; col++)
+        {
+            int row;
+            int rowBits = scanColumn(col);
+            if ( !rowBits )
+              continue;
+            
+            for (row=0; row < MAX_ROWS; row++)
+            {
+              if ( rowBits & (1 << row) )
+              {
+                uint8_t key = hid_keys[col * MAX_ROWS + row];
+                if ( IS_MODIFIER(key) )
+                  report.data[1] |= MODIFIER_BIT(key);
+                else if ( key != KEY_NONE )
+                {
+                  if ( ocount < REPORT_LEN )
+                    report.data[ocount++] = key;
+                }
+                //kbd.printf("c%dr%d ", col, row);
+              }
+            }
+        }
+
+        while( ocount < REPORT_LEN )
+          report.data[ocount++] = KEY_NONE;
+            
+        report.length = REPORT_LEN;  
+        kbd.send(&report);
+    }
+}