Updates ambient brightness estimation code
Also renames the `lux` field from `photo_read_t` to the more generic `brightness`.
This commit is contained in:
parent
be366d5c3c
commit
d2daefaf46
6 changed files with 21 additions and 13 deletions
|
|
@ -39,7 +39,7 @@ Sensor data is encoded in unsigned 16 bits (2 bytes), and whenever multiple
|
||||||
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
|
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
|
||||||
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
|
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
|
||||||
| 10-15 | b-parasite's own MAC address, big-endian format |
|
| 10-15 | b-parasite's own MAC address, big-endian format |
|
||||||
| 16-17 | Ambient light, scaled from from 0 (dark) to 0xffff (bright) |
|
| 16-17 | Ambient brightness level from 0 (dark) to 0xffff (bright) |
|
||||||
|
|
||||||
# Supported Modules
|
# Supported Modules
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ static void rtc_callback() {
|
||||||
|
|
||||||
prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius,
|
prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius,
|
||||||
temp_humi.humidity, soil_read.relative,
|
temp_humi.humidity, soil_read.relative,
|
||||||
photo_read.lux, run_counter);
|
photo_read.brightness, run_counter);
|
||||||
prst_adv_start();
|
prst_adv_start();
|
||||||
nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS);
|
nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS);
|
||||||
prst_adv_stop();
|
prst_adv_stop();
|
||||||
|
|
|
||||||
|
|
@ -146,13 +146,21 @@ prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage) {
|
||||||
1e4 * (battery_voltage - ret.voltage) / ret.voltage;
|
1e4 * (battery_voltage - ret.voltage) / ret.voltage;
|
||||||
|
|
||||||
// TODO: Now that we have the resistor value of the photo resistor, we need to
|
// TODO: Now that we have the resistor value of the photo resistor, we need to
|
||||||
// estimate the value in lux. This needs to be calibrated with a real board in
|
// estimate the brightness level. This needs to be calibrated with a real
|
||||||
// complete dark and in a super bright environment.
|
// board in complete dark and in a super bright environment. This current
|
||||||
// This current value is just a placeholder.
|
// value is just a placeholder.
|
||||||
ret.lux = (uint16_t)UINT16_MAX * (photo_resistance / 1e4);
|
// Dark resistance: 1 MOhm.
|
||||||
|
const double kDarkResistance = 1e6;
|
||||||
|
// Light resistance: 10 kOhm.
|
||||||
|
const double kLightResistance = 1e4;
|
||||||
|
// A value in 0x0 (dark) - 0xffff (light).
|
||||||
|
// A little better, but still not great.
|
||||||
|
ret.brightness = (uint16_t)UINT16_MAX * (kDarkResistance - photo_resistance) /
|
||||||
|
(kDarkResistance - kLightResistance);
|
||||||
|
|
||||||
#if PRST_ADC_PHOTO_DEBUG
|
#if PRST_ADC_PHOTO_DEBUG
|
||||||
NRF_LOG_INFO("[adc] Read lux level: %d (raw); %d (lux)", ret.raw, ret.lux);
|
NRF_LOG_INFO("[adc] Read brightness level: %d (raw); %d (brightness)",
|
||||||
|
ret.raw, ret.brightness);
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
@ -21,7 +21,7 @@ typedef struct prst_adc_photo_sensor {
|
||||||
// A value from 0x0000 (no light) 0xffff (direct sun).
|
// A value from 0x0000 (no light) 0xffff (direct sun).
|
||||||
// Might need calibration.
|
// Might need calibration.
|
||||||
double voltage;
|
double voltage;
|
||||||
uint16_t lux;
|
uint16_t brightness;
|
||||||
} prst_adc_photo_sensor_t;
|
} prst_adc_photo_sensor_t;
|
||||||
|
|
||||||
void prst_adc_init();
|
void prst_adc_init();
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@
|
||||||
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
|
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
|
||||||
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
|
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
|
||||||
| 10-15 | b-parasite's own MAC address |
|
| 10-15 | b-parasite's own MAC address |
|
||||||
| 16-17 | Lux level from the photoresistor |
|
| 16-17 | Ambient brightness level from 0 (dark) to 0xffff (bright) |
|
||||||
*/
|
*/
|
||||||
#define SERVICE_DATA_LEN 18
|
#define SERVICE_DATA_LEN 18
|
||||||
static uint8_t service_data[SERVICE_DATA_LEN];
|
static uint8_t service_data[SERVICE_DATA_LEN];
|
||||||
|
|
@ -136,7 +136,7 @@ void prst_ble_init() {
|
||||||
|
|
||||||
void prst_ble_update_adv_data(uint16_t batt_millivolts,
|
void prst_ble_update_adv_data(uint16_t batt_millivolts,
|
||||||
uint16_t temp_millicelcius, uint16_t humidity,
|
uint16_t temp_millicelcius, uint16_t humidity,
|
||||||
uint16_t soil_moisture, uint16_t lux,
|
uint16_t soil_moisture, uint16_t brightness,
|
||||||
uint8_t run_counter) {
|
uint8_t run_counter) {
|
||||||
// 4 bits for a small wrap-around counter for deduplicating messages on the
|
// 4 bits for a small wrap-around counter for deduplicating messages on the
|
||||||
// receiver.
|
// receiver.
|
||||||
|
|
@ -154,8 +154,8 @@ void prst_ble_update_adv_data(uint16_t batt_millivolts,
|
||||||
service_data[8] = soil_moisture >> 8;
|
service_data[8] = soil_moisture >> 8;
|
||||||
service_data[9] = soil_moisture & 0xff;
|
service_data[9] = soil_moisture & 0xff;
|
||||||
|
|
||||||
service_data[16] = lux >> 8;
|
service_data[16] = brightness >> 8;
|
||||||
service_data[17] = lux & 0xff;
|
service_data[17] = brightness & 0xff;
|
||||||
|
|
||||||
// Encodes adv_data_ into .gap_adv_data_.
|
// Encodes adv_data_ into .gap_adv_data_.
|
||||||
uint32_t err_code = ble_advdata_encode(
|
uint32_t err_code = ble_advdata_encode(
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ void prst_adv_stop();
|
||||||
|
|
||||||
void prst_ble_update_adv_data(uint16_t batt_millivolts,
|
void prst_ble_update_adv_data(uint16_t batt_millivolts,
|
||||||
uint16_t temp_millicelcius, uint16_t humidity,
|
uint16_t temp_millicelcius, uint16_t humidity,
|
||||||
uint16_t soil_moisture, uint16_t lux,
|
uint16_t soil_moisture, uint16_t brightness,
|
||||||
uint8_t run_counter);
|
uint8_t run_counter);
|
||||||
|
|
||||||
#endif // _PRST_BLE_H_
|
#endif // _PRST_BLE_H_
|
||||||
Loading…
Add table
Reference in a new issue