-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmit.ino
69 lines (56 loc) · 1.64 KB
/
transmit.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
/**
* This unit contains the RF Transmit code
* @see: https://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/
*/
// Initialise the radio
RF24 radio(PIN_RF_CE, PIN_RF_CSN);
/**
* @description
* Transmit the whole control state as a string
*/
void transmitControlState() {
char transmitBuffer[DATA_PACKET_SIZE];
// Example Transmit:
// PPTABCXXXYYYZZUNLR (18 Bytes)
// 990000127127000000 (18 bytes)
sprintf(transmitBuffer, "%02d%d%d%d%d%03d%03d%02d%d%d%d%d", batteryLevel, btnToggle, btnA, btnB, btnC, joyX, joyY, potZ, dPadUp, dPadDown, dPadLeft, dPadRight);
#ifdef DEBUG_SERIAL
Serial.print("Transmit: ");
Serial.println(transmitBuffer);
#endif
// Actually transmit
radio.write(&transmitBuffer, sizeof(transmitBuffer));
lastStateTransmit = millis();
}
/**
* @description
* Set the current radio address based on the current mode
*
* @todo: change this from a horriffic switch into an array of addresses
*/
void setRadioAddress(byte forMode) {
// Transmitter adddresses
byte address[5] = {'d', 'J', 'P', '0', forMode};
#ifdef DEBUG_SERIAL
Serial.print("ADDR: ");
for(int i = 0; i < 5; i++) {
Serial.print(address[i], HEX);
}
Serial.println("");
#endif
radio.openWritingPipe(address);
radio.stopListening();
// Transmit the initial control state for the receiver
delay(10); // TODO: determine if this is necessary
transmitControlState();
}
/**
* @descripion init the RF channel
*/
void initRadio() {
radio.begin();
radio.setDataRate(RADIO_DATA_RATE);
radio.setRetries(1,5); // delay, count
radio.setPALevel(RADIO_POWER);
setRadioAddress(mode);
}