diff --git a/examples/RadioLibExamples/SX1280/Ranging/54_Ranging_Master/54_Ranging_Master.ino b/examples/RadioLibExamples/SX1280/Ranging/54_Ranging_Master/54_Ranging_Master.ino new file mode 100644 index 0000000..558471c --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/54_Ranging_Master/54_Ranging_Master.ino @@ -0,0 +1,230 @@ +/***************************************************************************************************** + Programs for Arduino - Copyright of the author Stuart Robinson - 16/03/20 + + This program is supplied as is, it is up to the user of the program to decide if the program is + suitable for the intended purpose and free from errors. +*******************************************************************************************************/ + +#define Program_Version "V1.0" + +#include +#include +#include +#include "Settings.h" + +SX128XLT LT; + + +#ifdef ENABLEOLED +#include //https://github.com/olikraus/u8g2 +U8X8_SSD1306_128X64_NONAME_HW_I2C disp(U8X8_PIN_NONE); //standard 0.96" SSD1306 +// U8X8_SH1106_128X64_NONAME_HW_I2C disp(U8X8_PIN_NONE); //1.3" OLED often sold as 1.3" SSD1306 +#endif + + +uint16_t rangeing_errors, rangeings_valid; +uint16_t IrqStatus; +uint32_t endwaitmS, startrangingmS; +float range_result_sum, range_result_average, rangeing_results; +double distance, distance_sum, distance_average; +bool ranging_error; +int32_t range_result; +int16_t RangingRSSI; + + +void loop() +{ + uint8_t index; + distance_sum = 0; + range_result_sum = 0; + rangeing_results = 0; //count of valid results in each loop + + for (index = 1; index <= rangeingcount; index++) { + + startrangingmS = millis(); + + Serial.print(F("Start Ranging")); + + bool res = LT.transmitRanging(RangingAddress, TXtimeoutmS, RangingTXPower, WAIT_TX); + if (!res) { + Serial.println(" Failed!"); + } else { + Serial.println(" Successed!"); + } + IrqStatus = LT.readIrqStatus(); + + if (IrqStatus & IRQ_RANGING_MASTER_RESULT_VALID) { + rangeing_results++; + rangeings_valid++; + digitalWrite(LED1, HIGH); + Serial.print(F("Valid")); + range_result = LT.getRangingResultRegValue(RANGING_RESULT_RAW); + Serial.print(F(",Register,")); + Serial.print(range_result); + + if (range_result > 800000) { + range_result = 0; + } + range_result_sum = range_result_sum + range_result; + + distance = LT.getRangingDistance(RANGING_RESULT_RAW, range_result, distance_adjustment); + distance_sum = distance_sum + distance; + + Serial.print(F(",Distance,")); + Serial.print(distance, 1); + Serial.print(F(",RSSIReg,")); + Serial.print(LT.readRegister(REG_RANGING_RSSI)); + RangingRSSI = LT.getRangingRSSI(); + Serial.print(F(",RSSI,")); + Serial.print(RangingRSSI); + Serial.print(F("dBm")); + digitalWrite(LED1, LOW); + } else { + rangeing_errors++; + distance = 0; + range_result = 0; + Serial.print(F("NotValid")); + Serial.print(F(",Irq,")); + Serial.print(IrqStatus, HEX); + } + delay(packet_delaymS); + + if (index == rangeingcount) { + range_result_average = (range_result_sum / rangeing_results); + + if (rangeing_results == 0) { + distance_average = 0; + } else { + distance_average = (distance_sum / rangeing_results); + } + + Serial.print(F(",TotalValid,")); + Serial.print(rangeings_valid); + Serial.print(F(",TotalErrors,")); + Serial.print(rangeing_errors); + Serial.print(F(",AverageRAWResult,")); + Serial.print(range_result_average); + Serial.print(F(",AverageDistance,")); + Serial.print(distance_average, 1); + +#ifdef ENABLEDISPLAY + display_screen1(); +#endif + + delay(2000); + + } + Serial.println(); + } + +} + +#ifdef ENABLEDISPLAY +void display_screen1() +{ + disp.clear(); + disp.setCursor(0, 0); + disp.print(F("Distance ")); + disp.print(distance_average, 1); + disp.print(F("m")); + disp.setCursor(0, 2); + disp.print(F("RSSI ")); + disp.print(RangingRSSI); + disp.print(F("dBm")); + disp.setCursor(0, 4); + disp.print(F("OK,")); + disp.print(rangeings_valid); + disp.print(F(",Err,")); + disp.print(rangeing_errors); +} +#endif + + +void led_Flash(uint16_t flashes, uint16_t delaymS) +{ + uint16_t index; + + for (index = 1; index <= flashes; index++) { + digitalWrite(LED1, HIGH); + delay(delaymS); + digitalWrite(LED1, LOW); + delay(delaymS); + } +} + + +void setup() +{ + + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + Wire.begin(I2C_SDA, I2C_SCL); + + + pinMode(LED1, OUTPUT); //setup pin as output for indicator LED + led_Flash(4, 125); //two quick LED flashes to indicate program start + + Serial.begin(115200); + Serial.println(); + Serial.print(F(__TIME__)); + Serial.print(F(" ")); + Serial.println(F(__DATE__)); + Serial.println(F(Program_Version)); + Serial.println(); + Serial.println(F("54_Ranging_Master Starting")); + + + led_Flash(2, 125); + + if (LT.begin(NSS, NRESET, RFBUSY, DIO1, DIO2, DIO3, RX_EN, TX_EN, LORA_DEVICE)) { + Serial.println(F("Device found")); + led_Flash(2, 125); + delay(1000); + } else { + Serial.println(F("No device responding")); + while (1) { + led_Flash(50, 50); //long fast speed flash indicates device error + } + } + + + LT.setupRanging(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, RangingAddress, RANGING_MASTER); + + LT.setRangingCalibration(Calibration); //override automatic lookup of calibration value from library table + + Serial.println(); + LT.printModemSettings(); //reads and prints the configured LoRa settings, useful check + Serial.println(); + LT.printOperatingSettings(); //reads and prints the configured operating settings, useful check + Serial.println(); + Serial.println(); + LT.printRegisters(0x900, 0x9FF); //print contents of device registers, normally 0x900 to 0x9FF + Serial.println(); + Serial.println(); + + +#ifdef ENABLEDISPLAY + Serial.println("Display Enabled"); + disp.begin(); + disp.setFont(u8x8_font_chroma48medium8_r); + disp.setCursor(0, 0); + disp.print(F("Ranging RAW Ready")); + disp.setCursor(0, 1); + disp.print(F("Power ")); + disp.print(RangingTXPower); + disp.print(F("dBm")); + disp.setCursor(0, 2); + disp.print(F("Cal ")); + disp.print(Calibration); + disp.setCursor(0, 3); + disp.print(F("Adjust ")); + disp.print(distance_adjustment, 4); +#endif + + Serial.print(F("Address ")); + Serial.println(RangingAddress); + Serial.print(F("CalibrationValue ")); + Serial.println(LT.getSetCalibrationValue()); + Serial.println(F("Ranging master RAW ready")); + + delay(2000); +} diff --git a/examples/RadioLibExamples/SX1280/Ranging/54_Ranging_Master/Settings.h b/examples/RadioLibExamples/SX1280/Ranging/54_Ranging_Master/Settings.h new file mode 100644 index 0000000..d9d882a --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/54_Ranging_Master/Settings.h @@ -0,0 +1,54 @@ +/***************************************************************************************************** + Programs for Arduino - Copyright of the author Stuart Robinson - 04/11/21 + + This program is supplied as is, it is up to the user of the program to decide if the program is + suitable for the intended purpose and free from errors. +*******************************************************************************************************/ + +//******* Setup hardware pin definitions here ! *************** + +//These are the pin definitions for one of my own boards, the Easy Pro Mini, +//be sure to change the definitions to match your own setup. Some pins such as DIO2, +//DIO3, BUZZER may not be in used by this sketch so they do not need to be +//connected and should be included and be set to -1. + +#define I2C_SDA 18 +#define I2C_SCL 17 +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 3 +#define RADIO_MOSI_PIN 6 + +#define NSS 7 +#define RFBUSY 36 +#define NRESET 8 +#define LED1 37 +#define DIO1 9 +#define DIO2 -1//33 +#define DIO3 -1//34 +#define RX_EN 21 +#define TX_EN 10 + +#define LORA_DEVICE DEVICE_SX1280 //we need to define the device we are using + +//******* Setup LoRa Parameters Here ! *************** + +//LoRa Modem Parameters +const uint32_t Frequency = 2445000000; //frequency of transmissions in hz +const int32_t Offset = 0; //offset frequency in hz for calibration purposes +const uint8_t Bandwidth = LORA_BW_0800; //LoRa bandwidth +const uint8_t SpreadingFactor = LORA_SF8; //LoRa spreading factor +const uint8_t CodeRate = LORA_CR_4_5; //LoRa coding rate +const uint16_t Calibration = 11350; //Manual Ranging calibrarion value + +const int8_t RangingTXPower = 3; //Transmit power used +const uint32_t RangingAddress = 16; //must match address in recever + +const uint16_t waittimemS = 10000; //wait this long in mS for packet before assuming timeout +const uint16_t TXtimeoutmS = 5000; //ranging TX timeout in mS +const uint16_t packet_delaymS = 0; //forced extra delay in mS between ranging requests +const uint16_t rangeingcount = 5; //number of times ranging is cqarried out for each distance measurment +float distance_adjustment = 1.0000; //adjustment factor to calculated distance + + +#define ENABLEOLED //enable this define to use display +#define ENABLEDISPLAY //enable this define to use display diff --git a/examples/RadioLibExamples/SX1280/Ranging/55_Ranging_Slave/55_Ranging_Slave.ino b/examples/RadioLibExamples/SX1280/Ranging/55_Ranging_Slave/55_Ranging_Slave.ino new file mode 100644 index 0000000..7c6a540 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/55_Ranging_Slave/55_Ranging_Slave.ino @@ -0,0 +1,141 @@ +/******************************************************************************************************* + Programs for Arduino - Copyright of the author Stuart Robinson - 16/03/20 + + This program is supplied as is, it is up to the user of the program to decide if the program is + suitable for the intended purpose and free from errors. +*******************************************************************************************************/ + + +/******************************************************************************************************* + Program Operation - + + Serial monitor baud rate is set at 9600 +*******************************************************************************************************/ + +#define programversion "V1.0" + +#include +#include +#include +#include "Settings.h" + +SX128XLT LT; + +uint32_t endwaitmS; +uint16_t IrqStatus; +uint32_t response_sent; + + +void loop() +{ + LT.receiveRanging(RangingAddress, 0, TXpower, NO_WAIT); + + endwaitmS = millis() + rangingRXTimeoutmS; + + while (!digitalRead(DIO1) && (millis() <= endwaitmS)); //wait for Ranging valid or timeout + + if (millis() >= endwaitmS) { + Serial.println("Error - Ranging Receive Timeout!!"); + led_Flash(2, 100); //single flash to indicate timeout + } else { + IrqStatus = LT.readIrqStatus(); + digitalWrite(LED1, HIGH); + + if (IrqStatus & IRQ_RANGING_SLAVE_RESPONSE_DONE) { + response_sent++; + Serial.print(response_sent); + Serial.print(" Response sent"); + } else { + Serial.print("Slave error,"); + Serial.print(",Irq,"); + Serial.print(IrqStatus, HEX); + LT.printIrqStatus(); + } + digitalWrite(LED1, LOW); + Serial.println(); + } + +} + + +void led_Flash(unsigned int flashes, unsigned int delaymS) +{ + //flash LED to show board is alive + unsigned int index; + + for (index = 1; index <= flashes; index++) { + digitalWrite(LED1, HIGH); + delay(delaymS); + digitalWrite(LED1, LOW); + delay(delaymS); + } +} + + +void setup() +{ + + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + Wire.begin(I2C_SDA, I2C_SCL); + + Serial.begin(115200); //setup Serial console ouput + Serial.println(); + Serial.println(__FILE__); + Serial.print(F("Compiled ")); + Serial.print(__TIME__); + Serial.print(F(" ")); + Serial.println(__DATE__); + Serial.println(F(programversion)); + Serial.println(F("Stuart Robinson")); + Serial.println(); + + Serial.println("55_Ranging_Slave Starting"); + + + pinMode(LED1, OUTPUT); + led_Flash(2, 125); + + if (LT.begin(NSS, NRESET, RFBUSY, DIO1, DIO2, DIO3, RX_EN, TX_EN, LORA_DEVICE)) { + Serial.println(F("Device found")); + led_Flash(2, 125); + delay(1000); + } else { + Serial.println(F("No device responding")); + while (1) { + led_Flash(50, 50); //long fast speed flash indicates device error + } + } + + //The function call list below shows the complete setup for the LoRa device for ranging using the information + //defined in the Settings.h file. + //The 'Setup LoRa device for Ranging' list below can be replaced with a single function call, note that + //the calibration value will be loaded automatically from the table in the library; + //LT.setupRanging(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, RangingAddress, RangingRole); + + LT.setupRanging(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, RangingAddress, RANGING_SLAVE); + + //*************************************************************************************************** + //Setup LoRa device for Ranging Slave + //*************************************************************************************************** + /* + LT.setMode(MODE_STDBY_RC); + LT.setPacketType(PACKET_TYPE_RANGING); + LT.setModulationParams(SpreadingFactor, Bandwidth, CodeRate); + LT.setPacketParams(12, LORA_PACKET_VARIABLE_LENGTH, 0, LORA_CRC_ON, LORA_IQ_NORMAL, 0, 0); + LT.setRfFrequency(Frequency, Offset); + LT.setTxParams(TXpower, RADIO_RAMP_02_US); + LT.setRangingMasterAddress(RangingAddress); + LT.setRangingSlaveAddress(RangingAddress); + LT.setRangingCalibration(LT.lookupCalibrationValue(SpreadingFactor, Bandwidth)); + LT.setRangingRole(RANGING_SLAVE); + LT.writeRegister(REG_RANGING_FILTER_WINDOW_SIZE, 8); //set up window size for ranging averaging + LT.setHighSensitivity(); + */ + //*************************************************************************************************** + + LT.setRangingCalibration(11300); //override automatic lookup of calibration value from library table + + Serial.print(F("Calibration,")); + Serial.println(LT.getSetCalibrationValue()); //reads the calibratuion value currently set + delay(2000); +} diff --git a/examples/RadioLibExamples/SX1280/Ranging/55_Ranging_Slave/Settings.h b/examples/RadioLibExamples/SX1280/Ranging/55_Ranging_Slave/Settings.h new file mode 100644 index 0000000..7e78d09 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/55_Ranging_Slave/Settings.h @@ -0,0 +1,47 @@ +/***************************************************************************************************** + Programs for Arduino - Copyright of the author Stuart Robinson - 16/03/20 + + This program is supplied as is, it is up to the user of the program to decide if the program is + suitable for the intended purpose and free from errors. +*******************************************************************************************************/ + +//******* Setup hardware pin definitions here ! *************** + +//These are the pin definitions for one of my own boards, the Easy Pro Mini, +//be sure to change the definitions to match your own setup. Some pins such as DIO2, +//DIO3, BUZZER may not be in used by this sketch so they do not need to be +//connected and should be included and be set to -1. + + +#define I2C_SDA 18 +#define I2C_SCL 17 +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 3 +#define RADIO_MOSI_PIN 6 + +#define NSS 7 +#define RFBUSY 36 +#define NRESET 8 +#define LED1 37 +#define DIO1 9 +#define DIO2 -1//33 +#define DIO3 -1//34 +#define RX_EN 21 +#define TX_EN 10 + +#define LORA_DEVICE DEVICE_SX1280 //we need to define the device we are using + +//******* Setup LoRa Parameters Here ! *************** + +//LoRa Modem Parameters +const uint32_t Frequency = 2445000000; //frequency of transmissions in hz +const int32_t Offset = 0; //offset frequency in hz for calibration purposes +const uint8_t Bandwidth = LORA_BW_0800; //LoRa bandwidth +const uint8_t SpreadingFactor = LORA_SF8; //LoRa spreading factor +const uint8_t CodeRate = LORA_CR_4_5; //LoRa coding rate +const uint16_t Calibration = 11350; //Manual Ranging calibration value + +const int8_t TXpower = 3; //Transmit power used +const uint32_t RangingAddress = 16; //must match address in master + +const uint16_t rangingRXTimeoutmS = 0xFFFF; //ranging RX timeout in mS diff --git a/examples/RadioLibExamples/SX1280/Ranging/README.MD b/examples/RadioLibExamples/SX1280/Ranging/README.MD new file mode 100644 index 0000000..4ccc803 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/README.MD @@ -0,0 +1,3 @@ +# Note + +- SX1280 ranging is only an experimental test at present, and there is no guarantee that it can be used. Any suggestion or perfect PR is welcome. \ No newline at end of file diff --git a/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/SX128x_RangingMaster.ino b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/SX128x_RangingMaster.ino new file mode 100644 index 0000000..e5678a9 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/SX128x_RangingMaster.ino @@ -0,0 +1,136 @@ +/* + RadioLib SX128x Ranging Example + + This example performs ranging exchange between two + SX1280 LoRa radio modules. Ranging allows to measure + distance between the modules using time-of-flight + measurement. + + Only SX1280 and SX1282 without external RF switch support ranging! + + Note that to get accurate ranging results, calibration is needed! + The process is described in Semtech SX1280 Application Note AN1200.29 + + For default module settings, see the wiki page + https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx128x---lora-modem + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ +*/ + +// include the library +#include +#include "boards.h" + +SX1280 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN); + + +void setup() +{ + initBoard(); + // When the power is turned on, a delay is required. + delay(1500); + + if (u8g2) { + u8g2->clearBuffer(); + u8g2->setCursor(0, 16); + u8g2->println( "RangingMaster"); + u8g2->sendBuffer(); + } + + + // initialize SX1280 with default settings + Serial.print(F("[SX1280] Initializing ... ")); + int state = radio.begin(); + if (state == RADIOLIB_ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } + + //Set ANT Control pins + radio.setRfSwitchPins(RADIO_RX_PIN, RADIO_TX_PIN); + + // Set output power to 3 dBm !!Cannot be greater than 3dbm!! + if (radio.setOutputPower(3) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) { + Serial.println(F("Selected output power is invalid for this module!")); + while (true); + } + + Serial.println("SX128x_RangingMaster start!"); + + +} + +void loop() +{ + Serial.print(F("[SX1280] Ranging ... ")); + + // start ranging exchange + // range as master: true + // slave address: 0x12345678 + int state = radio.range(true, 0x12345678); + + // the other module must be configured as slave with the same address + /* + */ + // int state = radio.range(false, 0x12345678); + + // if ranging calibration is known, it can be provided + // this should improve the accuracy and precision + /* + uint16_t calibration[3][6] = { + { 10299, 10271, 10244, 10242, 10230, 10246 }, + { 11486, 11474, 11453, 11426, 11417, 11401 }, + { 13308, 13493, 13528, 13515, 13430, 13376 } + }; + + int state = radio.range(true, 0x12345678, calibration); + */ + + if (u8g2) { + u8g2->clearBuffer(); + u8g2->setCursor(0, 16); + u8g2->println( "RangingMaster"); + u8g2->setCursor(0, 32); + } + + if (state == RADIOLIB_ERR_NONE) { + // ranging finished successfully + + float raw = radio.getRangingResult(); + Serial.println(F("success!")); + Serial.print(F("[SX1280] Distance:\t\t\t")); + Serial.print(raw); + Serial.println(F(" meters (raw)")); + + if (u8g2) { + u8g2->print( "Distance:"); u8g2->print( raw); u8g2->print("meters(raw)"); + } + + } else if (state == RADIOLIB_ERR_RANGING_TIMEOUT) { + // timed out waiting for ranging packet + Serial.println(F("timed out!")); + + if (u8g2) { + u8g2->print( "Timed out!"); + } + + } else { + // some other error occurred + Serial.print(F("failed, code ")); + Serial.println(state); + if (u8g2) { + u8g2->print( "Failed!"); + } + } + + if (u8g2) { + u8g2->sendBuffer(); + } + + // wait for a second before ranging again + delay(1000); +} diff --git a/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/boards.h b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/boards.h new file mode 100644 index 0000000..f811bc4 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/boards.h @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include "utilities.h" + +#ifdef HAS_SDCARD +#include +#include +#endif + +#ifdef HAS_DISPLAY +#include +U8G2_SSD1306_128X64_NONAME_F_HW_I2C *u8g2 = nullptr; +#endif + +Ticker ledTicker; +SPIClass SDSPI(HSPI); + +void initBoard() +{ + Serial.begin(115200); + Serial.println("initBoard"); + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + Wire.begin(I2C_SDA, I2C_SCL); + + +#ifdef HAS_GPS + Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); +#endif + +#if OLED_RST + pinMode(OLED_RST, OUTPUT); + digitalWrite(OLED_RST, HIGH); delay(20); + digitalWrite(OLED_RST, LOW); delay(20); + digitalWrite(OLED_RST, HIGH); delay(20); +#endif + +#ifdef BOARD_LED + /* + * T-BeamV1.0, V1.1 LED defaults to low level as trun on, + * so it needs to be forced to pull up + * * * * */ +#if LED_ON == LOW + gpio_hold_dis(GPIO_NUM_4); +#endif + pinMode(BOARD_LED, OUTPUT); + ledTicker.attach_ms(500, []() { + static bool level; + digitalWrite(BOARD_LED, level); + level = !level; + }); +#endif + + +#ifdef HAS_DISPLAY + Wire.beginTransmission(0x3C); + if (Wire.endTransmission() == 0) { + Serial.println("Started OLED"); + u8g2 = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE); + u8g2->begin(); + u8g2->clearBuffer(); + u8g2->setFlipMode(0); + u8g2->setFontMode(1); // Transparent + u8g2->setDrawColor(1); + u8g2->setFontDirection(0); + u8g2->firstPage(); + do { + u8g2->setFont(u8g2_font_inb19_mr); + u8g2->drawStr(0, 30, "LilyGo"); + u8g2->drawHLine(2, 35, 47); + u8g2->drawHLine(3, 36, 47); + u8g2->drawVLine(45, 32, 12); + u8g2->drawVLine(46, 33, 12); + u8g2->setFont(u8g2_font_inb19_mf); + u8g2->drawStr(58, 60, "LoRa"); + } while ( u8g2->nextPage() ); + u8g2->sendBuffer(); + u8g2->setFont(u8g2_font_fur11_tf); + delay(3000); + } +#endif + + +#ifdef HAS_SDCARD + if (u8g2) { + u8g2->setFont(u8g2_font_ncenB08_tr); + } + pinMode(SDCARD_MISO, INPUT_PULLUP); + SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS); + if (u8g2) { + u8g2->clearBuffer(); + } + + if (!SD.begin(SDCARD_CS, SDSPI)) { + + Serial.println("setupSDCard FAIL"); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->println( "SDCard FAILED");; + } while ( u8g2->nextPage() ); + } + + } else { + uint32_t cardSize = SD.cardSize() / (1024 * 1024); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->print( "SDCard:");; + u8g2->print(cardSize / 1024.0);; + u8g2->println(" GB");; + } while ( u8g2->nextPage() ); + } + + Serial.print("setupSDCard PASS . SIZE = "); + Serial.print(cardSize / 1024.0); + Serial.println(" GB"); + } + if (u8g2) { + u8g2->sendBuffer(); + } + delay(3000); +#endif + +} + + diff --git a/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/utilities.h b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/utilities.h new file mode 100644 index 0000000..8e804de --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingMaster/utilities.h @@ -0,0 +1,221 @@ + +#pragma once + +/* +* This sample program only supports SX1280 +* */ +// #define LILYGO_TBeam_V0_7 //NO SUPPOTR +// #define LILYGO_TBeam_V1_X //NO SUPPOTR +// #define LILYGO_T3_V1_0 //NO SUPPOTR +// #define LILYGO_T3_V1_3 //NO SUPPOTR +// #define LILYGO_T3_V1_6 //NO SUPPOTR +// #define LILYGO_T3_V2_0 //NO SUPPOTR +// #define LILYGO_T3_V1_8 +// #define LILYGO_T3_S3_V1_0 + + +#define UNUSE_PIN (0) + +#if defined(LILYGO_TBeam_V0_7) +#define GPS_RX_PIN 12 +#define GPS_TX_PIN 15 +#define BUTTON_PIN 39 +#define BUTTON_PIN_MASK GPIO_SEL_39 +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define GPS_BAUD_RATE 9600 +#define HAS_GPS +#define HAS_DISPLAY //Optional, bring your own board, no OLED !! + +#elif defined(LILYGO_TBeam_V1_X) + +#define GPS_RX_PIN 34 +#define GPS_TX_PIN 12 +#define BUTTON_PIN 38 +#define BUTTON_PIN_MASK GPIO_SEL_38 +#define I2C_SDA 21 +#define I2C_SCL 22 +#define PMU_IRQ 35 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define GPS_BAUD_RATE 9600 +#define HAS_GPS +#define HAS_DISPLAY //Optional, bring your own board, no OLED !! + +#elif defined(LILYGO_T3_V1_0) +#define I2C_SDA 4 +#define I2C_SCL 15 +#define OLED_RST 16 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_V1_3) + +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define ADC_PIN 35 + +#define HAS_DISPLAY +#elif defined(LILYGO_T3_V1_6) +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 25 +#define LED_ON HIGH + +#define ADC_PIN 35 + +#define HAS_SDCARD +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_V2_0) +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN UNUSE_PIN +#define RADIO_BUSY_PIN UNUSE_PIN + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 0 +#define LED_ON LOW + +#define HAS_DISPLAY +#define HAS_SDCARD + +#elif defined(LILYGO_T3_V1_8) + +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO1_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO2_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 25 +#define LED_ON HIGH + +#define ADC_PIN 35 + +#define HAS_SDCARD +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_S3_V1_0) + +#define I2C_SDA 18 +#define I2C_SCL 17 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 3 +#define RADIO_MOSI_PIN 6 +#define RADIO_CS_PIN 7 +#define RADIO_DIO1_PIN 9 +#define RADIO_DIO2_PIN 33 +#define RADIO_DIO3_PIN 34 +#define RADIO_RST_PIN 8 +#define RADIO_BUSY_PIN 36 + +#define RADIO_RX_PIN 21 +#define RADIO_TX_PIN 10 + +#define SDCARD_MOSI 11 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 37 +#define LED_ON HIGH + +#define BAT_ADC_PIN 1 +#define BUTTON_PIN 0 + +// #define HAS_SDCARD +#define HAS_DISPLAY + +#else +#error "For the first use, please define the board version and model in " +#endif + + + + + + + + + diff --git a/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/SX128x_RangingSlave.ino b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/SX128x_RangingSlave.ino new file mode 100644 index 0000000..aa48bba --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/SX128x_RangingSlave.ino @@ -0,0 +1,126 @@ +/* + RadioLib SX128x Ranging Example + + This example performs ranging exchange between two + SX1280 LoRa radio modules. Ranging allows to measure + distance between the modules using time-of-flight + measurement. + + Only SX1280 and SX1282 without external RF switch support ranging! + + Note that to get accurate ranging results, calibration is needed! + The process is described in Semtech SX1280 Application Note AN1200.29 + + For default module settings, see the wiki page + https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx128x---lora-modem + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ +*/ + +// include the library +#include +#include "boards.h" + +SX1280 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN); + + +void setup() +{ + initBoard(); + // When the power is turned on, a delay is required. + delay(1500); + + + if (u8g2) { + u8g2->clearBuffer(); + u8g2->setCursor(0, 16); + u8g2->println( "RangingSlave"); + u8g2->sendBuffer(); + } + + // initialize SX1280 with default settings + Serial.print(F("[SX1280] Initializing ... ")); + int state = radio.begin(); + if (state == RADIOLIB_ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } + + //Set ANT Control pins + radio.setRfSwitchPins(RADIO_RX_PIN, RADIO_TX_PIN); + + // Set output power to 3 dBm !!Cannot be greater than 3dbm!! + if (radio.setOutputPower(3) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) { + Serial.println(F("Selected output power is invalid for this module!")); + while (true); + } + + Serial.println("SX128x_RangingSlave start!"); +} + +void loop() +{ + Serial.print(F("[SX1280] Ranging ... ")); + + // start ranging exchange + // range as master: true + // slave address: 0x12345678 + // int state = radio.range(true, 0x12345678); + + // the other module must be configured as slave with the same address + int state = radio.range(false, 0x12345678); + + // if ranging calibration is known, it can be provided + // this should improve the accuracy and precision + /* + uint16_t calibration[3][6] = { + { 10299, 10271, 10244, 10242, 10230, 10246 }, + { 11486, 11474, 11453, 11426, 11417, 11401 }, + { 13308, 13493, 13528, 13515, 13430, 13376 } + }; + + int state = radio.range(true, 0x12345678, calibration); + */ + if (u8g2) { + u8g2->clearBuffer(); + u8g2->setCursor(0, 16); + u8g2->println( "RangingSlave"); + u8g2->setCursor(0, 32); + } + + if (state == RADIOLIB_ERR_NONE) { + // ranging finished successfully + + float raw = radio.getRangingResult(); + Serial.println(F("success!")); + Serial.print(F("[SX1280] Distance:\t\t\t")); + Serial.print(raw); + Serial.println(F(" meters (raw)")); + + if (u8g2) { + u8g2->print( "Distance:"); u8g2->print( raw); u8g2->print("meters(raw)"); + } + + } else if (state == RADIOLIB_ERR_RANGING_TIMEOUT) { + // timed out waiting for ranging packet + Serial.println(F("timed out!")); + if (u8g2) { + u8g2->print( "Timed out!"); + } + } else { + // some other error occurred + Serial.print(F("failed, code ")); + Serial.println(state); + if (u8g2) { + u8g2->print( "Failed!"); + } + + } + + // wait for a second before ranging again + delay(1000); +} diff --git a/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/boards.h b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/boards.h new file mode 100644 index 0000000..b29a784 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/boards.h @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include "utilities.h" + +#ifdef HAS_SDCARD +#include +#include +#endif + +#ifdef HAS_DISPLAY +#include +U8G2_SSD1306_128X64_NONAME_F_HW_I2C *u8g2 = nullptr; +#endif + +Ticker ledTicker; +SPIClass SDSPI(HSPI); + + +void initBoard() +{ + Serial.begin(115200); + Serial.println("initBoard"); + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + Wire.begin(I2C_SDA, I2C_SCL); + + +#ifdef HAS_GPS + Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); +#endif + +#if OLED_RST + pinMode(OLED_RST, OUTPUT); + digitalWrite(OLED_RST, HIGH); delay(20); + digitalWrite(OLED_RST, LOW); delay(20); + digitalWrite(OLED_RST, HIGH); delay(20); +#endif + +#ifdef BOARD_LED + /* + * T-BeamV1.0, V1.1 LED defaults to low level as trun on, + * so it needs to be forced to pull up + * * * * */ +#if LED_ON == LOW + gpio_hold_dis(GPIO_NUM_4); +#endif + pinMode(BOARD_LED, OUTPUT); + ledTicker.attach_ms(500, []() { + static bool level; + digitalWrite(BOARD_LED, level); + level = !level; + }); +#endif + + +#ifdef HAS_DISPLAY + Wire.beginTransmission(0x3C); + if (Wire.endTransmission() == 0) { + Serial.println("Started OLED"); + u8g2 = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE); + u8g2->begin(); + u8g2->clearBuffer(); + u8g2->setFlipMode(0); + u8g2->setFontMode(1); // Transparent + u8g2->setDrawColor(1); + u8g2->setFontDirection(0); + u8g2->firstPage(); + do { + u8g2->setFont(u8g2_font_inb19_mr); + u8g2->drawStr(0, 30, "LilyGo"); + u8g2->drawHLine(2, 35, 47); + u8g2->drawHLine(3, 36, 47); + u8g2->drawVLine(45, 32, 12); + u8g2->drawVLine(46, 33, 12); + u8g2->setFont(u8g2_font_inb19_mf); + u8g2->drawStr(58, 60, "LoRa"); + } while ( u8g2->nextPage() ); + u8g2->sendBuffer(); + u8g2->setFont(u8g2_font_fur11_tf); + delay(3000); + } +#endif + + +#ifdef HAS_SDCARD + if (u8g2) { + u8g2->setFont(u8g2_font_ncenB08_tr); + } + pinMode(SDCARD_MISO, INPUT_PULLUP); + SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS); + if (u8g2) { + u8g2->clearBuffer(); + } + + if (!SD.begin(SDCARD_CS, SDSPI)) { + + Serial.println("setupSDCard FAIL"); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->println( "SDCard FAILED");; + } while ( u8g2->nextPage() ); + } + + } else { + uint32_t cardSize = SD.cardSize() / (1024 * 1024); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->print( "SDCard:");; + u8g2->print(cardSize / 1024.0);; + u8g2->println(" GB");; + } while ( u8g2->nextPage() ); + } + + Serial.print("setupSDCard PASS . SIZE = "); + Serial.print(cardSize / 1024.0); + Serial.println(" GB"); + } + if (u8g2) { + u8g2->sendBuffer(); + } + delay(3000); +#endif + + + +} + + diff --git a/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/utilities.h b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/utilities.h new file mode 100644 index 0000000..8e804de --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging/SX128x_RangingSlave/utilities.h @@ -0,0 +1,221 @@ + +#pragma once + +/* +* This sample program only supports SX1280 +* */ +// #define LILYGO_TBeam_V0_7 //NO SUPPOTR +// #define LILYGO_TBeam_V1_X //NO SUPPOTR +// #define LILYGO_T3_V1_0 //NO SUPPOTR +// #define LILYGO_T3_V1_3 //NO SUPPOTR +// #define LILYGO_T3_V1_6 //NO SUPPOTR +// #define LILYGO_T3_V2_0 //NO SUPPOTR +// #define LILYGO_T3_V1_8 +// #define LILYGO_T3_S3_V1_0 + + +#define UNUSE_PIN (0) + +#if defined(LILYGO_TBeam_V0_7) +#define GPS_RX_PIN 12 +#define GPS_TX_PIN 15 +#define BUTTON_PIN 39 +#define BUTTON_PIN_MASK GPIO_SEL_39 +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define GPS_BAUD_RATE 9600 +#define HAS_GPS +#define HAS_DISPLAY //Optional, bring your own board, no OLED !! + +#elif defined(LILYGO_TBeam_V1_X) + +#define GPS_RX_PIN 34 +#define GPS_TX_PIN 12 +#define BUTTON_PIN 38 +#define BUTTON_PIN_MASK GPIO_SEL_38 +#define I2C_SDA 21 +#define I2C_SCL 22 +#define PMU_IRQ 35 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define GPS_BAUD_RATE 9600 +#define HAS_GPS +#define HAS_DISPLAY //Optional, bring your own board, no OLED !! + +#elif defined(LILYGO_T3_V1_0) +#define I2C_SDA 4 +#define I2C_SCL 15 +#define OLED_RST 16 + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_V1_3) + +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define ADC_PIN 35 + +#define HAS_DISPLAY +#elif defined(LILYGO_T3_V1_6) +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO1_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 25 +#define LED_ON HIGH + +#define ADC_PIN 35 + +#define HAS_SDCARD +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_V2_0) +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO0_PIN 26 +#define RADIO_RST_PIN 14 +#define RADIO_DIO1_PIN UNUSE_PIN +#define RADIO_BUSY_PIN UNUSE_PIN + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 0 +#define LED_ON LOW + +#define HAS_DISPLAY +#define HAS_SDCARD + +#elif defined(LILYGO_T3_V1_8) + +#define I2C_SDA 21 +#define I2C_SCL 22 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 19 +#define RADIO_MOSI_PIN 27 +#define RADIO_CS_PIN 18 +#define RADIO_DIO1_PIN 26 +#define RADIO_RST_PIN 23 +#define RADIO_DIO2_PIN 33 +#define RADIO_BUSY_PIN 32 + +#define SDCARD_MOSI 15 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 25 +#define LED_ON HIGH + +#define ADC_PIN 35 + +#define HAS_SDCARD +#define HAS_DISPLAY + +#elif defined(LILYGO_T3_S3_V1_0) + +#define I2C_SDA 18 +#define I2C_SCL 17 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 3 +#define RADIO_MOSI_PIN 6 +#define RADIO_CS_PIN 7 +#define RADIO_DIO1_PIN 9 +#define RADIO_DIO2_PIN 33 +#define RADIO_DIO3_PIN 34 +#define RADIO_RST_PIN 8 +#define RADIO_BUSY_PIN 36 + +#define RADIO_RX_PIN 21 +#define RADIO_TX_PIN 10 + +#define SDCARD_MOSI 11 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 37 +#define LED_ON HIGH + +#define BAT_ADC_PIN 1 +#define BUTTON_PIN 0 + +// #define HAS_SDCARD +#define HAS_DISPLAY + +#else +#error "For the first use, please define the board version and model in " +#endif + + + + + + + + +