RaidenX Iron Man
Dependencies: MMA8451Q USBDevice mbed
Fork of FRDM_MMA8451Q by
main.cpp@8:de23747efc37, 2015-09-20 (annotated)
- Committer:
- christianle
- Date:
- Sun Sep 20 23:25:54 2015 +0000
- Revision:
- 8:de23747efc37
- Parent:
- 5:bf5becf7469c
- Child:
- 9:07e3e2f6d6a9
Init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 2:41db78380a6e | 1 | #include "mbed.h" |
chris | 2:41db78380a6e | 2 | #include "MMA8451Q.h" |
christianle | 8:de23747efc37 | 3 | #include "USBKeyboard.h" |
christianle | 8:de23747efc37 | 4 | #include "USBHID.h" |
chris | 2:41db78380a6e | 5 | |
chris | 2:41db78380a6e | 6 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
christianle | 8:de23747efc37 | 7 | #define THRESHOLD 0.1 |
christianle | 8:de23747efc37 | 8 | |
christianle | 8:de23747efc37 | 9 | typedef struct { |
christianle | 8:de23747efc37 | 10 | unsigned char usage; |
christianle | 8:de23747efc37 | 11 | unsigned char modifier; |
christianle | 8:de23747efc37 | 12 | } KEYMAP; |
christianle | 8:de23747efc37 | 13 | |
christianle | 8:de23747efc37 | 14 | #define KEYMAP_SIZE (152) |
christianle | 8:de23747efc37 | 15 | const KEYMAP keymap[KEYMAP_SIZE] = { |
christianle | 8:de23747efc37 | 16 | {0, 0}, /* NUL */ |
christianle | 8:de23747efc37 | 17 | {0, 0}, /* SOH */ |
christianle | 8:de23747efc37 | 18 | {0, 0}, /* STX */ |
christianle | 8:de23747efc37 | 19 | {0, 0}, /* ETX */ |
christianle | 8:de23747efc37 | 20 | {0, 0}, /* EOT */ |
christianle | 8:de23747efc37 | 21 | {0, 0}, /* ENQ */ |
christianle | 8:de23747efc37 | 22 | {0, 0}, /* ACK */ |
christianle | 8:de23747efc37 | 23 | {0, 0}, /* BEL */ |
christianle | 8:de23747efc37 | 24 | {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */ |
christianle | 8:de23747efc37 | 25 | {0x2b, 0}, /* TAB */ /* Keyboard Tab */ |
christianle | 8:de23747efc37 | 26 | {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */ |
christianle | 8:de23747efc37 | 27 | {0, 0}, /* VT */ |
christianle | 8:de23747efc37 | 28 | {0, 0}, /* FF */ |
christianle | 8:de23747efc37 | 29 | {0, 0}, /* CR */ |
christianle | 8:de23747efc37 | 30 | {0, 0}, /* SO */ |
christianle | 8:de23747efc37 | 31 | {0, 0}, /* SI */ |
christianle | 8:de23747efc37 | 32 | {0, 0}, /* DEL */ |
christianle | 8:de23747efc37 | 33 | {0, 0}, /* DC1 */ |
christianle | 8:de23747efc37 | 34 | {0, 0}, /* DC2 */ |
christianle | 8:de23747efc37 | 35 | {0, 0}, /* DC3 */ |
christianle | 8:de23747efc37 | 36 | {0, 0}, /* DC4 */ |
christianle | 8:de23747efc37 | 37 | {0, 0}, /* NAK */ |
christianle | 8:de23747efc37 | 38 | {0, 0}, /* SYN */ |
christianle | 8:de23747efc37 | 39 | {0, 0}, /* ETB */ |
christianle | 8:de23747efc37 | 40 | {0, 0}, /* CAN */ |
christianle | 8:de23747efc37 | 41 | {0, 0}, /* EM */ |
christianle | 8:de23747efc37 | 42 | {0, 0}, /* SUB */ |
christianle | 8:de23747efc37 | 43 | {0, 0}, /* ESC */ |
christianle | 8:de23747efc37 | 44 | {0, 0}, /* FS */ |
christianle | 8:de23747efc37 | 45 | {0, 0}, /* GS */ |
christianle | 8:de23747efc37 | 46 | {0, 0}, /* RS */ |
christianle | 8:de23747efc37 | 47 | {0, 0}, /* US */ |
christianle | 8:de23747efc37 | 48 | {0x2c, 0}, /* */ |
christianle | 8:de23747efc37 | 49 | {0x1e, KEY_SHIFT}, /* ! */ |
christianle | 8:de23747efc37 | 50 | {0x34, KEY_SHIFT}, /* " */ |
christianle | 8:de23747efc37 | 51 | {0x20, KEY_SHIFT}, /* # */ |
christianle | 8:de23747efc37 | 52 | {0x21, KEY_SHIFT}, /* $ */ |
christianle | 8:de23747efc37 | 53 | {0x22, KEY_SHIFT}, /* % */ |
christianle | 8:de23747efc37 | 54 | {0x24, KEY_SHIFT}, /* & */ |
christianle | 8:de23747efc37 | 55 | {0x34, 0}, /* ' */ |
christianle | 8:de23747efc37 | 56 | {0x26, KEY_SHIFT}, /* ( */ |
christianle | 8:de23747efc37 | 57 | {0x27, KEY_SHIFT}, /* ) */ |
christianle | 8:de23747efc37 | 58 | {0x25, KEY_SHIFT}, /* * */ |
christianle | 8:de23747efc37 | 59 | {0x2e, KEY_SHIFT}, /* + */ |
christianle | 8:de23747efc37 | 60 | {0x36, 0}, /* , */ |
christianle | 8:de23747efc37 | 61 | {0x2d, 0}, /* - */ |
christianle | 8:de23747efc37 | 62 | {0x37, 0}, /* . */ |
christianle | 8:de23747efc37 | 63 | {0x38, 0}, /* / */ |
christianle | 8:de23747efc37 | 64 | {0x27, 0}, /* 0 */ |
christianle | 8:de23747efc37 | 65 | {0x1e, 0}, /* 1 */ |
christianle | 8:de23747efc37 | 66 | {0x1f, 0}, /* 2 */ |
christianle | 8:de23747efc37 | 67 | {0x20, 0}, /* 3 */ |
christianle | 8:de23747efc37 | 68 | {0x21, 0}, /* 4 */ |
christianle | 8:de23747efc37 | 69 | {0x22, 0}, /* 5 */ |
christianle | 8:de23747efc37 | 70 | {0x23, 0}, /* 6 */ |
christianle | 8:de23747efc37 | 71 | {0x24, 0}, /* 7 */ |
christianle | 8:de23747efc37 | 72 | {0x25, 0}, /* 8 */ |
christianle | 8:de23747efc37 | 73 | {0x26, 0}, /* 9 */ |
christianle | 8:de23747efc37 | 74 | {0x33, KEY_SHIFT}, /* : */ |
christianle | 8:de23747efc37 | 75 | {0x33, 0}, /* ; */ |
christianle | 8:de23747efc37 | 76 | {0x36, KEY_SHIFT}, /* < */ |
christianle | 8:de23747efc37 | 77 | {0x2e, 0}, /* = */ |
christianle | 8:de23747efc37 | 78 | {0x37, KEY_SHIFT}, /* > */ |
christianle | 8:de23747efc37 | 79 | {0x38, KEY_SHIFT}, /* ? */ |
christianle | 8:de23747efc37 | 80 | {0x1f, KEY_SHIFT}, /* @ */ |
christianle | 8:de23747efc37 | 81 | {0x04, KEY_SHIFT}, /* A */ |
christianle | 8:de23747efc37 | 82 | {0x05, KEY_SHIFT}, /* B */ |
christianle | 8:de23747efc37 | 83 | {0x06, KEY_SHIFT}, /* C */ |
christianle | 8:de23747efc37 | 84 | {0x07, KEY_SHIFT}, /* D */ |
christianle | 8:de23747efc37 | 85 | {0x08, KEY_SHIFT}, /* E */ |
christianle | 8:de23747efc37 | 86 | {0x09, KEY_SHIFT}, /* F */ |
christianle | 8:de23747efc37 | 87 | {0x0a, KEY_SHIFT}, /* G */ |
christianle | 8:de23747efc37 | 88 | {0x0b, KEY_SHIFT}, /* H */ |
christianle | 8:de23747efc37 | 89 | {0x0c, KEY_SHIFT}, /* I */ |
christianle | 8:de23747efc37 | 90 | {0x0d, KEY_SHIFT}, /* J */ |
christianle | 8:de23747efc37 | 91 | {0x0e, KEY_SHIFT}, /* K */ |
christianle | 8:de23747efc37 | 92 | {0x0f, KEY_SHIFT}, /* L */ |
christianle | 8:de23747efc37 | 93 | {0x10, KEY_SHIFT}, /* M */ |
christianle | 8:de23747efc37 | 94 | {0x11, KEY_SHIFT}, /* N */ |
christianle | 8:de23747efc37 | 95 | {0x12, KEY_SHIFT}, /* O */ |
christianle | 8:de23747efc37 | 96 | {0x13, KEY_SHIFT}, /* P */ |
christianle | 8:de23747efc37 | 97 | {0x14, KEY_SHIFT}, /* Q */ |
christianle | 8:de23747efc37 | 98 | {0x15, KEY_SHIFT}, /* R */ |
christianle | 8:de23747efc37 | 99 | {0x16, KEY_SHIFT}, /* S */ |
christianle | 8:de23747efc37 | 100 | {0x17, KEY_SHIFT}, /* T */ |
christianle | 8:de23747efc37 | 101 | {0x18, KEY_SHIFT}, /* U */ |
christianle | 8:de23747efc37 | 102 | {0x19, KEY_SHIFT}, /* V */ |
christianle | 8:de23747efc37 | 103 | {0x1a, KEY_SHIFT}, /* W */ |
christianle | 8:de23747efc37 | 104 | {0x1b, KEY_SHIFT}, /* X */ |
christianle | 8:de23747efc37 | 105 | {0x1c, KEY_SHIFT}, /* Y */ |
christianle | 8:de23747efc37 | 106 | {0x1d, KEY_SHIFT}, /* Z */ |
christianle | 8:de23747efc37 | 107 | {0x2f, 0}, /* [ */ |
christianle | 8:de23747efc37 | 108 | {0x31, 0}, /* \ */ |
christianle | 8:de23747efc37 | 109 | {0x30, 0}, /* ] */ |
christianle | 8:de23747efc37 | 110 | {0x23, KEY_SHIFT}, /* ^ */ |
christianle | 8:de23747efc37 | 111 | {0x2d, KEY_SHIFT}, /* _ */ |
christianle | 8:de23747efc37 | 112 | {0x35, 0}, /* ` */ |
christianle | 8:de23747efc37 | 113 | {0x04, 0}, /* a */ |
christianle | 8:de23747efc37 | 114 | {0x05, 0}, /* b */ |
christianle | 8:de23747efc37 | 115 | {0x06, 0}, /* c */ |
christianle | 8:de23747efc37 | 116 | {0x07, 0}, /* d */ |
christianle | 8:de23747efc37 | 117 | {0x08, 0}, /* e */ |
christianle | 8:de23747efc37 | 118 | {0x09, 0}, /* f */ |
christianle | 8:de23747efc37 | 119 | {0x0a, 0}, /* g */ |
christianle | 8:de23747efc37 | 120 | {0x0b, 0}, /* h */ |
christianle | 8:de23747efc37 | 121 | {0x0c, 0}, /* i */ |
christianle | 8:de23747efc37 | 122 | {0x0d, 0}, /* j */ |
christianle | 8:de23747efc37 | 123 | {0x0e, 0}, /* k */ |
christianle | 8:de23747efc37 | 124 | {0x0f, 0}, /* l */ |
christianle | 8:de23747efc37 | 125 | {0x10, 0}, /* m */ |
christianle | 8:de23747efc37 | 126 | {0x11, 0}, /* n */ |
christianle | 8:de23747efc37 | 127 | {0x12, 0}, /* o */ |
christianle | 8:de23747efc37 | 128 | {0x13, 0}, /* p */ |
christianle | 8:de23747efc37 | 129 | {0x14, 0}, /* q */ |
christianle | 8:de23747efc37 | 130 | {0x15, 0}, /* r */ |
christianle | 8:de23747efc37 | 131 | {0x16, 0}, /* s */ |
christianle | 8:de23747efc37 | 132 | {0x17, 0}, /* t */ |
christianle | 8:de23747efc37 | 133 | {0x18, 0}, /* u */ |
christianle | 8:de23747efc37 | 134 | {0x19, 0}, /* v */ |
christianle | 8:de23747efc37 | 135 | {0x1a, 0}, /* w */ |
christianle | 8:de23747efc37 | 136 | {0x1b, 0}, /* x */ |
christianle | 8:de23747efc37 | 137 | {0x1c, 0}, /* y */ |
christianle | 8:de23747efc37 | 138 | {0x1d, 0}, /* z */ |
christianle | 8:de23747efc37 | 139 | {0x2f, KEY_SHIFT}, /* { */ |
christianle | 8:de23747efc37 | 140 | {0x31, KEY_SHIFT}, /* | */ |
christianle | 8:de23747efc37 | 141 | {0x30, KEY_SHIFT}, /* } */ |
christianle | 8:de23747efc37 | 142 | {0x35, KEY_SHIFT}, /* ~ */ |
christianle | 8:de23747efc37 | 143 | {0,0}, /* DEL */ |
christianle | 8:de23747efc37 | 144 | |
christianle | 8:de23747efc37 | 145 | {0x3a, 0}, /* F1 */ |
christianle | 8:de23747efc37 | 146 | {0x3b, 0}, /* F2 */ |
christianle | 8:de23747efc37 | 147 | {0x3c, 0}, /* F3 */ |
christianle | 8:de23747efc37 | 148 | {0x3d, 0}, /* F4 */ |
christianle | 8:de23747efc37 | 149 | {0x3e, 0}, /* F5 */ |
christianle | 8:de23747efc37 | 150 | {0x3f, 0}, /* F6 */ |
christianle | 8:de23747efc37 | 151 | {0x40, 0}, /* F7 */ |
christianle | 8:de23747efc37 | 152 | {0x41, 0}, /* F8 */ |
christianle | 8:de23747efc37 | 153 | {0x42, 0}, /* F9 */ |
christianle | 8:de23747efc37 | 154 | {0x43, 0}, /* F10 */ |
christianle | 8:de23747efc37 | 155 | {0x44, 0}, /* F11 */ |
christianle | 8:de23747efc37 | 156 | {0x45, 0}, /* F12 */ |
christianle | 8:de23747efc37 | 157 | |
christianle | 8:de23747efc37 | 158 | {0x46, 0}, /* PRINT_SCREEN */ |
christianle | 8:de23747efc37 | 159 | {0x47, 0}, /* SCROLL_LOCK */ |
christianle | 8:de23747efc37 | 160 | {0x39, 0}, /* CAPS_LOCK */ |
christianle | 8:de23747efc37 | 161 | {0x53, 0}, /* NUM_LOCK */ |
christianle | 8:de23747efc37 | 162 | {0x49, 0}, /* INSERT */ |
christianle | 8:de23747efc37 | 163 | {0x4a, 0}, /* HOME */ |
christianle | 8:de23747efc37 | 164 | {0x4b, 0}, /* PAGE_UP */ |
christianle | 8:de23747efc37 | 165 | {0x4e, 0}, /* PAGE_DOWN */ |
christianle | 8:de23747efc37 | 166 | |
christianle | 8:de23747efc37 | 167 | {0x4f, 0}, /* RIGHT_ARROW */ |
christianle | 8:de23747efc37 | 168 | {0x50, 0}, /* LEFT_ARROW */ |
christianle | 8:de23747efc37 | 169 | {0x51, 0}, /* DOWN_ARROW */ |
christianle | 8:de23747efc37 | 170 | {0x52, 0}, /* UP_ARROW */ |
christianle | 8:de23747efc37 | 171 | }; |
chris | 2:41db78380a6e | 172 | |
chris | 4:367de1084ea9 | 173 | int main(void) { |
emilmont | 5:bf5becf7469c | 174 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
emilmont | 5:bf5becf7469c | 175 | PwmOut rled(LED_RED); |
emilmont | 5:bf5becf7469c | 176 | PwmOut bled(LED_BLUE); |
christianle | 8:de23747efc37 | 177 | |
christianle | 8:de23747efc37 | 178 | Serial mac(USBTX, USBRX); |
christianle | 8:de23747efc37 | 179 | |
christianle | 8:de23747efc37 | 180 | USBKeyboard keyboard; |
christianle | 8:de23747efc37 | 181 | HID_REPORT report; |
christianle | 8:de23747efc37 | 182 | |
christianle | 8:de23747efc37 | 183 | report.data[0] = 1; // this is the USB ID |
christianle | 8:de23747efc37 | 184 | report.data[1] = 0; // this is a modifier key (ie: ctrl/shift/alt) |
christianle | 8:de23747efc37 | 185 | report.data[2] = 0; // i haven't figured this one out |
christianle | 8:de23747efc37 | 186 | report.data[3] = 0; // UP |
christianle | 8:de23747efc37 | 187 | report.data[4] = 0; // DOWN |
christianle | 8:de23747efc37 | 188 | report.data[5] = 0; // LEFT |
christianle | 8:de23747efc37 | 189 | report.data[6] = 0; // RIGHT |
christianle | 8:de23747efc37 | 190 | report.data[7] = keymap['z'].usage; // SHOOT |
christianle | 8:de23747efc37 | 191 | report.data[8] = 0; // BOMB |
christianle | 8:de23747efc37 | 192 | report.length = 9; |
chris | 4:367de1084ea9 | 193 | |
emilmont | 5:bf5becf7469c | 194 | while (true) { |
christianle | 8:de23747efc37 | 195 | float x = acc.getAccX(); |
christianle | 8:de23747efc37 | 196 | float y = acc.getAccY(); |
christianle | 8:de23747efc37 | 197 | |
christianle | 8:de23747efc37 | 198 | if (x < 0 - THRESHOLD) { |
christianle | 8:de23747efc37 | 199 | mac.printf("Left\r\n"); |
christianle | 8:de23747efc37 | 200 | report.data[6] = 0; |
christianle | 8:de23747efc37 | 201 | report.data[5] = keymap[LEFT_ARROW].usage; |
christianle | 8:de23747efc37 | 202 | } |
christianle | 8:de23747efc37 | 203 | |
christianle | 8:de23747efc37 | 204 | if (x > 0 + THRESHOLD) { |
christianle | 8:de23747efc37 | 205 | mac.printf("Right\r\n"); |
christianle | 8:de23747efc37 | 206 | report.data[6] = keymap[RIGHT_ARROW].usage; |
christianle | 8:de23747efc37 | 207 | report.data[5] = 0; |
christianle | 8:de23747efc37 | 208 | } |
christianle | 8:de23747efc37 | 209 | |
christianle | 8:de23747efc37 | 210 | if (y < 0 - THRESHOLD) { |
christianle | 8:de23747efc37 | 211 | mac.printf("Up\r\n"); |
christianle | 8:de23747efc37 | 212 | report.data[3] = keymap[UP_ARROW].usage; |
christianle | 8:de23747efc37 | 213 | report.data[4] = 0; |
christianle | 8:de23747efc37 | 214 | } |
christianle | 8:de23747efc37 | 215 | |
christianle | 8:de23747efc37 | 216 | if (y > 0 + THRESHOLD) { |
christianle | 8:de23747efc37 | 217 | mac.printf("Down\r\n"); |
christianle | 8:de23747efc37 | 218 | report.data[3] = 0; |
christianle | 8:de23747efc37 | 219 | report.data[4] = keymap[DOWN_ARROW].usage; |
christianle | 8:de23747efc37 | 220 | } |
christianle | 8:de23747efc37 | 221 | |
chris | 3:f2d3e041d8f2 | 222 | rled = 1.0 - abs(acc.getAccX()); |
christianle | 8:de23747efc37 | 223 | bled = 1.0 - abs(acc.getAccY()); |
christianle | 8:de23747efc37 | 224 | |
christianle | 8:de23747efc37 | 225 | keyboard.send(&report); |
chris | 3:f2d3e041d8f2 | 226 | wait(0.1); |
chris | 2:41db78380a6e | 227 | } |
chris | 2:41db78380a6e | 228 | } |