From 5cfd69479a315078a2802253cea685ed24289380 Mon Sep 17 00:00:00 2001 From: rbaron Date: Mon, 8 Aug 2022 19:17:40 +0200 Subject: [PATCH] Advertises while in deep sleep. Related to the investigation in #48 --- code/b-parasite/config/prst_config.h | 2 +- code/b-parasite/src/main.c | 58 ++++++++++++++++++---------- code/b-parasite/src/prst/rtc.c | 5 ++- code/b-parasite/src/prst/rtc.h | 4 ++ 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/code/b-parasite/config/prst_config.h b/code/b-parasite/config/prst_config.h index 49b405e..faba1e1 100644 --- a/code/b-parasite/config/prst_config.h +++ b/code/b-parasite/config/prst_config.h @@ -44,7 +44,7 @@ #define PRST_BLE_ADV_NAME "prst" // Total time spend advertising. -#define PRST_BLE_ADV_TIME_IN_MS 1000 +#define PRST_BLE_ADV_TIME_IN_S 1 // Interval between advertising packets. // From the specs, this value has to be greater or equal 20ms. #define PRST_BLE_ADV_INTERVAL_IN_MS 30 diff --git a/code/b-parasite/src/main.c b/code/b-parasite/src/main.c index 9370c9d..b7becdf 100644 --- a/code/b-parasite/src/main.c +++ b/code/b-parasite/src/main.c @@ -17,6 +17,13 @@ // A small wrap-around counter for deduplicating BLE packets on the receiver. static uint8_t run_counter = 0; +typedef enum { + SLEEPING, + ADVERTISING, +} State; + +static State state = SLEEPING; + static void log_init(void) { APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); NRF_LOG_DEFAULT_BACKENDS_INIT(); @@ -59,35 +66,44 @@ static void rtc_callback() { #if PRST_BLINK_LED nrf_gpio_pin_set(PRST_LED_PIN); #endif - prst_shtc3_read_t temp_humi = prst_shtc3_read(); - nrf_gpio_pin_set(PRST_FAST_DISCH_PIN); - prst_pwm_init(); - prst_pwm_start(); - prst_adc_batt_read_t batt_read = prst_adc_batt_read(); - 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); - uint16_t lux = 0; + if (state == SLEEPING) { + prst_shtc3_read_t temp_humi = prst_shtc3_read(); + nrf_gpio_pin_set(PRST_FAST_DISCH_PIN); + prst_pwm_init(); + prst_pwm_start(); + prst_adc_batt_read_t batt_read = prst_adc_batt_read(); + 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); + + uint16_t lux = 0; #if PRST_HAS_LDR || PRST_HAS_PHOTOTRANSISTOR - nrf_gpio_pin_set(PRST_PHOTO_V_PIN); - nrf_delay_ms(50); - prst_adc_photo_sensor_t photo_read = prst_adc_photo_read(batt_read.voltage); - lux = photo_read.brightness; - nrf_gpio_pin_clear(PRST_PHOTO_V_PIN); + nrf_gpio_pin_set(PRST_PHOTO_V_PIN); + nrf_delay_ms(50); + prst_adc_photo_sensor_t photo_read = prst_adc_photo_read(batt_read.voltage); + lux = photo_read.brightness; + nrf_gpio_pin_clear(PRST_PHOTO_V_PIN); #endif - prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_celsius, - temp_humi.humidity, soil_read.relative, lux, - run_counter); - prst_adv_start(); - nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS); - prst_adv_stop(); + prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_celsius, + temp_humi.humidity, soil_read.relative, lux, + run_counter); + + state = ADVERTISING; + prst_adv_start(); + prst_rtc_set_timer(PRST_BLE_ADV_TIME_IN_S); + run_counter++; + } else if (state == ADVERTISING) { + prst_adv_stop(); + state = SLEEPING; + prst_rtc_set_timer(PRST_DEEP_SLEEP_IN_SECONDS); + } #if PRST_BLINK_LED nrf_gpio_pin_clear(PRST_LED_PIN); #endif + NRF_LOG_FLUSH(); - run_counter++; } int main(void) { diff --git a/code/b-parasite/src/prst/rtc.c b/code/b-parasite/src/prst/rtc.c index 2f0db11..1a66bd9 100644 --- a/code/b-parasite/src/prst/rtc.c +++ b/code/b-parasite/src/prst/rtc.c @@ -34,13 +34,14 @@ void prst_rtc_init() { // Disable events we're not interested in so they don't trigger interrupts. nrf_drv_rtc_tick_disable(&rtc_); nrf_drv_rtc_overflow_disable(&rtc_); +} +void prst_rtc_set_timer(uint16_t seconds) { // Make sure we're counting from 0. nrf_drv_rtc_counter_clear(&rtc_); // Set compare channel to trigger interrupt after specified time. - err_code = nrf_drv_rtc_cc_set(&rtc_, 2, PRST_DEEP_SLEEP_IN_SECONDS * 8, true); - APP_ERROR_CHECK(err_code); + APP_ERROR_CHECK(nrf_drv_rtc_cc_set(&rtc_, 2, seconds * 8, true)); // Power on RTC instance. nrf_drv_rtc_enable(&rtc_); diff --git a/code/b-parasite/src/prst/rtc.h b/code/b-parasite/src/prst/rtc.h index 9cebe6d..cb4ca58 100644 --- a/code/b-parasite/src/prst/rtc.h +++ b/code/b-parasite/src/prst/rtc.h @@ -1,10 +1,14 @@ #ifndef _PRST_RTC_H_ #define _PRST_RTC_H_ +#include + typedef void (*prst_rtc_callback_t)(void); void prst_rtc_set_callback(prst_rtc_callback_t cb); void prst_rtc_init(); +void prst_rtc_set_timer(uint16_t seconds); + #endif // _PRST_RTC_H_ \ No newline at end of file