-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiver.ino
75 lines (65 loc) · 2.24 KB
/
Receiver.ino
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
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <HID-Project.h>
RF24 radio(9, 10); // CE, CSN pins
const byte address[6] = "00001"; // Address for communication
struct JoystickData {
int xAxis;
int yAxis;
int zAxis;
int rYaxis;
int rXaxis;
int rZaxis;
int switch1;
int switch2;
int switch3;
int switch4;
int switch5;
int switch6;
int switch7;
int switch8;
};
JoystickData joystick;
void setup() {
Serial.begin(9600);
Gamepad.begin(); // Initialize the gamepad
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(true);
radio.setDataRate(RF24_2MBPS);
radio.setPALevel(RF24_PA_HIGH);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(&joystick, sizeof(joystick));
// Map joystick values (0-1023) to 8-bit HID joystick range (0-255)
int8_t mappedX = map(joystick.xAxis, 0, 1023, -127, 127); // X-axis
int8_t mappedY = map(joystick.yAxis, 0, 1023, -127, 127); // Y-axis
int8_t mappedZ = map(joystick.zAxis, 0, 1023, -127, 127); // Z-axis
int16_t mappedRy = map(joystick.rYaxis, 0, 1023, -32768, 32767); // right Y-axis
int16_t mappedRx = map(joystick.rXaxis, 0, 1023, -32768, 32767); // right X-axis
int8_t mappedRz = map(joystick.rZaxis, 0, 1023, -127, 128); // right Z-axis
// Update Gamepad axes with 8-bit resolution
Gamepad.xAxis(mappedX);
Gamepad.yAxis(mappedY);
Gamepad.zAxis(mappedZ);
Gamepad.ryAxis(mappedRy);
Gamepad.rxAxis(mappedRx);
Gamepad.rzAxis(mappedRz);
// Map buttons to HID Gamepad
uint8_t buttonStates = 0; // 8-bit value for buttons
if (joystick.switch1 == LOW) buttonStates |= (1 << 4); // Button 5
if (joystick.switch2 == LOW) buttonStates |= (1 << 5); // Button 6
if (joystick.switch3 == LOW) buttonStates |= (1 << 3); // Button 4
if (joystick.switch4 == LOW) buttonStates |= (1 << 0); // Button 1
if (joystick.switch5 == LOW) buttonStates |= (1 << 1); // Button 2
if (joystick.switch6 == LOW) buttonStates |= (1 << 2); // Button 3
if (joystick.switch7 == LOW) buttonStates |= (1 << 6); // Button 7
if (joystick.switch8 == LOW) buttonStates |= (1 << 7); // Button 8
Gamepad.buttons(buttonStates);
// Send updated gamepad state
Gamepad.write();
}
}