+/rtos

git clone https://git.julia.blue/+/rtos

Joseph RyanAdd variable mouse speedee76735

master
6.7 KiB223 linesraw
1#include "mouse.h"
2#include "OS.h"
3#include "printf.h"
4#include "std.h"
5#include "tivaware/gpio.h"
6#include "tivaware/hw_memmap.h"
7#include "tivaware/rom.h"
8#include "tivaware/sysctl.h"
9#include "usblib/device/usbdhidmouse.h"
10#include "usblib/usblib.h"
11
12const uint8_t LangDescriptor[] = {4, USB_DTYPE_STRING,
13                                  USBShort(USB_LANG_EN_US)};
14const uint8_t ManufacturerString[] = {
15    (4 + 1) * 2, USB_DTYPE_STRING, 'R', 0, 'T', 0, 'O', 0, 'S', 0};
16const uint8_t ProductString[] = {
17    (4 + 1) * 2, USB_DTYPE_STRING, 'L', 0, 'a', 0, 'b', 0, '7', 0};
18const uint8_t SerialNumberString[] = {(8 + 1) * 2, USB_DTYPE_STRING,
19                                      '1',         0,
20                                      '2',         0,
21                                      '3',         0,
22                                      '4',         0,
23                                      '5',         0,
24                                      '6',         0,
25                                      '7',         0,
26                                      '8',         0};
27const uint8_t HIDInterfaceString[] =
28    "(\x03H\0I\0D\0 \0M\0o\0u\0s\0e\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e";
29
30const uint8_t ConfigString[] =
31    "0\x03H\0I\0D\0 \0M\0o\0u\0s\0e\0 \0C\0o\0n\0f\0i\0g\0u\0r\0a\0t\0i\0o\0n";
32
33#define NUM_STRING_DESCRIPTORS (sizeof(StringDescriptors) / sizeof(uint8_t*))
34
35const uint8_t* const StringDescriptors[] = {
36    LangDescriptor,     ManufacturerString, ProductString,
37    SerialNumberString, HIDInterfaceString, ConfigString};
38
39static Sema4 mouse_ready;
40uint32_t mouse_handler(void* pvCBData, uint32_t ui32Event, uint32_t ui32MsgData,
41                       void* pvMsgData) {
42    switch (ui32Event) {
43    case USB_EVENT_CONNECTED: {
44        printf("\nHost Connected...\r\n");
45        OS_Signal(&mouse_ready);
46        break;
47    }
48    case USB_EVENT_DISCONNECTED: {
49        printf("\nHost Disconnected...\r\n");
50        break;
51    }
52    case USB_EVENT_TX_COMPLETE: {
53        // printf("TX complete\n\r");
54        OS_Signal(&mouse_ready);
55        break;
56    }
57    case USB_EVENT_SUSPEND: {
58        OS_Signal(&mouse_ready);
59        printf("\nBus Suspended\n\r");
60        break;
61    }
62    case USB_EVENT_RESUME: {
63        OS_Signal(&mouse_ready);
64        printf("\nBus Resume\n\r");
65        break;
66    }
67        // We ignore all other events.
68    default: {
69        break;
70    }
71    }
72    return 0;
73}
74
75tUSBDHIDMouseDevice mouse_dev = {
76    // The Vendor ID you have been assigned by USB-IF.
77    .ui16VID = 0xFFFF,
78    // The product ID you have assigned for this device.
79    .ui16PID = 0xFFFF,
80    // The power consumption of your device in milliamps.
81    .ui16MaxPowermA = 0,
82    .ui8PwrAttributes = USB_CONF_ATTR_SELF_PWR,
83    // A pointer to your mouse callback event handler.
84    .pfnCallback = mouse_handler,
85    // A value that you want passed to the callback alongside every event.
86    .pvCBData = (void*)&mouse_dev,
87    // A pointer to your string table.
88    .ppui8StringDescriptors = StringDescriptors,
89    // The number of entries in your string table. This must equal
90    // (1 + (5 * (num languages))).
91    .ui32NumStringDescriptors = NUM_STRING_DESCRIPTORS};
92
93typedef enum { LEFT = 1, RIGHT, MIDDLE } MouseButton;
94
95static bool held_down[3] = {false, false, false};
96void mouse_toggle(MouseButton button) {
97    --button;
98    OS_Wait(&mouse_ready);
99    USBDHIDMouseStateChange(&mouse_dev, 0, 0,
100                            held_down[button] ? 0 : 1 << button);
101    held_down[button] = !held_down[button];
102}
103
104void mouse_click(MouseButton button) {
105    mouse_toggle(button), mouse_toggle(button);
106}
107
108void mouse_move(int8_t x, int8_t y) {
109    OS_Wait(&mouse_ready);
110    USBDHIDMouseStateChange(&mouse_dev, x, y, 0);
111}
112
113static char continuous_dir = ' ';
114static int8_t speed = 5;
115static bool circle = false;
116
117void mouse_continuous(void) {
118    while (true) {
119        OS_Sleep(hz(60));
120        switch (continuous_dir) {
121        case 'q': mouse_move(-speed, -speed); break;
122        case 'w': mouse_move(0, -speed); break;
123        case 'e': mouse_move(speed, -speed); break;
124        case 'a': mouse_move(-speed, 0); break;
125        case 'd': mouse_move(speed, 0); break;
126        case 'z': mouse_move(-speed, speed); break;
127        case 'x': mouse_move(0, speed); break;
128        case 'c': mouse_move(speed, speed); break;
129        }
130    }
131}
132
133void mouse_circle(void) {
134    const float f = 1; // in radians per second
135    float t = 0;
136    while (circle) {
137        mouse_move(speed * cos(t), speed * sin(t));
138        t += f / 60;
139        if (t > PI) {
140            t -= 2 * PI;
141        }
142        OS_Sleep(hz(60));
143    }
144}
145
146void mouse_center(void) {
147    continuous_dir = ' ';
148    circle = false;
149    const uint16_t width = 1920;
150    const uint16_t height = 1080;
151    for (int i = 0; i < width / 100; ++i) { mouse_move(-100, 0); }
152    for (int i = 0; i < width / 100 / 2; ++i) { mouse_move(100, 0); }
153    for (int i = 0; i < height / 100; ++i) { mouse_move(0, -100); }
154    for (int i = 0; i < height / 100 / 2; ++i) { mouse_move(0, 100); }
155    for (int i = 0; i < 3; ++i) {
156        if (held_down[i]) {
157            mouse_toggle(i + 1);
158        }
159    }
160}
161
162bool mouse_cmd(char c) {
163    const int8_t s = 10;
164    switch (c) {
165    case 127: return false;
166    case '=':
167    case '+': ++speed; goto show;
168    case '-':
169    case '_':
170        --speed;
171    show:
172        printf("\x1b[K\x1b[GSpeed %d", speed);
173        break;
174    case ',': mouse_click(LEFT); break;
175    case '.': mouse_click(MIDDLE); break;
176    case '/': mouse_click(RIGHT); break;
177    case '<': mouse_toggle(LEFT); break;
178    case '>': mouse_toggle(MIDDLE); break;
179    case '?': mouse_toggle(RIGHT); break;
180    case ' ': mouse_center(); break;
181    case 'Q': mouse_move(-s, -s); break;
182    case 'W': mouse_move(0, -s); break;
183    case 'E': mouse_move(s, -s); break;
184    case 'A': mouse_move(-s, 0); break;
185    case 'D': mouse_move(s, 0); break;
186    case 'Z': mouse_move(-s, s); break;
187    case 'X': mouse_move(0, s); break;
188    case 'C': mouse_move(s, s); break;
189    case 'S':
190    case 's':
191    case 'q':
192    case 'w':
193    case 'e':
194    case 'a':
195    case 'd':
196    case 'z':
197    case 'x':
198    case 'c': continuous_dir = c; break;
199    case 'O':
200    case 'o':
201        if (circle) {
202            circle = false;
203        } else {
204            circle = true;
205            OS_AddThread(mouse_circle, "spin", 256, 2);
206        }
207        break;
208    }
209    return true;
210}
211
212void mouse_init(void) {
213    OS_InitSemaphore(&mouse_ready, -1);
214    ROM_GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);
215    USBStackModeSet(0, eUSBModeForceDevice, 0);
216
217    if (!USBDHIDMouseInit(0, &mouse_dev)) {
218        printf("error: hidmouseinit\n\r");
219    }
220    OS_Wait(&mouse_ready);
221    OS_AddThread(mouse_continuous, "Continuous mouse movement", 512, 2);
222    OS_Sleep(seconds(1.5f));
223}