Added Leds managment

This commit is contained in:
Charles 2018-04-02 03:00:27 +02:00
parent c5d82fb1e0
commit 121e79cd7c

View File

@ -25,6 +25,9 @@ Refer to LICENSE.txt file in repository for more details.
#include "main.h" #include "main.h"
#include "globals.h" #include "globals.h"
// std::set for unified array functions
#include <set>
// OLED driver // OLED driver
#include <U8x8lib.h> #include <U8x8lib.h>
@ -42,7 +45,7 @@ configData_t cfg; // struct holds current device configuration
osjob_t sendjob, initjob; // LMIC osjob_t sendjob, initjob; // LMIC
// Initialize global variables // Initialize global variables
int macnum = 0, wifimac = 0, mactot = 0, salt; int macnum = 0, salt;
uint64_t uptimecounter = 0; uint64_t uptimecounter = 0;
bool joinstate = false; bool joinstate = false;
@ -52,7 +55,6 @@ std::set<uint32_t> wifis; // associative container holds filtered Wifi MAC adres
#ifdef BLECOUNTER #ifdef BLECOUNTER
std::set<uint32_t> bles; // associative container holds filtered BLE MAC adresses std::set<uint32_t> bles; // associative container holds filtered BLE MAC adresses
int scanTime; int scanTime;
int blemac = 0;
#endif #endif
// this variable will be changed in the ISR, and read in main loop // this variable will be changed in the ISR, and read in main loop
@ -74,29 +76,19 @@ void eraseConfig(void);
void saveConfig(void); void saveConfig(void);
void loadConfig(void); void loadConfig(void);
/* begin LMIC specific parts ------------------------------------------------------------ */ #ifdef HAS_LED
void set_onboard_led(int st);
#endif
// LMIC enhanced Pin mapping /* begin LMIC specific parts ------------------------------------------------------------ */
const lmic_pinmap lmic_pins = {
.mosi = PIN_SPI_MOSI,
.miso = PIN_SPI_MISO,
.sck = PIN_SPI_SCK,
.nss = PIN_SPI_SS,
.rxtx = LMIC_UNUSED_PIN,
.rst = RST,
.dio = {DIO0, DIO1, DIO2}
};
// defined in lorawan.cpp // defined in lorawan.cpp
void gen_lora_deveui(uint8_t * pdeveui); void gen_lora_deveui(uint8_t * pdeveui);
void RevBytes(unsigned char* b, size_t c); void RevBytes(unsigned char* b, size_t c);
#ifdef VERBOSE #ifdef VERBOSE
void printKeys(void); void printKeys(void);
#endif #endif // VERBOSE
// LMIC functions
void onEvent(ev_t ev);
void do_send(osjob_t* j);
// LMIC callback functions // LMIC callback functions
void os_getDevKey (u1_t *buf) { void os_getDevKey (u1_t *buf) {
@ -119,6 +111,21 @@ void os_getDevEui (u1_t* buf) {
gen_lora_deveui(buf); // generate DEVEUI from device's MAC gen_lora_deveui(buf); // generate DEVEUI from device's MAC
} }
// LMIC enhanced Pin mapping
const lmic_pinmap lmic_pins = {
.mosi = PIN_SPI_MOSI,
.miso = PIN_SPI_MISO,
.sck = PIN_SPI_SCK,
.nss = PIN_SPI_SS,
.rxtx = LMIC_UNUSED_PIN,
.rst = RST,
.dio = {DIO0, DIO1, DIO2}
};
// LMIC functions
void onEvent(ev_t ev);
void do_send(osjob_t* j);
// LoRaWAN Initjob // LoRaWAN Initjob
static void lora_init (osjob_t* j) { static void lora_init (osjob_t* j) {
// reset MAC state // reset MAC state
@ -132,8 +139,47 @@ static void lora_init (osjob_t* j) {
// LMIC Task // LMIC 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
static bool led_state ;
bool new_led_state ;
while(1) { while(1) {
uint16_t color;
os_runloop_once(); os_runloop_once();
// All follow is Led management
// Let join at the begining of if sequence,
// is prior to send because joining state send data
if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) {
color = COLOR_YELLOW;
// Joining Quick blink 20ms on each 1/5 second
new_led_state = ((millis() % 200) < 20) ? HIGH : LOW;
// Small blink 10ms on each 1/2sec (not when joining)
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
color = COLOR_BLUE;
new_led_state = ((millis() % 500) < 20) ? HIGH : LOW;
}
// This should not happen so indicate a pb
if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) {
color = COLOR_RED;
// Heartbeat long blink 200ms on each 2 seconds
new_led_state = ((millis() % 2000) < 200) ? HIGH : LOW;
}
// led need to change state ?
// avoid digitalWrite() for nothing
if (led_state != new_led_state) {
if (new_led_state == HIGH) {
set_onboard_led(1);
rgb_set_color(color);
} else {
set_onboard_led(0);
rgb_set_color(COLOR_NONE);
}
led_state = new_led_state;
}
vTaskDelay(10/portTICK_PERIOD_MS); vTaskDelay(10/portTICK_PERIOD_MS);
yield(); yield();
} }
@ -166,12 +212,18 @@ void lorawan_loop(void * pvParameters) {
void set_onboard_led(int st){ void set_onboard_led(int st){
#ifdef HAS_LED #ifdef HAS_LED
switch (st) { switch (st) {
case 1: digitalWrite(HAS_LED, HIGH); break; #ifdef LED_ACTIVE_LOW
case 0: digitalWrite(HAS_LED, LOW); break; case 1: digitalWrite(HAS_LED, LOW); break;
case 0: digitalWrite(HAS_LED, HIGH); break;
#else
case 1: digitalWrite(HAS_LED, HIGH); break;
case 0: digitalWrite(HAS_LED, LOW); break;
#endif
} }
#endif #endif
}; };
#ifdef HAS_BUTTON #ifdef HAS_BUTTON
// Button Handling, board dependent -> perhaps to be moved to hal/<$board.h> // Button Handling, board dependent -> perhaps to be moved to hal/<$board.h>
// IRAM_ATTR necessary here, see https://github.com/espressif/arduino-esp32/issues/855 // IRAM_ATTR necessary here, see https://github.com/espressif/arduino-esp32/issues/855
@ -204,8 +256,8 @@ void wifi_sniffer_loop(void * pvParameters) {
yield(); yield();
wifi_sniffer_set_channel(channel); wifi_sniffer_set_channel(channel);
channel = (channel % WIFI_CHANNEL_MAX) + 1; channel = (channel % WIFI_CHANNEL_MAX) + 1;
ESP_LOGI(TAG, "Wifi set channel %d", channel);
// Prepare and execute LoRaWAN data upload
u8x8.setCursor(0,5); u8x8.setCursor(0,5);
u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %4i", cfg.rssilimit); u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %4i", cfg.rssilimit);
u8x8.setCursor(11,5); u8x8.setCursor(11,5);
@ -213,12 +265,13 @@ void wifi_sniffer_loop(void * pvParameters) {
u8x8.setCursor(0,4); u8x8.setCursor(0,4);
u8x8.printf("MAC#: %-5i", wifis.size()); u8x8.printf("MAC#: %-5i", wifis.size());
// execute BLE count if BLE function is enabled
#ifdef BLECOUNTER #ifdef BLECOUNTER
// Once 2 full Wifi Channels scan, do a BLE scan // Once 2 full Wifi Channels scan, do a BLE scan
if (nloop % (WIFI_CHANNEL_MAX*2) == 0 ) { if (nloop % (WIFI_CHANNEL_MAX*2) == 0 ) {
// execute BLE count if BLE function is enabled // execute BLE count if BLE function is enabled
if (cfg.blescan) if (cfg.blescan)
BLECount(); BLECount();
} }
#endif #endif
@ -261,8 +314,10 @@ void wifi_sniffer_loop(void * pvParameters) {
} }
u8x8.clearLine(6); u8x8.clearLine(6);
if (cfg.screenon && cfg.screensaver) vTaskDelay(2000/portTICK_PERIOD_MS); // pause for displaying results if (cfg.screenon && cfg.screensaver) {
vTaskDelay(2000/portTICK_PERIOD_MS); // pause for displaying results
}
yield(); yield();
u8x8.setPowerSave(1 && cfg.screensaver); // set display off if screensaver is enabled u8x8.setPowerSave(1 && cfg.screensaver); // set display off if screensaver is enabled
} }
@ -360,7 +415,8 @@ void setup() {
#endif #endif
ESP_LOGI(TAG, "Starting %s %s", PROGNAME, PROGVERSION); ESP_LOGI(TAG, "Starting %s %s", PROGNAME, PROGVERSION);
rgb_set_color(COLOR_NONE);
// system event handler for wifi task, needed for wifi_sniffer_init() // system event handler for wifi task, needed for wifi_sniffer_init()
esp_event_loop_init(NULL, NULL); esp_event_loop_init(NULL, NULL);
@ -379,7 +435,7 @@ void setup() {
// Read settings from NVRAM // Read settings from NVRAM
loadConfig(); // includes initialize if necessary loadConfig(); // includes initialize if necessary
// initialize hardware // initialize hardware
#ifdef HAS_LED #ifdef HAS_LED
// initialize LED // initialize LED