Appiko
Modules | Macros | Enumerations | Functions | Variables
The code for the PIR based Sense units.

The PIR sense application's main file that makes it operate. More...

Modules

 The support code for the PIR based Sense units.
 The PIR sense application's support file that handles ble operations.
 
 The support code for the PIR based Sense units.
 The PIR sense application's support file that handles camera triggering.
 
 The support code to store sensepi_configs in flash
 Functions to store and manage sensepi_configs in non volatile memory. This will enable the firmware to reload the previously written config in case of power reset or system reset.
 

Macros

#define APP_DEVICE_NAME_CHAR   'S','e','n','s','e','P','i'
 
#define APP_UUID_COMPLETE   0x0a, 0xde, 0xfb, 0x07, 0x74, 0x83, 0x66, 0xb0, 0x0d, 0x48, 0xf5, 0x07, 0x50, 0xdc, 0x73, 0x3c
 
#define APP_ADV_DATA
 
#define APP_SCAN_RSP_DATA
 
#define WDT_PERIOD_MS   301000
 
#define ENABLE_WDT   1
 
#define PIR_SENSE_INTERVAL_MS   50
 
#define PIR_SENSE_THRESHOLD   600
 
#define SENSE_FAST_TICK_INTERVAL_MS   60
 
#define SENSE_SLOW_TICK_INTERVAL_MS   300000
 
#define ADV_FAST_TICK_INTERVAL_MS   60
 
#define ADV_SLOW_TICK_INTERVAL_MS   1100
 
#define CONN_FAST_TICK_INTERVAL_MS   60
 
#define CONN_SLOW_TICK_INTERVAL_MS   1100
 
#define CONN_TIMEOUT_MS   (10*60*1000)
 

Enumerations

enum  sense_states {
  SENSING, ADVERTISING, CONNECTED, SENSING,
  ADVERTISING, CONNECTED, SENSING, ADVERTISING,
  CONNECTED, SENSING, ADVERTISING, CONNECTED
}
 

Functions

void wdt_prior_reset_callback (void)
 
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 the application. More...
 
void state_change_handler (uint32_t new_state)
 The handler that is called whenever the application transitions to a new state. More...
 
void button_handler (button_ui_steps step, button_ui_action act)
 Handler for all button related events. More...
 
void leds_init (void)
 Initialize and blink the LEDs momentarily. To be used at the start of the program.
 
void boot_pwr_config (void)
 Prints the reason for the last reset, enables the internal DC-DC converter if the board supports it and puts the nRF SoC to the low power mode.
 
void load_last_config ()
 function to load previous sensepi configuration present in flash memory More...
 
void slumber (void)
 
int main (void)
 Function for application main entry.
 

Variables

sense_states current_state
 

Detailed Description

SensePi Firmware

SensePi is a motion based camera triggering device. This device can be used to trigger DSLRs and Point and Shoot cameras. This device can be used as a trigger for other systems as well.

There is no active sensor being used in this device.

Application Explanation :

SensePi Firmware is an application which handles multiple modules which serves a different purpose like handling camera triggering, wireless connection, data storage into non-volatile memory.

This application also handles events generated by some sub-modules like button press module, add tick module, irq msg handler, etc.

Working Principle :

SensePi Firmware works on event-driven principle. That means core will wake-up only when some interrupt or events happens. Other times it'll be in sleep mode. This will result in a drastic drop in power consumption. Also by making it event-driven its response latency is very low. There are almost no instructions to perform between event and handler operations. The response time of the device is very low. Control diagram will explain how program's flow is.

Firmware's working can be explained as a state machine having 3 states. The application will be always in one these states. These states are:

This state machine mechanism is handled by following sub-modules :

1 IRQ message module :

The state machine mechanism is handled by application level events. These events are Add Tick Event and State Change Event. These events are triggered by interrupts of different modules. To handle these events we have used the circular buffer. These events are treated as messages in the buffer. In interrupt handler of respective modules, these events are pushed into the circular buffer as a message. These messages then popped in the main thread and events will be handled.

2 System tick module :

This module generates Add Tick Event after a certain number of ms_timer_ticks has passed, say 'n' ticks. This module uses ms_timer to generate an event. Every time the processor wakes up, this module checks how many ms_timer_ticks have passed since last event occurrence, let's say it is 'tick count'. If that 'tick count' is greater than that of 'n/2' it generates Add Tick event and starts counting ms_timer_ticks again. If 'tick count' is less than 'n/2' module will not do anything. If the processor doesn't wake up for 'n' ms_timer_ticks, this module will generate Add Tick event. The value of 'n' can be changed. This value will decide maximum sleep time at any instance of time.

3 Button Press module :

Button press module works on button press duration. This sub-module will come into play when we press the button. From that instance, it'll calculate how long the button has been pressed. Here we have divide button press durations in 3 different 'Durations': Pressed, Short and Long. As soon as the user presses the button, system ticks duration is changed to 100ms(in form of ms_timer_ticks here onwards). At every Add Tick Event, it adds 100ms to count and checks at which 'Duration' module is. At 'short press duration', the module will change SensePi's state to advertisement state. At 'long press duration' the module forces SensePi into bootloader mode for a firmware update.

States Machine :

In each state, different sets of peripheral modules are active or serving different requests. In SensePi we use BLE stack for Bluetooth connectivity.

1 Advertisement :

In this state, device's Bluetooth is on and ready to connect. To understand working of Advertisement state with respect to BLE stack read this article. In advertisement device will transmit specified data periodically. Then user can connect to device, possibly using 'Appiko setup' app or 'nrf Connect' app. Timeout for this state is 5 mins.

2 Connected :

In this state, device is connected to a mobile device and hence cannot connect with other mobile devices. To understand working of Connected state with respect to BLE protocol read this article. A user can send configurations from mobile to the device. Timeout for this state is 10 mins.

3 Sensing :

This is the default state for the device. After Timeout or after disconnect SensePi will comeback in this state. This state handles PIR sensing & camera triggering. There is no timeout for this state.

By using this state machine approach we were able to avoid a lot of conflicting scenarios

Control Flow Diagram :

The state diagram for SensePi is given below:

As one can see, the default state when SensePi starts it's operation is Sensing. If someone presses button present on SensePi, it'll go into Advertising state. There it'll wit for 3 mins for the Connection request. If it receives a connection request before 3 mins over, it'll connect with the requesting device.

Once SensePi connects with some device, it can remain in this state for 10 mins. After 10 mins timeout will occur and it'll go back to Sensing state. If a user have proper mobile device and proper application to change the configuration, a user can do it in these 10 mins. And a user can also terminate the connection by sending disconnect request.

Data Flow Diagram :

This State machine implementation is done by building a firmware application in a modular structure. There are 3 supporting modules which handles different aspects of firmware. Those modules are :

  1. Bluetooth communication module
  1. Camera triggering module
  1. Data storage module

An interactions between these modules and application can be represented as follow :

In the context of this diagram communication means data transfer between any two modules. Here all the arrows represents a transfer of sensepi_config until specified otherwise. Data transfer happens bidirectionally between modules, except for 2 cases where it's unidirectional.

We can observe over there, all modules interact with the main application directly. Almost all the inter-module communication is done through the main application. Only once direct communication between two modules happens when Camera module saves latest configuration in Flash memory through Data storage module. Passing of new configuration from Bluetooth communication module to camera trigger module is also done through main application.

On system power reset or firmware update, main application will load the latest configuration present in flash memory through Data storage module and will pass it on to Camera trigger module.

Different Modules of application

1 Bluetooth communication module

This module handles Bluetooth communication. i.e. this module decides what kind of data is to be sent over Bluetooth. The structure which is to be used to send the data over Bluetooth is defined in this module. This module also handles how to serve different requests that it might get while using Bluetooth to communicate with other devices. One can refer to data structures over here.(link)

2 Camera triggering module

This module handles the operation of a device when it's in sensing state. This module handles the camera triggering related operations. This module uses the data sent by a mobile device to the Bluetooth communication module and does the required configurations. These configurations contain mode of operation (i.e. timer, motion or both), light conditions (i.e day, night or both), type of trigger (different modes in which cameras can be triggered).

3 Data storage Module

This module handles the storage if data such as current config and firmware id in permanent memory. This data enables the device to restore the configuration data after OTA update is done or if the power supply gets disturbed. This module uses flash memory to store the data

Macro Definition Documentation

◆ ADV_FAST_TICK_INTERVAL_MS

#define ADV_FAST_TICK_INTERVAL_MS   60

The fast tick interval in ms in the Advertising mode

Definition at line 112 of file main.c.

◆ ADV_SLOW_TICK_INTERVAL_MS

#define ADV_SLOW_TICK_INTERVAL_MS   1100

The slow tick interval in ms in the Advertising mode

Definition at line 114 of file main.c.

◆ APP_ADV_DATA

#define APP_ADV_DATA
Value:
{ \
0x02, BLE_GAP_AD_TYPE_FLAGS, BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE, \
sizeof(app_device_name) + 1, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, APP_DEVICE_NAME_CHAR, \
0x11, BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE, APP_UUID_COMPLETE \
}
#define APP_DEVICE_NAME_CHAR
Definition: main.c:68
#define APP_UUID_COMPLETE
Definition: main.c:73

The data to be sent in the advertising payload. It is of the format of a sequence of {Len, type, data}

Definition at line 77 of file main.c.

◆ APP_DEVICE_NAME_CHAR

#define APP_DEVICE_NAME_CHAR   'S','e','n','s','e','P','i'

< Name of device, to be included in the advertising data.

Definition at line 68 of file main.c.

◆ APP_SCAN_RSP_DATA

#define APP_SCAN_RSP_DATA
Value:
{ \
0x02, BLE_GAP_AD_TYPE_TX_POWER_LEVEL, 0 , \
0x11, BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, \
'x', 'x','x', 'x', 'x', 'x' , 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', \
0x04, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, 0 , 0 , 0 \
}

The data to be sent in the scan response payload. It is of the format of a sequence of {Len, type, data}

Definition at line 85 of file main.c.

◆ APP_UUID_COMPLETE

#define APP_UUID_COMPLETE   0x0a, 0xde, 0xfb, 0x07, 0x74, 0x83, 0x66, 0xb0, 0x0d, 0x48, 0xf5, 0x07, 0x50, 0xdc, 0x73, 0x3c

Complete 128 bit UUID of the SensePi service 3c73dc50-07f5-480d-b066-837407fbde0a

Definition at line 73 of file main.c.

◆ CONN_FAST_TICK_INTERVAL_MS

#define CONN_FAST_TICK_INTERVAL_MS   60

The fast tick interval in ms in the Connected mode

Definition at line 117 of file main.c.

◆ CONN_SLOW_TICK_INTERVAL_MS

#define CONN_SLOW_TICK_INTERVAL_MS   1100

The slow tick interval in ms in the Connected mode

Definition at line 119 of file main.c.

◆ CONN_TIMEOUT_MS

#define CONN_TIMEOUT_MS   (10*60*1000)

The time in ms (min*sec*ms) to timeout of the Connected mode

Definition at line 122 of file main.c.

◆ ENABLE_WDT

#define ENABLE_WDT   1

Flag to specify if the Watchdog timer is used or not

Definition at line 97 of file main.c.

◆ PIR_SENSE_INTERVAL_MS

#define PIR_SENSE_INTERVAL_MS   50

The interval at which the PIR sensor's signal is sampled. 20 Hz interval is chosen so that as per Nyquist's criterion signals up to 10 Hz can be sensed.

Definition at line 102 of file main.c.

◆ PIR_SENSE_THRESHOLD

#define PIR_SENSE_THRESHOLD   600

The static threshold above and below which PIR detects motion

Definition at line 104 of file main.c.

◆ SENSE_FAST_TICK_INTERVAL_MS

#define SENSE_FAST_TICK_INTERVAL_MS   60

The fast tick interval in ms in the Sense mode

Definition at line 107 of file main.c.

◆ SENSE_SLOW_TICK_INTERVAL_MS

#define SENSE_SLOW_TICK_INTERVAL_MS   300000

The slow tick interval in ms in the Sense mode

Definition at line 109 of file main.c.

◆ WDT_PERIOD_MS

#define WDT_PERIOD_MS   301000

The WDT bites if not fed every 301 sec (5 min)

Warning
All the tick intervals must be lower than this

Definition at line 94 of file main.c.

Enumeration Type Documentation

◆ sense_states

Defines the states possible in the SensePi device

Enumerator
SENSING 

Use PIR sensor to sense motion based on the set configuration.

ADVERTISING 

BLE advertising to get connected to an app.

CONNECTED 

BLE connection established with an app.

SENSING 

Use IR Tx-Rx to sense motion based on the set configuration.

ADVERTISING 

BLE advertising to get connected to an app.

CONNECTED 

BLE connection established with an app.

SENSING 

Use IR Tx-Rx to sense motion based on the set configuration.

ADVERTISING 

BLE advertising to get connected to an app.

CONNECTED 

BLE connection established with an app.

SENSING 

Use IR Tx-Rx to sense motion based on the set configuration.

ADVERTISING 

BLE advertising to get connected to an app.

CONNECTED 

BLE connection established with an app.

Definition at line 125 of file main.c.

Function Documentation

◆ button_handler()

void button_handler ( button_ui_steps  step,
button_ui_action  act 
)
Parameters
stepThe step reached by the button press
actThe action of the button press i.e. if a step is crossed or the button is released

Definition at line 378 of file main.c.

References BUTTON_UI_ACT_CROSS, button_ui_config_wake(), BUTTON_UI_STEP_WAKE, current_state, DEVICE_TICK_FAST, device_tick_switch_mode(), irq_msg_push(), and SENSING.

◆ load_last_config()

void load_last_config ( )

function to load previous sensebe configuration present in flash memory

Definition at line 502 of file main.c.

References sensepi_store_config_is_memory_empty(), and sensepi_store_config_write().

◆ next_interval_handler()

void next_interval_handler ( uint32_t  interval)
Parameters
intervalThe interval from the last call of this function

Definition at line 256 of file main.c.

References ADVERTISING, button_ui_add_tick(), CONNECTED, current_state, sensepi_cam_trigger_add_tick(), and SENSING.

◆ slumber()

void slumber ( void  )

Different calls to sleep depending on the status of Softdevice

Definition at line 514 of file main.c.

◆ state_change_handler()

void state_change_handler ( uint32_t  new_state)
Parameters
new_stateThe state to which the application has transitioned to.

Get config from sensepi_cam_trigger and send to the BLE module

Definition at line 287 of file main.c.

References current_state, device_tick_init(), DEVICE_TICK_SAME, led_ui_type_stop_all(), MS_TIMER_TICKS_MS, SENSE_FAST_TICK_INTERVAL_MS, SENSE_SLOW_TICK_INTERVAL_MS, and SENSING.

◆ wdt_prior_reset_callback()

void wdt_prior_reset_callback ( void  )

Function called just before reset due to WDT

Definition at line 180 of file main.c.

Variable Documentation

◆ current_state

sense_states current_state

Stores the current state of the device

Definition at line 137 of file main.c.

Referenced by button_handler(), next_interval_handler(), and state_change_handler().