state machine (part 2)

This commit is contained in:
Klaus K Wilting 2018-04-17 18:46:01 +02:00
parent 688d0993af
commit eefac59030
2 changed files with 25 additions and 29 deletions

View File

@ -11,9 +11,9 @@
; ---> SELECT TARGET PLATFORM HERE! <--- ; ---> SELECT TARGET PLATFORM HERE! <---
[platformio] [platformio]
env_default = heltec_wifi_lora_32 ;env_default = heltec_wifi_lora_32
;env_default = ttgov1 ;env_default = ttgov1
;env_default = ttgov2 env_default = ttgov2
;env_default = lopy ;env_default = lopy
;env_default = lopy4 ;env_default = lopy4
;env_default = lolin32lite_lora ;env_default = lolin32lite_lora
@ -31,8 +31,8 @@ build_flags =
; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <---
; otherwise device may crash in dense environments due to serial buffer overflow ; otherwise device may crash in dense environments due to serial buffer overflow
; ;
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
; ;

View File

@ -44,13 +44,13 @@ configData_t cfg; // struct holds current device configuration
osjob_t sendjob, initjob; // LMIC osjob_t sendjob, initjob; // LMIC
// Initialize global variables // Initialize global variables
char display_lora[16], display_lmic[16]; #define DISPLAYREFRESH_MS (1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS)) // calculate ms from fps
uint8_t channel = 0; char display_lora[16], display_lmic[16]; // display buffers
int macnum = 0;
uint64_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0; uint64_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0;
bool joinstate = false;
uint8_t DisplayState, LEDState; uint8_t DisplayState, LEDState;
uint16_t LEDBlinkduration = 500, LEDInterval = 1000, color = COLOR_NONE; uint16_t LEDBlinkduration = 500, LEDInterval = 1000, color = COLOR_NONE;
uint8_t channel = 0; // wifi channel counter
bool joinstate = false;
std::set<uint16_t> macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) std::set<uint16_t> macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE)
std::set<uint16_t> wifis; // associative container holds unique Wifi MAC adress hashes std::set<uint16_t> wifis; // associative container holds unique Wifi MAC adress hashes
@ -86,7 +86,6 @@ void gen_lora_deveui(uint8_t * pdeveui);
void RevBytes(unsigned char* b, size_t c); void RevBytes(unsigned char* b, size_t c);
void get_hard_deveui(uint8_t *pdeveui); void get_hard_deveui(uint8_t *pdeveui);
#ifdef VERBOSE #ifdef VERBOSE
void printKeys(void); void printKeys(void);
#endif // VERBOSE #endif // VERBOSE
@ -145,6 +144,13 @@ static void lora_init (osjob_t* j) {
LMIC_startJoining(); LMIC_startJoining();
} }
void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_state) {
color = set_color;
LEDBlinkduration = set_blinkduration;
LEDInterval = set_interval;
LEDState = set_state;
}
// LMIC FreeRTos Task // LMIC FreeRTos Task
void lorawan_loop(void * pvParameters) { void lorawan_loop(void * pvParameters) {
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check
@ -153,31 +159,21 @@ void lorawan_loop(void * pvParameters) {
os_runloop_once(); os_runloop_once();
// LED management for viusalizing LoRaWAN state // LED indicators for viusalizing LoRaWAN state
if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) {
// quick blink 20ms on each 1/5 second // quick blink 20ms on each 1/5 second
color = COLOR_YELLOW; set_LED(COLOR_YELLOW, 20, 200, 1);
LEDBlinkduration = 20;
LEDInterval = 200;
LEDState = 1;
// TX data pending // TX data pending
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
// small blink 10ms on each 1/2sec (not when joining) // small blink 10ms on each 1/2sec (not when joining)
color = COLOR_BLUE; set_LED(COLOR_BLUE, 10, 500, 1);
LEDBlinkduration = 10;
LEDInterval = 500;
LEDState =1;
// This should not happen so indicate a problem // This should not happen so indicate a problem
} else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) { } else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) {
// heartbeat long blink 200ms on each 2 seconds // heartbeat long blink 200ms on each 2 seconds
color = COLOR_RED; set_LED(COLOR_RED, 200, 2000, 1);
LEDBlinkduration = 200;
LEDInterval = 2000;
LEDState = 1;
} else { } else {
// led off // led off
color = COLOR_NONE; set_LED(COLOR_NONE, 0, 0, 0);
LEDState = 0;
} }
vTaskDelay(10/portTICK_PERIOD_MS); vTaskDelay(10/portTICK_PERIOD_MS);
@ -390,9 +386,9 @@ uint64_t uptime() {
void updateDisplay() { void updateDisplay() {
// timed display refresh according to frames per second setting // timed display refresh according to frames per second setting
if (currentMillis - previousDisplaymillis >= ( 1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS))) { if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) {
refreshDisplay(); refreshDisplay();
previousDisplaymillis += ( 1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS)); previousDisplaymillis += DISPLAYREFRESH_MS;
} }
// set display on/off according to current device configuration // set display on/off according to current device configuration
if (DisplayState != cfg.screenon) { if (DisplayState != cfg.screenon) {
@ -418,15 +414,15 @@ uint64_t uptime() {
#ifdef HAS_LED #ifdef HAS_LED
void updateLEDstatus() { void updateLEDstatus() {
if (LEDState == LOW) { if (LEDState == 0) {
if (currentMillis - previousLEDmillis >= LEDInterval) { if (currentMillis - previousLEDmillis >= LEDInterval) {
LEDState = HIGH; LEDState = 1;
previousLEDmillis += LEDInterval; previousLEDmillis += LEDInterval;
} }
} }
else { else {
if (currentMillis - previousLEDmillis >= LEDBlinkduration) { if (currentMillis - previousLEDmillis >= LEDBlinkduration) {
LEDState = LOW; LEDState = 0;
previousLEDmillis += LEDBlinkduration; previousLEDmillis += LEDBlinkduration;
} }
} }