From 758a960371cfdca76580d9985cedfeee958fc3a9 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 22:42:06 +0200 Subject: [PATCH] new led management (part 1) --- platformio.ini | 4 +-- src/lorawan.cpp | 14 ++++---- src/macsniff.cpp | 12 +++---- src/main.cpp | 84 +++++++++++++++++++----------------------------- 4 files changed, 47 insertions(+), 67 deletions(-) diff --git a/platformio.ini b/platformio.ini index 2d11dae3..8be3a272 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,9 +11,9 @@ ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] -;env_default = heltec_wifi_lora_32 +env_default = heltec_wifi_lora_32 ;env_default = ttgov1 -env_default = ttgov2 +;env_default = ttgov2 ;env_default = lopy ;env_default = lopy4 ;env_default = lolin32lite_lora diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 86e4fff8..1832d2f5 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -116,13 +116,13 @@ void do_send(osjob_t* j){ mydata[1] = data & 0xff; #ifdef BLECOUNTER - // Sum of unique BLE MACs seen - data = (uint16_t) bles.size(); - mydata[2] = (data & 0xff00) >> 8; - mydata[3] = data & 0xff; + // Sum of unique BLE MACs seen + data = (uint16_t) bles.size(); + mydata[2] = (data & 0xff00) >> 8; + mydata[3] = data & 0xff; #else - mydata[2] = 0; - mydata[3] = 0; + mydata[2] = 0; + mydata[3] = 0; #endif // Total BLE+WIFI unique MACs seen @@ -164,7 +164,7 @@ void onEvent (ev_t ev) { case EV_JOINED: strcpy_P(buff, PSTR("JOINED")); - sprintf(display_lora, " "); // erase "Join Wait" message from display + sprintf(display_lora, ""); // erase "Join Wait" message from display // Disable link check validation (automatically enabled // during join, but not supported by TTN at this time). LMIC_setLinkCheckMode(0); diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 6084d6be..e57526b1 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -11,6 +11,9 @@ // Local logging tag static const char *TAG = "macsniff"; +// defined in main.cpp +void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count); + static wifi_country_t wifi_country = {.cc=WIFI_MY_COUNTRY, .schan=WIFI_CHANNEL_MIN, .nchan=WIFI_CHANNEL_MAX, .policy=WIFI_COUNTRY_POLICY_MANUAL}; uint16_t salt; @@ -49,21 +52,16 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // Insert only if it was not found on global count if (added) { - if (sniff_type == MAC_SNIFF_WIFI ) { - rgb_set_color(COLOR_GREEN); + set_LED(COLOR_GREEN, 20, 0, 1); wifis.insert(hashedmac); // add hashed MAC to wifi container } #ifdef BLECOUNTER else if (sniff_type == MAC_SNIFF_BLE ) { - rgb_set_color(COLOR_MAGENTA); + set_LED(COLOR_MAGENTA, 20, 0, 1); bles.insert(hashedmac); // add hashed MAC to BLE container } #endif - - // Not sure user will have time to see the LED - // TBD do light off further in the code - rgb_set_color(COLOR_NONE); } ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", diff --git a/src/main.cpp b/src/main.cpp index 1da702c7..5c0e608b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,9 +46,9 @@ osjob_t sendjob, initjob; // LMIC // Initialize global variables char display_lora[16], display_lmic[16]; // display buffers uint64_t uptimecounter = 0; -uint32_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0; -uint8_t DisplayState, LEDState; -uint16_t LEDBlinkduration = 500, LEDInterval = 1000, color = COLOR_NONE; +uint32_t currentMillis = 0, previousDisplaymillis = 0; +uint8_t DisplayState, LEDState = 0, LEDcount = 0; +uint16_t LEDBlinkduration = 0, LEDInterval = 0, color=COLOR_NONE; uint8_t channel = 0; // wifi channel counter bool joinstate = false; @@ -79,6 +79,14 @@ void eraseConfig(void); void saveConfig(void); void loadConfig(void); +void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count) { + color = set_color; // set color for RGB LED + LEDBlinkduration = set_blinkduration; // duration on + LEDInterval = set_interval; // duration off - on - off + LEDcount = set_count * 2; // number of blinks before LED off + LEDState = set_count ? 1 : 0; // sets LED to off if 0 blinks +} + /* begin LMIC specific parts ------------------------------------------------------------ */ // defined in lorawan.cpp @@ -144,13 +152,6 @@ static void lora_init (osjob_t* j) { 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 void lorawan_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check @@ -161,16 +162,16 @@ void lorawan_loop(void * pvParameters) { // LED indicators for viusalizing LoRaWAN state if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { - // quick blink 20ms on each 1/5 second - set_LED(COLOR_YELLOW, 20, 200, 1); + // 5 quick blinks 20ms on each 1/5 second while joining + set_LED(COLOR_YELLOW, 20, 200, 5); // TX data pending } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { - // small blink 10ms on each 1/2sec (not when joining) - set_LED(COLOR_BLUE, 10, 500, 1); + // 3 small blink 10ms on each 1/2sec (not when joining) + set_LED(COLOR_BLUE, 10, 500, 3); // This should not happen so indicate a problem } else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) { - // heartbeat long blink 200ms on each 2 seconds - set_LED(COLOR_RED, 200, 2000, 1); + // 5 heartbeat long blink 200ms on each 2 seconds + set_LED(COLOR_RED, 200, 2000, 5); } else { // led off set_LED(COLOR_NONE, 0, 0, 0); @@ -270,7 +271,7 @@ void sniffer_loop(void * pvParameters) { vTaskDelay(1000/portTICK_PERIOD_MS); yield(); } - sprintf(display_lora, " "); // clear LoRa wait message fromd display + sprintf(display_lora, ""); // clear LoRa wait message fromd display } // end of send data cycle @@ -412,39 +413,30 @@ uint64_t uptime() { #endif #ifdef HAS_LED - void updateLEDstatus() { - - if (LEDState == 0) { - if (currentMillis - previousLEDmillis >= LEDInterval) { - LEDState = 1; - previousLEDmillis += LEDInterval; - } - } - else { - if (currentMillis - previousLEDmillis >= LEDBlinkduration) { - LEDState = 0; - previousLEDmillis += LEDBlinkduration; - } - } - - }; // updateLEDstatus() - - void refreshLED() { + void switchLED() { static bool previousLEDState; // led need to change state? avoid digitalWrite() for nothing if (LEDState != previousLEDState) { #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, LEDState); - #else digitalWrite(HAS_LED, !LEDState); + #else + digitalWrite(HAS_LED, LEDState); #endif #ifdef HAS_RGB_LED rgb_set_color(color); #endif previousLEDState = LEDState; + LEDcount--; // decrement blink counter } - }; // refreshLED() + }; // switchLED() + + void switchLEDstate() { + if (!LEDcount) // no more blinks? -> switch off LED + LEDState = 0; + else if (LEDInterval) // blinks left? -> toggle LED and decrement blinks + LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? 1 : 0; + } // switchLEDstate() #endif @@ -469,7 +461,6 @@ void setup() { #endif ESP_LOGI(TAG, "Starting %s %s", PROGNAME, PROGVERSION); - rgb_set_color(COLOR_NONE); // initialize system event handler for wifi task, needed for wifi_sniffer_init() esp_event_loop_init(NULL, NULL); @@ -492,17 +483,8 @@ void setup() { // initialize led if needed #ifdef HAS_LED - LEDState = 0; - color = COLOR_NONE; pinMode(HAS_LED, OUTPUT); - #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, LEDState); - #else - digitalWrite(HAS_LED, !LEDState); - #endif - #ifdef HAS_RBG_LED - rgb_set_color (color); - #endif + set_LED(COLOR_NONE, 0, 0, 0); // LED off #endif // initialize button handling if needed @@ -593,8 +575,8 @@ void loop() { #endif #ifdef HAS_LED - updateLEDstatus(); - refreshLED(); + switchLEDstate(); + switchLED(); #endif //sendPayload();