|
|
@ -1,5 +1,26 @@ |
|
|
|
#include "RCController.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Data Structure:
|
|
|
|
// Old format:
|
|
|
|
// TTTT TTTT TTTT ---- YYYY YYYY YYYY ---- PPPP PPPP PPPP ---- RRRR RRRR RRRR ---- SSSS SSSS = 72-bits ( 9 bytes )
|
|
|
|
//
|
|
|
|
// 2021 New Data format for saving 2 bytes:
|
|
|
|
// 1234 5678 9TTT TTTT TTTT -YYY YYYY YYYY -PPP PPPP PPPP -RRR RRRR RRRR (7 Bytes)
|
|
|
|
// |-------| |-------| |-------| |-------| |-------| |-------| |-------|
|
|
|
|
// Byte 00 Byte 01 Byte 02 Byte 03 Byte 04 Byte 05 Byte 06
|
|
|
|
struct TxMessage { |
|
|
|
uint8_t Byte00; |
|
|
|
uint8_t Byte01; |
|
|
|
uint8_t Byte02; |
|
|
|
uint8_t Byte03; |
|
|
|
uint8_t Byte04; |
|
|
|
uint8_t Byte05; |
|
|
|
uint8_t Byte06; |
|
|
|
}; |
|
|
|
TxMessage txmessage; |
|
|
|
|
|
|
|
|
|
|
|
RCController::RCController(void) { |
|
|
|
updated = true; |
|
|
|
} |
|
|
@ -27,17 +48,84 @@ void RCController::draw_gimbal(TFT_eSprite *m, uint16_t x, uint16_t y) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RCController::update(void) { |
|
|
|
if(!nrf_begin) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// Mapping before sending...
|
|
|
|
// txmessage.throttle = map(throttle_value, throttle_min, throttle_max, 1000, 2000);
|
|
|
|
|
|
|
|
// Trying to add in another bits of the switch here so we can save more bandwidth using the same rate
|
|
|
|
// txmessage.throttle = map(throttle_value, throttle_min, throttle_max, 1000, 2000);
|
|
|
|
|
|
|
|
// 1234 5678 9TTT TTTT TTTT -YYY YYYY YYYY -PPP PPPP PPPP -RRR RRRR RRRR (7 Bytes)
|
|
|
|
// |-------| |-------| |-------| |-------| |-------| |-------| |-------|
|
|
|
|
// Byte 00 Byte 01 Byte 02 Byte 03 Byte 04 Byte 05 Byte 06
|
|
|
|
|
|
|
|
// Generating the data
|
|
|
|
uint16_t raw_data1 = 0; |
|
|
|
uint16_t raw_data2 = 0; |
|
|
|
|
|
|
|
// Byte00 ~ Byte06 - START
|
|
|
|
raw_data1 = map(inputs.throttle, 0, 4096, 1000, 2000); |
|
|
|
//// txmessage.Byte00 = sw1 << 7 | sw2 << 6 | sw3 << 5 | sw4 << 4 | sw5 << 3 | sw6 << 2 | sw7 << 1 | sw8;
|
|
|
|
txmessage.Byte00 = 0x00; |
|
|
|
txmessage.Byte01 = 1 << 7 | (raw_data1 >> 4); |
|
|
|
raw_data2 = map(inputs.yaw, 0, 4096, 2000, 1000); |
|
|
|
txmessage.Byte02 = (raw_data1 & 0x000F) << 4 | raw_data2 >> 8; |
|
|
|
txmessage.Byte03 = (raw_data2 & 0x00FF); |
|
|
|
raw_data1 = map(inputs.pitch, 0, 4096, 2000, 1000); |
|
|
|
|
|
|
|
txmessage.Byte04 = raw_data1 >> 4; |
|
|
|
raw_data2 = map(inputs.roll, 0, 4096, 1000, 2000); |
|
|
|
txmessage.Byte05 = (raw_data1 & 0x000F) << 4 | raw_data2 >> 8; |
|
|
|
txmessage.Byte06 = (raw_data2 & 0x00FF); |
|
|
|
// Byte00 ~ Byte06 - END
|
|
|
|
|
|
|
|
// Sending Data over NRF
|
|
|
|
tx.write(&txmessage, sizeof(txmessage)); |
|
|
|
|
|
|
|
// Profiling..
|
|
|
|
nrf_profiling_raw++; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RCController::show(TFT_eSprite *m) { |
|
|
|
if(!nrf_begin) { |
|
|
|
nrf_begin = true; |
|
|
|
////////////////////ble.begin();
|
|
|
|
tx.begin(); |
|
|
|
tx.setAutoAck(false); |
|
|
|
tx.setPayloadSize(sizeof(TxMessage)); |
|
|
|
tx.setChannel(8); ///// Temporary hard code first
|
|
|
|
tx.setDataRate(RF24_250KBPS); // Lowest.. at 250kbps
|
|
|
|
|
|
|
|
switch(txpower) { // Set the transmit power
|
|
|
|
case 0: |
|
|
|
tx.setPALevel(RF24_PA_MIN); |
|
|
|
break; |
|
|
|
case 1: |
|
|
|
tx.setPALevel(RF24_PA_LOW); |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
tx.setPALevel(RF24_PA_HIGH); |
|
|
|
break; |
|
|
|
case 3: |
|
|
|
tx.setPALevel(RF24_PA_MAX); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
tx.openWritingPipe(pipeIn); |
|
|
|
// tx.printDetails();
|
|
|
|
Serial.println("nrf tx started."); |
|
|
|
} |
|
|
|
|
|
|
|
if((uint32_t) (millis() - last_updated) > 50) { |
|
|
|
updated = true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if((uint32_t) (millis() - last_voltage_read) > 500) { |
|
|
|
voltage = M5.Axp.GetBatVoltage(); |
|
|
|
current = M5.Axp.GetBatCurrent(); |
|
|
@ -45,13 +133,19 @@ void RCController::show(TFT_eSprite *m) { |
|
|
|
last_voltage_read = millis(); |
|
|
|
} |
|
|
|
|
|
|
|
if((uint32_t) (millis() - last_profile_update) > 1000) { |
|
|
|
nrf_profiling = nrf_profiling_raw; |
|
|
|
nrf_profiling_raw = 0; |
|
|
|
last_profile_update = millis(); |
|
|
|
} |
|
|
|
|
|
|
|
if(updated) { |
|
|
|
m->fillRect(0, 0, 320, 240, BLACK); |
|
|
|
m->setTextColor(WHITE, BLACK); |
|
|
|
m->setTextSize(2); |
|
|
|
m->setCursor(0, 0); |
|
|
|
m->print("Bat: "); |
|
|
|
m->print(String(voltage) + "V " + String(current) + "mA"); |
|
|
|
m->print(String(voltage) + "V " + String(current) + "mA " + String(nrf_profiling)); |
|
|
|
|
|
|
|
// Draw the gimbal box
|
|
|
|
draw_gimbal(m, 69, 150); |
|
|
@ -115,7 +209,7 @@ void RCController::show(TFT_eSprite *m) { |
|
|
|
|
|
|
|
// Key Input Handler
|
|
|
|
if(M5.BtnC.wasPressed()) { |
|
|
|
if(ble_begin) { |
|
|
|
if(nrf_begin) { |
|
|
|
nrf_begin = false; |
|
|
|
//////// ble.end();
|
|
|
|
} |
|
|
|