ryou sato / Mbed 2 deprecated LPC11U35_CTswitch_relay

Dependencies:   mbed LPC11U35_MCP41HV51-503EST

Committer:
ryousato
Date:
Mon Aug 17 01:01:49 2020 +0000
Revision:
0:df1e1f84ded8
1

Who changed what in which revision?

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