From 1a24689652c45d0a93999fba3b91817ae6868f1d Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 20 Nov 2018 15:44:33 +0100 Subject: [PATCH] rcommand 0x13 user sensor on/off --- README.md | 5 +++++ include/sensor.h | 1 + src/hal/generic.h | 4 ++-- src/hal/heltecv2.h | 3 +++ src/hal/ttgobeam.h | 2 ++ src/rcommand.cpp | 28 ++++++++++++++++++++++++++-- src/sensor.cpp | 16 +++++++++++++++- 7 files changed, 54 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 213e83c4..8a587fd0 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,11 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. byte 1 = beacon ID (0..255) bytes 2..7 = beacon MAC with 6 digits (e.g. MAC 80:ab:00:01:02:03 -> 0x80ab00010203) +0x13 set user sensor mode + + byte 1 = user sensor number (1..4) + byte 2 = sensor mode (0 = disabled / 1 = enabled [default]) + 0x80 get device configuration Device answers with it's current configuration on Port 3. diff --git a/include/sensor.h b/include/sensor.h index 1b752dc6..9048a666 100644 --- a/include/sensor.h +++ b/include/sensor.h @@ -1,6 +1,7 @@ #ifndef _SENSOR_H #define _SENSOR_H +uint8_t sensor_mask(uint8_t sensor_no); uint8_t * sensor_read(uint8_t sensor); #endif \ No newline at end of file diff --git a/src/hal/generic.h b/src/hal/generic.h index 953fb475..96eca070 100644 --- a/src/hal/generic.h +++ b/src/hal/generic.h @@ -13,8 +13,8 @@ #define SPI_SCLK GPIO_NUM_18 #define SPI_CS GPIO_NUM_5 -#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDA=4/SCL=15); comment out if not present - +// enable only if device has these sensors, otherwise comment these lines +#define HAS_BME 0x77 // BME680 sensor on I2C bus (default SDA=4/SCL=15); comment out if not present #define HAS_SENSORS 1 // comment out if device has user defined sensors #define CFG_sx1276_radio 1 // select LoRa chip diff --git a/src/hal/heltecv2.h b/src/hal/heltecv2.h index 29ecdced..5ee40f90 100644 --- a/src/hal/heltecv2.h +++ b/src/hal/heltecv2.h @@ -5,6 +5,9 @@ // Hardware related definitions for Heltec V2 LoRa-32 Board +//#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out +//if not present + #define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1276_radio 1 diff --git a/src/hal/ttgobeam.h b/src/hal/ttgobeam.h index 495b070f..b5134fd7 100644 --- a/src/hal/ttgobeam.h +++ b/src/hal/ttgobeam.h @@ -5,6 +5,8 @@ // Hardware related definitions for TTGO T-Beam board +#define HAS_SENSORS 1 // test + #define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1276_radio 1 // HPD13A LoRa SoC diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 3918af5f..0ce4bb6a 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -134,6 +134,29 @@ void set_gps(uint8_t val[]) { } } +void set_sensor(uint8_t val[]) { + switch (val[0]) { // check if valid sensor number 1...4 + case 1: + case 2: + case 3: + case 4: + break; // valid sensor number -> continue + default: + ESP_LOGW( + TAG, + "Remote command set sensor mode called with invalid sensor number"); + return; // invalid sensor number -> exit + } + + ESP_LOGI(TAG, "Remote command: set sensor #%d mode to %s", val[0], + val[1] ? "on" : "off"); + + if (val[1]) + cfg.payloadmask |= sensor_mask(val[0]); // set bit + else + cfg.payloadmask &= ~sensor_mask(val[0]); // clear bit +} + void set_beacon(uint8_t val[]) { uint8_t id = val[0]; // use first parameter as beacon storage id memmove(val, val + 1, 6); // strip off storage id @@ -269,8 +292,9 @@ cmd_t table[] = { {0x0d, set_vendorfilter, 1, false}, {0x0e, set_blescan, 1, true}, {0x0f, set_wifiant, 1, true}, {0x10, set_rgblum, 1, true}, {0x11, set_monitor, 1, true}, {0x12, set_beacon, 7, false}, - {0x80, get_config, 0, false}, {0x81, get_status, 0, false}, - {0x84, get_gps, 0, false}, {0x85, get_bme, 0, false}, + {0x13, set_sensor, 2, true}, {0x80, get_config, 0, false}, + {0x81, get_status, 0, false}, {0x84, get_gps, 0, false}, + {0x85, get_bme, 0, false}, }; const uint8_t cmdtablesize = diff --git a/src/sensor.cpp b/src/sensor.cpp index aaf512a8..ac5459f8 100644 --- a/src/sensor.cpp +++ b/src/sensor.cpp @@ -4,7 +4,21 @@ // Local logging tag static const char TAG[] = "main"; -uint8_t * sensor_read(uint8_t sensor) { +uint8_t sensor_mask(uint8_t sensor_no) { + switch (sensor_no) { + case 1: + return (uint8_t)SENSOR1_DATA; + case 2: + return (uint8_t)SENSOR2_DATA; + break; + case 3: + return (uint8_t)SENSOR3_DATA; + case 4: + return (uint8_t)SENSOR4_DATA; + } +} + +uint8_t *sensor_read(uint8_t sensor) { static uint8_t buf[SENSORBUFFER] = {0}; uint8_t length = 3;