From c9b05859e46881c2604f68f8706b44a0b2fb696e Mon Sep 17 00:00:00 2001 From: chentadot <69039534+chentadot@users.noreply.github.com> Date: Mon, 5 Jul 2021 21:59:39 +0300 Subject: [PATCH] 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. --- code/b-parasite/config/prst_config.h | 4 ++++ code/b-parasite/src/main.c | 7 ++++++- code/b-parasite/src/prst/adc.c | 29 ++++++++++++++++++++++++++++ code/b-parasite/src/prst/adc.h | 8 ++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/code/b-parasite/config/prst_config.h b/code/b-parasite/config/prst_config.h index 6b77478..3c52b5f 100644 --- a/code/b-parasite/config/prst_config.h +++ b/code/b-parasite/config/prst_config.h @@ -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 diff --git a/code/b-parasite/src/main.c b/code/b-parasite/src/main.c index dccd4bc..25d6759 100644 --- a/code/b-parasite/src/main.c +++ b/code/b-parasite/src/main.c @@ -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(); diff --git a/code/b-parasite/src/prst/adc.c b/code/b-parasite/src/prst/adc.c index 025fbb8..666b66d 100644 --- a/code/b-parasite/src/prst/adc.c +++ b/code/b-parasite/src/prst/adc.c @@ -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; } \ No newline at end of file diff --git a/code/b-parasite/src/prst/adc.h b/code/b-parasite/src/prst/adc.h index 6b2bebe..9a0265a 100644 --- a/code/b-parasite/src/prst/adc.h +++ b/code/b-parasite/src/prst/adc.h @@ -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_ \ No newline at end of file