This is a data logger program to be implemented with an instrument amplifier.

Dependencies:   mbed

Committer:
KISScientific
Date:
Tue Apr 04 18:01:11 2017 +0000
Revision:
0:d75ca4e39672
This is a data logger program.

Who changed what in which revision?

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