Kalibriersoftware Stromwerte

Dependencies:   Matrix mbed

Committer:
Racer01014
Date:
Mon Nov 23 16:09:54 2015 +0000
Revision:
0:5e35c180ed4a
-

Who changed what in which revision?

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