SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lharoon 0:22612ae617a0 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
lharoon 0:22612ae617a0 2 *
lharoon 0:22612ae617a0 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
lharoon 0:22612ae617a0 4 * and associated documentation files (the "Software"), to deal in the Software without
lharoon 0:22612ae617a0 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
lharoon 0:22612ae617a0 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
lharoon 0:22612ae617a0 7 * Software is furnished to do so, subject to the following conditions:
lharoon 0:22612ae617a0 8 *
lharoon 0:22612ae617a0 9 * The above copyright notice and this permission notice shall be included in all copies or
lharoon 0:22612ae617a0 10 * substantial portions of the Software.
lharoon 0:22612ae617a0 11 *
lharoon 0:22612ae617a0 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
lharoon 0:22612ae617a0 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
lharoon 0:22612ae617a0 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
lharoon 0:22612ae617a0 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lharoon 0:22612ae617a0 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
lharoon 0:22612ae617a0 17 */
lharoon 0:22612ae617a0 18
lharoon 0:22612ae617a0 19 #include "stdint.h"
lharoon 0:22612ae617a0 20 #include "USBMouse.h"
lharoon 0:22612ae617a0 21
lharoon 0:22612ae617a0 22 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
lharoon 0:22612ae617a0 23 switch (mouse_type) {
lharoon 0:22612ae617a0 24 case REL_MOUSE:
lharoon 0:22612ae617a0 25 while (x > 127) {
lharoon 0:22612ae617a0 26 if (!mouseSend(127, 0, button, z)) return false;
lharoon 0:22612ae617a0 27 x = x - 127;
lharoon 0:22612ae617a0 28 }
lharoon 0:22612ae617a0 29 while (x < -128) {
lharoon 0:22612ae617a0 30 if (!mouseSend(-128, 0, button, z)) return false;
lharoon 0:22612ae617a0 31 x = x + 128;
lharoon 0:22612ae617a0 32 }
lharoon 0:22612ae617a0 33 while (y > 127) {
lharoon 0:22612ae617a0 34 if (!mouseSend(0, 127, button, z)) return false;
lharoon 0:22612ae617a0 35 y = y - 127;
lharoon 0:22612ae617a0 36 }
lharoon 0:22612ae617a0 37 while (y < -128) {
lharoon 0:22612ae617a0 38 if (!mouseSend(0, -128, button, z)) return false;
lharoon 0:22612ae617a0 39 y = y + 128;
lharoon 0:22612ae617a0 40 }
lharoon 0:22612ae617a0 41 return mouseSend(x, y, button, z);
lharoon 0:22612ae617a0 42 case ABS_MOUSE:
lharoon 0:22612ae617a0 43 HID_REPORT report;
lharoon 0:22612ae617a0 44
lharoon 0:22612ae617a0 45 report.data[0] = x & 0xff;
lharoon 0:22612ae617a0 46 report.data[1] = (x >> 8) & 0xff;
lharoon 0:22612ae617a0 47 report.data[2] = y & 0xff;
lharoon 0:22612ae617a0 48 report.data[3] = (y >> 8) & 0xff;
lharoon 0:22612ae617a0 49 report.data[4] = -z;
lharoon 0:22612ae617a0 50 report.data[5] = button & 0x07;
lharoon 0:22612ae617a0 51
lharoon 0:22612ae617a0 52 report.length = 6;
lharoon 0:22612ae617a0 53
lharoon 0:22612ae617a0 54 return send(&report);
lharoon 0:22612ae617a0 55 default:
lharoon 0:22612ae617a0 56 return false;
lharoon 0:22612ae617a0 57 }
lharoon 0:22612ae617a0 58 }
lharoon 0:22612ae617a0 59
lharoon 0:22612ae617a0 60 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
lharoon 0:22612ae617a0 61 HID_REPORT report;
lharoon 0:22612ae617a0 62 report.data[0] = buttons & 0x07;
lharoon 0:22612ae617a0 63 report.data[1] = x;
lharoon 0:22612ae617a0 64 report.data[2] = y;
lharoon 0:22612ae617a0 65 report.data[3] = -z; // >0 to scroll down, <0 to scroll up
lharoon 0:22612ae617a0 66
lharoon 0:22612ae617a0 67 report.length = 4;
lharoon 0:22612ae617a0 68
lharoon 0:22612ae617a0 69 return send(&report);
lharoon 0:22612ae617a0 70 }
lharoon 0:22612ae617a0 71
lharoon 0:22612ae617a0 72 bool USBMouse::move(int16_t x, int16_t y) {
lharoon 0:22612ae617a0 73 return update(x, y, button, 0);
lharoon 0:22612ae617a0 74 }
lharoon 0:22612ae617a0 75
lharoon 0:22612ae617a0 76 bool USBMouse::scroll(int8_t z) {
lharoon 0:22612ae617a0 77 return update(0, 0, button, z);
lharoon 0:22612ae617a0 78 }
lharoon 0:22612ae617a0 79
lharoon 0:22612ae617a0 80
lharoon 0:22612ae617a0 81 bool USBMouse::doubleClick() {
lharoon 0:22612ae617a0 82 if (!click(MOUSE_LEFT))
lharoon 0:22612ae617a0 83 return false;
lharoon 0:22612ae617a0 84 wait(0.1);
lharoon 0:22612ae617a0 85 return click(MOUSE_LEFT);
lharoon 0:22612ae617a0 86 }
lharoon 0:22612ae617a0 87
lharoon 0:22612ae617a0 88 bool USBMouse::click(uint8_t button) {
lharoon 0:22612ae617a0 89 if (!update(0, 0, button, 0))
lharoon 0:22612ae617a0 90 return false;
lharoon 0:22612ae617a0 91 wait(0.01);
lharoon 0:22612ae617a0 92 return update(0, 0, 0, 0);
lharoon 0:22612ae617a0 93 }
lharoon 0:22612ae617a0 94
lharoon 0:22612ae617a0 95 bool USBMouse::press(uint8_t button_) {
lharoon 0:22612ae617a0 96 button = button_ & 0x07;
lharoon 0:22612ae617a0 97 return update(0, 0, button, 0);
lharoon 0:22612ae617a0 98 }
lharoon 0:22612ae617a0 99
lharoon 0:22612ae617a0 100 bool USBMouse::release(uint8_t button_) {
lharoon 0:22612ae617a0 101 button = (button & (~button_)) & 0x07;
lharoon 0:22612ae617a0 102 return update(0, 0, button, 0);
lharoon 0:22612ae617a0 103 }
lharoon 0:22612ae617a0 104
lharoon 0:22612ae617a0 105
lharoon 0:22612ae617a0 106 uint8_t * USBMouse::reportDesc() {
lharoon 0:22612ae617a0 107
lharoon 0:22612ae617a0 108 if (mouse_type == REL_MOUSE) {
lharoon 0:22612ae617a0 109 static uint8_t reportDescriptor[] = {
lharoon 0:22612ae617a0 110 USAGE_PAGE(1), 0x01, // Genric Desktop
lharoon 0:22612ae617a0 111 USAGE(1), 0x02, // Mouse
lharoon 0:22612ae617a0 112 COLLECTION(1), 0x01, // Application
lharoon 0:22612ae617a0 113 USAGE(1), 0x01, // Pointer
lharoon 0:22612ae617a0 114 COLLECTION(1), 0x00, // Physical
lharoon 0:22612ae617a0 115
lharoon 0:22612ae617a0 116 REPORT_COUNT(1), 0x03,
lharoon 0:22612ae617a0 117 REPORT_SIZE(1), 0x01,
lharoon 0:22612ae617a0 118 USAGE_PAGE(1), 0x09, // Buttons
lharoon 0:22612ae617a0 119 USAGE_MINIMUM(1), 0x1,
lharoon 0:22612ae617a0 120 USAGE_MAXIMUM(1), 0x3,
lharoon 0:22612ae617a0 121 LOGICAL_MINIMUM(1), 0x00,
lharoon 0:22612ae617a0 122 LOGICAL_MAXIMUM(1), 0x01,
lharoon 0:22612ae617a0 123 INPUT(1), 0x02,
lharoon 0:22612ae617a0 124 REPORT_COUNT(1), 0x01,
lharoon 0:22612ae617a0 125 REPORT_SIZE(1), 0x05,
lharoon 0:22612ae617a0 126 INPUT(1), 0x01,
lharoon 0:22612ae617a0 127
lharoon 0:22612ae617a0 128 REPORT_COUNT(1), 0x03,
lharoon 0:22612ae617a0 129 REPORT_SIZE(1), 0x08,
lharoon 0:22612ae617a0 130 USAGE_PAGE(1), 0x01,
lharoon 0:22612ae617a0 131 USAGE(1), 0x30, // X
lharoon 0:22612ae617a0 132 USAGE(1), 0x31, // Y
lharoon 0:22612ae617a0 133 USAGE(1), 0x38, // scroll
lharoon 0:22612ae617a0 134 LOGICAL_MINIMUM(1), 0x81,
lharoon 0:22612ae617a0 135 LOGICAL_MAXIMUM(1), 0x7f,
lharoon 0:22612ae617a0 136 INPUT(1), 0x06, // Relative data
lharoon 0:22612ae617a0 137
lharoon 0:22612ae617a0 138 END_COLLECTION(0),
lharoon 0:22612ae617a0 139 END_COLLECTION(0),
lharoon 0:22612ae617a0 140 };
lharoon 0:22612ae617a0 141 reportLength = sizeof(reportDescriptor);
lharoon 0:22612ae617a0 142 return reportDescriptor;
lharoon 0:22612ae617a0 143 } else if (mouse_type == ABS_MOUSE) {
lharoon 0:22612ae617a0 144 static uint8_t reportDescriptor[] = {
lharoon 0:22612ae617a0 145
lharoon 0:22612ae617a0 146 USAGE_PAGE(1), 0x01, // Generic Desktop
lharoon 0:22612ae617a0 147 USAGE(1), 0x02, // Mouse
lharoon 0:22612ae617a0 148 COLLECTION(1), 0x01, // Application
lharoon 0:22612ae617a0 149 USAGE(1), 0x01, // Pointer
lharoon 0:22612ae617a0 150 COLLECTION(1), 0x00, // Physical
lharoon 0:22612ae617a0 151
lharoon 0:22612ae617a0 152 USAGE_PAGE(1), 0x01, // Generic Desktop
lharoon 0:22612ae617a0 153 USAGE(1), 0x30, // X
lharoon 0:22612ae617a0 154 USAGE(1), 0x31, // Y
lharoon 0:22612ae617a0 155 LOGICAL_MINIMUM(1), 0x00, // 0
lharoon 0:22612ae617a0 156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
lharoon 0:22612ae617a0 157 REPORT_SIZE(1), 0x10,
lharoon 0:22612ae617a0 158 REPORT_COUNT(1), 0x02,
lharoon 0:22612ae617a0 159 INPUT(1), 0x02, // Data, Variable, Absolute
lharoon 0:22612ae617a0 160
lharoon 0:22612ae617a0 161 USAGE_PAGE(1), 0x01, // Generic Desktop
lharoon 0:22612ae617a0 162 USAGE(1), 0x38, // scroll
lharoon 0:22612ae617a0 163 LOGICAL_MINIMUM(1), 0x81, // -127
lharoon 0:22612ae617a0 164 LOGICAL_MAXIMUM(1), 0x7f, // 127
lharoon 0:22612ae617a0 165 REPORT_SIZE(1), 0x08,
lharoon 0:22612ae617a0 166 REPORT_COUNT(1), 0x01,
lharoon 0:22612ae617a0 167 INPUT(1), 0x06, // Data, Variable, Relative
lharoon 0:22612ae617a0 168
lharoon 0:22612ae617a0 169 USAGE_PAGE(1), 0x09, // Buttons
lharoon 0:22612ae617a0 170 USAGE_MINIMUM(1), 0x01,
lharoon 0:22612ae617a0 171 USAGE_MAXIMUM(1), 0x03,
lharoon 0:22612ae617a0 172 LOGICAL_MINIMUM(1), 0x00, // 0
lharoon 0:22612ae617a0 173 LOGICAL_MAXIMUM(1), 0x01, // 1
lharoon 0:22612ae617a0 174 REPORT_COUNT(1), 0x03,
lharoon 0:22612ae617a0 175 REPORT_SIZE(1), 0x01,
lharoon 0:22612ae617a0 176 INPUT(1), 0x02, // Data, Variable, Absolute
lharoon 0:22612ae617a0 177 REPORT_COUNT(1), 0x01,
lharoon 0:22612ae617a0 178 REPORT_SIZE(1), 0x05,
lharoon 0:22612ae617a0 179 INPUT(1), 0x01, // Constant
lharoon 0:22612ae617a0 180
lharoon 0:22612ae617a0 181 END_COLLECTION(0),
lharoon 0:22612ae617a0 182 END_COLLECTION(0)
lharoon 0:22612ae617a0 183 };
lharoon 0:22612ae617a0 184 reportLength = sizeof(reportDescriptor);
lharoon 0:22612ae617a0 185 return reportDescriptor;
lharoon 0:22612ae617a0 186 }
lharoon 0:22612ae617a0 187 return NULL;
lharoon 0:22612ae617a0 188 }
lharoon 0:22612ae617a0 189
lharoon 0:22612ae617a0 190