ADC Niose test Connect four analog signals to your MBED. and then run the Windows app. The four traces are displayed on an oscilloscope like display. I have used a USB HID DEVICE link, so connections to D+, D- are required. The MBED code is otherwise quite basic, So you can modify it to your own test needs. Additionaly, there is a 16 bit count value, in my MBED code Mainly to test if MSB & LSB are correct.
HID/USBMouse.cpp@0:cbe01b678bd4, 2011-11-19 (annotated)
- Committer:
- ceri
- Date:
- Sat Nov 19 22:54:22 2011 +0000
- Revision:
- 0:cbe01b678bd4
just enough to work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ceri | 0:cbe01b678bd4 | 1 | // USBMouse.c |
ceri | 0:cbe01b678bd4 | 2 | // USB device example: Relative mouse |
ceri | 0:cbe01b678bd4 | 3 | // Copyright (c) 2011 ARM Limited. All rights reserved. |
ceri | 0:cbe01b678bd4 | 4 | |
ceri | 0:cbe01b678bd4 | 5 | #include "stdint.h" |
ceri | 0:cbe01b678bd4 | 6 | #include "USBMouse.h" |
ceri | 0:cbe01b678bd4 | 7 | |
ceri | 0:cbe01b678bd4 | 8 | bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) { |
ceri | 0:cbe01b678bd4 | 9 | switch (mouse_type) { |
ceri | 0:cbe01b678bd4 | 10 | case REL_MOUSE: |
ceri | 0:cbe01b678bd4 | 11 | while (x > 127) { |
ceri | 0:cbe01b678bd4 | 12 | if (!mouseSend(127, 0, button, z)) return false; |
ceri | 0:cbe01b678bd4 | 13 | x = x - 127; |
ceri | 0:cbe01b678bd4 | 14 | } |
ceri | 0:cbe01b678bd4 | 15 | while (x < -128) { |
ceri | 0:cbe01b678bd4 | 16 | if (!mouseSend(-128, 0, button, z)) return false; |
ceri | 0:cbe01b678bd4 | 17 | x = x + 128; |
ceri | 0:cbe01b678bd4 | 18 | } |
ceri | 0:cbe01b678bd4 | 19 | while (y > 127) { |
ceri | 0:cbe01b678bd4 | 20 | if (!mouseSend(0, 127, button, z)) return false; |
ceri | 0:cbe01b678bd4 | 21 | y = y - 127; |
ceri | 0:cbe01b678bd4 | 22 | } |
ceri | 0:cbe01b678bd4 | 23 | while (y < -128) { |
ceri | 0:cbe01b678bd4 | 24 | if (!mouseSend(0, -128, button, z)) return false; |
ceri | 0:cbe01b678bd4 | 25 | y = y + 128; |
ceri | 0:cbe01b678bd4 | 26 | } |
ceri | 0:cbe01b678bd4 | 27 | return mouseSend(x, y, button, z); |
ceri | 0:cbe01b678bd4 | 28 | case ABS_MOUSE: |
ceri | 0:cbe01b678bd4 | 29 | HID_REPORT report; |
ceri | 0:cbe01b678bd4 | 30 | |
ceri | 0:cbe01b678bd4 | 31 | report.data[0] = x & 0xff; |
ceri | 0:cbe01b678bd4 | 32 | report.data[1] = (x >> 8) & 0xff; |
ceri | 0:cbe01b678bd4 | 33 | report.data[2] = y & 0xff; |
ceri | 0:cbe01b678bd4 | 34 | report.data[3] = (y >> 8) & 0xff; |
ceri | 0:cbe01b678bd4 | 35 | report.data[4] = -z; |
ceri | 0:cbe01b678bd4 | 36 | report.data[5] = button & 0x07; |
ceri | 0:cbe01b678bd4 | 37 | |
ceri | 0:cbe01b678bd4 | 38 | report.length = 6; |
ceri | 0:cbe01b678bd4 | 39 | |
ceri | 0:cbe01b678bd4 | 40 | return send(&report); |
ceri | 0:cbe01b678bd4 | 41 | default: |
ceri | 0:cbe01b678bd4 | 42 | return false; |
ceri | 0:cbe01b678bd4 | 43 | } |
ceri | 0:cbe01b678bd4 | 44 | } |
ceri | 0:cbe01b678bd4 | 45 | |
ceri | 0:cbe01b678bd4 | 46 | bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) { |
ceri | 0:cbe01b678bd4 | 47 | HID_REPORT report; |
ceri | 0:cbe01b678bd4 | 48 | report.data[0] = buttons & 0x07; |
ceri | 0:cbe01b678bd4 | 49 | report.data[1] = x; |
ceri | 0:cbe01b678bd4 | 50 | report.data[2] = y; |
ceri | 0:cbe01b678bd4 | 51 | report.data[3] = -z; // >0 to scroll down, <0 to scroll up |
ceri | 0:cbe01b678bd4 | 52 | |
ceri | 0:cbe01b678bd4 | 53 | report.length = 4; |
ceri | 0:cbe01b678bd4 | 54 | |
ceri | 0:cbe01b678bd4 | 55 | return send(&report); |
ceri | 0:cbe01b678bd4 | 56 | } |
ceri | 0:cbe01b678bd4 | 57 | |
ceri | 0:cbe01b678bd4 | 58 | bool USBMouse::move(int16_t x, int16_t y) { |
ceri | 0:cbe01b678bd4 | 59 | return update(x, y, button, 0); |
ceri | 0:cbe01b678bd4 | 60 | } |
ceri | 0:cbe01b678bd4 | 61 | |
ceri | 0:cbe01b678bd4 | 62 | bool USBMouse::scroll(int8_t z) { |
ceri | 0:cbe01b678bd4 | 63 | return update(0, 0, button, z); |
ceri | 0:cbe01b678bd4 | 64 | } |
ceri | 0:cbe01b678bd4 | 65 | |
ceri | 0:cbe01b678bd4 | 66 | |
ceri | 0:cbe01b678bd4 | 67 | bool USBMouse::doubleClick() { |
ceri | 0:cbe01b678bd4 | 68 | if (!click(MOUSE_LEFT)) |
ceri | 0:cbe01b678bd4 | 69 | return false; |
ceri | 0:cbe01b678bd4 | 70 | wait(0.1); |
ceri | 0:cbe01b678bd4 | 71 | return click(MOUSE_LEFT); |
ceri | 0:cbe01b678bd4 | 72 | } |
ceri | 0:cbe01b678bd4 | 73 | |
ceri | 0:cbe01b678bd4 | 74 | bool USBMouse::click(uint8_t button) { |
ceri | 0:cbe01b678bd4 | 75 | if (!update(0, 0, button, 0)) |
ceri | 0:cbe01b678bd4 | 76 | return false; |
ceri | 0:cbe01b678bd4 | 77 | wait(0.01); |
ceri | 0:cbe01b678bd4 | 78 | return update(0, 0, 0, 0); |
ceri | 0:cbe01b678bd4 | 79 | } |
ceri | 0:cbe01b678bd4 | 80 | |
ceri | 0:cbe01b678bd4 | 81 | bool USBMouse::press(uint8_t button_) { |
ceri | 0:cbe01b678bd4 | 82 | button = button_ & 0x07; |
ceri | 0:cbe01b678bd4 | 83 | return update(0, 0, button, 0); |
ceri | 0:cbe01b678bd4 | 84 | } |
ceri | 0:cbe01b678bd4 | 85 | |
ceri | 0:cbe01b678bd4 | 86 | bool USBMouse::release(uint8_t button_) { |
ceri | 0:cbe01b678bd4 | 87 | button = (button & (~button_)) & 0x07; |
ceri | 0:cbe01b678bd4 | 88 | return update(0, 0, button, 0); |
ceri | 0:cbe01b678bd4 | 89 | } |
ceri | 0:cbe01b678bd4 | 90 | |
ceri | 0:cbe01b678bd4 | 91 | |
ceri | 0:cbe01b678bd4 | 92 | uint8_t * USBMouse::reportDesc() { |
ceri | 0:cbe01b678bd4 | 93 | |
ceri | 0:cbe01b678bd4 | 94 | if (mouse_type == REL_MOUSE) { |
ceri | 0:cbe01b678bd4 | 95 | static uint8_t reportDescriptor[] = { |
ceri | 0:cbe01b678bd4 | 96 | USAGE_PAGE(1), 0x01, // Genric Desktop |
ceri | 0:cbe01b678bd4 | 97 | USAGE(1), 0x02, // Mouse |
ceri | 0:cbe01b678bd4 | 98 | COLLECTION(1), 0x01, // Application |
ceri | 0:cbe01b678bd4 | 99 | USAGE(1), 0x01, // Pointer |
ceri | 0:cbe01b678bd4 | 100 | COLLECTION(1), 0x00, // Physical |
ceri | 0:cbe01b678bd4 | 101 | |
ceri | 0:cbe01b678bd4 | 102 | REPORT_COUNT(1), 0x03, |
ceri | 0:cbe01b678bd4 | 103 | REPORT_SIZE(1), 0x01, |
ceri | 0:cbe01b678bd4 | 104 | USAGE_PAGE(1), 0x09, // Buttons |
ceri | 0:cbe01b678bd4 | 105 | USAGE_MINIMUM(1), 0x1, |
ceri | 0:cbe01b678bd4 | 106 | USAGE_MAXIMUM(1), 0x3, |
ceri | 0:cbe01b678bd4 | 107 | LOGICAL_MINIMUM(1), 0x00, |
ceri | 0:cbe01b678bd4 | 108 | LOGICAL_MAXIMUM(1), 0x01, |
ceri | 0:cbe01b678bd4 | 109 | INPUT(1), 0x02, |
ceri | 0:cbe01b678bd4 | 110 | REPORT_COUNT(1), 0x01, |
ceri | 0:cbe01b678bd4 | 111 | REPORT_SIZE(1), 0x05, |
ceri | 0:cbe01b678bd4 | 112 | INPUT(1), 0x01, |
ceri | 0:cbe01b678bd4 | 113 | |
ceri | 0:cbe01b678bd4 | 114 | REPORT_COUNT(1), 0x03, |
ceri | 0:cbe01b678bd4 | 115 | REPORT_SIZE(1), 0x08, |
ceri | 0:cbe01b678bd4 | 116 | USAGE_PAGE(1), 0x01, |
ceri | 0:cbe01b678bd4 | 117 | USAGE(1), 0x30, // X |
ceri | 0:cbe01b678bd4 | 118 | USAGE(1), 0x31, // Y |
ceri | 0:cbe01b678bd4 | 119 | USAGE(1), 0x38, // scroll |
ceri | 0:cbe01b678bd4 | 120 | LOGICAL_MINIMUM(1), 0x81, |
ceri | 0:cbe01b678bd4 | 121 | LOGICAL_MAXIMUM(1), 0x7f, |
ceri | 0:cbe01b678bd4 | 122 | INPUT(1), 0x06, // Relative data |
ceri | 0:cbe01b678bd4 | 123 | |
ceri | 0:cbe01b678bd4 | 124 | END_COLLECTION(0), |
ceri | 0:cbe01b678bd4 | 125 | END_COLLECTION(0), |
ceri | 0:cbe01b678bd4 | 126 | }; |
ceri | 0:cbe01b678bd4 | 127 | reportLength = sizeof(reportDescriptor); |
ceri | 0:cbe01b678bd4 | 128 | return reportDescriptor; |
ceri | 0:cbe01b678bd4 | 129 | } else if (mouse_type == ABS_MOUSE) { |
ceri | 0:cbe01b678bd4 | 130 | static uint8_t reportDescriptor[] = { |
ceri | 0:cbe01b678bd4 | 131 | |
ceri | 0:cbe01b678bd4 | 132 | USAGE_PAGE(1), 0x01, // Generic Desktop |
ceri | 0:cbe01b678bd4 | 133 | USAGE(1), 0x02, // Mouse |
ceri | 0:cbe01b678bd4 | 134 | COLLECTION(1), 0x01, // Application |
ceri | 0:cbe01b678bd4 | 135 | USAGE(1), 0x01, // Pointer |
ceri | 0:cbe01b678bd4 | 136 | COLLECTION(1), 0x00, // Physical |
ceri | 0:cbe01b678bd4 | 137 | |
ceri | 0:cbe01b678bd4 | 138 | USAGE_PAGE(1), 0x01, // Generic Desktop |
ceri | 0:cbe01b678bd4 | 139 | USAGE(1), 0x30, // X |
ceri | 0:cbe01b678bd4 | 140 | USAGE(1), 0x31, // Y |
ceri | 0:cbe01b678bd4 | 141 | LOGICAL_MINIMUM(1), 0x00, // 0 |
ceri | 0:cbe01b678bd4 | 142 | LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 |
ceri | 0:cbe01b678bd4 | 143 | REPORT_SIZE(1), 0x10, |
ceri | 0:cbe01b678bd4 | 144 | REPORT_COUNT(1), 0x02, |
ceri | 0:cbe01b678bd4 | 145 | INPUT(1), 0x02, // Data, Variable, Absolute |
ceri | 0:cbe01b678bd4 | 146 | |
ceri | 0:cbe01b678bd4 | 147 | USAGE_PAGE(1), 0x01, // Generic Desktop |
ceri | 0:cbe01b678bd4 | 148 | USAGE(1), 0x38, // scroll |
ceri | 0:cbe01b678bd4 | 149 | LOGICAL_MINIMUM(1), 0x81, // -127 |
ceri | 0:cbe01b678bd4 | 150 | LOGICAL_MAXIMUM(1), 0x7f, // 127 |
ceri | 0:cbe01b678bd4 | 151 | REPORT_SIZE(1), 0x08, |
ceri | 0:cbe01b678bd4 | 152 | REPORT_COUNT(1), 0x01, |
ceri | 0:cbe01b678bd4 | 153 | INPUT(1), 0x06, // Data, Variable, Relative |
ceri | 0:cbe01b678bd4 | 154 | |
ceri | 0:cbe01b678bd4 | 155 | USAGE_PAGE(1), 0x09, // Buttons |
ceri | 0:cbe01b678bd4 | 156 | USAGE_MINIMUM(1), 0x01, |
ceri | 0:cbe01b678bd4 | 157 | USAGE_MAXIMUM(1), 0x03, |
ceri | 0:cbe01b678bd4 | 158 | LOGICAL_MINIMUM(1), 0x00, // 0 |
ceri | 0:cbe01b678bd4 | 159 | LOGICAL_MAXIMUM(1), 0x01, // 1 |
ceri | 0:cbe01b678bd4 | 160 | REPORT_COUNT(1), 0x03, |
ceri | 0:cbe01b678bd4 | 161 | REPORT_SIZE(1), 0x01, |
ceri | 0:cbe01b678bd4 | 162 | INPUT(1), 0x02, // Data, Variable, Absolute |
ceri | 0:cbe01b678bd4 | 163 | REPORT_COUNT(1), 0x01, |
ceri | 0:cbe01b678bd4 | 164 | REPORT_SIZE(1), 0x05, |
ceri | 0:cbe01b678bd4 | 165 | INPUT(1), 0x01, // Constant |
ceri | 0:cbe01b678bd4 | 166 | |
ceri | 0:cbe01b678bd4 | 167 | END_COLLECTION(0), |
ceri | 0:cbe01b678bd4 | 168 | END_COLLECTION(0) |
ceri | 0:cbe01b678bd4 | 169 | }; |
ceri | 0:cbe01b678bd4 | 170 | reportLength = sizeof(reportDescriptor); |
ceri | 0:cbe01b678bd4 | 171 | return reportDescriptor; |
ceri | 0:cbe01b678bd4 | 172 | } |
ceri | 0:cbe01b678bd4 | 173 | return NULL; |
ceri | 0:cbe01b678bd4 | 174 | } |
ceri | 0:cbe01b678bd4 | 175 | |
ceri | 0:cbe01b678bd4 | 176 |