Skip to content

Commit 55135d8

Browse files
committed
Incremental steps towards remote command control
1 parent 83b5585 commit 55135d8

File tree

6 files changed

+97
-458
lines changed

6 files changed

+97
-458
lines changed

Firmware/LoRaSerial_Firmware/Commands.ino

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
//Check to see if a valid command has been received
88
void checkCommand()
99
{
10+
//Check if this command was received over the RF link or local serial
11+
if (serialState == SERIAL_PASSTHROUGH && commandBuffer[0] == 'R')
12+
{
13+
//We have a remote command, change the data to AT command, but pass response to command back out over RF link
14+
commandBuffer[0] = 'A';
15+
}
16+
1017
systemPrintln();
1118

1219
if (commandLength < 2) //Too short
@@ -16,6 +23,10 @@ void checkCommand()
1623
else if (isATcommand(commandBuffer) == false)
1724
reportERROR();
1825

26+
//Check for 'RT'
27+
else if (isRTcommand(commandBuffer) == false)
28+
sendRemoteCommand();
29+
1930
//'AT'
2031
else if (commandLength == 2)
2132
reportOK();
@@ -37,31 +48,20 @@ void checkCommand()
3748
systemPrintln();
3849
break;
3950
case ('O'): //Exit command mode
40-
//If linked, send new settings to remote unit
41-
if (isLinked() == true)
42-
{
43-
//Todo check to see if there are new settings to transmit or not
44-
settingsDelivered = 0;
45-
sendCommandDataPacket(); //Send updated settings to remote
46-
changeState(RADIO_LINKED_COMMAND_TRANSMITTING);
47-
}
51+
//Apply settings and return
52+
generateHopTable(); //Generate freq with new settings
53+
configureRadio(); //Apply any new settings
54+
55+
digitalWrite(pin_linkLED, LOW);
56+
digitalWrite(pin_activityLED, LOW);
57+
if (settings.pointToPoint == true)
58+
changeState(RADIO_NO_LINK_RECEIVING_STANDBY);
4859
else
49-
{
50-
//If not linked, apply settings and return
51-
generateHopTable(); //Generate freq with new settings
52-
configureRadio(); //Apply any new settings
53-
54-
digitalWrite(pin_linkLED, LOW);
55-
digitalWrite(pin_activityLED, LOW);
56-
if (settings.pointToPoint == true)
57-
changeState(RADIO_NO_LINK_RECEIVING_STANDBY);
58-
else
59-
changeState(RADIO_BROADCASTING_RECEIVING_STANDBY);
60+
changeState(RADIO_BROADCASTING_RECEIVING_STANDBY);
6061

61-
serialState = RADIO_SERIAL_PASSTHROUGH;
62+
serialState = SERIAL_PASSTHROUGH;
6263

63-
reportOK();
64-
}
64+
reportOK();
6565
break;
6666
case ('T'): //Enter training mode
6767
reportOK();
@@ -722,6 +722,31 @@ bool isATcommand(char *buffer)
722722
return (false);
723723
}
724724

725+
//Check if RT appears in the correct position
726+
bool isRTcommand(char *buffer)
727+
{
728+
if (buffer[0] == 'R')
729+
if (buffer[1] == 'T')
730+
return (true);
731+
return (false);
732+
}
733+
734+
//Send the AT command over RF link, if available
735+
void sendRemoteCommand()
736+
{
737+
if (isLinked() == true && radioState == RADIO_LINKED_RECEIVING_STANDBY)
738+
{
739+
for (int x = 0 ; x < commandLength ; x++)
740+
outgoingPacket[x] = commandBuffer[x];
741+
742+
packetSize = commandLength;
743+
744+
//Fire off packet
745+
sendCommandDataPacket();
746+
//changeState();
747+
}
748+
}
749+
725750
//Show current settings in user friendly way
726751
void displayParameters(char parameterType)
727752
{

Firmware/LoRaSerial_Firmware/Radio.ino

Lines changed: 11 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ PacketType identifyPacketType()
9797
return (PROCESS_TRAINING_CONTROL_PACKET);
9898
}
9999

100-
//If this packet is marked as a remote command, it's either an ack or a ping
100+
//If this packet is marked as a remote command, it's either an ack or a zero length packet (not known)
101101
else if (receiveTrailer.remoteCommand == 1)
102102
{
103103
if (receiveTrailer.ack == 1)
@@ -106,22 +106,15 @@ PacketType identifyPacketType()
106106
return (PACKET_COMMAND_ACK);
107107
}
108108

109-
LRS_DEBUG_PRINTLN(F("RX: Command Ping"));
110-
return (PACKET_COMMAND_PING);
109+
LRS_DEBUG_PRINTLN(F("RX: Unknown Command"));
110+
return (PROCESS_BAD_PACKET);
111111
}
112112

113113
//Not training, not command packet, just a ping
114114
LRS_DEBUG_PRINTLN(F("RX: Control Packet"));
115115
return (PROCESS_CONTROL_PACKET);
116116
}
117117

118-
//If we have one byte, and remote command, we are being asked for our settings
119-
if (receivedBytes == 1 && receiveTrailer.remoteCommand == 1)
120-
{
121-
LRS_DEBUG_PRINTLN(F("RX: Command Request Settings"));
122-
return (PACKET_COMMAND_REQUEST_SETTINGS);
123-
}
124-
125118
//Update lastPacket details with current packet
126119
memcpy(lastPacket, incomingBuffer, receivedBytes);
127120
lastPacketSize = receivedBytes;
@@ -136,15 +129,9 @@ PacketType identifyPacketType()
136129

137130
else if (receiveTrailer.remoteCommand == 1)
138131
{
139-
if (receiveTrailer.ack == 1)
140-
{
141-
//Remote is giving us their settings
142-
LRS_DEBUG_PRINTLN(F("RX: Command Current Settings"));
143-
return (PACKET_COMMAND_CURRENT_SETTINGS);
144-
}
145-
//New settings from remote
146-
LRS_DEBUG_PRINTLN(F("RX: Command New Settings"));
147-
return (PACKET_COMMAND_NEW_SETTINGS);
132+
//New data from remote
133+
LRS_DEBUG_PRINTLN(F("RX: Command Data"));
134+
return (PACKET_COMMAND_DATA);
148135
}
149136

150137
LRS_DEBUG_PRINTLN(F("RX: Data packet"));
@@ -490,110 +477,19 @@ void sendCommandAckPacket()
490477
sendPacket();
491478
}
492479

493-
//Create short packet of 2 control bytes - query remote radio for proof of life (ack)
494-
void sendCommandPingPacket()
495-
{
496-
LRS_DEBUG_PRINT(F("TX: Command Ping "));
497-
responseTrailer.ack = 0; //This is not an ACK to a previous transmission
498-
responseTrailer.resend = 0; //This is not a resend
499-
responseTrailer.train = 0; //This is not a training packet
500-
responseTrailer.remoteCommand = 1; //This is a remote command packet
501-
502-
packetSize = 2;
503-
packetSent = 0; //Reset the number of times we've sent this packet
504-
505-
//SF6 requires an implicit header which means there is no dataLength in the header
506-
//Because we cannot predict when a ping packet will be received, the receiver will always
507-
//expecting 255 bytes. Pings must be increased to 255 bytes. ACKs are still 2 bytes.
508-
if (settings.radioSpreadFactor == 6)
509-
{
510-
//Manually store actual data length 3 bytes from the end (before NetID)
511-
//Manual packet size is whatever has been processed + 1 for the manual packetSize byte
512-
outgoingPacket[255 - 3] = packetSize + 1;
513-
packetSize = 255; //We're now going to transmit 255 bytes
514-
}
515-
516-
expectingAck = true; //We expect destination to ack
517-
sendPacket();
518-
}
519-
520-
//Create short packet of 2 control bytes, 1 dummy byte, with remoteSetting = 1
521-
void sendCommandRequestSettingsPacket()
522-
{
523-
LRS_DEBUG_PRINT(F("TX: Settings Request "));
524-
responseTrailer.ack = 0; //This is not an ACK to a previous transmission
525-
responseTrailer.resend = 0; //This is not a resend
526-
responseTrailer.train = 0; //This is not a training packet
527-
responseTrailer.remoteCommand = 1; //This is a remote command packet
528-
529-
outgoingPacket[0] = 0xAA; //Avoid DC bias
530-
531-
packetSize = 3;
532-
packetSent = 0; //Reset the number of times we've sent this packet
533-
534-
//SF6 requires an implicit header which means there is no dataLength in the header
535-
//Because we cannot predict when a ping packet will be received, the receiver will always
536-
//expecting 255 bytes. Pings must be increased to 255 bytes. ACKs are still 2 bytes.
537-
if (settings.radioSpreadFactor == 6)
538-
{
539-
//Manually store actual data length 3 bytes from the end (before NetID)
540-
//Manual packet size is whatever has been processed + 1 for the manual packetSize byte
541-
outgoingPacket[255 - 3] = packetSize + 1;
542-
packetSize = 255; //We're now going to transmit 255 bytes
543-
}
544-
545-
expectingAck = true; //We expect destination to ack
546-
sendPacket();
547-
}
548-
549-
//Create packet of unit's settings with remote command = 1, ack = 0
550-
void sendCommandCurrentSettingsPacket()
480+
//Create packet of serial command with remote command = 1, ack = 0
481+
void sendCommandDataPacket()
551482
{
552-
LRS_DEBUG_PRINT(F("TX: Settings Data "));
483+
LRS_DEBUG_PRINT(F("TX: Command Data "));
553484
responseTrailer.ack = 0; //This is not an ACK to a previous transmission.
554485
responseTrailer.resend = 0; //This is not a resend
555486
responseTrailer.train = 0; //This is not training packet
556487
responseTrailer.remoteCommand = 1; //This is a remote control packet
557488

558-
moveSettingsToPacket(settings, outgoingPacket); //Copy settings to outgoingPacket
559-
packetSize = sizeof(settings);
560-
561-
packetSize += 2;
562-
packetSent = 0; //Reset the number of times we've sent this packet
563-
564-
//SF6 requires an implicit header which means there is no dataLength in the header
565-
//Because we cannot predict when a ping packet will be received, the receiver will always
566-
//expecting 255 bytes. Pings must be increased to 255 bytes. ACKs are still 2 bytes.
567-
if (settings.radioSpreadFactor == 6)
568-
{
569-
//Manually store actual data length 3 bytes from the end (before NetID)
570-
//Manual packet size is whatever has been processed + 1 for the manual packetSize byte
571-
outgoingPacket[255 - 3] = packetSize + 1;
572-
packetSize = 255; //We're now going to transmit 255 bytes
573-
}
574-
575-
expectingAck = true; //We do expect destination to ack
576-
sendPacket();
577-
}
578-
579-
//Create packet of unit's settings with remote command = 1
580-
void sendCommandNewSettingsPacket()
581-
{
582-
LRS_DEBUG_PRINT(F("TX: Settings Data "));
583-
responseTrailer.ack = 0; //This is not a normal ping packet.
584-
responseTrailer.resend = 0; //This is not a resend
585-
responseTrailer.train = 0; //This is not training packet
586-
responseTrailer.remoteCommand = 1; //This is a remote command packet
587-
588-
moveSettingsToPacket(settings, outgoingPacket); //Copy settings to outgoingPacket
589-
packetSize = sizeof(settings);
590-
591-
packetSize += 2;
489+
packetSize += 2; //Make room for control bytes
592490
packetSent = 0; //Reset the number of times we've sent this packet
593491

594492
//SF6 requires an implicit header which means there is no dataLength in the header
595-
//Because we cannot predict when a ping packet will be received, the receiver will always
596-
//expecting 255 bytes. Pings must be increased to 255 bytes. ACKs are still 2 bytes.
597493
if (settings.radioSpreadFactor == 6)
598494
{
599495
//Manually store actual data length 3 bytes from the end (before NetID)
@@ -602,24 +498,7 @@ void sendCommandNewSettingsPacket()
602498
packetSize = 255; //We're now going to transmit 255 bytes
603499
}
604500

605-
expectingAck = false; //We do not expect destination to ack
606-
sendPacket();
607-
}
608-
609-
//Create short packet of 2 control bytes - do not expect ack, remoteCommand = 1
610-
void sendCommandDataPacketAck()
611-
{
612-
LRS_DEBUG_PRINT(F("TX: Ack "));
613-
responseTrailer.ack = 0; //This is not a normal packet, no ack
614-
responseTrailer.resend = 0; //This is not a resend
615-
responseTrailer.train = 0; //This is not a training packet
616-
responseTrailer.remoteCommand = 1; //This is a remote control packet
617-
responseTrailer.remoteCommandAck = 1; //ACK the reception of the remote command data packet
618-
619-
packetSize = 2;
620-
packetSent = 0; //Reset the number of times we've sent this packet
621-
622-
expectingAck = false; //We do not expect destination to ack
501+
expectingAck = true; //We expect destination to ack
623502
sendPacket();
624503
}
625504

Firmware/LoRaSerial_Firmware/Serial.ino

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void updateSerial()
8080
byte incoming = systemRead();
8181

8282
//Process serial into either rx buffer or command buffer
83-
if (serialState == RADIO_SERIAL_COMMAND)
83+
if (serialState == SERIAL_COMMAND)
8484
{
8585
if (incoming == '\r' && commandLength > 0)
8686
checkCommand(); //Process potential command
@@ -112,19 +112,7 @@ void updateSerial()
112112

113113
systemPrintln(F("\r\nOK"));
114114

115-
serialState = RADIO_SERIAL_COMMAND;
116-
117-
//If we are linked to a remote radio, request remote settings
118-
if (isLinked() == true)
119-
{
120-
//Transmit empty packet with remoteSettings = 1 to get remote's settings
121-
sendCommandPacket();
122-
123-
//Recalculate packetAirTime because we need to wait not for a 2-byte response, but a ~73 byte response
124-
packetAirTime = calcAirTime(sizeof(settings));
125-
126-
changeState(RADIO_LINKED_COMMAND_TRANSMITTING);
127-
}
115+
serialState = SERIAL_COMMAND;
128116

129117
escapeCharsReceived = 0;
130118
lastByteReceived_ms = millis();

0 commit comments

Comments
 (0)