Control the wondrous spinning-frog game of Zuma's Revenge with a rotating chair and an Airzooka. Maps compass rotation, flex sensor and push button input to USB actions to control Zuma's Revenge (http://www.popcap.com/games/zumas-revenge/online)

Dependencies:   LSM303DLHC mbed

Note that content for USB HID and USB Device is actually from the USBDevice mbed library. However, we made a couple of small changes to this library (allowing USB clicks at a particular location) that required us to break it off from the main project if we wanted to publish without pushing upstream.

Committer:
andrewhead
Date:
Mon Sep 29 01:12:20 2014 +0000
Revision:
0:4df415dde990
Initial Commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewhead 0:4df415dde990 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
andrewhead 0:4df415dde990 2 *
andrewhead 0:4df415dde990 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
andrewhead 0:4df415dde990 4 * and associated documentation files (the "Software"), to deal in the Software without
andrewhead 0:4df415dde990 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
andrewhead 0:4df415dde990 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
andrewhead 0:4df415dde990 7 * Software is furnished to do so, subject to the following conditions:
andrewhead 0:4df415dde990 8 *
andrewhead 0:4df415dde990 9 * The above copyright notice and this permission notice shall be included in all copies or
andrewhead 0:4df415dde990 10 * substantial portions of the Software.
andrewhead 0:4df415dde990 11 *
andrewhead 0:4df415dde990 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
andrewhead 0:4df415dde990 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
andrewhead 0:4df415dde990 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
andrewhead 0:4df415dde990 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
andrewhead 0:4df415dde990 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
andrewhead 0:4df415dde990 17 */
andrewhead 0:4df415dde990 18
andrewhead 0:4df415dde990 19 #ifndef USBKEYBOARD_H
andrewhead 0:4df415dde990 20 #define USBKEYBOARD_H
andrewhead 0:4df415dde990 21
andrewhead 0:4df415dde990 22 #include "USBHID.h"
andrewhead 0:4df415dde990 23 #include "Stream.h"
andrewhead 0:4df415dde990 24
andrewhead 0:4df415dde990 25 /* Modifiers */
andrewhead 0:4df415dde990 26 enum MODIFIER_KEY {
andrewhead 0:4df415dde990 27 KEY_CTRL = 1,
andrewhead 0:4df415dde990 28 KEY_SHIFT = 2,
andrewhead 0:4df415dde990 29 KEY_ALT = 4,
andrewhead 0:4df415dde990 30 };
andrewhead 0:4df415dde990 31
andrewhead 0:4df415dde990 32
andrewhead 0:4df415dde990 33 enum MEDIA_KEY {
andrewhead 0:4df415dde990 34 KEY_NEXT_TRACK, /*!< next Track Button */
andrewhead 0:4df415dde990 35 KEY_PREVIOUS_TRACK, /*!< Previous track Button */
andrewhead 0:4df415dde990 36 KEY_STOP, /*!< Stop Button */
andrewhead 0:4df415dde990 37 KEY_PLAY_PAUSE, /*!< Play/Pause Button */
andrewhead 0:4df415dde990 38 KEY_MUTE, /*!< Mute Button */
andrewhead 0:4df415dde990 39 KEY_VOLUME_UP, /*!< Volume Up Button */
andrewhead 0:4df415dde990 40 KEY_VOLUME_DOWN, /*!< Volume Down Button */
andrewhead 0:4df415dde990 41 };
andrewhead 0:4df415dde990 42
andrewhead 0:4df415dde990 43 enum FUNCTION_KEY {
andrewhead 0:4df415dde990 44 KEY_F1 = 128, /* F1 key */
andrewhead 0:4df415dde990 45 KEY_F2, /* F2 key */
andrewhead 0:4df415dde990 46 KEY_F3, /* F3 key */
andrewhead 0:4df415dde990 47 KEY_F4, /* F4 key */
andrewhead 0:4df415dde990 48 KEY_F5, /* F5 key */
andrewhead 0:4df415dde990 49 KEY_F6, /* F6 key */
andrewhead 0:4df415dde990 50 KEY_F7, /* F7 key */
andrewhead 0:4df415dde990 51 KEY_F8, /* F8 key */
andrewhead 0:4df415dde990 52 KEY_F9, /* F9 key */
andrewhead 0:4df415dde990 53 KEY_F10, /* F10 key */
andrewhead 0:4df415dde990 54 KEY_F11, /* F11 key */
andrewhead 0:4df415dde990 55 KEY_F12, /* F12 key */
andrewhead 0:4df415dde990 56
andrewhead 0:4df415dde990 57 KEY_PRINT_SCREEN, /* Print Screen key */
andrewhead 0:4df415dde990 58 KEY_SCROLL_LOCK, /* Scroll lock */
andrewhead 0:4df415dde990 59 KEY_CAPS_LOCK, /* caps lock */
andrewhead 0:4df415dde990 60 KEY_NUM_LOCK, /* num lock */
andrewhead 0:4df415dde990 61 KEY_INSERT, /* Insert key */
andrewhead 0:4df415dde990 62 KEY_HOME, /* Home key */
andrewhead 0:4df415dde990 63 KEY_PAGE_UP, /* Page Up key */
andrewhead 0:4df415dde990 64 KEY_PAGE_DOWN, /* Page Down key */
andrewhead 0:4df415dde990 65
andrewhead 0:4df415dde990 66 RIGHT_ARROW, /* Right arrow */
andrewhead 0:4df415dde990 67 LEFT_ARROW, /* Left arrow */
andrewhead 0:4df415dde990 68 DOWN_ARROW, /* Down arrow */
andrewhead 0:4df415dde990 69 UP_ARROW, /* Up arrow */
andrewhead 0:4df415dde990 70 };
andrewhead 0:4df415dde990 71
andrewhead 0:4df415dde990 72 /**
andrewhead 0:4df415dde990 73 * USBKeyboard example
andrewhead 0:4df415dde990 74 * @code
andrewhead 0:4df415dde990 75 *
andrewhead 0:4df415dde990 76 * #include "mbed.h"
andrewhead 0:4df415dde990 77 * #include "USBKeyboard.h"
andrewhead 0:4df415dde990 78 *
andrewhead 0:4df415dde990 79 * USBKeyboard key;
andrewhead 0:4df415dde990 80 *
andrewhead 0:4df415dde990 81 * int main(void)
andrewhead 0:4df415dde990 82 * {
andrewhead 0:4df415dde990 83 * while (1)
andrewhead 0:4df415dde990 84 * {
andrewhead 0:4df415dde990 85 * key.printf("Hello World\r\n");
andrewhead 0:4df415dde990 86 * wait(1);
andrewhead 0:4df415dde990 87 * }
andrewhead 0:4df415dde990 88 * }
andrewhead 0:4df415dde990 89 *
andrewhead 0:4df415dde990 90 * @endcode
andrewhead 0:4df415dde990 91 */
andrewhead 0:4df415dde990 92 class USBKeyboard: public USBHID, public Stream {
andrewhead 0:4df415dde990 93 public:
andrewhead 0:4df415dde990 94
andrewhead 0:4df415dde990 95 /**
andrewhead 0:4df415dde990 96 * Constructor
andrewhead 0:4df415dde990 97 *
andrewhead 0:4df415dde990 98 *
andrewhead 0:4df415dde990 99 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
andrewhead 0:4df415dde990 100 * @param vendor_id Your vendor_id (default: 0x1235)
andrewhead 0:4df415dde990 101 * @param product_id Your product_id (default: 0x0050)
andrewhead 0:4df415dde990 102 * @param product_release Your preoduct_release (default: 0x0001)
andrewhead 0:4df415dde990 103 *
andrewhead 0:4df415dde990 104 */
andrewhead 0:4df415dde990 105 USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001):
andrewhead 0:4df415dde990 106 USBHID(0, 0, vendor_id, product_id, product_release, false) {
andrewhead 0:4df415dde990 107 lock_status = 0;
andrewhead 0:4df415dde990 108 connect();
andrewhead 0:4df415dde990 109 };
andrewhead 0:4df415dde990 110
andrewhead 0:4df415dde990 111 /**
andrewhead 0:4df415dde990 112 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
andrewhead 0:4df415dde990 113 *
andrewhead 0:4df415dde990 114 * @code
andrewhead 0:4df415dde990 115 * //To send CTRL + s (save)
andrewhead 0:4df415dde990 116 * keyboard.keyCode('s', KEY_CTRL);
andrewhead 0:4df415dde990 117 * @endcode
andrewhead 0:4df415dde990 118 *
andrewhead 0:4df415dde990 119 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
andrewhead 0:4df415dde990 120 * @param key character to send
andrewhead 0:4df415dde990 121 * @returns true if there is no error, false otherwise
andrewhead 0:4df415dde990 122 */
andrewhead 0:4df415dde990 123 bool keyCode(uint8_t key, uint8_t modifier = 0);
andrewhead 0:4df415dde990 124
andrewhead 0:4df415dde990 125 /**
andrewhead 0:4df415dde990 126 * Send a character
andrewhead 0:4df415dde990 127 *
andrewhead 0:4df415dde990 128 * @param c character to be sent
andrewhead 0:4df415dde990 129 * @returns true if there is no error, false otherwise
andrewhead 0:4df415dde990 130 */
andrewhead 0:4df415dde990 131 virtual int _putc(int c);
andrewhead 0:4df415dde990 132
andrewhead 0:4df415dde990 133 /**
andrewhead 0:4df415dde990 134 * Control media keys
andrewhead 0:4df415dde990 135 *
andrewhead 0:4df415dde990 136 * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
andrewhead 0:4df415dde990 137 * @returns true if there is no error, false otherwise
andrewhead 0:4df415dde990 138 */
andrewhead 0:4df415dde990 139 bool mediaControl(MEDIA_KEY key);
andrewhead 0:4df415dde990 140
andrewhead 0:4df415dde990 141 /*
andrewhead 0:4df415dde990 142 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
andrewhead 0:4df415dde990 143 *
andrewhead 0:4df415dde990 144 * @returns pointer to the report descriptor
andrewhead 0:4df415dde990 145 */
andrewhead 0:4df415dde990 146 virtual uint8_t * reportDesc();
andrewhead 0:4df415dde990 147
andrewhead 0:4df415dde990 148 /*
andrewhead 0:4df415dde990 149 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
andrewhead 0:4df415dde990 150 *
andrewhead 0:4df415dde990 151 * @returns if handle by subclass, return true
andrewhead 0:4df415dde990 152 */
andrewhead 0:4df415dde990 153 virtual bool EP1_OUT_callback();
andrewhead 0:4df415dde990 154
andrewhead 0:4df415dde990 155 /**
andrewhead 0:4df415dde990 156 * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important:
andrewhead 0:4df415dde990 157 * - First bit: NUM_LOCK
andrewhead 0:4df415dde990 158 * - Second bit: CAPS_LOCK
andrewhead 0:4df415dde990 159 * - Third bit: SCROLL_LOCK
andrewhead 0:4df415dde990 160 *
andrewhead 0:4df415dde990 161 * @returns status of lock keys
andrewhead 0:4df415dde990 162 */
andrewhead 0:4df415dde990 163 uint8_t lockStatus();
andrewhead 0:4df415dde990 164
andrewhead 0:4df415dde990 165 protected:
andrewhead 0:4df415dde990 166 /*
andrewhead 0:4df415dde990 167 * Get configuration descriptor
andrewhead 0:4df415dde990 168 *
andrewhead 0:4df415dde990 169 * @returns pointer to the configuration descriptor
andrewhead 0:4df415dde990 170 */
andrewhead 0:4df415dde990 171 virtual uint8_t * configurationDesc();
andrewhead 0:4df415dde990 172
andrewhead 0:4df415dde990 173 private:
andrewhead 0:4df415dde990 174 //dummy otherwise it doesn,t compile (we must define all methods of an abstract class)
andrewhead 0:4df415dde990 175 virtual int _getc() {
andrewhead 0:4df415dde990 176 return -1;
andrewhead 0:4df415dde990 177 };
andrewhead 0:4df415dde990 178
andrewhead 0:4df415dde990 179 uint8_t lock_status;
andrewhead 0:4df415dde990 180
andrewhead 0:4df415dde990 181 };
andrewhead 0:4df415dde990 182
andrewhead 0:4df415dde990 183 #endif