-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnunchuck_mouse.c
325 lines (275 loc) · 8.79 KB
/
nunchuck_mouse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <stdio.h>
#include "hardware/i2c.h"
#include "pico/binary_info.h"
#include "pico/stdlib.h"
#include "pico/time.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "tusb.h"
#include "usb_descriptors.h"
#define ADDR 0x52
const uint BLUE_LED_PIN = 20;
const uint GREEN_LED_PIN = 19;
const uint RED_LED_PIN = 18;
void hid_task(void);
void send_hid_report(uint8_t report_id);
typedef struct {
int ax;
int ay;
int az;
int z;
int c;
int jx;
int jy;
} NunchuckState;
NunchuckState ns = {0};
#define DEAD_ZONE_SIZE 6
#define JOY_X_CENTER 122
#define JOY_Y_CENTER 128
//Accel value for "up"
#define ACCEL_CENTER 512
#define ACCEL_MIN 125
#define MOUSE_EXPONENT 2.0f
#define SCROLL_EXPONENT 1.5f
int main() {
stdio_init_all();
tusb_init();
gpio_init(BLUE_LED_PIN);
gpio_set_dir(BLUE_LED_PIN, GPIO_OUT);
gpio_init(GREEN_LED_PIN);
gpio_set_dir(GREEN_LED_PIN, GPIO_OUT);
gpio_init(RED_LED_PIN);
gpio_set_dir(RED_LED_PIN, GPIO_OUT);
// I2C is "open drain", pull ups to keep signal high when no data is being sent
i2c_init(i2c1, 100 * 1000);
gpio_set_function(2, GPIO_FUNC_I2C);
gpio_set_function(3, GPIO_FUNC_I2C);
gpio_pull_up(2);
gpio_pull_up(3);
//
uint8_t startup_sequence1[] = {0xF0,0x55};
// uint8_t startup_sequence1[] = {0x55,0xF0};
uint8_t startup_sequence2[] = {0xFB,0x00};
// uint8_t startup_sequence2[] = {0x00,0xFB};
uint8_t startup_sequence3[] = {0x40,0x00};
uint8_t buf[6];
i2c_write_blocking(i2c1, ADDR, startup_sequence1, 2, false); // true to keep master control of bus
sleep_ms(100);
i2c_write_blocking(i2c1, ADDR, startup_sequence2, 2, false); // true to keep master control of bus
// i2c_write_blocking(i2c1, ADDR, startup_sequence3, 2, false); // true to keep master control of bus
uint16_t count = 0;
uint8_t blink = 0;
while (1) {
tud_task();
uint8_t read_cmd[] = {0x00};
int w1 = i2c_write_blocking(i2c1, ADDR, read_cmd, 1, false);
sleep_ms(2);
int w2 = i2c_read_blocking(i2c1, ADDR, buf, 6, false); // false, we're done reading
// count++;
if (count > 50) {
printf("ax: %d ay: %d az: %d jx: %d jy: %d z: %d c: %d\n",ns.ax,ns.ay,ns.az,ns.jx,ns.jy,ns.z,ns.c);
count = 0;
}
ns.ax = ((buf[5] & 0xC0) >> 6) | (buf[2] << 2);
ns.ay = ((buf[5] & 0x30) >> 4) | (buf[3] << 2);
ns.az = ((buf[5] & 0x0C) >> 2) | (buf[4] << 2);
ns.jx = buf[0];
ns.jy = buf[1];
ns.z = buf[5] & 0x01;
ns.c = (buf[5] & 0x02)>>1;
sleep_ms(6);
hid_task();
}
}
int8_t apply_exponential_curve(int joystick_value, float exponent) {
if (abs(joystick_value) < DEAD_ZONE_SIZE){
joystick_value = 0;
}
// Normalize joystick value from -1024 to 1024 to -1 to 1
float normalized_input = joystick_value / 128.0f;
// Apply exponential curve
float output = pow(fabs(normalized_input), exponent);
// Preserve the sign of the input
output *= (normalized_input < 0) ? -1.0f : 1.0f;
// Scale output to max range (e.g., -127 to 127)
output *= 127.0f;
return (int8_t)output;
}
// Invoked when sent REPORT successfully to host
// Application can use this to send the next report
// Note: For composite reports, report[0] is report ID
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len)
{
(void) instance;
(void) len;
uint8_t next_report_id = report[0] + 1;
if (next_report_id < REPORT_ID_COUNT)
{
send_hid_report(next_report_id);
}
}
// Invoked when received SET_REPORT control request or
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
{
(void) instance;
// if (report_type == HID_REPORT_TYPE_OUTPUT)
// {
// // Set keyboard LED e.g Capslock, Numlock etc...
// if (report_id == REPORT_ID_KEYBOARD)
// {
// // bufsize should be (at least) 1
// if ( bufsize < 1 ) return;
//
// uint8_t const kbd_leds = buffer[0];
//
// if (kbd_leds & KEYBOARD_LED_CAPSLOCK)
// {
// // Capslock On: disable blink, turn led on
// blink_interval_ms = 0;
// // board_led_write(true);
// }else
// {
// // Caplocks Off: back to normal blink
// // board_led_write(false);
// // blink_interval_ms = BLINK_MOUNTED;
// }
// }
// }
}
// Invoked when received GET_REPORT control request
// Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
{
// TODO not Implemented
(void) instance;
(void) report_id;
(void) report_type;
(void) buffer;
(void) reqlen;
return 0;
}
//--------------------------------------------------------------------+
// USB HID
//--------------------------------------------------------------------+
void send_hid_report(uint8_t report_id)
{
// skip if hid is not ready yet
if ( !tud_hid_ready() ) return;
switch(report_id)
{
case REPORT_ID_KEYBOARD:
{
tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
// // use to avoid send multiple consecutive zero report for keyboard
// static bool has_keyboard_key = false;
//
// if ( btn )
// {
// uint8_t keycode[6] = { 0 };
// keycode[0] = HID_KEY_A;
//
// tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
// has_keyboard_key = true;
// }else
// {
// // send empty key report if previously has key pressed
// if (has_keyboard_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
// has_keyboard_key = false;
// }
}
break;
case REPORT_ID_MOUSE:
{
int8_t scroll, mouse_x, mouse_y = 0;
uint8_t buttons = (!(ns.c & 0x01 )) | (!(ns.z & 0x01) << 1);
if (abs(ns.ax - ACCEL_CENTER) > ACCEL_MIN){
gpio_put(GREEN_LED_PIN,1);
scroll = apply_exponential_curve(((int8_t) ( (int16_t)ns.jy - 128)),SCROLL_EXPONENT);
}
else{
mouse_x = ((int8_t) ( (int16_t)ns.jx - 122));
mouse_y = -((int8_t) ( (int16_t)ns.jy - 128));
gpio_put(GREEN_LED_PIN,0);
mouse_x = apply_exponential_curve(mouse_x,MOUSE_EXPONENT);
mouse_y = apply_exponential_curve(mouse_y,MOUSE_EXPONENT);
}
// no button, right + down, no scroll, no pan
tud_hid_mouse_report(REPORT_ID_MOUSE, buttons, mouse_x, mouse_y, scroll, 0);
}
break;
// case REPORT_ID_CONSUMER_CONTROL:
// {
// // use to avoid send multiple consecutive zero report
// static bool has_consumer_key = false;
//
// if ( btn )
// {
// // volume down
// uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT;
// tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2);
// has_consumer_key = true;
// }else
// {
// // send empty key report (release key) if previously has key pressed
// uint16_t empty_key = 0;
// if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2);
// has_consumer_key = false;
// }
// }
// break;
//
// case REPORT_ID_GAMEPAD:
// {
// // use to avoid send multiple consecutive zero report for keyboard
// static bool has_gamepad_key = false;
//
// hid_gamepad_report_t report =
// {
// .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
// .hat = 0, .buttons = 0
// };
//
// if ( btn )
// {
// report.hat = GAMEPAD_HAT_UP;
// report.buttons = GAMEPAD_BUTTON_A;
// tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
//
// has_gamepad_key = true;
// }else
// {
// report.hat = GAMEPAD_HAT_CENTERED;
// report.buttons = 0;
// if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
// has_gamepad_key = false;
// }
// }
// break;
default: break;
}
}
// Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
// tud_hid_report_complete_cb() is used to send the next report after previous one is complete
void hid_task(void)
{
// Poll every 10ms
const uint32_t interval_ms = 10;
static uint32_t start_ms = 0;
uint32_t milis = to_ms_since_boot(get_absolute_time());
if ( milis - start_ms < interval_ms) return; // not enough time
start_ms += interval_ms;
// Remote wakeup
if ( tud_suspended() )
{
// Wake up host if we are in suspend mode
// and REMOTE_WAKEUP feature is enabled by host
tud_remote_wakeup();
}else
{
// Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
send_hid_report(REPORT_ID_KEYBOARD);
}
}