clang-format: rcommand.cpp, rgb_led.cpp+h, rokkithash.cpp, vendor_array

This commit is contained in:
Klaus K Wilting 2018-06-12 19:57:30 +02:00
parent 6250a0c308
commit b79442f26b
5 changed files with 568 additions and 1744 deletions

View File

@ -1,6 +1,7 @@
// remote command interpreter // remote command interpreter
// parses multiple number of command / value pairs from LoRaWAN remote command port (RCMDPORT) // parses multiple number of command / value pairs from LoRaWAN remote command
// checks commands and executes each command with 1 argument per command // port (RCMDPORT) checks commands and executes each command with 1 argument per
// command
// Basic Config // Basic Config
#include "globals.h" #include "globals.h"
@ -31,18 +32,22 @@ typedef struct {
// function sends result of get commands to LoRaWAN network // function sends result of get commands to LoRaWAN network
void do_transmit(osjob_t *j) { void do_transmit(osjob_t *j) {
// check if there is a pending TX/RX job running, if yes then reschedule transmission // check if there is a pending TX/RX job running, if yes then reschedule
// transmission
if (LMIC.opmode & OP_TXRXPEND) { if (LMIC.opmode & OP_TXRXPEND) {
ESP_LOGI(TAG, "LoRa busy, rescheduling"); ESP_LOGI(TAG, "LoRa busy, rescheduling");
sprintf(display_lmic, "LORA BUSY"); sprintf(display_lmic, "LORA BUSY");
os_setTimedCallback(&rcmdjob, os_getTime()+sec2osticks(RETRANSMIT_RCMD), do_transmit); os_setTimedCallback(&rcmdjob, os_getTime() + sec2osticks(RETRANSMIT_RCMD),
do_transmit);
} }
LMIC_setTxData2(RCMDPORT, rcmd_data, rcmd_data_size, 0); // send data unconfirmed on RCMD Port LMIC_setTxData2(RCMDPORT, rcmd_data, rcmd_data_size,
0); // send data unconfirmed on RCMD Port
ESP_LOGI(TAG, "%d bytes queued to send", rcmd_data_size); ESP_LOGI(TAG, "%d bytes queued to send", rcmd_data_size);
sprintf(display_lmic, "PACKET QUEUED"); sprintf(display_lmic, "PACKET QUEUED");
} }
// help function to transmit result of get commands, since callback function do_transmit() cannot have params // help function to transmit result of get commands, since callback function
// do_transmit() cannot have params
void transmit(xref2u1_t mydata, u1_t mydata_size) { void transmit(xref2u1_t mydata, u1_t mydata_size) {
rcmd_data = mydata; rcmd_data = mydata;
rcmd_data_size = mydata_size; rcmd_data_size = mydata_size;
@ -51,26 +56,48 @@ void transmit(xref2u1_t mydata, u1_t mydata_size){
// help function to assign LoRa datarates to numeric spreadfactor values // help function to assign LoRa datarates to numeric spreadfactor values
void switch_lora(uint8_t sf, uint8_t tx) { void switch_lora(uint8_t sf, uint8_t tx) {
if ( tx > 20 ) return; if (tx > 20)
return;
cfg.txpower = tx; cfg.txpower = tx;
switch (sf) { switch (sf) {
case 7: LMIC_setDrTxpow(DR_SF7,tx); cfg.lorasf=sf; break; case 7:
case 8: LMIC_setDrTxpow(DR_SF8,tx); cfg.lorasf=sf; break; LMIC_setDrTxpow(DR_SF7, tx);
case 9: LMIC_setDrTxpow(DR_SF9,tx); cfg.lorasf=sf; break; cfg.lorasf = sf;
case 10: LMIC_setDrTxpow(DR_SF10,tx); cfg.lorasf=sf; break; break;
case 8:
LMIC_setDrTxpow(DR_SF8, tx);
cfg.lorasf = sf;
break;
case 9:
LMIC_setDrTxpow(DR_SF9, tx);
cfg.lorasf = sf;
break;
case 10:
LMIC_setDrTxpow(DR_SF10, tx);
cfg.lorasf = sf;
break;
case 11: case 11:
#if defined(CFG_eu868) #if defined(CFG_eu868)
LMIC_setDrTxpow(DR_SF11,tx); cfg.lorasf=sf; break; LMIC_setDrTxpow(DR_SF11, tx);
cfg.lorasf = sf;
break;
#elif defined(CFG_us915) #elif defined(CFG_us915)
LMIC_setDrTxpow(DR_SF11CR,tx); cfg.lorasf=sf; break; LMIC_setDrTxpow(DR_SF11CR, tx);
cfg.lorasf = sf;
break;
#endif #endif
case 12: case 12:
#if defined(CFG_eu868) #if defined(CFG_eu868)
LMIC_setDrTxpow(DR_SF12,tx); cfg.lorasf=sf; break; LMIC_setDrTxpow(DR_SF12, tx);
cfg.lorasf = sf;
break;
#elif defined(CFG_us915) #elif defined(CFG_us915)
LMIC_setDrTxpow(DR_SF12CR,tx); cfg.lorasf=sf; break; LMIC_setDrTxpow(DR_SF12CR, tx);
cfg.lorasf = sf;
break;
#endif #endif
default: break; default:
break;
} }
} }
@ -80,7 +107,9 @@ void set_reset(uint8_t val) {
case 0: // restart device case 0: // restart device
ESP_LOGI(TAG, "Remote command: restart device"); ESP_LOGI(TAG, "Remote command: restart device");
sprintf(display_lora, "Reset pending"); sprintf(display_lora, "Reset pending");
vTaskDelay(10000/portTICK_PERIOD_MS); // wait for LMIC to confirm LoRa downlink to server vTaskDelay(
10000 /
portTICK_PERIOD_MS); // wait for LMIC to confirm LoRa downlink to server
esp_restart(); esp_restart();
break; break;
case 1: // reset MAC counter case 1: // reset MAC counter
@ -104,23 +133,28 @@ void set_rssi(uint8_t val) {
void set_sendcycle(uint8_t val) { void set_sendcycle(uint8_t val) {
cfg.sendcycle = val; cfg.sendcycle = val;
ESP_LOGI(TAG, "Remote command: set payload send cycle to %d seconds", cfg.sendcycle*2); ESP_LOGI(TAG, "Remote command: set payload send cycle to %d seconds",
cfg.sendcycle * 2);
}; };
void set_wifichancycle(uint8_t val) { void set_wifichancycle(uint8_t val) {
cfg.wifichancycle = val; cfg.wifichancycle = val;
// modify wifi channel rotation IRQ // modify wifi channel rotation IRQ
timerAlarmWrite(channelSwitch, cfg.wifichancycle * 10000, true); // reload interrupt after each trigger of channel switch cycle timerAlarmWrite(
ESP_LOGI(TAG, "Remote command: set Wifi channel switch interval to %.1f seconds", cfg.wifichancycle/float(100)); channelSwitch, cfg.wifichancycle * 10000,
true); // reload interrupt after each trigger of channel switch cycle
ESP_LOGI(TAG,
"Remote command: set Wifi channel switch interval to %.1f seconds",
cfg.wifichancycle / float(100));
}; };
void set_blescantime(uint8_t val) { void set_blescantime(uint8_t val) {
cfg.blescantime = val; cfg.blescantime = val;
ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds", cfg.blescantime/float(100)); ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds",
cfg.blescantime / float(100));
#ifdef BLECOUNTER #ifdef BLECOUNTER
// stop & restart BLE scan task to apply new parameter // stop & restart BLE scan task to apply new parameter
if (cfg.blescan) if (cfg.blescan) {
{
stop_BLEscan(); stop_BLEscan();
start_BLEscan(); start_BLEscan();
} }
@ -147,24 +181,36 @@ void set_countmode(uint8_t val) {
void set_screensaver(uint8_t val) { void set_screensaver(uint8_t val) {
ESP_LOGI(TAG, "Remote command: set screen saver to %s ", val ? "on" : "off"); ESP_LOGI(TAG, "Remote command: set screen saver to %s ", val ? "on" : "off");
switch (val) { switch (val) {
case 1: cfg.screensaver = val; break; case 1:
default: cfg.screensaver = 0; break; cfg.screensaver = val;
break;
default:
cfg.screensaver = 0;
break;
} }
}; };
void set_display(uint8_t val) { void set_display(uint8_t val) {
ESP_LOGI(TAG, "Remote command: set screen to %s", val ? "on" : "off"); ESP_LOGI(TAG, "Remote command: set screen to %s", val ? "on" : "off");
switch (val) { switch (val) {
case 1: cfg.screenon = val; break; case 1:
default: cfg.screenon = 0; break; cfg.screenon = val;
break;
default:
cfg.screenon = 0;
break;
} }
}; };
void set_gps(uint8_t val) { void set_gps(uint8_t val) {
ESP_LOGI(TAG, "Remote command: set GPS to %s", val ? "on" : "off"); ESP_LOGI(TAG, "Remote command: set GPS to %s", val ? "on" : "off");
switch (val) { switch (val) {
case 1: cfg.gpsmode = val; break; case 1:
default: cfg.gpsmode = 0; break; cfg.gpsmode = val;
break;
default:
cfg.gpsmode = 0;
break;
} }
}; };
@ -176,8 +222,12 @@ void set_lorasf(uint8_t val) {
void set_loraadr(uint8_t val) { void set_loraadr(uint8_t val) {
ESP_LOGI(TAG, "Remote command: set LoRa ADR mode to %s", val ? "on" : "off"); ESP_LOGI(TAG, "Remote command: set LoRa ADR mode to %s", val ? "on" : "off");
switch (val) { switch (val) {
case 1: cfg.adrmode = val; break; case 1:
default: cfg.adrmode = 0; break; cfg.adrmode = val;
break;
default:
cfg.adrmode = 0;
break;
} }
LMIC_setAdrMode(cfg.adrmode); LMIC_setAdrMode(cfg.adrmode);
}; };
@ -202,10 +252,15 @@ void set_blescan(uint8_t val) {
}; };
void set_wifiant(uint8_t val) { void set_wifiant(uint8_t val) {
ESP_LOGI(TAG, "Remote command: set Wifi antenna to %s", val ? "external" : "internal"); ESP_LOGI(TAG, "Remote command: set Wifi antenna to %s",
val ? "external" : "internal");
switch (val) { switch (val) {
case 1: cfg.wifiant = val; break; case 1:
default: cfg.wifiant = 0; break; cfg.wifiant = val;
break;
default:
cfg.wifiant = 0;
break;
} }
#ifdef HAS_ANTENNA_SWITCH #ifdef HAS_ANTENNA_SWITCH
antenna_select(cfg.wifiant); antenna_select(cfg.wifiant);
@ -213,10 +268,15 @@ void set_wifiant(uint8_t val) {
}; };
void set_vendorfilter(uint8_t val) { void set_vendorfilter(uint8_t val) {
ESP_LOGI(TAG, "Remote command: set vendorfilter mode to %s", val ? "on" : "off"); ESP_LOGI(TAG, "Remote command: set vendorfilter mode to %s",
val ? "on" : "off");
switch (val) { switch (val) {
case 1: cfg.vendorfilter = val; break; case 1:
default: cfg.vendorfilter = 0; break; cfg.vendorfilter = val;
break;
default:
cfg.vendorfilter = 0;
break;
} }
}; };
@ -262,7 +322,9 @@ void get_gps (uint8_t val) {
#ifdef HAS_GPS #ifdef HAS_GPS
gps_read(); gps_read();
transmit((byte *)&gps_status, sizeof(gps_status)); transmit((byte *)&gps_status, sizeof(gps_status));
ESP_LOGI(TAG, "lat=%f / lon=%f | Sats=%u | HDOP=%u | Alti=%u", gps_status.latitude / 1000000, gps_status.longitude / 1000000, gps_status.satellites, gps_status.hdop, gps_status.altitude); ESP_LOGI(TAG, "lat=%f / lon=%f | Sats=%u | HDOP=%u | Alti=%u",
gps_status.latitude / 1000000, gps_status.longitude / 1000000,
gps_status.satellites, gps_status.hdop, gps_status.altitude);
#else #else
ESP_LOGE(TAG, "GPS not present"); ESP_LOGE(TAG, "GPS not present");
#endif #endif
@ -271,40 +333,33 @@ void get_gps (uint8_t val) {
// assign previously defined functions to set of numeric remote commands // assign previously defined functions to set of numeric remote commands
// format: opcode, function, flag (1 = do make settings persistent / 0 = don't) // format: opcode, function, flag (1 = do make settings persistent / 0 = don't)
// //
cmd_t table[] = { cmd_t table[] = {{0x01, set_rssi, true}, {0x02, set_countmode, true},
{0x01, set_rssi, true}, {0x03, set_gps, true}, {0x04, set_display, true},
{0x02, set_countmode, true}, {0x05, set_lorasf, true}, {0x06, set_lorapower, true},
{0x03, set_gps, true}, {0x07, set_loraadr, true}, {0x08, set_screensaver, true},
{0x04, set_display, true}, {0x09, set_reset, false}, {0x0a, set_sendcycle, true},
{0x05, set_lorasf, true}, {0x0b, set_wifichancycle, true}, {0x0c, set_blescantime, true},
{0x06, set_lorapower, true}, {0x0d, set_vendorfilter, false}, {0x0e, set_blescan, true},
{0x07, set_loraadr, true}, {0x0f, set_wifiant, true}, {0x10, set_rgblum, true},
{0x08, set_screensaver, true}, {0x80, get_config, false}, {0x81, get_uptime, false},
{0x09, set_reset, false}, {0x82, get_cputemp, false}, {0x83, get_voltage, false},
{0x0a, set_sendcycle, true}, {0x84, get_gps, false}};
{0x0b, set_wifichancycle, true},
{0x0c, set_blescantime, true},
{0x0d, set_vendorfilter, false},
{0x0e, set_blescan, true},
{0x0f, set_wifiant, true},
{0x10, set_rgblum, true},
{0x80, get_config, false},
{0x81, get_uptime, false},
{0x82, get_cputemp, false},
{0x83, get_voltage, false},
{0x84, get_gps, false}
};
// check and execute remote command // check and execute remote command
void rcommand(uint8_t cmd, uint8_t arg) { void rcommand(uint8_t cmd, uint8_t arg) {
int i = sizeof(table) / sizeof(table[0]); // number of commands in command table int i =
sizeof(table) / sizeof(table[0]); // number of commands in command table
bool store_flag = false; bool store_flag = false;
while (i--) { while (i--) {
if (cmd == table[i].nam) { // check if valid command if (cmd == table[i].nam) { // check if valid command
table[i].func(arg); // then execute assigned function table[i].func(arg); // then execute assigned function
if ( table[i].store ) store_flag = true; // set save flag if function needs to store configuration if (table[i].store)
store_flag =
true; // set save flag if function needs to store configuration
break; // exit check loop, since command was found break; // exit check loop, since command was found
} }
} }
if (store_flag) saveConfig(); // if save flag is set: store new configuration in NVS to make it persistent if (store_flag)
saveConfig(); // if save flag is set: store new configuration in NVS to make
// it persistent
} }

View File

@ -6,8 +6,7 @@
// RGB Led instance // RGB Led instance
SmartLed rgb_led(LED_WS2812, 1, HAS_RGB_LED); SmartLed rgb_led(LED_WS2812, 1, HAS_RGB_LED);
float rgb_CalcColor(float p, float q, float t) float rgb_CalcColor(float p, float q, float t) {
{
if (t < 0.0f) if (t < 0.0f)
t += 1.0f; t += 1.0f;
if (t > 1.0f) if (t > 1.0f)
@ -30,19 +29,15 @@ float rgb_CalcColor(float p, float q, float t)
// HslColor using H, S, L values (0.0 - 1.0) // HslColor using H, S, L values (0.0 - 1.0)
// L should be limited to between (0.0 - 0.5) // L should be limited to between (0.0 - 0.5)
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
RGBColor rgb_hsl2rgb(float h, float s, float l) RGBColor rgb_hsl2rgb(float h, float s, float l) {
{
RGBColor RGB_color; RGBColor RGB_color;
float r; float r;
float g; float g;
float b; float b;
if (s == 0.0f || l == 0.0f) if (s == 0.0f || l == 0.0f) {
{
r = g = b = l; // achromatic or black r = g = b = l; // achromatic or black
} } else {
else
{
float q = l < 0.5f ? l * (1.0f + s) : l + s - (l * s); float q = l < 0.5f ? l * (1.0f + s) : l + s - (l * s);
float p = 2.0f * l - q; float p = 2.0f * l - q;
r = rgb_CalcColor(p, q, h + 1.0f / 3.0f); r = rgb_CalcColor(p, q, h + 1.0f / 3.0f);

View File

@ -19,8 +19,7 @@
#define COLOR_WHITE 360 #define COLOR_WHITE 360
#define COLOR_NONE 999 #define COLOR_NONE 999
struct RGBColor struct RGBColor {
{
uint8_t R; uint8_t R;
uint8_t G; uint8_t G;
uint8_t B; uint8_t B;

View File

@ -40,7 +40,8 @@ uint32_t rokkit(const char * data, int len) {
uint32_t hash, tmp; uint32_t hash, tmp;
int rem; int rem;
if (len <= 0 || data == 0) return 0; if (len <= 0 || data == 0)
return 0;
hash = len; hash = len;
rem = len & 3; rem = len & 3;
len >>= 2; len >>= 2;
@ -57,16 +58,19 @@ uint32_t rokkit(const char * data, int len) {
/* Handle end cases */ /* Handle end cases */
switch (rem) { switch (rem) {
case 3: hash += *((uint16_t*)data); case 3:
hash += *((uint16_t *)data);
hash ^= hash << 16; hash ^= hash << 16;
hash ^= ((signed char)data[2]) << 18; hash ^= ((signed char)data[2]) << 18;
hash += hash >> 11; hash += hash >> 11;
break; break;
case 2: hash += *((uint16_t*)data); case 2:
hash += *((uint16_t *)data);
hash ^= hash << 11; hash ^= hash << 11;
hash += hash >> 17; hash += hash >> 17;
break; break;
case 1: hash += (signed char)*data; case 1:
hash += (signed char)*data;
hash ^= hash << 10; hash ^= hash << 10;
hash += hash >> 1; hash += hash >> 1;
} }

File diff suppressed because it is too large Load Diff