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_RX
*/
#define DEVICE_MODE_TX
// #define DEVICE_MODE_RX
// #define DEVICE_MODE_TX
#define DEVICE_MODE_RX
#define FEATURE_TX_OLED
// #define FORCE_TX_WITHOUT_INPUT

View File

@@ -126,6 +126,20 @@ void onQspSuccess(QspConfiguration_t *qsp, TxDeviceState_t *txDeviceState, RxDev
qsp->forcePongFrame = true;
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:
txDeviceState->roundtrip = qsp->payload[0];
txDeviceState->roundtrip += (uint32_t) qsp->payload[1] << 8;
@@ -191,12 +205,13 @@ void setup(void)
*/
Serial1.begin(100000, SERIAL_8E2);
platformNode.isBindMode = true;
platformNode.bindModeExitMillis = millis() + 1000;
platformNode.enterBindMode();
LoRa.receive(); //TODO this probably should be moved somewhere....
#endif
#ifdef DEVICE_MODE_TX
randomSeed(analogRead(A4));
platformNode.seed();
#ifdef FEATURE_TX_OLED
@@ -225,17 +240,11 @@ void setup(void)
button0.start();
button1.start();
platformNode.loadBindKey(platformNode.bindKey);
#endif
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;
@@ -294,18 +303,17 @@ int8_t getFrameToTransmit(QspConfiguration_t *qsp) {
void loop(void)
{
// delay(1000);
// 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]);
static uint32_t nextKey = millis();
uint32_t currentMillis = millis();
#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
* When not in bind mode. Bind mode is single frequency operation
@@ -403,7 +411,15 @@ void loop(void)
break;
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;
}
@@ -552,11 +568,18 @@ void loop(void)
platformNode.nextLedUpdate = currentMillis + 200;
}
#else
platformNode.nextLedUpdate = currentMillis + 200;
if (platformNode.platformState == DEVICE_STATE_FAILSAFE) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
if (platformNode.isBindMode) {
platformNode.nextLedUpdate = currentMillis + 50;
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
}
@@ -570,7 +593,6 @@ void onReceive(int packetSize)
* If not reading, then we might start
*/
if (radioNode.bytesToRead == NO_DATA_TO_READ) {
if (packetSize >= MIN_PACKET_SIZE && packetSize <= MAX_PACKET_SIZE) {
//We have a packet candidate that might contain a valid QSP packet
radioNode.bytesToRead = packetSize;

View File

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

View File

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

View File

@@ -244,6 +244,6 @@ void encodeBindPayload(QspConfiguration_t *qsp, uint8_t bindKey[]) {
for (uint8_t i = 0; i < qspFrameLengths[QSP_FRAME_PING]; i++) {
qsp->payload[i] = bindKey[i];
}
qsp->payloadLength = qspFrameLengths[QSP_FRAME_PING];
}