working version of sx1276 with integrated gps and lcd
Dependencies: SX1276Lib AdaFruit_RGBLCD MCP23017 mbed
Fork of AdaFruit_RGBLCD by
Revision 33:319cbac3b6eb, committed 2015-09-03
- Comitter:
- ftagius
- Date:
- Thu Sep 03 14:33:55 2015 +0000
- Parent:
- 32:a2472bbe7c92
- Commit message:
- sx1276 demo program requiring only the freedom board and a semtech shield
Changed in this revision
--- a/AdaFruit_RGBLCDShield.lib Wed Jul 29 12:14:42 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/teams/CaryCoders/code/AdaFruit_RGBLCDShield/#bc884631d057
--- a/AdafruitGPS.lib Wed Jul 29 12:14:42 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/teams/CaryCoders/code/AdafruitGPS/#59bfc720698f
--- a/LCDadafruit/lcd.cpp Wed Jul 29 12:14:42 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -#include "mbed.h" -#include "lcd.h" - -LCD::LCD(I2C & in_cI2C) - : Serial(USBTX,USBRX) - , m_cI2C(in_cI2C) -{ -}
--- a/LCDadafruit/lcd.h Wed Jul 29 12:14:42 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#ifndef __LCD_H__ -#define __LCD_H__ - -class LCD - : public Serial // for printf -{ -public: - LCD(I2C & in_cI2C); - virtual int _putc(int c) = 0; - - virtual void clear() = 0; - virtual uint8_t columns() = 0; - virtual void createChar(uint8_t location, uint8_t charmap[]) = 0; - virtual void home() = 0; - virtual uint8_t rows() = 0; - virtual void setCursor(uint8_t in_nX, uint8_t in_nY) = 0; - virtual void showBlink(bool in_bShow) = 0; - virtual void showCursor(bool in_bShow) = 0; - virtual void showDisplay(bool in_bShow) = 0; - -protected: - I2C & m_cI2C; -}; - -#endif // __LCD_H__
--- a/LCDadafruit/lcdadafruit.cpp Wed Jul 29 12:14:42 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#include "mbed.h" -#include "lcdadafruit.h" - -LCDadafruit::LCDadafruit(I2C & in_cI2C) - : LCD(in_cI2C) - , m_cMCP(in_cI2C, 0x20 << 1) - , m_cLCD(m_cMCP) -{ - m_cLCD.begin(16,2); -} - -int LCDadafruit::_putc(int in_nValue) -{ - return m_cLCD._putc(in_nValue); -} - -void LCDadafruit::clear() -{ - m_cLCD.clear(); -} - -uint8_t LCDadafruit::columns() -{ - return 16; -} - -void LCDadafruit::createChar(uint8_t location, uint8_t charmap[]) -{ - m_cLCD.createChar(location,charmap); -} - -void LCDadafruit::home() -{ - m_cLCD.home(); -} - -uint8_t LCDadafruit::rows() -{ - return 2; -} - -void LCDadafruit::setCursor(uint8_t in_nX, uint8_t in_nY) -{ - m_cLCD.setCursor(in_nX,in_nY); -} - -void LCDadafruit::showBlink(bool in_bShow) -{ - - if (in_bShow) m_cLCD.blink(); else m_cLCD.noBlink(); -} - -void LCDadafruit::showCursor(bool in_bShow) -{ - if (in_bShow) m_cLCD.cursor(); else m_cLCD.noCursor(); -} - -void LCDadafruit::showDisplay(bool in_bShow) -{ - if (in_bShow) m_cLCD.display(); else m_cLCD.noDisplay(); -}
--- a/LCDadafruit/lcdadafruit.h Wed Jul 29 12:14:42 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -#ifndef __LCDADAFRUIT_H__ -#define __LCDADAFRUIT_H__ - -#include "lcd.h" -#include "Adafruit_RGBLCDShield.h" -#include "MCP23017.h" - -class LCDadafruit - : public LCD -{ -public: - LCDadafruit(I2C & in_cI2C); - virtual int _putc(int c); - - virtual uint8_t columns(); - virtual void clear(); - virtual void createChar(uint8_t location, uint8_t charmap[]); - virtual void home(); - virtual uint8_t rows(); - virtual void setCursor(uint8_t in_nX, uint8_t in_nY); - virtual void showBlink(bool in_bBlink); - virtual void showCursor(bool in_bShow); - virtual void showDisplay(bool in_bBlink); - -protected: - MCP23017 m_cMCP; - Adafruit_RGBLCDShield m_cLCD; -}; - -#endif // __LCDADAFRUIT_H__
--- a/SX1276Lib.lib Wed Jul 29 12:14:42 2015 +0000 +++ b/SX1276Lib.lib Thu Sep 03 14:33:55 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/CaryCoders/code/SX1276Lib/#29f09ac61412 +http://developer.mbed.org/teams/CaryCoders/code/SX1276Lib/#20ce6dab2237
--- a/hello.cpp Wed Jul 29 12:14:42 2015 +0000 +++ b/hello.cpp Thu Sep 03 14:33:55 2015 +0000 @@ -3,14 +3,16 @@ #include "main.h" #include "debug.h" #include "serial_api.h" +#ifndef STANDALONE #include "GPS.h" +#endif #include <math.h> void start_hello(void) { int len = get_kbd_str(pcbuf, PCBUF_SIZE); - //printf("in start gps/hello\r\n"); + //printf("in start gps/hello len=%d\r\n",len); if (len < 0) { fflush(stdout); Radio.Rx( RX_TIMEOUT_VALUE ); @@ -28,6 +30,7 @@ } else { + Radio.Rx( RX_TIMEOUT_VALUE / 3 ); hello(); } } @@ -37,8 +40,8 @@ // srand ( randomSeed() ); // generate a random wait time - // int mywait = ((rand()%2 +1)); - int mywait = 3; + int mywait = ((rand()%2 +1)); + // int mywait = 3; int i=0; int rcvd=0; int lost=0; @@ -78,25 +81,33 @@ r_latitude = 0; r_longitude = 0; // printf("rcv - rssi = %d snr = %d msg=%s\r\n", RssiValue, SnrValue, BufferRx); + #ifndef STANDALONE cLCD.setCursor(0,0); cLCD.printf("RI:NA SR:NA "); + #endif case RX: if( BufferSize > 0 ) { + RXin = !RXin; if( strncmp( ( const char* )BufferRx, ( const char* )HelloMsg, 5 ) == 0 ) { // radfta led = !led; printf("rcv - rssi = %d snr = %d msg=%s ", RssiValue, SnrValue, BufferRx ); printf("per=%d\r\n", per); + #ifndef STANDALONE cLCD.setCursor(0,0); cLCD.printf("RI:%d SR:%d ", RssiValue, SnrValue); + #endif } + #ifndef STANDALONE else { if(sscanf((char *)BufferRx, "GP,%f,%f", &r_latitude, &r_longitude ) >= 1) - { + { + r_latitude_last = r_latitude; + r_longitude_last = r_longitude; // radfta led = !led; printf("rssi=%d snr=%d rlat=%0.5f rlon=%0.5f llat=%0.5f llon=%0.5f dst=%0.0fm ", RssiValue, SnrValue, r_latitude, r_longitude, gpsd.lat_deg, gpsd.lon_deg, distance); printf("per=%d\r\n", per); @@ -107,27 +118,31 @@ else printf("strange received message: %s\r\n", BufferRx); - } + } + #else + printf("rssi=%d \tsnr=%d \tper=%d \tmsg=%s\r\n", RssiValue, SnrValue, per, BufferRx); + #endif } case RX_ERROR: - +#ifndef STANDALONE if (gpsEnabled) sprintf((char *)BufferTx, "GP,%4.5f,%4.5f", gpsd.lat_deg, gpsd.lon_deg); else +#endif strcpy( ( char* )BufferTx, ( char* )HelloMsg ); -#if 0 -radfta +#if 1 BufferSize=strlen((char *)BufferTx); //printf("Sending new buffer\r\n"); Radio.Send( BufferTx, BufferSize ); + State = LOWPOWER; +#else + State = CAD; #endif - State = LOWPOWER; break; case TX: - //printf("tx state\r\n"); - //wait_ms (30); - + // printf("tx state\r\n"); + // wait_ms (10); Radio.Rx( RX_TIMEOUT_VALUE / mywait ); State = LOWPOWER; break; @@ -138,10 +153,18 @@ break; case LOWPOWER: //printf("in wait\r\n"); - // wait_ms (1); + //wait_ms (1); + //wait_us(200); +#ifndef STANDALONE + check_gps(); +#endif + break; + case CAD: + printf("in cad\r\n"); + //State = LOWPOWER; break; default: - debug("state is set to low power\r\n"); + //debug("state is set to CAD\r\n"); State = LOWPOWER; break; }
--- a/main.cpp Wed Jul 29 12:14:42 2015 +0000 +++ b/main.cpp Thu Sep 03 14:33:55 2015 +0000 @@ -1,8 +1,10 @@ #include "mbed.h" +#include "main.h" +#ifndef STANDALONE #include "lcdadafruit.h" +#include "GPS.h" +#endif #include "sx1276-hal.h" -#include "GPS.h" -#include "main.h" #include "debug.h" #include "serial_api.h" #include "datetime.h" @@ -11,9 +13,9 @@ #define USE_MODEM_LORA 1 -//#define RF_FREQUENCY 868000000 // Hz +#define RF_FREQUENCY 868000000 // Hz //#define RF_FREQUENCY 880030000 -#define RF_FREQUENCY 915000000.0 // Hz +//#define RF_FREQUENCY 914000000.0 // Hz //#define RF_FREQUENCY 413000000.0 // Hz @@ -30,7 +32,9 @@ float Frequency = RF_FREQUENCY; float distance = -1; float r_latitude = 0; +float r_latitude_last = 0; float r_longitude = 0; +float r_longitude_last = 0; int TxPower = TX_OUTPUT_POWER; int Bandwidth = LORA_BANDWIDTH; int SpreadingFactor = LORA_SPREADING_FACTOR; @@ -43,13 +47,16 @@ typedef RadioState States_t; volatile States_t State = LOWPOWER; +//SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, OnCadDone ); SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, NULL ); // for hand wired I2C cI2C(PTC9, PTC8); I2C cI2C(PTC9, PTC8); // for stacked shield I2C cI2C(PTC2, PTC1); //I2C cI2C(PTC2, PTC1); +#ifndef STANDALONE LCDadafruit cLCD(cI2C); GPS gpsd(PTE0,PTE1); +#endif // for other board GPS gpsd(PTE20, PTE21); @@ -67,7 +74,7 @@ int max_pkts = 20; int pkt_data[20]; int per = 0; -int tx_rate_us = 100000; // 1 second +int tx_rate_us = 1000000; // 1 second app_e app = APP_NONE; bool isMaster = true; bool AlwaysMaster = false; @@ -75,15 +82,24 @@ bool ackRcvd = true; bool rxTimeout = false; bool gpsEnabled = true; -InterruptIn PPS(PTA1); //pps IRQ +InterruptIn PPS(PTD7); //pps IRQ +DigitalOut RXin(PTD6); Ticker usec_tick; void tx_pkt() { led = !led; + BufferSize=strlen((char *)BufferTx); + #if 1 // printf("Buffersize = %d\r\n",BufferSize); - Radio.Send( BufferTx, BufferSize ); - //Radio.Rx( RX_TIMEOUT_VALUE / 1 ); + // Radio.Send( BufferTx, BufferSize ); + Radio.Rx( RX_TIMEOUT_VALUE / 1 ); +#else + // printf("calling start cat\r\n"); + // Radio.Send( BufferTx, BufferSize ); + // Radio.Rx( RX_TIMEOUT_VALUE / 1 ); + // Radio.StartCad(); +#endif } void ppsSync() @@ -95,15 +111,16 @@ { // detach the timer usec_tick.detach(); - printf("call attach\r\n"); - //wait_ms(500); + + wait_ms(500); usec_tick.attach_us(&tx_pkt, tx_rate_us); + // printf("call attach\r\n"); } else { if (pps_count > 10) { - pps_count = 1; + pps_count = 0; } } @@ -115,18 +132,22 @@ { int i; + RXin=0; Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? const int refresh_Time = 1000; //refresh time in ms bool gpsFix=false; - gps_setup(); + pc.baud(9600); cI2C.frequency(400000); // I2C can handle two different frequencies - switch to high speed if asked + #ifndef STANDALONE + gps_setup(); cLCD.clear(); cLCD.home(); cLCD.setCursor(0,0); cLCD.printf(" HOMER "); cLCD.setCursor(0,1); cLCD.printf(" DOH! "); + #endif rxTimeout = false; // PTD1 (the SCK pin for the one SPI interface onboard the KL25z) is actually an output to the Blue LED. @@ -154,6 +175,8 @@ debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "> Board Type: SX1276MB1LAS < \r\n" ); debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "> Board Type: SX1276MB1MAS < \r\n" ); Radio.SetChannel( Frequency ); + // Radio.StartCad(); + // printf("start cad\r\n"); debug_if( !LORA_FHSS_ENABLED, "> LORA Mode < \r\n"); configRxTx(); @@ -171,6 +194,7 @@ Radio.Tx( TX_TIMEOUT_VALUE ); app = APP_HELLO; + //app = APP_CHAT; } else { @@ -178,17 +202,22 @@ pc.printf("Starting GPS App\r\n"); } - refresh_Timer.start(); //starts the clock on the timer - usec_tick.attach_us(&tx_pkt, tx_rate_us); // attach a timer to periodically call tx. this timer will be restarted in ppsSync to synronize timer accross systems - PPS.rise(&ppsSync); // call ppsSync when the PPS irq is detected - + refresh_Timer.start(); //starts the clock on the timer used for gps refreshes + // usec_tick.attach_us(&tx_pkt, tx_rate_us); // attach a timer to periodically call tx. this timer will be restarted in ppsSync to synronize timer accross systems + // PPS.rise(&ppsSync); // call ppsSync when the PPS irq is detected + // printf("start cad 2\r\n"); + // Radio.StartCad(); + while( 1 ) { +#ifndef STANDALONE + //printf("call check \r\n"); check_gps(); if (refresh_Timer.read_ms() >= refresh_Time) { - // printf("reset timer popped\r\n"); - if (gpsd.fix) { + //printf("reset timer popped\r\n"); + if (gpsd.fix) + { // got a gps fix if (gpsFix == false) { @@ -227,6 +256,7 @@ refresh_Timer.reset(); } +#endif switch (app) { case APP_PING: start_ping_pong(); @@ -235,9 +265,12 @@ console_chat(); break; case APP_GPS: +#ifndef STANDALONE check_gps(); +#endif break; case APP_HELLO: + wait(1); start_hello(); break; case APP_CONSOLE:
--- a/main.h Wed Jul 29 12:14:42 2015 +0000 +++ b/main.h Thu Sep 03 14:33:55 2015 +0000 @@ -14,8 +14,11 @@ */ #ifndef __MAIN_H__ #define __MAIN_H__ +#define STANDALONE 1 +#ifndef STANDALONE #include "lcdadafruit.h" #include "GPS.h" +#endif #include "enums.h" #include "typedefs.h" @@ -55,7 +58,7 @@ /*! * @brief Function executed on CAD Done event */ -void OnCadDone( void ); +void OnCadDone( bool activity ); char* itoa(int val, int base); unsigned int randomSeed(void); @@ -89,7 +92,7 @@ /* Set this flag to '1' to display debug messages on the console */ #define DEBUG_MESSAGE 1 #define TX_OUTPUT_POWER 20 // 14 dBm -#define LORA_BANDWIDTH 2 // [0: 125 kHz, +#define LORA_BANDWIDTH 0 // [0: 125 kHz, // 1: 250 kHz, // 2: 500 kHz, // 3: Reserved] @@ -115,10 +118,12 @@ #endif typedef RadioState States_t; +#ifndef STANDALONE extern LCDadafruit cLCD; +extern GPS gpsd; +#endif extern volatile States_t State; extern SX1276MB1xAS Radio; -extern GPS gpsd; extern uint8_t PingMsg[]; extern uint8_t PongMsg[]; extern uint8_t HelloMsg[]; @@ -129,6 +134,8 @@ extern float Frequency; extern float distance; extern float r_latitude; +extern float r_latitude_last; +extern float r_longitude_last; extern float r_longitude; extern int TxPower; extern int Bandwidth; @@ -148,6 +155,7 @@ extern int txLen; extern int16_t RssiValue; extern int8_t SnrValue; +extern DigitalOut RXin; #define PCBUF_SIZE 64 #define PI 3.14159265
--- a/utilities.cpp Wed Jul 29 12:14:42 2015 +0000 +++ b/utilities.cpp Thu Sep 03 14:33:55 2015 +0000 @@ -4,6 +4,7 @@ #include "debug.h" #include "serial_api.h" +#ifndef STANDALONE void gps_setup(void) { printf("setting up gps\r\n"); @@ -18,8 +19,8 @@ gpsd.minute=1; gpsd.seconds=1; } - -int get_kbd_str(char* buf, int size) +#endif +int get_kbd_str2(char* buf, int size) { char c; int i; @@ -51,25 +52,82 @@ buf[i++] = c; pc.putc(c); } + } else { + if (app == APP_PING) + ping_pong(); + if (app == APP_HELLO) + hello(); +#ifndef STANDALONE + if (app == APP_GPS) + check_gps(); +#endif + #if 1 + if (app == APP_CHAT) + { + // printf("utility call to console chat\r\n"); + check_rx_chat(); + } + #endif + } + } // ...for() +} + +int get_kbd_str(char* buf, int size) +{ + char c; + int i; + static int prev_len; + + for (i = 0;;) { + if (pc.readable()) { + c = pc.getc(); + if (c == 8 && i > 0) { + pc.putc(8); + pc.putc(' '); + pc.putc(8); + i--; + } else if (c == '\r') { + if (i == 0) { + if (app == APP_CHAT) + return 0; + else + return prev_len; // repeat previous + } else { + buf[i] = 0; // null terminate + prev_len = i; + return i; + } + } else if (c == 3) { + { + // ctrl-C abort + //printf("ctrl c\r\n"); + app = APP_CONSOLE; + return -1; + } + } else if (i < size) { + buf[i++] = c; + pc.putc(c); + } } else { #if 0 - return 0; + return 1; #else if (app == APP_PING) ping_pong(); if (app == APP_HELLO) { - // if (check_gps() == 0) - // { - return 0; - //hello(); - // } +#ifndef STANDALONE + check_gps(); +#endif + hello(); + } - +#ifndef STANDALONE if (app == APP_GPS) check_gps(); +#endif #if 1 if (app == APP_CHAT) { @@ -311,6 +369,24 @@ } +void OnCadDone( bool activity ) +{ + //Radio.Sleep(); + State = CAD; + if (activity) + { + debug_if( DEBUG_MESSAGE, "> OnCadDone\n\r" ); + printf("on cad done\r\n"); + Radio.StartCad(); + } + else + { + // printf("false\r\n"); + Radio.StartCad(); + } +} + +#ifndef STANDALONE void find_distance() { //float dist = 0; @@ -325,6 +401,7 @@ float d = 0; distance = 0; + if (gpsd.lat_deg == 0.0) { distance = -1; @@ -336,8 +413,8 @@ return; } - lat1 = r_latitude; - lon1 = r_longitude; + lat1 = r_latitude_last; + lon1 = r_longitude_last; lat2 = gpsd.lat_deg; lon2 = gpsd.lon_deg; @@ -354,7 +431,13 @@ d = 0; } distance = distance + d; - // printf("distance: %f\r\n", gpsd.distance); +#if 0 + if (distance > 10000) + { + printf("distance: %f\r\n", distance); + printf("lat1=%f lon1=%f lat2=%f lon2=%f\r\n",lat1,lon1,lat2,lon2); + } +#endif } @@ -375,7 +458,8 @@ //check if we recieved a new message from GPS, if so, attempt to parse it, if ( gpsd.newNMEAreceived() ) { - if (r_latitude != 0) + //printf("nmea received \r\n"); + if (r_latitude_last != 0) { find_distance(); //printf("distance is %f\r\n",distance); @@ -386,10 +470,22 @@ cLCD.setCursor(0,1); cLCD.printf("d:%0.1fm per:%d ", distance,per); } + } + //printf("distance = %f, %f\r\n",distance, r_latitude_last); } + else + { + cLCD.setCursor(0,1); + if (gpsd.lat_deg != 0) + cLCD.printf("%0.4f %0.4f", gpsd.lat_deg, gpsd.lon_deg); + else + cLCD.printf("No GPS. per:%d ",per); + } + + - if ( !gpsd.parse(gpsd.lastNMEA()) ) + if ( !gpsd.parse(gpsd.lastNMEA()) ) { // radfta led = !led; #if 0 @@ -419,4 +515,4 @@ } } - +#endif