35 #include "led_sense.h"    36 #include "evt_sd_handler.h"    44 #include "hal_nop_delay.h"    46 #include "irq_msg_util.h"    47 #include "device_tick.h"    48 #include "pir_sense.h"    49 #include "hal_pin_analog_input.h"    50 #include "aa_aaa_battery_check.h"    51 #include "button_ui.h"    55 #include "app_error.h"    56 #include "out_pattern_gen.h"    58 #include "sensepi_ble.h"    59 #include "sensepi_cam_trigger.h"    60 #include "dev_id_fw_ver.h"    61 #include "sensepi_store_config.h"    68 #define APP_DEVICE_NAME_CHAR           'S','e','n','s','e','P','i'    73 #define APP_UUID_COMPLETE        0x0a, 0xde, 0xfb, 0x07, 0x74, 0x83, 0x66, 0xb0, 0x0d, 0x48, 0xf5, 0x07, 0x50, 0xdc, 0x73, 0x3c    77 #define APP_ADV_DATA   {                                        \    78                        0x02, BLE_GAP_AD_TYPE_FLAGS, BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE,    \    79                        sizeof(app_device_name) + 1, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, APP_DEVICE_NAME_CHAR,   \    80                        0x11, BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE, APP_UUID_COMPLETE     \    85 #define APP_SCAN_RSP_DATA  {                                        \    86                            0x02, BLE_GAP_AD_TYPE_TX_POWER_LEVEL, 0   ,     \    87                            0x11, BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME,  \    88                            'x', 'x','x', 'x', 'x', 'x' , 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',   \    89                            0x04, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, 0 ,  0 , 0 \    94 #define WDT_PERIOD_MS              301000   102 #define PIR_SENSE_INTERVAL_MS      50   104 #define PIR_SENSE_THRESHOLD        600   107 #define SENSE_FAST_TICK_INTERVAL_MS      60   109 #define SENSE_SLOW_TICK_INTERVAL_MS      300000   112 #define ADV_FAST_TICK_INTERVAL_MS  60   114 #define ADV_SLOW_TICK_INTERVAL_MS  1100   117 #define CONN_FAST_TICK_INTERVAL_MS  60   119 #define CONN_SLOW_TICK_INTERVAL_MS  1100   122 #define CONN_TIMEOUT_MS             (10*60*1000)   140 static uint32_t conn_count;
   142 static sensepi_config_t sensepi_ble_default_config = {
   143     .pir_conf.oper_time.day_or_night = 1,
   144     .pir_conf.oper_time.threshold = 0b0000000,
   145     .pir_conf.amplification = 20,
   146     .pir_conf.threshold = 175,
   147     .pir_conf.mode = 0x00000000,
   148     .pir_conf.intr_trig_timer = 50,
   150     .timer_conf.oper_time.day_or_night = 1,
   151     .timer_conf.oper_time.threshold = 0b0000000,
   152     .timer_conf.mode = 0x00000000,
   153     .timer_conf.timer_interval = 50,
   158 static uint32_t out_pin_array[] = {JACK_FOCUS_PIN, JACK_TRIGGER_PIN};
   162     .config_sensepi = &sensepi_ble_default_config,
   165     .led_sense_out_pin = LED_GREEN,
   171     .signal_out_pin_array = out_pin_array,
   181     log_printf(
"WDT reset\n");
   184 void prepare_init_ble_adv()
   190     memcpy(&app_scan_rsp_data[23], 
fw_ver_get(), 
sizeof(fw_ver_t));
   193     memcpy(&app_scan_rsp_data[5], 
dev_id_get(), 
sizeof(dev_id_t));
   195     sensepi_ble_adv_data_t app_adv_data_struct =
   197         .adv_data = app_adv_data,
   198         .scan_rsp_data = app_scan_rsp_data,
   212 static void ble_evt_handler(ble_evt_t * evt)
   214     log_printf(
"ble evt %x\n", evt->header.evt_id);
   215     switch (evt->header.evt_id)
   217     case BLE_GAP_EVT_CONNECTED:
   220     case BLE_GAP_EVT_DISCONNECTED:
   223     case BLE_GAP_EVT_ADV_SET_TERMINATED:
   226     case BLE_GAP_EVT_CONN_PARAM_UPDATE:
   227         log_printf(
"sup time %d s, max intvl %d ms, min intvl %d ms, slave lat %d\n",
   228             evt->evt.gap_evt.params.conn_param_update.conn_params.conn_sup_timeout/100,
   229             (5*evt->evt.gap_evt.params.conn_param_update.conn_params.max_conn_interval)/4,
   230             (5*evt->evt.gap_evt.params.conn_param_update.conn_params.min_conn_interval)/4,
   231             evt->evt.gap_evt.params.conn_param_update.conn_params.slave_latency);
   240 static void get_sensepi_config_t(sensepi_config_t *config)
   242     log_printf(
"Trig mode %d, PIR ope time %08x, PIR mode %08x, PIR amp %d, PIR thres %d, \   243              PIR int trig time %04d, Timer oper %x, Timer mode %x, timer interval %04d \n",
   244             config->trig_conf, config->pir_conf.oper_time, config->pir_conf.mode,
   245             config->pir_conf.amplification, config->pir_conf.threshold,
   246             config->pir_conf.intr_trig_timer,
   247             config->timer_conf.oper_time, config->timer_conf.mode, config->timer_conf.timer_interval);
   258     log_printf(
"in %d\n", interval);
   264         log_printf(
"Nxt Evt Hndlr : SENSING\n");
   272         conn_count += interval;
   289     log_printf(
"State change %d\n", new_state);
   292         log_printf(
"new state same as current state\n");
   301             sd_softdevice_disable();
   302             log_printf(
"State Change : SENSING\n");
   329             uint8_t is_sd_enabled;
   330             sd_softdevice_is_enabled(&is_sd_enabled);
   332             if(is_sd_enabled == 0)
   337                 prepare_init_ble_adv();
   339                 sensepi_sysinfo sysinfo;
   340                 memcpy(&sysinfo.id, 
dev_id_get(), 
sizeof(dev_id_t));
   342                 memcpy(&sysinfo.fw_ver, 
fw_ver_get(), 
sizeof(fw_ver_t));
   385             log_printf(
"fast\n");
   389         case BUTTON_UI_STEP_PRESS:
   397                 NRF_POWER->GPREGRET = 0xB1;
   398                 log_printf(
"Trying to do system reset..!!");
   399                 uint8_t is_sd_enabled;
   400                 sd_softdevice_is_enabled(&is_sd_enabled);
   401                 if(is_sd_enabled == 0)
   403                     sd_nvic_SystemReset();
   416         log_printf(
"slow\n");
   422         case BUTTON_UI_STEP_PRESS:
   438     hal_nop_delay_ms(600);
   441     hal_nop_delay_ms(600);
   453     log_printf(
"Reset because of ");
   454     if(NRF_POWER->RESETREAS == 0)
   456         log_printf(
"power on or brownout, ");
   458     if(NRF_POWER->RESETREAS & POWER_RESETREAS_DIF_Msk)
   460         log_printf(
"entering into debug interface from Sys OFF, ");
   462     if(NRF_POWER->RESETREAS & POWER_RESETREAS_DOG_Msk)
   464         log_printf(
"watchdog bite, ");
   466     if(NRF_POWER->RESETREAS & POWER_RESETREAS_LOCKUP_Msk)
   468         log_printf(
"CPU lockup, ");
   470     if(NRF_POWER->RESETREAS & POWER_RESETREAS_OFF_Msk)
   472         log_printf(
"wake up from SYS OFF by GPIO, ");
   474     if(NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk)
   476         log_printf(
"pin reset, ");
   478     if(NRF_POWER->RESETREAS & POWER_RESETREAS_SREQ_Msk)
   480         log_printf(
"software reset, ");
   485     NRF_POWER->RESETREAS = (POWER_RESETREAS_DIF_Msk |
   486                             POWER_RESETREAS_DOG_Msk |
   487                             POWER_RESETREAS_LOCKUP_Msk |
   488                             POWER_RESETREAS_OFF_Msk |
   489                             POWER_RESETREAS_RESETPIN_Msk |
   490                             POWER_RESETREAS_SREQ_Msk);
   493 #if DC_DC_CIRCUITRY == true  //Defined in the board header file   494     NRF_POWER->DCDCEN = POWER_DCDCEN_DCDCEN_Enabled << POWER_DCDCEN_DCDCEN_Pos;
   496     NRF_POWER->TASKS_LOWPWR = 1;
   516     uint8_t is_sd_enabled;
   517     sd_softdevice_is_enabled(&is_sd_enabled);
   519     if(is_sd_enabled == 0)
   538     log_printf(
"\n\nHello SensePi World!\n");
   554         irq_msg_callbacks cb =
 #define PIR_AMP_OFFSET_PIN
 
void sensepi_cam_trigger_stop()
Function to stop PIR sensing.
 
bool sensepi_store_config_is_memory_empty(void)
 
uint8_t aa_aaa_battery_status()
Function to get battery status. It'll convert battery ADC value to 8bit.
 
void sensepi_cam_trigger_add_tick(uint32_t interval)
Function to decide what to decide at current tick.
 
void sensepi_ble_update_sysinfo(sensepi_sysinfo *sysinfo)
Updates the characteristic that stores the sysinfo.
 
fw_ver_t * fw_ver_get(void)
Gets a pointer to the location where the Device ID is stored.
 
void boot_pwr_config(void)
Prints the reason for the last reset, enables the internal DC-DC converter if the board supports it a...
 
void load_last_config()
function to load previous sensepi configuration present in flash memory
 
BLE connection established with an app.
 
void sensepi_ble_gap_params_init(void)
Generic Access Profile initialization. The device name, and the preferred connection parameters are s...
 
void sensepi_store_config_check_fw_ver()
Function to check the major number of firmware if latest major number \ firmware version is greater t...
 
#define APP_SCAN_RSP_DATA
 
void sensepi_ble_stack_init(void)
Function for initializing the BLE stack by enabling the SoftDevice and the BLE event interrupt.
 
sense_states current_state
 
#define ADV_SLOW_TICK_INTERVAL_MS
 
void sensepi_ble_adv_start(void)
Function to start advertising.
 
void led_ui_loop_start(led_sequences seq, led_ui_priority_t priority)
Start a sequence to play repeatedly.
 
void sensepi_ble_service_init(void)
Create the Service and its characteristics for the SensePi device. There is a read-only characteristi...
 
#define LEDS_ACTIVE_STATE
 
#define SENSE_SLOW_TICK_INTERVAL_MS
 
void sensepi_ble_init(void(*ble_sd_evt)(ble_evt_t *evt), void(*config_update)(sensepi_config_t *cfg))
Initialize the handlers to pass the BLE SD events and the configuration received from the mobile app.
 
void sensepi_ble_update_config(sensepi_config_t *config)
Updates the characteristic that stores the SensePi config.
 
void state_change_handler(uint32_t new_state)
The handler that is called whenever the application transitions to a new state.
 
void lfclk_init(lfclk_src_t lfclk_src)
Function to initialize the LF clock.
 
Use PIR sensor to sense motion based on the set configuration.
 
Stucture for passing the configuration for initializing the Device Tick module.
 
void led_sense_init(uint32_t led_out_pin, uint32_t led_sense_analog_pin, uint32_t led_off_val)
Initialize the LED light sensing module.
 
void button_handler(button_ui_steps step, button_ui_action act)
Handler for all button related events.
 
#define ADV_FAST_TICK_INTERVAL_MS
 
void sensepi_cam_trigger_init(sensepi_cam_trigger_init_config_t *config_sensepi_cam_trigger)
Function to initiate SensePi_PIR module.
 
#define APP_DEVICE_NAME_CHAR
 
sensepi_config_t * sensepi_store_config_get_last_config()
 
void sensepi_ble_disconn(void)
Disconnect the current active connection, if already connected.
 
#define CONN_SLOW_TICK_INTERVAL_MS
 
#define MS_TIMER_TICKS_MS(ms)
 
void wdt_prior_reset_callback(void)
 
sensepi_config_t * sensepi_cam_trigger_get_sensepi_config()
Function to get the current configuration to send it to mobile app.
 
void device_tick_init(device_tick_cfg *cfg)
Initializes and starts the Device tick module. Provides a next interval event immediately....
 
#define PIR_AMP_SIGNAL_PIN
 
void ms_timer_init(uint32_t irq_priority)
 
void led_ui_type_stop_all(led_ui_seq_t type)
Stop all sequence of a particular type.
 
dev_id_t * dev_id_get(void)
Gets a pointer to the location where the Device ID is stored.
 
void sensepi_cam_trigger_update(sensepi_config_t *update_config)
Function to update the configuration at every instance when it is changed in the program.
 
BLE advertising to get connected to an app.
 
void device_tick_process(void)
The function to be called whenever the SoC wakes up to see if more than half the time of the current ...
 
void sensepi_ble_adv_init(sensepi_ble_adv_data_t *sensepi_ble_adv_data)
Function to initializing the advertising.
 
void sensepi_store_config_write(sensepi_config_t *latest_config)
Function to write the sensepi_config_t at address location received from get_next_location().
 
#define CONN_FAST_TICK_INTERVAL_MS
 
void irq_msg_init(irq_msg_callbacks *cb_ptr)
 
void leds_init(void)
Initialize and blink the LEDs momentarily. To be used at the start of the program.
 
void irq_msg_process(void)
 
void device_tick_switch_mode(device_tick_mode mode)
Switches from the fast to slow mode or vice versa.
 
int main(void)
Function for application main entry.
 
Strcture to configure SensePi_PIR module.
 
void hal_wdt_init(uint32_t period_ms, void(*wdt_timeout_handler)(void))
 
#define SENSE_FAST_TICK_INTERVAL_MS
 
void next_interval_handler(uint32_t interval)
The next interval handler is used for providing a periodic tick to be used by the various modules of ...
 
void irq_msg_push(irq_msg_types pushed_msg, void *more_data)
 
void sensepi_cam_trigger_start()
Function to start PIR sensing.