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 /* USBMouse.h */
lharoon 0:22612ae617a0 2 /* USB device example: relative mouse */
lharoon 0:22612ae617a0 3 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
lharoon 0:22612ae617a0 4
lharoon 0:22612ae617a0 5 #ifndef USBMOUSE_H
lharoon 0:22612ae617a0 6 #define USBMOUSE_H
lharoon 0:22612ae617a0 7
lharoon 0:22612ae617a0 8 #include "USBHID.h"
lharoon 0:22612ae617a0 9
lharoon 0:22612ae617a0 10 #define REPORT_ID_MOUSE 2
lharoon 0:22612ae617a0 11
lharoon 0:22612ae617a0 12 /* Common usage */
lharoon 0:22612ae617a0 13
lharoon 0:22612ae617a0 14 enum MOUSE_BUTTON
lharoon 0:22612ae617a0 15 {
lharoon 0:22612ae617a0 16 MOUSE_LEFT = 1,
lharoon 0:22612ae617a0 17 MOUSE_RIGHT = 2,
lharoon 0:22612ae617a0 18 MOUSE_MIDDLE = 4,
lharoon 0:22612ae617a0 19 };
lharoon 0:22612ae617a0 20
lharoon 0:22612ae617a0 21 /* X and Y limits */
lharoon 0:22612ae617a0 22 /* These values do not directly map to screen pixels */
lharoon 0:22612ae617a0 23 /* Zero may be interpreted as meaning 'no movement' */
lharoon 0:22612ae617a0 24 #define X_MIN_ABS (1) /*!< Minimum value on x-axis */
lharoon 0:22612ae617a0 25 #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */
lharoon 0:22612ae617a0 26 #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */
lharoon 0:22612ae617a0 27 #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */
lharoon 0:22612ae617a0 28
lharoon 0:22612ae617a0 29 #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */
lharoon 0:22612ae617a0 30 #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */
lharoon 0:22612ae617a0 31 #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */
lharoon 0:22612ae617a0 32 #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */
lharoon 0:22612ae617a0 33
lharoon 0:22612ae617a0 34 enum MOUSE_TYPE
lharoon 0:22612ae617a0 35 {
lharoon 0:22612ae617a0 36 ABS_MOUSE,
lharoon 0:22612ae617a0 37 REL_MOUSE,
lharoon 0:22612ae617a0 38 };
lharoon 0:22612ae617a0 39
lharoon 0:22612ae617a0 40 /**
lharoon 0:22612ae617a0 41 *
lharoon 0:22612ae617a0 42 * USBMouse example
lharoon 0:22612ae617a0 43 * @code
lharoon 0:22612ae617a0 44 * #include "mbed.h"
lharoon 0:22612ae617a0 45 * #include "USBMouse.h"
lharoon 0:22612ae617a0 46 *
lharoon 0:22612ae617a0 47 * USBMouse mouse;
lharoon 0:22612ae617a0 48 *
lharoon 0:22612ae617a0 49 * int main(void)
lharoon 0:22612ae617a0 50 * {
lharoon 0:22612ae617a0 51 * while (1)
lharoon 0:22612ae617a0 52 * {
lharoon 0:22612ae617a0 53 * mouse.move(20, 0);
lharoon 0:22612ae617a0 54 * wait(0.5);
lharoon 0:22612ae617a0 55 * }
lharoon 0:22612ae617a0 56 * }
lharoon 0:22612ae617a0 57 *
lharoon 0:22612ae617a0 58 * @endcode
lharoon 0:22612ae617a0 59 *
lharoon 0:22612ae617a0 60 *
lharoon 0:22612ae617a0 61 * @code
lharoon 0:22612ae617a0 62 * #include "mbed.h"
lharoon 0:22612ae617a0 63 * #include "USBMouse.h"
lharoon 0:22612ae617a0 64 * #include <math.h>
lharoon 0:22612ae617a0 65 *
lharoon 0:22612ae617a0 66 * USBMouse mouse(ABS_MOUSE);
lharoon 0:22612ae617a0 67 *
lharoon 0:22612ae617a0 68 * int main(void)
lharoon 0:22612ae617a0 69 * {
lharoon 0:22612ae617a0 70 * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
lharoon 0:22612ae617a0 71 * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
lharoon 0:22612ae617a0 72 * uint16_t x_screen = 0;
lharoon 0:22612ae617a0 73 * uint16_t y_screen = 0;
lharoon 0:22612ae617a0 74 *
lharoon 0:22612ae617a0 75 * uint32_t x_origin = x_center;
lharoon 0:22612ae617a0 76 * uint32_t y_origin = y_center;
lharoon 0:22612ae617a0 77 * uint32_t radius = 5000;
lharoon 0:22612ae617a0 78 * uint32_t angle = 0;
lharoon 0:22612ae617a0 79 *
lharoon 0:22612ae617a0 80 * while (1)
lharoon 0:22612ae617a0 81 * {
lharoon 0:22612ae617a0 82 * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
lharoon 0:22612ae617a0 83 * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
lharoon 0:22612ae617a0 84 *
lharoon 0:22612ae617a0 85 * mouse.move(x_screen, y_screen);
lharoon 0:22612ae617a0 86 * angle += 3;
lharoon 0:22612ae617a0 87 * wait(0.01);
lharoon 0:22612ae617a0 88 * }
lharoon 0:22612ae617a0 89 * }
lharoon 0:22612ae617a0 90 *
lharoon 0:22612ae617a0 91 * @endcode
lharoon 0:22612ae617a0 92 */
lharoon 0:22612ae617a0 93 class USBMouse: public USBHID
lharoon 0:22612ae617a0 94 {
lharoon 0:22612ae617a0 95 public:
lharoon 0:22612ae617a0 96
lharoon 0:22612ae617a0 97 /**
lharoon 0:22612ae617a0 98 * Constructor
lharoon 0:22612ae617a0 99 *
lharoon 0:22612ae617a0 100 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
lharoon 0:22612ae617a0 101 * @param vendor_id Your vendor_id (default: 0x1234)
lharoon 0:22612ae617a0 102 * @param product_id Your product_id (default: 0x0001)
lharoon 0:22612ae617a0 103 * @param product_release Your preoduct_release (default: 0x0001)
lharoon 0:22612ae617a0 104 *
lharoon 0:22612ae617a0 105 */
lharoon 0:22612ae617a0 106 USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001):
lharoon 0:22612ae617a0 107 USBHID(0, 0, vendor_id, product_id, product_release, false)
lharoon 0:22612ae617a0 108 {
lharoon 0:22612ae617a0 109 button = 0;
lharoon 0:22612ae617a0 110 this->mouse_type = mouse_type;
lharoon 0:22612ae617a0 111 connect();
lharoon 0:22612ae617a0 112 };
lharoon 0:22612ae617a0 113
lharoon 0:22612ae617a0 114 /**
lharoon 0:22612ae617a0 115 * Write a state of the mouse
lharoon 0:22612ae617a0 116 *
lharoon 0:22612ae617a0 117 * @param x x-axis position
lharoon 0:22612ae617a0 118 * @param y y-axis position
lharoon 0:22612ae617a0 119 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
lharoon 0:22612ae617a0 120 * @param z wheel state (>0 to scroll down, <0 to scroll up)
lharoon 0:22612ae617a0 121 * @returns true if there is no error, false otherwise
lharoon 0:22612ae617a0 122 */
lharoon 0:22612ae617a0 123 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
lharoon 0:22612ae617a0 124
lharoon 0:22612ae617a0 125
lharoon 0:22612ae617a0 126 /**
lharoon 0:22612ae617a0 127 * Move the cursor to (x, y)
lharoon 0:22612ae617a0 128 *
lharoon 0:22612ae617a0 129 * @param x-axis position
lharoon 0:22612ae617a0 130 * @param y-axis position
lharoon 0:22612ae617a0 131 * @returns true if there is no error, false otherwise
lharoon 0:22612ae617a0 132 */
lharoon 0:22612ae617a0 133 bool move(int16_t x, int16_t y);
lharoon 0:22612ae617a0 134
lharoon 0:22612ae617a0 135 /**
lharoon 0:22612ae617a0 136 * Press one or several buttons
lharoon 0:22612ae617a0 137 *
lharoon 0:22612ae617a0 138 * @param button button state (ex: press(MOUSE_LEFT))
lharoon 0:22612ae617a0 139 * @returns true if there is no error, false otherwise
lharoon 0:22612ae617a0 140 */
lharoon 0:22612ae617a0 141 bool press(uint8_t button);
lharoon 0:22612ae617a0 142
lharoon 0:22612ae617a0 143 /**
lharoon 0:22612ae617a0 144 * Release one or several buttons
lharoon 0:22612ae617a0 145 *
lharoon 0:22612ae617a0 146 * @param button button state (ex: release(MOUSE_LEFT))
lharoon 0:22612ae617a0 147 * @returns true if there is no error, false otherwise
lharoon 0:22612ae617a0 148 */
lharoon 0:22612ae617a0 149 bool release(uint8_t button);
lharoon 0:22612ae617a0 150
lharoon 0:22612ae617a0 151 /**
lharoon 0:22612ae617a0 152 * Double click (MOUSE_LEFT)
lharoon 0:22612ae617a0 153 *
lharoon 0:22612ae617a0 154 * @returns true if there is no error, false otherwise
lharoon 0:22612ae617a0 155 */
lharoon 0:22612ae617a0 156 bool doubleClick();
lharoon 0:22612ae617a0 157
lharoon 0:22612ae617a0 158 /**
lharoon 0:22612ae617a0 159 * Click
lharoon 0:22612ae617a0 160 *
lharoon 0:22612ae617a0 161 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
lharoon 0:22612ae617a0 162 * @returns true if there is no error, false otherwise
lharoon 0:22612ae617a0 163 */
lharoon 0:22612ae617a0 164 bool click(uint8_t button);
lharoon 0:22612ae617a0 165
lharoon 0:22612ae617a0 166 /**
lharoon 0:22612ae617a0 167 * Scrolling
lharoon 0:22612ae617a0 168 *
lharoon 0:22612ae617a0 169 * @param z value of the wheel (>0 to go down, <0 to go up)
lharoon 0:22612ae617a0 170 * @returns true if there is no error, false otherwise
lharoon 0:22612ae617a0 171 */
lharoon 0:22612ae617a0 172 bool scroll(int8_t z);
lharoon 0:22612ae617a0 173
lharoon 0:22612ae617a0 174 /*
lharoon 0:22612ae617a0 175 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
lharoon 0:22612ae617a0 176 *
lharoon 0:22612ae617a0 177 * @returns pointer to the report descriptor
lharoon 0:22612ae617a0 178 */
lharoon 0:22612ae617a0 179 virtual uint8_t * reportDesc();
lharoon 0:22612ae617a0 180
lharoon 0:22612ae617a0 181 private:
lharoon 0:22612ae617a0 182 MOUSE_TYPE mouse_type;
lharoon 0:22612ae617a0 183 uint8_t button;
lharoon 0:22612ae617a0 184 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
lharoon 0:22612ae617a0 185 };
lharoon 0:22612ae617a0 186
lharoon 0:22612ae617a0 187 #endif