Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Perijah 0:ecc3925d3f8c 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Perijah 0:ecc3925d3f8c 2 *
Perijah 0:ecc3925d3f8c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Perijah 0:ecc3925d3f8c 4 * and associated documentation files (the "Software"), to deal in the Software without
Perijah 0:ecc3925d3f8c 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Perijah 0:ecc3925d3f8c 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Perijah 0:ecc3925d3f8c 7 * Software is furnished to do so, subject to the following conditions:
Perijah 0:ecc3925d3f8c 8 *
Perijah 0:ecc3925d3f8c 9 * The above copyright notice and this permission notice shall be included in all copies or
Perijah 0:ecc3925d3f8c 10 * substantial portions of the Software.
Perijah 0:ecc3925d3f8c 11 *
Perijah 0:ecc3925d3f8c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Perijah 0:ecc3925d3f8c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Perijah 0:ecc3925d3f8c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Perijah 0:ecc3925d3f8c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Perijah 0:ecc3925d3f8c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Perijah 0:ecc3925d3f8c 17 */
Perijah 0:ecc3925d3f8c 18
Perijah 0:ecc3925d3f8c 19 #include "stdint.h"
Perijah 0:ecc3925d3f8c 20 #include "USBMouse.h"
Perijah 0:ecc3925d3f8c 21
Perijah 0:ecc3925d3f8c 22 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
Perijah 0:ecc3925d3f8c 23 switch (mouse_type) {
Perijah 0:ecc3925d3f8c 24 case REL_MOUSE:
Perijah 0:ecc3925d3f8c 25 while (x > 127) {
Perijah 0:ecc3925d3f8c 26 if (!mouseSend(127, 0, button, z)) return false;
Perijah 0:ecc3925d3f8c 27 x = x - 127;
Perijah 0:ecc3925d3f8c 28 }
Perijah 0:ecc3925d3f8c 29 while (x < -128) {
Perijah 0:ecc3925d3f8c 30 if (!mouseSend(-128, 0, button, z)) return false;
Perijah 0:ecc3925d3f8c 31 x = x + 128;
Perijah 0:ecc3925d3f8c 32 }
Perijah 0:ecc3925d3f8c 33 while (y > 127) {
Perijah 0:ecc3925d3f8c 34 if (!mouseSend(0, 127, button, z)) return false;
Perijah 0:ecc3925d3f8c 35 y = y - 127;
Perijah 0:ecc3925d3f8c 36 }
Perijah 0:ecc3925d3f8c 37 while (y < -128) {
Perijah 0:ecc3925d3f8c 38 if (!mouseSend(0, -128, button, z)) return false;
Perijah 0:ecc3925d3f8c 39 y = y + 128;
Perijah 0:ecc3925d3f8c 40 }
Perijah 0:ecc3925d3f8c 41 return mouseSend(x, y, button, z);
Perijah 0:ecc3925d3f8c 42 case ABS_MOUSE:
Perijah 0:ecc3925d3f8c 43 HID_REPORT report;
Perijah 0:ecc3925d3f8c 44
Perijah 0:ecc3925d3f8c 45 report.data[0] = x & 0xff;
Perijah 0:ecc3925d3f8c 46 report.data[1] = (x >> 8) & 0xff;
Perijah 0:ecc3925d3f8c 47 report.data[2] = y & 0xff;
Perijah 0:ecc3925d3f8c 48 report.data[3] = (y >> 8) & 0xff;
Perijah 0:ecc3925d3f8c 49 report.data[4] = -z;
Perijah 0:ecc3925d3f8c 50 report.data[5] = button & 0x07;
Perijah 0:ecc3925d3f8c 51
Perijah 0:ecc3925d3f8c 52 report.length = 6;
Perijah 0:ecc3925d3f8c 53
Perijah 0:ecc3925d3f8c 54 return send(&report);
Perijah 0:ecc3925d3f8c 55 default:
Perijah 0:ecc3925d3f8c 56 return false;
Perijah 0:ecc3925d3f8c 57 }
Perijah 0:ecc3925d3f8c 58 }
Perijah 0:ecc3925d3f8c 59
Perijah 0:ecc3925d3f8c 60 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
Perijah 0:ecc3925d3f8c 61 HID_REPORT report;
Perijah 0:ecc3925d3f8c 62 report.data[0] = buttons & 0x07;
Perijah 0:ecc3925d3f8c 63 report.data[1] = x;
Perijah 0:ecc3925d3f8c 64 report.data[2] = y;
Perijah 0:ecc3925d3f8c 65 report.data[3] = -z; // >0 to scroll down, <0 to scroll up
Perijah 0:ecc3925d3f8c 66
Perijah 0:ecc3925d3f8c 67 report.length = 4;
Perijah 0:ecc3925d3f8c 68
Perijah 0:ecc3925d3f8c 69 return send(&report);
Perijah 0:ecc3925d3f8c 70 }
Perijah 0:ecc3925d3f8c 71
Perijah 0:ecc3925d3f8c 72 bool USBMouse::move(int16_t x, int16_t y) {
Perijah 0:ecc3925d3f8c 73 return update(x, y, button, 0);
Perijah 0:ecc3925d3f8c 74 }
Perijah 0:ecc3925d3f8c 75
Perijah 0:ecc3925d3f8c 76 bool USBMouse::scroll(int8_t z) {
Perijah 0:ecc3925d3f8c 77 return update(0, 0, button, z);
Perijah 0:ecc3925d3f8c 78 }
Perijah 0:ecc3925d3f8c 79
Perijah 0:ecc3925d3f8c 80
Perijah 0:ecc3925d3f8c 81 bool USBMouse::doubleClick() {
Perijah 0:ecc3925d3f8c 82 if (!click(MOUSE_LEFT))
Perijah 0:ecc3925d3f8c 83 return false;
Perijah 0:ecc3925d3f8c 84 wait(0.1);
Perijah 0:ecc3925d3f8c 85 return click(MOUSE_LEFT);
Perijah 0:ecc3925d3f8c 86 }
Perijah 0:ecc3925d3f8c 87
Perijah 0:ecc3925d3f8c 88 bool USBMouse::click(uint8_t button) {
Perijah 0:ecc3925d3f8c 89 if (!update(0, 0, button, 0))
Perijah 0:ecc3925d3f8c 90 return false;
Perijah 0:ecc3925d3f8c 91 wait(0.01);
Perijah 0:ecc3925d3f8c 92 return update(0, 0, 0, 0);
Perijah 0:ecc3925d3f8c 93 }
Perijah 0:ecc3925d3f8c 94
Perijah 0:ecc3925d3f8c 95 bool USBMouse::press(uint8_t button_) {
Perijah 0:ecc3925d3f8c 96 button = button_ & 0x07;
Perijah 0:ecc3925d3f8c 97 return update(0, 0, button, 0);
Perijah 0:ecc3925d3f8c 98 }
Perijah 0:ecc3925d3f8c 99
Perijah 0:ecc3925d3f8c 100 bool USBMouse::release(uint8_t button_) {
Perijah 0:ecc3925d3f8c 101 button = (button & (~button_)) & 0x07;
Perijah 0:ecc3925d3f8c 102 return update(0, 0, button, 0);
Perijah 0:ecc3925d3f8c 103 }
Perijah 0:ecc3925d3f8c 104
Perijah 0:ecc3925d3f8c 105
Perijah 0:ecc3925d3f8c 106 uint8_t * USBMouse::reportDesc() {
Perijah 0:ecc3925d3f8c 107
Perijah 0:ecc3925d3f8c 108 if (mouse_type == REL_MOUSE) {
Perijah 0:ecc3925d3f8c 109 static uint8_t reportDescriptor[] = {
Perijah 0:ecc3925d3f8c 110 USAGE_PAGE(1), 0x01, // Genric Desktop
Perijah 0:ecc3925d3f8c 111 USAGE(1), 0x02, // Mouse
Perijah 0:ecc3925d3f8c 112 COLLECTION(1), 0x01, // Application
Perijah 0:ecc3925d3f8c 113 USAGE(1), 0x01, // Pointer
Perijah 0:ecc3925d3f8c 114 COLLECTION(1), 0x00, // Physical
Perijah 0:ecc3925d3f8c 115
Perijah 0:ecc3925d3f8c 116 REPORT_COUNT(1), 0x03,
Perijah 0:ecc3925d3f8c 117 REPORT_SIZE(1), 0x01,
Perijah 0:ecc3925d3f8c 118 USAGE_PAGE(1), 0x09, // Buttons
Perijah 0:ecc3925d3f8c 119 USAGE_MINIMUM(1), 0x1,
Perijah 0:ecc3925d3f8c 120 USAGE_MAXIMUM(1), 0x3,
Perijah 0:ecc3925d3f8c 121 LOGICAL_MINIMUM(1), 0x00,
Perijah 0:ecc3925d3f8c 122 LOGICAL_MAXIMUM(1), 0x01,
Perijah 0:ecc3925d3f8c 123 INPUT(1), 0x02,
Perijah 0:ecc3925d3f8c 124 REPORT_COUNT(1), 0x01,
Perijah 0:ecc3925d3f8c 125 REPORT_SIZE(1), 0x05,
Perijah 0:ecc3925d3f8c 126 INPUT(1), 0x01,
Perijah 0:ecc3925d3f8c 127
Perijah 0:ecc3925d3f8c 128 REPORT_COUNT(1), 0x03,
Perijah 0:ecc3925d3f8c 129 REPORT_SIZE(1), 0x08,
Perijah 0:ecc3925d3f8c 130 USAGE_PAGE(1), 0x01,
Perijah 0:ecc3925d3f8c 131 USAGE(1), 0x30, // X
Perijah 0:ecc3925d3f8c 132 USAGE(1), 0x31, // Y
Perijah 0:ecc3925d3f8c 133 USAGE(1), 0x38, // scroll
Perijah 0:ecc3925d3f8c 134 LOGICAL_MINIMUM(1), 0x81,
Perijah 0:ecc3925d3f8c 135 LOGICAL_MAXIMUM(1), 0x7f,
Perijah 0:ecc3925d3f8c 136 INPUT(1), 0x06, // Relative data
Perijah 0:ecc3925d3f8c 137
Perijah 0:ecc3925d3f8c 138 END_COLLECTION(0),
Perijah 0:ecc3925d3f8c 139 END_COLLECTION(0),
Perijah 0:ecc3925d3f8c 140 };
Perijah 0:ecc3925d3f8c 141 reportLength = sizeof(reportDescriptor);
Perijah 0:ecc3925d3f8c 142 return reportDescriptor;
Perijah 0:ecc3925d3f8c 143 } else if (mouse_type == ABS_MOUSE) {
Perijah 0:ecc3925d3f8c 144 static uint8_t reportDescriptor[] = {
Perijah 0:ecc3925d3f8c 145
Perijah 0:ecc3925d3f8c 146 USAGE_PAGE(1), 0x01, // Generic Desktop
Perijah 0:ecc3925d3f8c 147 USAGE(1), 0x02, // Mouse
Perijah 0:ecc3925d3f8c 148 COLLECTION(1), 0x01, // Application
Perijah 0:ecc3925d3f8c 149 USAGE(1), 0x01, // Pointer
Perijah 0:ecc3925d3f8c 150 COLLECTION(1), 0x00, // Physical
Perijah 0:ecc3925d3f8c 151
Perijah 0:ecc3925d3f8c 152 USAGE_PAGE(1), 0x01, // Generic Desktop
Perijah 0:ecc3925d3f8c 153 USAGE(1), 0x30, // X
Perijah 0:ecc3925d3f8c 154 USAGE(1), 0x31, // Y
Perijah 0:ecc3925d3f8c 155 LOGICAL_MINIMUM(1), 0x00, // 0
Perijah 0:ecc3925d3f8c 156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
Perijah 0:ecc3925d3f8c 157 REPORT_SIZE(1), 0x10,
Perijah 0:ecc3925d3f8c 158 REPORT_COUNT(1), 0x02,
Perijah 0:ecc3925d3f8c 159 INPUT(1), 0x02, // Data, Variable, Absolute
Perijah 0:ecc3925d3f8c 160
Perijah 0:ecc3925d3f8c 161 USAGE_PAGE(1), 0x01, // Generic Desktop
Perijah 0:ecc3925d3f8c 162 USAGE(1), 0x38, // scroll
Perijah 0:ecc3925d3f8c 163 LOGICAL_MINIMUM(1), 0x81, // -127
Perijah 0:ecc3925d3f8c 164 LOGICAL_MAXIMUM(1), 0x7f, // 127
Perijah 0:ecc3925d3f8c 165 REPORT_SIZE(1), 0x08,
Perijah 0:ecc3925d3f8c 166 REPORT_COUNT(1), 0x01,
Perijah 0:ecc3925d3f8c 167 INPUT(1), 0x06, // Data, Variable, Relative
Perijah 0:ecc3925d3f8c 168
Perijah 0:ecc3925d3f8c 169 USAGE_PAGE(1), 0x09, // Buttons
Perijah 0:ecc3925d3f8c 170 USAGE_MINIMUM(1), 0x01,
Perijah 0:ecc3925d3f8c 171 USAGE_MAXIMUM(1), 0x03,
Perijah 0:ecc3925d3f8c 172 LOGICAL_MINIMUM(1), 0x00, // 0
Perijah 0:ecc3925d3f8c 173 LOGICAL_MAXIMUM(1), 0x01, // 1
Perijah 0:ecc3925d3f8c 174 REPORT_COUNT(1), 0x03,
Perijah 0:ecc3925d3f8c 175 REPORT_SIZE(1), 0x01,
Perijah 0:ecc3925d3f8c 176 INPUT(1), 0x02, // Data, Variable, Absolute
Perijah 0:ecc3925d3f8c 177 REPORT_COUNT(1), 0x01,
Perijah 0:ecc3925d3f8c 178 REPORT_SIZE(1), 0x05,
Perijah 0:ecc3925d3f8c 179 INPUT(1), 0x01, // Constant
Perijah 0:ecc3925d3f8c 180
Perijah 0:ecc3925d3f8c 181 END_COLLECTION(0),
Perijah 0:ecc3925d3f8c 182 END_COLLECTION(0)
Perijah 0:ecc3925d3f8c 183 };
Perijah 0:ecc3925d3f8c 184 reportLength = sizeof(reportDescriptor);
Perijah 0:ecc3925d3f8c 185 return reportDescriptor;
Perijah 0:ecc3925d3f8c 186 }
Perijah 0:ecc3925d3f8c 187 return NULL;
Perijah 0:ecc3925d3f8c 188 }
Perijah 0:ecc3925d3f8c 189
Perijah 0:ecc3925d3f8c 190 #define DEFAULT_CONFIGURATION (1)
Perijah 0:ecc3925d3f8c 191 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
Perijah 0:ecc3925d3f8c 192 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
Perijah 0:ecc3925d3f8c 193 + (1 * HID_DESCRIPTOR_LENGTH) \
Perijah 0:ecc3925d3f8c 194 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
Perijah 0:ecc3925d3f8c 195
Perijah 0:ecc3925d3f8c 196 uint8_t * USBMouse::configurationDesc() {
Perijah 0:ecc3925d3f8c 197 static uint8_t configurationDescriptor[] = {
Perijah 0:ecc3925d3f8c 198 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
Perijah 0:ecc3925d3f8c 199 CONFIGURATION_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 200 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
Perijah 0:ecc3925d3f8c 201 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
Perijah 0:ecc3925d3f8c 202 0x01, // bNumInterfaces
Perijah 0:ecc3925d3f8c 203 DEFAULT_CONFIGURATION, // bConfigurationValue
Perijah 0:ecc3925d3f8c 204 0x00, // iConfiguration
Perijah 0:ecc3925d3f8c 205 C_RESERVED | C_SELF_POWERED, // bmAttributes
Perijah 0:ecc3925d3f8c 206 C_POWER(0), // bMaxPowerHello World from Mbed
Perijah 0:ecc3925d3f8c 207
Perijah 0:ecc3925d3f8c 208 INTERFACE_DESCRIPTOR_LENGTH, // bLength
Perijah 0:ecc3925d3f8c 209 INTERFACE_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 210 0x00, // bInterfaceNumber
Perijah 0:ecc3925d3f8c 211 0x00, // bAlternateSetting
Perijah 0:ecc3925d3f8c 212 0x02, // bNumEndpoints
Perijah 0:ecc3925d3f8c 213 HID_CLASS, // bInterfaceClass
Perijah 0:ecc3925d3f8c 214 1, // bInterfaceSubClass
Perijah 0:ecc3925d3f8c 215 2, // bInterfaceProtocol (mouse)
Perijah 0:ecc3925d3f8c 216 0x00, // iInterface
Perijah 0:ecc3925d3f8c 217
Perijah 0:ecc3925d3f8c 218 HID_DESCRIPTOR_LENGTH, // bLength
Perijah 0:ecc3925d3f8c 219 HID_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 220 LSB(HID_VERSION_1_11), // bcdHID (LSB)
Perijah 0:ecc3925d3f8c 221 MSB(HID_VERSION_1_11), // bcdHID (MSB)
Perijah 0:ecc3925d3f8c 222 0x00, // bCountryCode
Perijah 0:ecc3925d3f8c 223 0x01, // bNumDescriptors
Perijah 0:ecc3925d3f8c 224 REPORT_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 225 LSB(reportDescLength()), // wDescriptorLength (LSB)
Perijah 0:ecc3925d3f8c 226 MSB(reportDescLength()), // wDescriptorLength (MSB)
Perijah 0:ecc3925d3f8c 227
Perijah 0:ecc3925d3f8c 228 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Perijah 0:ecc3925d3f8c 229 ENDPOINT_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 230 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Perijah 0:ecc3925d3f8c 231 E_INTERRUPT, // bmAttributes
Perijah 0:ecc3925d3f8c 232 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Perijah 0:ecc3925d3f8c 233 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Perijah 0:ecc3925d3f8c 234 1, // bInterval (milliseconds)
Perijah 0:ecc3925d3f8c 235
Perijah 0:ecc3925d3f8c 236 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Perijah 0:ecc3925d3f8c 237 ENDPOINT_DESCRIPTOR, // bDescriptorType
Perijah 0:ecc3925d3f8c 238 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
Perijah 0:ecc3925d3f8c 239 E_INTERRUPT, // bmAttributes
Perijah 0:ecc3925d3f8c 240 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Perijah 0:ecc3925d3f8c 241 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Perijah 0:ecc3925d3f8c 242 1, // bInterval (milliseconds)
Perijah 0:ecc3925d3f8c 243 };
Perijah 0:ecc3925d3f8c 244 return configurationDescriptor;
Perijah 0:ecc3925d3f8c 245 }