bugfixes in LED blink routine

This commit is contained in:
Klaus K Wilting 2018-04-20 10:27:13 +02:00
parent de2aaf9e6a
commit 14e68260e6
3 changed files with 52 additions and 45 deletions

View File

@ -52,12 +52,14 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
if (added) { if (added) {
if (sniff_type == MAC_SNIFF_WIFI ) { if (sniff_type == MAC_SNIFF_WIFI ) {
macs_wifi++; // increment Wifi MACs counter macs_wifi++; // increment Wifi MACs counter
set_LED(COLOR_GREEN, 50, 0, 1); if (joinstate)
blink_LED(COLOR_GREEN, 50, 0);
} }
#ifdef BLECOUNTER #ifdef BLECOUNTER
else if (sniff_type == MAC_SNIFF_BLE ) { else if (sniff_type == MAC_SNIFF_BLE ) {
macs_ble++; // increment BLE Macs counter macs_ble++; // increment BLE Macs counter
set_LED(COLOR_MAGENTA, 50, 0, 1); if (joinstate)
blink_LED(COLOR_MAGENTA, 50, 0);
} }
#endif #endif
} }

View File

@ -41,13 +41,14 @@ Refer to LICENSE.txt file in repository for more details.
configData_t cfg; // struct holds current device configuration configData_t cfg; // struct holds current device configuration
osjob_t sendjob, initjob; // LMIC jobs osjob_t sendjob, initjob; // LMIC jobs
uint64_t uptimecounter = 0; // timer global for uptime counter uint64_t uptimecounter = 0; // timer global for uptime counter
uint32_t currentMillis = 0, previousDisplaymillis = 0 // timer globals for state machine uint32_t currentMillis = millis(); // timer global for state machine
uint8_t DisplayState, LEDcount = 0; // globals for state machine uint32_t previousDisplaymillis = currentMillis; // Display refresh for state machine
uint8_t DisplayState = 0; // globals for state machine
uint16_t LEDBlinkduration = 0, LEDInterval = 0, color = COLOR_NONE; // state machine variables uint16_t LEDBlinkduration = 0, LEDInterval = 0, color = COLOR_NONE; // state machine variables
uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display
uint8_t channel = 0; // wifi channel rotation counter global for display uint8_t channel = 0; // wifi channel rotation counter global for display
char display_lora[16], display_lmic[16]; // display buffers char display_lora[16], display_lmic[16]; // display buffers
enum states LEDState = LED_OFF; // LED state global for state machine enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine
bool joinstate = false; // LoRa network joined? global flag bool joinstate = false; // LoRa network joined? global flag
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)
@ -67,12 +68,11 @@ int redirect_log(const char * fmt, va_list args) {
} }
#endif #endif
void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count) { void blink_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval) {
color = set_color; // set color for RGB LED color = set_color; // set color for RGB LED
LEDBlinkduration = set_blinkduration; // duration on LEDBlinkduration = set_blinkduration; // duration on
LEDInterval = set_interval; // duration off - on - off LEDInterval = set_interval; // duration off - on - off
LEDcount = set_count * 2; // number of on/off cycles before LED off LEDState = LED_ON; // start blink
LEDState = set_count ? LED_ON : LED_OFF; // sets LED to off if 0 blinks
} }
void reset_counters() { void reset_counters() {
@ -151,18 +151,18 @@ void lorawan_loop(void * pvParameters) {
// LED indicators for viusalizing LoRaWAN state // LED indicators for viusalizing LoRaWAN state
if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) {
// 5 quick blinks 20ms on each 1/5 second while joining // 5 quick blinks 20ms on each 1/5 second while joining
set_LED(COLOR_YELLOW, 20, 200, 5); blink_LED(COLOR_YELLOW, 20, 200);
// TX data pending // TX data pending
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
// 3 small blink 10ms on each 1/2sec (not when joining) // 3 small blink 10ms on each 1/2sec (not when joining)
set_LED(COLOR_BLUE, 10, 500, 3); blink_LED(COLOR_BLUE, 10, 500);
// 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 ) {
// 5 heartbeat long blink 200ms on each 2 seconds // 5 heartbeat long blink 200ms on each 2 seconds
set_LED(COLOR_RED, 200, 2000, 5); blink_LED(COLOR_RED, 200, 2000);
} else { } else {
// led off // led off
set_LED(COLOR_NONE, 0, 0, 0); blink_LED(COLOR_NONE, 0, 0);
} }
vTaskDelay(10/portTICK_PERIOD_MS); vTaskDelay(10/portTICK_PERIOD_MS);
@ -344,13 +344,11 @@ uint64_t uptime() {
u8x8.printf("%-16s", "BLTH:off"); u8x8.printf("%-16s", "BLTH:off");
#endif #endif
// update wifi channel display (line 4) // update RSSI limiter status & wifi channel display (line 5)
u8x8.setCursor(11,4);
u8x8.printf("ch:%02i", channel);
// update RSSI limiter status display (line 5)
u8x8.setCursor(0,5); u8x8.setCursor(0,5);
u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit); u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit);
u8x8.setCursor(11,5);
u8x8.printf("ch:%02i", channel);
// update LoRa status display (line 6) // update LoRa status display (line 6)
u8x8.setCursor(0,6); u8x8.setCursor(0,6);
@ -391,7 +389,7 @@ uint64_t uptime() {
#ifdef HAS_LED #ifdef HAS_LED
void switchLED() { void switchLED() {
enum states previousLEDState;
// led need to change state? avoid digitalWrite() for nothing // led need to change state? avoid digitalWrite() for nothing
if (LEDState != previousLEDState) { if (LEDState != previousLEDState) {
#ifdef LED_ACTIVE_LOW #ifdef LED_ACTIVE_LOW
@ -404,16 +402,25 @@ uint64_t uptime() {
rgb_set_color(LEDState ? color : COLOR_NONE); rgb_set_color(LEDState ? color : COLOR_NONE);
#endif #endif
ESP_LOGI(TAG, "switched LED: %d -> %d", previousLEDState, LEDState);
previousLEDState = LEDState; previousLEDState = LEDState;
LEDcount--; // decrement blink counter
} }
}; // switchLED() }; // switchLED()
void switchLEDstate() { void switchLEDstate() {
if (!LEDcount) // no more blinks? -> switch off LED
LEDState = LED_OFF; // LEDInterval != 0 -> LED is currently blinking -> toggle if interval cycle time elapsed
else if (LEDInterval) // blinks left? -> toggle LED and decrement blinks // LEDInterval = 0 -> do one blink then turn off LED
if (LEDInterval) // LED is blinking, wait until time elapsed, then toggle
LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF; LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF;
else // only one blink
LEDState = (currentMillis < LEDBlinkduration) ? LED_ON : LED_OFF;
ESP_LOGI(TAG, "current LEDState %d", LEDState);
} // switchLEDstate() } // switchLEDstate()
#endif #endif
@ -462,7 +469,7 @@ void setup() {
// initialize led if needed // initialize led if needed
#ifdef HAS_LED #ifdef HAS_LED
pinMode(HAS_LED, OUTPUT); pinMode(HAS_LED, OUTPUT);
set_LED(COLOR_NONE, 0, 0, 0); // LED off blink_LED(COLOR_NONE, 0, 0); // LED off
#endif #endif
// initialize button handling if needed // initialize button handling if needed
@ -541,10 +548,15 @@ do_send(&sendjob);
// https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ // https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/
void loop() { void loop() {
// simple state machine for controlling display, LED, button, etc.
uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit)
currentMillis = millis(); // timebase for state machine in milliseconds (32bit) currentMillis = millis(); // timebase for state machine in milliseconds (32bit)
// simple state machine for controlling display, LED, button, etc. #ifdef HAS_LED
switchLEDstate();
switchLED();
#endif
#ifdef HAS_BUTTON #ifdef HAS_BUTTON
readButton(); readButton();
@ -554,13 +566,6 @@ void loop() {
updateDisplay(); updateDisplay();
#endif #endif
#ifdef HAS_LED
switchLEDstate();
switchLED();
#endif
//sendPayload();
} }
/* end Aruino LOOP ------------------------------------------------------------ */ /* end Aruino LOOP ------------------------------------------------------------ */

View File

@ -6,14 +6,14 @@
//--- Declarations --- //--- Declarations ---
enum states { enum states {
LED_ON, LED_OFF,
LED_OFF LED_ON
}; };
//--- Prototypes --- //--- Prototypes ---
// defined in main.cpp // defined in main.cpp
void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count); void blink_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval);
void reset_counters(); void reset_counters();
// defined in configmanager.cpp // defined in configmanager.cpp