First draft for Photoresistor
the code now should setup and use the photoresistor and read it's value by the adc. the returned value is 0 just as a place holder until the correct values are measured.
This commit is contained in:
parent
64b2bcc6c6
commit
c9b05859e4
4 changed files with 47 additions and 1 deletions
|
|
@ -6,6 +6,10 @@
|
|||
// Built-in LED.
|
||||
#define PRST_LED_PIN NRF_GPIO_PIN_MAP(0, 28)
|
||||
|
||||
// Photo Sensor
|
||||
#define PRST_PHOTO_V NRF_GPIO_PIN_MAP(1, 11)
|
||||
#define PRST_PHOTO_OUT NRF_GPIO_PIN_MAP(0, 2)
|
||||
|
||||
// Deep sleep.
|
||||
#define PRST_DEEP_SLEEP_IN_SECONDS 300
|
||||
|
||||
|
|
|
|||
|
|
@ -62,8 +62,13 @@ static void rtc_callback() {
|
|||
prst_adc_soil_moisture_t soil_read = prst_adc_soil_read(batt_read.voltage);
|
||||
prst_pwm_stop();
|
||||
nrf_gpio_pin_clear(PRST_FAST_DISCH_PIN);
|
||||
|
||||
nrf_gpio_pin_set(PRST_PHOTO_V); // set GPIO for photoresistor HIGH
|
||||
prst_adc_photo_sensor prst_adc_photo_read(double battery_voltage); // read PHOTO SENSOR value
|
||||
nrf_gpio_pin_clear(PRST_PHOTO_V); // not sure need this one
|
||||
|
||||
prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius,
|
||||
temp_humi.humidity, soil_read.relative, run_counter);
|
||||
temp_humi.humidity, soil_read.relative, prst_adc_photo_sensor.lux, run_counter);
|
||||
prst_adv_start();
|
||||
nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS);
|
||||
prst_adv_stop();
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@
|
|||
#define PRST_ADC_SOIL_INPUT NRF_SAADC_INPUT_AIN1
|
||||
#define PRST_ADC_SOIL_CHANNEL 1
|
||||
|
||||
#define PRST_ADC_PHOTO_INPUT NRF_SAADC_INPUT_AIN0
|
||||
#define PRST_ADC_PHOTO_CHANNEL 2
|
||||
|
||||
static nrf_saadc_value_t sample_adc_channel(uint8_t channel) {
|
||||
nrf_saadc_value_t result;
|
||||
// *WARNING* this function is blocking, which is ot ideal but okay, but it
|
||||
|
|
@ -64,6 +67,12 @@ void prst_adc_init() {
|
|||
|
||||
APP_ERROR_CHECK(
|
||||
nrf_drv_saadc_channel_init(PRST_ADC_SOIL_CHANNEL, &soil_channel_config));
|
||||
|
||||
nrf_saadc_channel_config_t photo_channel_config =
|
||||
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(PRST_ADC_PHOTO_INPUT);
|
||||
|
||||
APP_ERROR_CHECK(
|
||||
nrf_drv_saadc_channel_init(PRST_ADC_PHOTO_CHANNEL, &photo_channel_config));
|
||||
}
|
||||
|
||||
prst_adc_batt_read_t prst_adc_batt_read() {
|
||||
|
|
@ -118,5 +127,25 @@ prst_adc_soil_moisture_t prst_adc_soil_read(double battery_voltage) {
|
|||
" %% (percentage); %u (relative)",
|
||||
ret.raw, NRF_LOG_FLOAT(percentage * 100), ret.relative);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline double get_lux_level(double battery_voltage, nrf_saadc_value_t raw_adc_output) {
|
||||
const double x = battery_voltage;
|
||||
|
||||
// todo: add DEBUG lines
|
||||
|
||||
return 0; // place holder. need to map values to lux level.
|
||||
}
|
||||
|
||||
prst_adc_photo_sensor prst_adc_photo_read(double battery_voltage) {
|
||||
nrf_saadc_value_t raw_adc_output = sample_adc_channel(PRST_ADC_PHOTO_CHANNEL);
|
||||
const uint16_t lux = get_lux_level(battery_voltage, raw_adc_output);
|
||||
prst_adc_photo_sensor ret;
|
||||
ret.raw = raw_adc_output;
|
||||
ret.lux = lux;
|
||||
|
||||
// todo: add DEBUG lines
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -16,10 +16,18 @@ typedef struct prst_adc_soil_moisture {
|
|||
double percentage;
|
||||
} prst_adc_soil_moisture_t;
|
||||
|
||||
typedef struct prst_adc_photo_sensor {
|
||||
int16_t raw;
|
||||
// Should check minimum and maximum values
|
||||
uint16_t lux;
|
||||
} prst_adc_photo_sensor;
|
||||
|
||||
void prst_adc_init();
|
||||
|
||||
prst_adc_batt_read_t prst_adc_batt_read();
|
||||
|
||||
prst_adc_soil_moisture_t prst_adc_soil_read(double battery_voltage);
|
||||
|
||||
prst_adc_photo_sensor prst_adc_photo_read(double battery_voltage);
|
||||
|
||||
#endif // _PRST_ADC_H_
|
||||
Loading…
Add table
Reference in a new issue