Updates ambient brightness estimation code

Also renames the `lux` field from `photo_read_t` to the more generic `brightness`.
This commit is contained in:
rbaron 2021-09-11 12:13:55 +02:00
parent be366d5c3c
commit d2daefaf46
6 changed files with 21 additions and 13 deletions

View file

@ -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%) |
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
| 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

View file

@ -69,7 +69,7 @@ static void rtc_callback() {
prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius,
temp_humi.humidity, soil_read.relative,
photo_read.lux, run_counter);
photo_read.brightness, run_counter);
prst_adv_start();
nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS);
prst_adv_stop();

View file

@ -146,13 +146,21 @@ prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage) {
1e4 * (battery_voltage - ret.voltage) / ret.voltage;
// 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
// complete dark and in a super bright environment.
// This current value is just a placeholder.
ret.lux = (uint16_t)UINT16_MAX * (photo_resistance / 1e4);
// estimate the brightness level. This needs to be calibrated with a real
// board in complete dark and in a super bright environment. This current
// value is just a placeholder.
// 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
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
return ret;
}

View file

@ -21,7 +21,7 @@ typedef struct prst_adc_photo_sensor {
// A value from 0x0000 (no light) 0xffff (direct sun).
// Might need calibration.
double voltage;
uint16_t lux;
uint16_t brightness;
} prst_adc_photo_sensor_t;
void prst_adc_init();

View file

@ -33,7 +33,7 @@
| 6-7 | Relative air humidity, scaled 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 |
| 16-17 | Lux level from the photoresistor |
| 16-17 | Ambient brightness level from 0 (dark) to 0xffff (bright) |
*/
#define SERVICE_DATA_LEN 18
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,
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) {
// 4 bits for a small wrap-around counter for deduplicating messages on the
// receiver.
@ -154,8 +154,8 @@ void prst_ble_update_adv_data(uint16_t batt_millivolts,
service_data[8] = soil_moisture >> 8;
service_data[9] = soil_moisture & 0xff;
service_data[16] = lux >> 8;
service_data[17] = lux & 0xff;
service_data[16] = brightness >> 8;
service_data[17] = brightness & 0xff;
// Encodes adv_data_ into .gap_adv_data_.
uint32_t err_code = ble_advdata_encode(

View file

@ -13,7 +13,7 @@ void prst_adv_stop();
void prst_ble_update_adv_data(uint16_t batt_millivolts,
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);
#endif // _PRST_BLE_H_