Change coding style to match library style guide.

This commit is contained in:
Nathan Seidle 2022-01-17 11:43:13 -07:00
parent 20e1ab23df
commit c4fec32965
2 changed files with 73 additions and 96 deletions

View file

@ -18,68 +18,61 @@
The SX1276 / 7 / 8 / 9 supports FHSS or Frequency Hopping Spread Spectrum.
Once a hopping period is set and a transmission is started the radio
will begin triggering interrupts every hop period where the radio frequency
is changed to the next channel. This allows a simple mechanism to abide by
the FCC 400ms max dwell time rule.
https://www.govinfo.gov/content/pkg/CFR-2019-title47-vol1/pdf/CFR-2019-title47-vol1-sec15-247.pdf
is changed to the next channel.
*/
#include <RadioLib.h> //Click here to get the library: http://librarymanager/All#RadioLib
//Pins for RFM97 100mW Shield to SparkFun ESP32 Thing Plus C
int pin_cs = 15;
int pin_dio0 = 26;
int pin_dio1 = 25;
int pin_rst = 32;
// SX1276 has the following connections:
const int pin_cs = 10;
const int pin_dio0 = 2;
const int pin_dio1 = 9;
const int pin_rst = 3;
SX1276 radio = new Module(pin_cs, pin_dio0, pin_rst, pin_dio1);
int counter = 0;
volatile bool rxComplete = false;
volatile bool fhssChange = false;
//The channel frequencies can be generated randomly or hard coded
// the channel frequencies can be generated randomly or hard coded
float channels[] = {908.0, 906.0, 907.0, 905.0, 903.0, 910.0, 909.0};
int numberOfChannels = sizeof(channels) / sizeof(float);
int hopsCompleted = 0;
void setup()
{
Serial.begin(115200);
void setup() {
Serial.begin(9600);
//Begin radio on home channel
// begin radio on home channel
Serial.print(F("[SX127x] Initializing ... "));
int state = radio.begin(channels[0]);
if (state != RADIOLIB_ERR_NONE)
{
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("Failed with code: "));
Serial.println(state);
}
else
Serial.println(F("Success!"));
// Set hop period to enable FHSS
// We set an artifically short period to show lots of hops
// HoppingPeriod = Tsym * FreqHoppingPeriod
// Given defaults of spreadfactor = 9, bandwidth = 125, it follows Tsym = 4.10ms
// HoppingPeriod = 4.10 * 9 = 36.9ms. Can be as high as 400ms to be within regulatory limits
radio.setFHSSHoppingPeriod(9);
// set hop period to enable FHSS
state = radio.setFHSSHoppingPeriod(9);
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("Error setting hopping period: "));
Serial.println(state);
}
radio.setDio0Action(dio0ISR); // called when transmission is finished
radio.setDio1Action(dio1ISR); // called after a transmission has started, so we can move to next freq
Serial.print(F("Hopping period: "));
Serial.println(radio.getFHSSHoppingPeriod());
radio.setDio0Action(dio0ISR); //Called when transmission is finished
radio.setDio1Action(dio1ISR); //Called after a transmission has started, so we can move to next freq
radio.startReceive();
Serial.println(F("Waiting for new packet"));
// start listening for LoRa packets
Serial.print(F("[SX1278] Starting to listen ... "));
state = radio.startReceive();
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
}
void loop()
{
if (rxComplete == true)
{
void loop() {
if (rxComplete == true) {
uint8_t incomingBuffer[255];
radio.readData(incomingBuffer, 255);
uint8_t receivedBytes = radio.getPacketLength();
@ -95,11 +88,8 @@ void loop()
rxComplete = false;
}
if (fhssChange == true)
{
if (fhssChange == true) {
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
//Serial.print(F("Radio on channel: "));
//Serial.println(radio.getFHSSChannel());
hopsCompleted++;
radio.clearFHSSInt();
@ -107,16 +97,14 @@ void loop()
}
}
//ISR when DIO0 goes low
//Called when transmission is complete or when RX is received
void dio0ISR(void)
{
// ISR when DIO0 goes low
// called when transmission is complete or when RX is received
void dio0ISR(void) {
rxComplete = true;
}
//ISR when DIO1 goes low
//Called when FhssChangeChannel interrupt occurs (at the beginning of each transmission)
void dio1ISR(void)
{
// ISR when DIO1 goes low
// called when FhssChangeChannel interrupt occurs (at the beginning of each transmission)
void dio1ISR(void) {
fhssChange = true;
}
}

View file

@ -18,77 +18,71 @@
The SX1276 / 7 / 8 / 9 supports FHSS or Frequency Hopping Spread Spectrum.
Once a hopping period is set and a transmission is started the radio
will begin triggering interrupts every hop period where the radio frequency
is changed to the next channel. This allows a simple mechanism to abide by
the FCC 400ms max dwell time rule.
https://www.govinfo.gov/content/pkg/CFR-2019-title47-vol1/pdf/CFR-2019-title47-vol1-sec15-247.pdf
is changed to the next channel.
*/
#include <RadioLib.h> //Click here to get the library: http://librarymanager/All#RadioLib
//Pins for SparkFun 1W EBYTE Breakout to Uno
int pin_cs = 7;
int pin_dio0 = 3;
int pin_dio1 = 2;
int pin_rst = A2;
// SX1276 has the following connections:
const int pin_cs = 10;
const int pin_dio0 = 2;
const int pin_dio1 = 9;
const int pin_rst = 3;
SX1276 radio = new Module(pin_cs, pin_dio0, pin_rst, pin_dio1);
volatile bool xmitComplete = false;
volatile bool fhssChange = false;
//The channel frequencies can be generated randomly or hard coded
// the channel frequencies can be generated randomly or hard coded
float channels[] = {908.0, 906.0, 907.0, 905.0, 903.0, 910.0, 909.0};
int numberOfChannels = sizeof(channels) / sizeof(float);
int hopsCompleted = 0;
int counter = 0;
void setup()
{
Serial.begin(115200);
void setup() {
Serial.begin(9600);
//Begin radio on home channel
// begin radio on home channel
Serial.print(F("[SX127x] Initializing ... "));
int state = radio.begin(channels[0]);
if (state != RADIOLIB_ERR_NONE)
{
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("Failed with code: "));
Serial.println(state);
}
else
Serial.println(F("Success!"));
// Set hop period to enable FHSS
// We set an artifically short period to show lots of hops
// HoppingPeriod = Tsym * FreqHoppingPeriod
// Given defaults of spreadfactor = 9, bandwidth = 125, it follows Tsym = 4.10ms
// HoppingPeriod = 4.10 * 9 = 36.9ms. Can be as high as 400ms to be within regulatory limits
radio.setFHSSHoppingPeriod(9);
// set hop period to enable FHSS
state = radio.setFHSSHoppingPeriod(9);
if(state != RADIOLIB_ERR_NONE) {
Serial.print(F("Error setting hopping period: "));
Serial.println(state);
}
Serial.print(F("Hopping period: "));
Serial.println(radio.getFHSSHoppingPeriod());
radio.setDio0Action(dio0ISR); //Called when transmission is finished
radio.setDio1Action(dio1ISR); //Called after a transmission has started, so we can move to next freq
radio.setDio0Action(dio0ISR); // called when transmission is finished
radio.setDio1Action(dio1ISR); // called after a transmission has started, so we can move to next freq
Serial.print(F("Transmitting packet..."));
char output[256];
sprintf(output, "Let's create a really long packet to trigger lots of hop interrupts. A packet can be up to 256 bytes long. This packet is 222 bytes so using sf = 9, bw = 125, timeOnAir is 1488ms. 1488ms / (9*4.10ms) = 40 hops. Counter: %d", counter++);
String longOutput = "Let's create a really long packet to trigger lots of hop interrupts. A packet can be up to 256 bytes long. This packet is 222 bytes so using sf = 9, bw = 125, timeOnAir is 1488ms. 1488ms / (9*4.10ms) = 40 hops. Counter: ";
radio.startTransmit(output, strlen(output) - 1);
state = radio.startTransmit(longOutput + counter);
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("Error transmitting with code: "));
Serial.println(state);
}
}
void loop()
{
if (xmitComplete == true)
{
void loop() {
if (xmitComplete == true) {
xmitComplete = false;
Serial.println(F("Transmit complete"));
Serial.print(F("Radio after xmit is on channel: "));
Serial.println(radio.getFHSSChannel());
//The FHSS channel is automatically reset to 0 upon end of transmission
// the FHSS channel is automatically reset to 0 upon end of transmission
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]); //Return to home channel before next transaction
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]); // Return to home channel before next transaction
Serial.print(F("Hops completed: "));
Serial.println(hopsCompleted);
@ -97,11 +91,8 @@ void loop()
radio.startReceive();
}
if (fhssChange == true)
{
if (fhssChange == true) {
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
//Serial.print(F("Radio on channel: "));
//Serial.println(radio.getFHSSChannel());
hopsCompleted++;
fhssChange = false;
@ -109,16 +100,14 @@ void loop()
}
}
//ISR when DIO0 goes low
//Called when transmission is complete or when RX is received
void dio0ISR(void)
{
// ISR when DIO0 goes low
// called when transmission is complete or when RX is received
void dio0ISR(void) {
xmitComplete = true;
}
//ISR when DIO1 goes low
//Called when FhssChangeChannel interrupt occurs (at regular HoppingPeriods)
void dio1ISR(void)
{
// ISR when DIO1 goes low
// called when FhssChangeChannel interrupt occurs (at regular HoppingPeriods)
void dio1ISR(void) {
fhssChange = true;
}