Correctly working binding mode

This commit is contained in:
Pawel Spychalski (DzikuVx)
2018-05-31 15:53:52 +02:00
parent 71ad7498a5
commit 03882af791
5 changed files with 73 additions and 40 deletions

View File

@@ -15,8 +15,8 @@
* DEVICE_MODE_TX * DEVICE_MODE_TX
* DEVICE_MODE_RX * DEVICE_MODE_RX
*/ */
#define DEVICE_MODE_TX // #define DEVICE_MODE_TX
// #define DEVICE_MODE_RX #define DEVICE_MODE_RX
#define FEATURE_TX_OLED #define FEATURE_TX_OLED
// #define FORCE_TX_WITHOUT_INPUT // #define FORCE_TX_WITHOUT_INPUT

View File

@@ -126,6 +126,20 @@ void onQspSuccess(QspConfiguration_t *qsp, TxDeviceState_t *txDeviceState, RxDev
qsp->forcePongFrame = true; qsp->forcePongFrame = true;
break; break;
case QSP_FRAME_BIND:
#ifdef DEVICE_MODE_RX
if (platformNode.isBindMode) {
platformNode.bindKey[0] = qsp->payload[0];
platformNode.bindKey[1] = qsp->payload[1];
platformNode.bindKey[2] = qsp->payload[2];
platformNode.bindKey[3] = qsp->payload[3];
platformNode.saveBindKey(platformNode.bindKey);
platformNode.leaveBindMode();
}
#endif
break;
case QSP_FRAME_PONG: case QSP_FRAME_PONG:
txDeviceState->roundtrip = qsp->payload[0]; txDeviceState->roundtrip = qsp->payload[0];
txDeviceState->roundtrip += (uint32_t) qsp->payload[1] << 8; txDeviceState->roundtrip += (uint32_t) qsp->payload[1] << 8;
@@ -191,12 +205,13 @@ void setup(void)
*/ */
Serial1.begin(100000, SERIAL_8E2); Serial1.begin(100000, SERIAL_8E2);
platformNode.isBindMode = true; platformNode.enterBindMode();
platformNode.bindModeExitMillis = millis() + 1000; LoRa.receive(); //TODO this probably should be moved somewhere....
#endif #endif
#ifdef DEVICE_MODE_TX #ifdef DEVICE_MODE_TX
randomSeed(analogRead(A4));
platformNode.seed(); platformNode.seed();
#ifdef FEATURE_TX_OLED #ifdef FEATURE_TX_OLED
@@ -225,17 +240,11 @@ void setup(void)
button0.start(); button0.start();
button1.start(); button1.start();
platformNode.loadBindKey(platformNode.bindKey);
#endif #endif
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
platformNode.loadBindKey();
// platformNode.bindKey[0] = 44;
// platformNode.bindKey[1] = 72;
// platformNode.bindKey[2] = 30;
// platformNode.bindKey[3] = 239;
} }
uint8_t currentSequenceIndex = 0; uint8_t currentSequenceIndex = 0;
@@ -294,18 +303,17 @@ int8_t getFrameToTransmit(QspConfiguration_t *qsp) {
void loop(void) void loop(void)
{ {
// delay(1000); static uint32_t nextKey = millis();
// Serial.print(platformNode.bindKey[0]);
// Serial.print(" ");
// Serial.print(platformNode.bindKey[1]);
// Serial.print(" ");
// Serial.print(platformNode.bindKey[2]);
// Serial.print(" ");
// Serial.println(platformNode.bindKey[3]);
uint32_t currentMillis = millis(); uint32_t currentMillis = millis();
#ifdef DEVICE_MODE_RX #ifdef DEVICE_MODE_RX
//Make sure to leave bind mode when binding is done
if (platformNode.isBindMode && millis() > platformNode.bindModeExitMillis) {
platformNode.leaveBindMode();
}
/* /*
* This routine handles resync of TX/RX while hoppping frequencies * This routine handles resync of TX/RX while hoppping frequencies
* When not in bind mode. Bind mode is single frequency operation * When not in bind mode. Bind mode is single frequency operation
@@ -403,7 +411,15 @@ void loop(void)
break; break;
case QSP_FRAME_BIND: case QSP_FRAME_BIND:
encodeBindPayload(&qsp, platformNode.bindKey);
/*
* Key to be transmitted is stored in EEPROM
* During binding different key is used
*/
uint8_t key[4];
platformNode.loadBindKey(key);
encodeBindPayload(&qsp, key);
break; break;
} }
@@ -552,11 +568,18 @@ void loop(void)
platformNode.nextLedUpdate = currentMillis + 200; platformNode.nextLedUpdate = currentMillis + 200;
} }
#else #else
platformNode.nextLedUpdate = currentMillis + 200;
if (platformNode.platformState == DEVICE_STATE_FAILSAFE) { if (platformNode.isBindMode) {
digitalWrite(LED_BUILTIN, HIGH); platformNode.nextLedUpdate = currentMillis + 50;
} else {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
} else {
platformNode.nextLedUpdate = currentMillis + 200;
if (platformNode.platformState == DEVICE_STATE_FAILSAFE) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
} }
#endif #endif
} }
@@ -570,7 +593,6 @@ void onReceive(int packetSize)
* If not reading, then we might start * If not reading, then we might start
*/ */
if (radioNode.bytesToRead == NO_DATA_TO_READ) { if (radioNode.bytesToRead == NO_DATA_TO_READ) {
if (packetSize >= MIN_PACKET_SIZE && packetSize <= MAX_PACKET_SIZE) { if (packetSize >= MIN_PACKET_SIZE && packetSize <= MAX_PACKET_SIZE) {
//We have a packet candidate that might contain a valid QSP packet //We have a packet candidate that might contain a valid QSP packet
radioNode.bytesToRead = packetSize; radioNode.bytesToRead = packetSize;

View File

@@ -4,10 +4,11 @@ PlatformNode::PlatformNode(void) {
for (uint8_t i = 0; i < PLATFORM_TOTAL_CHANNEL_COUNT; i++) { for (uint8_t i = 0; i < PLATFORM_TOTAL_CHANNEL_COUNT; i++) {
_channels[i] = PLATFORM_DEFAULT_CHANNEL_VALUE; _channels[i] = PLATFORM_DEFAULT_CHANNEL_VALUE;
} }
randomSeed(analogRead(0));
} }
/**
* Return true if new bind key was generated
*/
void PlatformNode::seed(void) { void PlatformNode::seed(void) {
uint8_t val; uint8_t val;
@@ -22,11 +23,19 @@ void PlatformNode::seed(void) {
} }
} }
void PlatformNode::loadBindKey(void) { void PlatformNode::loadBindKey(uint8_t key[]) {
bindKey[0] = EEPROM.read(EEPROM_ADDRESS_BIND_0); key[0] = EEPROM.read(EEPROM_ADDRESS_BIND_0);
bindKey[1] = EEPROM.read(EEPROM_ADDRESS_BIND_1); key[1] = EEPROM.read(EEPROM_ADDRESS_BIND_1);
bindKey[2] = EEPROM.read(EEPROM_ADDRESS_BIND_2); key[2] = EEPROM.read(EEPROM_ADDRESS_BIND_2);
bindKey[3] = EEPROM.read(EEPROM_ADDRESS_BIND_3); key[3] = EEPROM.read(EEPROM_ADDRESS_BIND_3);
}
void PlatformNode::saveBindKey(uint8_t key[]) {
EEPROM.write(EEPROM_ADDRESS_BIND_0, key[0]);
EEPROM.write(EEPROM_ADDRESS_BIND_1, key[1]);
EEPROM.write(EEPROM_ADDRESS_BIND_2, key[2]);
EEPROM.write(EEPROM_ADDRESS_BIND_3, key[3]);
EEPROM.write(EEPROM_ADDRESS_BIND_KEY_SEEDED, 0xf1);
} }
int PlatformNode::getRcChannel(uint8_t channel) { int PlatformNode::getRcChannel(uint8_t channel) {
@@ -48,9 +57,9 @@ void PlatformNode::enterBindMode(void) {
// Set temporary bind mode // Set temporary bind mode
bindKey[0] = 0xf1; bindKey[0] = 0xf1;
bindKey[0] = 0x1e; bindKey[1] = 0x1e;
bindKey[0] = 0x07; bindKey[2] = 0x07;
bindKey[0] = 0x42; bindKey[3] = 0x42;
radioNode.set( radioNode.set(
0, // Minimum power 0, // Minimum power
@@ -59,10 +68,11 @@ void PlatformNode::enterBindMode(void) {
5, // same for coding rate 5, // same for coding rate
868000000 //Fixed frequency while binding 868000000 //Fixed frequency while binding
); );
bindModeExitMillis = millis() + 1000; //This happens only on RX
} }
void PlatformNode::leaveBindMode(void) { void PlatformNode::leaveBindMode(void) {
isBindMode = false; isBindMode = false;
loadBindKey(); loadBindKey(bindKey);
radioNode.reset(); radioNode.reset();
} }

View File

@@ -37,7 +37,8 @@ class PlatformNode {
void enterBindMode(void); void enterBindMode(void);
void leaveBindMode(void); void leaveBindMode(void);
void seed(void); void seed(void);
void loadBindKey(); void loadBindKey(uint8_t key[]);
void saveBindKey(uint8_t key[]);
uint8_t bindKey[4]; uint8_t bindKey[4];
uint32_t nextLedUpdate = 0; uint32_t nextLedUpdate = 0;
uint8_t platformState = DEVICE_STATE_UNDETERMINED; uint8_t platformState = DEVICE_STATE_UNDETERMINED;