Update main.cpp
This commit is contained in:
parent
e8b942a758
commit
3adf87f164
92
src/main.cpp
92
src/main.cpp
|
@ -1,16 +1,22 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
// CONFIGURATION: change values in config.h !
|
// CONFIGURATION:
|
||||||
|
// Change RegionCode, Frequency, Speed in config.h !
|
||||||
#define VERBOSE // define to SILENT to turn off serial messages
|
#define VERBOSE // define to SILENT to turn off serial messages
|
||||||
#define BLINK // define to NOBLINK to turn off LED signaling
|
#define BLINK // define to NOBLINK to turn off LED signaling
|
||||||
#define NO_OLED // define to NO_OLED to turn off display
|
#define NO_OLED // define to NO_OLED to turn off display
|
||||||
// OLED supported for cubecell_board_Plus (HTCC-AB02) and cubecell_gps (HTCC-AB02S)
|
// OLED supported for cubecell_board_Plus (HTCC-AB02) and cubecell_gps (HTCC-AB02S)
|
||||||
|
// :CONFIGURATION
|
||||||
|
|
||||||
MeshPacket thePacket;
|
static MeshPacket thePacket;
|
||||||
ChannelSettings ChanSet;
|
static ChannelSettings ChanSet;
|
||||||
static RadioEvents_t RadioEvents;
|
static RadioEvents_t RadioEvents;
|
||||||
uint32_t lastreceivedID = 0x00000000;
|
static TimerEvent_t CheckRadio;
|
||||||
|
static uint32_t lastreceivedID = 0;
|
||||||
|
static uint32_t airTime = 500; //ms
|
||||||
|
static bool noTimer = true;
|
||||||
|
static uint32_t startTime = 0;
|
||||||
|
|
||||||
#ifndef NOBLINK
|
#ifndef NOBLINK
|
||||||
#include "CubeCell_NeoPixel.h"
|
#include "CubeCell_NeoPixel.h"
|
||||||
|
@ -26,10 +32,11 @@ CubeCell_NeoPixel LED(1, RGB, NEO_GRB + NEO_KHZ800);
|
||||||
#include "cubecell_SSD1306Wire.h"
|
#include "cubecell_SSD1306Wire.h"
|
||||||
SSD1306Wire display(0x3c, 500000, I2C_NUM_0,GEOMETRY_128_64,GPIO10 );
|
SSD1306Wire display(0x3c, 500000, I2C_NUM_0,GEOMETRY_128_64,GPIO10 );
|
||||||
#endif
|
#endif
|
||||||
char str[64];
|
char str[32];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
TimerInit( &CheckRadio, onCheckRadio );
|
||||||
#ifndef NO_OLED
|
#ifndef NO_OLED
|
||||||
pinMode(Vext,OUTPUT);
|
pinMode(Vext,OUTPUT);
|
||||||
digitalWrite(Vext,LOW);
|
digitalWrite(Vext,LOW);
|
||||||
|
@ -55,15 +62,16 @@ void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
MSG("\nSetting up Radio:\n");
|
MSG("\nSetting up Radio:\n");
|
||||||
#endif
|
#endif
|
||||||
RadioEvents.TxDone = TxDone;
|
RadioEvents.TxDone = onTxDone;
|
||||||
RadioEvents.TxTimeout = TxTimeout;
|
RadioEvents.TxTimeout = onTxTimeout;
|
||||||
RadioEvents.RxDone = RxDone;
|
RadioEvents.RxDone = onRxDone;
|
||||||
Radio.Init( &RadioEvents );
|
Radio.Init( &RadioEvents );
|
||||||
Radio.Sleep();
|
Radio.Sleep();
|
||||||
memcpy(ChanSet.name, MESHTASTIC_NAME, 12);
|
memcpy(ChanSet.name, MESHTASTIC_NAME, 12);
|
||||||
ChanSet.channel_num = hash( MESHTASTIC_NAME ) % regions[REGION].numChannels; // see config.h
|
ChanSet.channel_num = hash( MESHTASTIC_NAME ) % regions[REGION].numChannels; // see config.h
|
||||||
ChanSet.tx_power = (regions[REGION].powerLimit == 0) ? TX_MAX_POWER : MIN(regions[REGION].powerLimit, TX_MAX_POWER) ;
|
ChanSet.tx_power = (regions[REGION].powerLimit == 0) ? TX_MAX_POWER : MIN(regions[REGION].powerLimit, TX_MAX_POWER) ;
|
||||||
//ChanSet.psk = MESHTASTIC_PSK;
|
//ChanSet.psk = MESHTASTIC_PSK;
|
||||||
|
ChanSet.psk = PSK_NOENCRYPTION;
|
||||||
/* FYI:
|
/* FYI:
|
||||||
"bandwidth":
|
"bandwidth":
|
||||||
[0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: 62.5kHz, 4: 41.67kHz, 5: 31.25kHz, 6: 20.83kHz, 7: 15.63kHz, 8: 10.42kHz, 9: 7.81kHz]
|
[0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: 62.5kHz, 4: 41.67kHz, 5: 31.25kHz, 6: 20.83kHz, 7: 15.63kHz, 8: 10.42kHz, 9: 7.81kHz]
|
||||||
|
@ -106,6 +114,8 @@ void setup() {
|
||||||
ChanSet.spread_factor = 7;
|
ChanSet.spread_factor = 7;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
airTime = floor(symbTime[MESHTASTIC_SPEED] * (36.5 + 20*8 + 16) + 0.5); // simplified AirTime for the smallest MPacket (length 20 bytes)
|
||||||
|
//real AirTime is much longer, but this will give a good compromise regarding battery savings vs. reaction time
|
||||||
ConfigureRadio( ChanSet );
|
ConfigureRadio( ChanSet );
|
||||||
#ifndef SILENT
|
#ifndef SILENT
|
||||||
MSG("..done! Switch to Receive Mode.\n");
|
MSG("..done! Switch to Receive Mode.\n");
|
||||||
|
@ -117,14 +127,20 @@ void setup() {
|
||||||
Radio.Rx(0); // initial mode = receive
|
Radio.Rx(0); // initial mode = receive
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop( )
|
void onCheckRadio(void){
|
||||||
{
|
noTimer=false;
|
||||||
lowPowerHandler( );
|
|
||||||
delay(500);
|
|
||||||
Radio.IrqProcess( );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TxDone( void )
|
void loop( )
|
||||||
|
{
|
||||||
|
noTimer = true;
|
||||||
|
TimerSetValue( &CheckRadio, airTime ); // ms
|
||||||
|
TimerStart( &CheckRadio ); // onCheckRadio() will set noTimer to false
|
||||||
|
while (noTimer) lowPowerHandler( );
|
||||||
|
Radio.IrqProcess(); // handle events from LoRa
|
||||||
|
}
|
||||||
|
|
||||||
|
void onTxDone( void )
|
||||||
{
|
{
|
||||||
Radio.Sleep( );
|
Radio.Sleep( );
|
||||||
#ifndef NOBLINK
|
#ifndef NOBLINK
|
||||||
|
@ -132,7 +148,7 @@ void TxDone( void )
|
||||||
LED.show( );
|
LED.show( );
|
||||||
#endif
|
#endif
|
||||||
#ifndef SILENT
|
#ifndef SILENT
|
||||||
MSG(".done! Switch to Receive Mode.\n");
|
MSG(".done (%ims)! Switch to Receive Mode.\n", millis() - startTime );
|
||||||
#endif
|
#endif
|
||||||
#ifndef NO_OLED
|
#ifndef NO_OLED
|
||||||
sprintf(str,"..done. RX Mode..");
|
sprintf(str,"..done. RX Mode..");
|
||||||
|
@ -142,7 +158,7 @@ void TxDone( void )
|
||||||
Radio.Rx( 0 ); // switch to receive mode
|
Radio.Rx( 0 ); // switch to receive mode
|
||||||
}
|
}
|
||||||
|
|
||||||
void TxTimeout( void )
|
void onTxTimeout( void )
|
||||||
{
|
{
|
||||||
Radio.Sleep( );
|
Radio.Sleep( );
|
||||||
#ifndef NOBLINK
|
#ifndef NOBLINK
|
||||||
|
@ -160,15 +176,20 @@ void TxTimeout( void )
|
||||||
Radio.Rx( 0 ); // switch to receive mode
|
Radio.Rx( 0 ); // switch to receive mode
|
||||||
}
|
}
|
||||||
|
|
||||||
void RxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
|
void onRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
|
||||||
{
|
{
|
||||||
if ( !(Radio.GetStatus() == RF_RX_RUNNING) ) Radio.Sleep( );
|
if ( !(Radio.GetStatus() == RF_RX_RUNNING) ) Radio.Sleep( );
|
||||||
if ( size > MAX_PAYLOAD_LENGTH ) size = MAX_PAYLOAD_LENGTH;
|
if ( size > MAX_PAYLOAD_LENGTH ) size = MAX_PAYLOAD_LENGTH;
|
||||||
if ( !(size > sizeof(PacketHeader)) ) {
|
if ( !(size > sizeof(PacketHeader)) ) {
|
||||||
#ifndef SILENT
|
#ifndef SILENT
|
||||||
MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr);
|
MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr);
|
||||||
MSG("Packet to small (No MeshPacket), discard. Switch to Receive Mode.\n");
|
MSG("Packet to small (No MeshPacket).\n");
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef NOBLINK
|
||||||
|
LED.setPixelColor( 0, RGB_RED ); // send mode
|
||||||
|
LED.show();
|
||||||
|
#endif
|
||||||
|
Radio.Send( payload, size );
|
||||||
Radio.Rx( 0 );
|
Radio.Rx( 0 );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -185,11 +206,11 @@ void RxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
|
||||||
memcpy(p->encrypted.bytes, payload + sizeof(PacketHeader), p->encrypted.size);
|
memcpy(p->encrypted.bytes, payload + sizeof(PacketHeader), p->encrypted.size);
|
||||||
#ifndef SILENT
|
#ifndef SILENT
|
||||||
MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr);
|
MSG("\nReceived packet! (Size %i bytes, RSSI %i, SNR %i)\n", size, rssi, snr);
|
||||||
MSG("TO: %.2X ", thePacket.to);
|
MSG("TO: %.2X ", p->to);
|
||||||
MSG(" FROM: %.2X ", thePacket.from);
|
MSG(" FROM: %.2X ", p->from);
|
||||||
MSG(" Packet ID: %.2X ", thePacket.id);
|
MSG(" Packet ID: %.2X ", p->id);
|
||||||
MSG(" Flags: WANT_ACK="); MSG((thePacket.want_ack) ? "YES " : "NO ");
|
MSG(" Flags: WANT_ACK="); MSG((p->want_ack) ? "YES " : "NO ");
|
||||||
MSG(" HOP_LIMIT=%i\n", thePacket.hop_limit);
|
MSG(" HOP_LIMIT=%i\n", p->hop_limit);
|
||||||
MSG("Payload:"); for ( int i=0; i < p->encrypted.size; i++ ) MSG(" %.2X", p->encrypted.bytes[i]);
|
MSG("Payload:"); for ( int i=0; i < p->encrypted.size; i++ ) MSG(" %.2X", p->encrypted.bytes[i]);
|
||||||
MSG("\n");
|
MSG("\n");
|
||||||
#endif
|
#endif
|
||||||
|
@ -199,32 +220,33 @@ void RxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
|
||||||
display.setFont(ArialMT_Plain_10);
|
display.setFont(ArialMT_Plain_10);
|
||||||
sprintf(str,"Size: %i RSSI %i SNR %i", size, rssi, snr);
|
sprintf(str,"Size: %i RSSI %i SNR %i", size, rssi, snr);
|
||||||
display.drawString(0,0,str);
|
display.drawString(0,0,str);
|
||||||
sprintf(str,"%X", thePacket.to);
|
sprintf(str,"%X", p->to);
|
||||||
display.drawString(0,11,"TO:");
|
display.drawString(0,11,"TO:");
|
||||||
display.drawString(40,11,str);
|
display.drawString(40,11,str);
|
||||||
sprintf(str,"%X", thePacket.from);
|
sprintf(str,"%X", p->from);
|
||||||
display.drawString(0,22,"FROM:");
|
display.drawString(0,22,"FROM:");
|
||||||
display.drawString(40,22,str);
|
display.drawString(40,22,str);
|
||||||
sprintf(str,"%X", thePacket.id);
|
sprintf(str,"%X", p->id);
|
||||||
display.drawString(0,32,"ID:");
|
display.drawString(0,32,"ID:");
|
||||||
display.drawString(40,32,str);
|
display.drawString(40,32,str);
|
||||||
display.display();
|
display.display();
|
||||||
#endif
|
#endif
|
||||||
if ( !(lastreceivedID == thePacket.id) ){
|
if ( !(lastreceivedID == thePacket.id) ){
|
||||||
// will repeat package
|
// will repeat package
|
||||||
#ifndef SILENT
|
lastreceivedID = thePacket.id;
|
||||||
MSG("Sending packet.. (Size: %i)..", size);
|
|
||||||
#endif
|
|
||||||
#ifndef NO_OLED
|
#ifndef NO_OLED
|
||||||
sprintf(str,"Sending..");
|
sprintf(str,"Sending..");
|
||||||
display.drawString(0,53,str);
|
display.drawString(0,53,str);
|
||||||
display.display();
|
display.display();
|
||||||
#endif
|
#endif
|
||||||
lastreceivedID = thePacket.id;
|
|
||||||
#ifndef NOBLINK
|
#ifndef NOBLINK
|
||||||
LED.setPixelColor( 0, RGB_RED ); // send mode
|
LED.setPixelColor( 0, RGB_RED ); // send mode
|
||||||
LED.show();
|
LED.show();
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef SILENT
|
||||||
|
MSG("Sending packet.. (Size: %i)..", size);
|
||||||
|
startTime = millis();
|
||||||
|
#endif
|
||||||
Radio.Send( payload, size );
|
Radio.Send( payload, size );
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
@ -263,6 +285,10 @@ void ConfigureRadio( ChannelSettings ChanSet )
|
||||||
MSG("Setting bandwidth to index %i ..\n",ChanSet.bandwidth);
|
MSG("Setting bandwidth to index %i ..\n",ChanSet.bandwidth);
|
||||||
MSG("Setting CodeRate to index %i .. \n", ChanSet.coding_rate);
|
MSG("Setting CodeRate to index %i .. \n", ChanSet.coding_rate);
|
||||||
MSG("Setting SpreadingFactor to %i ..\n",ChanSet.spread_factor);
|
MSG("Setting SpreadingFactor to %i ..\n",ChanSet.spread_factor);
|
||||||
|
MSG("(est. SymbolTime for setting is ");
|
||||||
|
Serial.print(symbTime[MESHTASTIC_SPEED]); MSG("ms)\n");
|
||||||
|
MSG("(LowPowerTime = (36.5 + 8 x 20 + 16) x SymbolTime (Preamble + 20 byte minimal packet length + 16 bit CRC) - very simplified calculation!)\n");
|
||||||
|
MSG("LowPowerTime: "); Serial.print(airTime); MSG("ms ..\n");
|
||||||
#endif
|
#endif
|
||||||
Radio.SetChannel( freq );
|
Radio.SetChannel( freq );
|
||||||
Radio.SetTxConfig( MODEM_LORA, ChanSet.tx_power ,0 , ChanSet.bandwidth, ChanSet.spread_factor, ChanSet.coding_rate,
|
Radio.SetTxConfig( MODEM_LORA, ChanSet.tx_power ,0 , ChanSet.bandwidth, ChanSet.spread_factor, ChanSet.coding_rate,
|
||||||
|
|
Loading…
Reference in New Issue