USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Fri Nov 11 15:22:53 2011 +0000
Revision:
2:27a7e7f8d399
we have 2MB with the sdcard!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:27a7e7f8d399 1 // USBMouse.c
samux 2:27a7e7f8d399 2 // USB device example: Relative mouse
samux 2:27a7e7f8d399 3 // Copyright (c) 2011 ARM Limited. All rights reserved.
samux 2:27a7e7f8d399 4
samux 2:27a7e7f8d399 5 #include "stdint.h"
samux 2:27a7e7f8d399 6 #include "USBMouse.h"
samux 2:27a7e7f8d399 7
samux 2:27a7e7f8d399 8 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
samux 2:27a7e7f8d399 9 switch (mouse_type) {
samux 2:27a7e7f8d399 10 case REL_MOUSE:
samux 2:27a7e7f8d399 11 while (x > 127) {
samux 2:27a7e7f8d399 12 if (!mouseSend(127, 0, button, z)) return false;
samux 2:27a7e7f8d399 13 x = x - 127;
samux 2:27a7e7f8d399 14 }
samux 2:27a7e7f8d399 15 while (x < -128) {
samux 2:27a7e7f8d399 16 if (!mouseSend(-128, 0, button, z)) return false;
samux 2:27a7e7f8d399 17 x = x + 128;
samux 2:27a7e7f8d399 18 }
samux 2:27a7e7f8d399 19 while (y > 127) {
samux 2:27a7e7f8d399 20 if (!mouseSend(0, 127, button, z)) return false;
samux 2:27a7e7f8d399 21 y = y - 127;
samux 2:27a7e7f8d399 22 }
samux 2:27a7e7f8d399 23 while (y < -128) {
samux 2:27a7e7f8d399 24 if (!mouseSend(0, -128, button, z)) return false;
samux 2:27a7e7f8d399 25 y = y + 128;
samux 2:27a7e7f8d399 26 }
samux 2:27a7e7f8d399 27 return mouseSend(x, y, button, z);
samux 2:27a7e7f8d399 28 case ABS_MOUSE:
samux 2:27a7e7f8d399 29 HID_REPORT report;
samux 2:27a7e7f8d399 30
samux 2:27a7e7f8d399 31 report.data[0] = x & 0xff;
samux 2:27a7e7f8d399 32 report.data[1] = (x >> 8) & 0xff;
samux 2:27a7e7f8d399 33 report.data[2] = y & 0xff;
samux 2:27a7e7f8d399 34 report.data[3] = (y >> 8) & 0xff;
samux 2:27a7e7f8d399 35 report.data[4] = -z;
samux 2:27a7e7f8d399 36 report.data[5] = button & 0x07;
samux 2:27a7e7f8d399 37
samux 2:27a7e7f8d399 38 report.length = 6;
samux 2:27a7e7f8d399 39
samux 2:27a7e7f8d399 40 return send(&report);
samux 2:27a7e7f8d399 41 default:
samux 2:27a7e7f8d399 42 return false;
samux 2:27a7e7f8d399 43 }
samux 2:27a7e7f8d399 44 }
samux 2:27a7e7f8d399 45
samux 2:27a7e7f8d399 46 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
samux 2:27a7e7f8d399 47 HID_REPORT report;
samux 2:27a7e7f8d399 48 report.data[0] = buttons & 0x07;
samux 2:27a7e7f8d399 49 report.data[1] = x;
samux 2:27a7e7f8d399 50 report.data[2] = y;
samux 2:27a7e7f8d399 51 report.data[3] = -z; // >0 to scroll down, <0 to scroll up
samux 2:27a7e7f8d399 52
samux 2:27a7e7f8d399 53 report.length = 4;
samux 2:27a7e7f8d399 54
samux 2:27a7e7f8d399 55 return send(&report);
samux 2:27a7e7f8d399 56 }
samux 2:27a7e7f8d399 57
samux 2:27a7e7f8d399 58 bool USBMouse::move(int16_t x, int16_t y) {
samux 2:27a7e7f8d399 59 return update(x, y, button, 0);
samux 2:27a7e7f8d399 60 }
samux 2:27a7e7f8d399 61
samux 2:27a7e7f8d399 62 bool USBMouse::scroll(int8_t z) {
samux 2:27a7e7f8d399 63 return update(0, 0, button, z);
samux 2:27a7e7f8d399 64 }
samux 2:27a7e7f8d399 65
samux 2:27a7e7f8d399 66
samux 2:27a7e7f8d399 67 bool USBMouse::doubleClick() {
samux 2:27a7e7f8d399 68 if (!click(MOUSE_LEFT))
samux 2:27a7e7f8d399 69 return false;
samux 2:27a7e7f8d399 70 wait(0.1);
samux 2:27a7e7f8d399 71 return click(MOUSE_LEFT);
samux 2:27a7e7f8d399 72 }
samux 2:27a7e7f8d399 73
samux 2:27a7e7f8d399 74 bool USBMouse::click(uint8_t button) {
samux 2:27a7e7f8d399 75 if (!update(0, 0, button, 0))
samux 2:27a7e7f8d399 76 return false;
samux 2:27a7e7f8d399 77 wait(0.01);
samux 2:27a7e7f8d399 78 return update(0, 0, 0, 0);
samux 2:27a7e7f8d399 79 }
samux 2:27a7e7f8d399 80
samux 2:27a7e7f8d399 81 bool USBMouse::press(uint8_t button_) {
samux 2:27a7e7f8d399 82 button = button_ & 0x07;
samux 2:27a7e7f8d399 83 return update(0, 0, button, 0);
samux 2:27a7e7f8d399 84 }
samux 2:27a7e7f8d399 85
samux 2:27a7e7f8d399 86 bool USBMouse::release(uint8_t button_) {
samux 2:27a7e7f8d399 87 button = (button & (~button_)) & 0x07;
samux 2:27a7e7f8d399 88 return update(0, 0, button, 0);
samux 2:27a7e7f8d399 89 }
samux 2:27a7e7f8d399 90
samux 2:27a7e7f8d399 91
samux 2:27a7e7f8d399 92 uint8_t * USBMouse::reportDesc() {
samux 2:27a7e7f8d399 93
samux 2:27a7e7f8d399 94 if (mouse_type == REL_MOUSE) {
samux 2:27a7e7f8d399 95 static uint8_t reportDescriptor[] = {
samux 2:27a7e7f8d399 96 USAGE_PAGE(1), 0x01, // Genric Desktop
samux 2:27a7e7f8d399 97 USAGE(1), 0x02, // Mouse
samux 2:27a7e7f8d399 98 COLLECTION(1), 0x01, // Application
samux 2:27a7e7f8d399 99 USAGE(1), 0x01, // Pointer
samux 2:27a7e7f8d399 100 COLLECTION(1), 0x00, // Physical
samux 2:27a7e7f8d399 101
samux 2:27a7e7f8d399 102 REPORT_COUNT(1), 0x03,
samux 2:27a7e7f8d399 103 REPORT_SIZE(1), 0x01,
samux 2:27a7e7f8d399 104 USAGE_PAGE(1), 0x09, // Buttons
samux 2:27a7e7f8d399 105 USAGE_MINIMUM(1), 0x1,
samux 2:27a7e7f8d399 106 USAGE_MAXIMUM(1), 0x3,
samux 2:27a7e7f8d399 107 LOGICAL_MINIMUM(1), 0x00,
samux 2:27a7e7f8d399 108 LOGICAL_MAXIMUM(1), 0x01,
samux 2:27a7e7f8d399 109 INPUT(1), 0x02,
samux 2:27a7e7f8d399 110 REPORT_COUNT(1), 0x01,
samux 2:27a7e7f8d399 111 REPORT_SIZE(1), 0x05,
samux 2:27a7e7f8d399 112 INPUT(1), 0x01,
samux 2:27a7e7f8d399 113
samux 2:27a7e7f8d399 114 REPORT_COUNT(1), 0x03,
samux 2:27a7e7f8d399 115 REPORT_SIZE(1), 0x08,
samux 2:27a7e7f8d399 116 USAGE_PAGE(1), 0x01,
samux 2:27a7e7f8d399 117 USAGE(1), 0x30, // X
samux 2:27a7e7f8d399 118 USAGE(1), 0x31, // Y
samux 2:27a7e7f8d399 119 USAGE(1), 0x38, // scroll
samux 2:27a7e7f8d399 120 LOGICAL_MINIMUM(1), 0x81,
samux 2:27a7e7f8d399 121 LOGICAL_MAXIMUM(1), 0x7f,
samux 2:27a7e7f8d399 122 INPUT(1), 0x06, // Relative data
samux 2:27a7e7f8d399 123
samux 2:27a7e7f8d399 124 END_COLLECTION(0),
samux 2:27a7e7f8d399 125 END_COLLECTION(0),
samux 2:27a7e7f8d399 126 };
samux 2:27a7e7f8d399 127 reportLength = sizeof(reportDescriptor);
samux 2:27a7e7f8d399 128 return reportDescriptor;
samux 2:27a7e7f8d399 129 } else if (mouse_type == ABS_MOUSE) {
samux 2:27a7e7f8d399 130 static uint8_t reportDescriptor[] = {
samux 2:27a7e7f8d399 131
samux 2:27a7e7f8d399 132 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 2:27a7e7f8d399 133 USAGE(1), 0x02, // Mouse
samux 2:27a7e7f8d399 134 COLLECTION(1), 0x01, // Application
samux 2:27a7e7f8d399 135 USAGE(1), 0x01, // Pointer
samux 2:27a7e7f8d399 136 COLLECTION(1), 0x00, // Physical
samux 2:27a7e7f8d399 137
samux 2:27a7e7f8d399 138 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 2:27a7e7f8d399 139 USAGE(1), 0x30, // X
samux 2:27a7e7f8d399 140 USAGE(1), 0x31, // Y
samux 2:27a7e7f8d399 141 LOGICAL_MINIMUM(1), 0x00, // 0
samux 2:27a7e7f8d399 142 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
samux 2:27a7e7f8d399 143 REPORT_SIZE(1), 0x10,
samux 2:27a7e7f8d399 144 REPORT_COUNT(1), 0x02,
samux 2:27a7e7f8d399 145 INPUT(1), 0x02, // Data, Variable, Absolute
samux 2:27a7e7f8d399 146
samux 2:27a7e7f8d399 147 USAGE_PAGE(1), 0x01, // Generic Desktop
samux 2:27a7e7f8d399 148 USAGE(1), 0x38, // scroll
samux 2:27a7e7f8d399 149 LOGICAL_MINIMUM(1), 0x81, // -127
samux 2:27a7e7f8d399 150 LOGICAL_MAXIMUM(1), 0x7f, // 127
samux 2:27a7e7f8d399 151 REPORT_SIZE(1), 0x08,
samux 2:27a7e7f8d399 152 REPORT_COUNT(1), 0x01,
samux 2:27a7e7f8d399 153 INPUT(1), 0x06, // Data, Variable, Relative
samux 2:27a7e7f8d399 154
samux 2:27a7e7f8d399 155 USAGE_PAGE(1), 0x09, // Buttons
samux 2:27a7e7f8d399 156 USAGE_MINIMUM(1), 0x01,
samux 2:27a7e7f8d399 157 USAGE_MAXIMUM(1), 0x03,
samux 2:27a7e7f8d399 158 LOGICAL_MINIMUM(1), 0x00, // 0
samux 2:27a7e7f8d399 159 LOGICAL_MAXIMUM(1), 0x01, // 1
samux 2:27a7e7f8d399 160 REPORT_COUNT(1), 0x03,
samux 2:27a7e7f8d399 161 REPORT_SIZE(1), 0x01,
samux 2:27a7e7f8d399 162 INPUT(1), 0x02, // Data, Variable, Absolute
samux 2:27a7e7f8d399 163 REPORT_COUNT(1), 0x01,
samux 2:27a7e7f8d399 164 REPORT_SIZE(1), 0x05,
samux 2:27a7e7f8d399 165 INPUT(1), 0x01, // Constant
samux 2:27a7e7f8d399 166
samux 2:27a7e7f8d399 167 END_COLLECTION(0),
samux 2:27a7e7f8d399 168 END_COLLECTION(0)
samux 2:27a7e7f8d399 169 };
samux 2:27a7e7f8d399 170 reportLength = sizeof(reportDescriptor);
samux 2:27a7e7f8d399 171 return reportDescriptor;
samux 2:27a7e7f8d399 172 }
samux 2:27a7e7f8d399 173 return NULL;
samux 2:27a7e7f8d399 174 }
samux 2:27a7e7f8d399 175
samux 2:27a7e7f8d399 176