Wirepas SDK
positioning_app/app.c
#define DEBUG_LOG_MODULE_NAME "POSAPP"
#ifdef DEBUG_POSLIB_LOG_MAX_LEVEL
#define DEBUG_LOG_MAX_LEVEL DEBUG_POSLIB_LOG_MAX_LEVEL
#else
#define DEBUG_LOG_MAX_LEVEL LVL_NOLOG
#endif
#include "debug_log.h"
#include "api.h"
#include "app_scheduler.h"
#include "poslib.h"
#include "stack_state.h"
#include "shared_data.h"
#include "shared_offline.h"
#include "shared_beacon.h"
#include "posapp_settings.h"
#include "board.h"
#include "led.h"
#include "gpio.h"
#include "voltage.h"
#ifdef MOTION_SUPPORTED
#include "motion.h"
#endif
#ifdef CONF_USE_PERSISTENT_MEMORY
#include "app_persistent.h"
#endif
#ifdef CONF_USE_LED_NOTIFICATION
#define LED_ON true
#define LED_OFF false
#define LED_ID 0
#endif
static uint8_t m_event_config_change_id = POSLIB_FLAG_EVENT_SUBSCRIBERS_MAX;
static uint8_t m_event_led_on_id = POSLIB_FLAG_EVENT_SUBSCRIBERS_MAX;
static uint8_t m_event_led_off_id = POSLIB_FLAG_EVENT_SUBSCRIBERS_MAX;
#ifdef BUTTON_ENABLED
static void button_pressed_cb(uint8_t pin, gpio_event_e event)
{
LOG(LVL_DEBUG, "Start oneshot. Button event %u button pin %u", event, pin);
}
static void enable_button(void)
{
gpio_res_e res;
uint8_t buttons_pins[] = BOARD_BUTTON_PIN_LIST;
gpio_pull_e pull = BOARD_BUTTON_INTERNAL_PULL ? GPIO_PULLUP: GPIO_PULLDOWN;
gpio_event_e event = BOARD_BUTTON_ACTIVE_LOW ? GPIO_EVENT_HL : GPIO_EVENT_LH;
#define DEBOUNCE_MS 100 //FixME: move to general definitions
#define BUTTON_ID 0 //FixME: move to general definitions
res = GPIO_register_for_event(buttons_pins[BUTTON_ID],
pull,
event,
DEBOUNCE_MS,
button_pressed_cb);
if (res == GPIO_RES_OK)
{
LOG(LVL_INFO, "Oneshot request active on button %u", BUTTON_ID);
}
else
{
LOG(LVL_ERROR, "Registration for button id: %u failed.", BUTTON_ID)
}
}
#else
static void enable_button(void) {}
#endif
#ifdef CONF_USE_PERSISTENT_MEMORY
static uint32_t App_persistent_data_write(void)
{
PosLib_getConfig(&settings);
PosApp_Settings_store(&settings);
}
#endif
#ifdef MOTION_SUPPORTED
static void motion_cb(poslib_motion_mode_e mode)
{
LOG(LVL_DEBUG, "Motion state: %u", mode);
}
bool update_motion(poslib_motion_mon_settings_t * motion)
{
if (motion->enabled)
{
posapp_motion_ret_e res_mon;
posapp_motion_mon_settings_t motion_cfg = {
.threshold_mg = motion->threshold_mg,
.duration_ms = motion->duration_ms,
.cb = motion_cb
};
res_mon = PosAppMotion_startMonitoring(&motion_cfg);
if (res_mon != POSAPP_MOTION_RET_OK)
{
LOG(LVL_ERROR, "Cannot activate motion monitoring! res: %u", res_mon);
return false;
}
}
else
{
PosAppMotion_stopMonitoring();
}
return true;
}
#endif
static void App_posLib_event(POSLIB_FLAG_EVENT_info_t * msg)
{
LOG(LVL_INFO, "PosLib event: %u, time: %u", msg->event_id, msg->time_hp)
if (msg->event_id & POSLIB_FLAG_EVENT_CONFIG_CHANGE)
{
PosLib_getConfig(&settings);
#ifdef MOTION_SUPPORTED
update_motion(&settings.motion);
#endif
#ifdef CONF_USE_PERSISTENT_MEMORY
#endif
}
}
static void App_posLib_led_on_event(POSLIB_FLAG_EVENT_info_t * msg)
{
LOG(LVL_INFO, "PosLib led on public event. Id: %u, time: %u", msg->event_id, msg->time_hp)
#ifdef CONF_USE_LED_NOTIFICATION
led_res_e res = Led_set(LED_ID, LED_ON);
if (res != LED_RES_OK)
{
LOG(LVL_ERROR, "Led set failed with led_id: %d", LED_ID)
}
#endif
}
static void App_posLib_led_off_event(POSLIB_FLAG_EVENT_info_t * msg)
{
LOG(LVL_INFO, "PosLib led off public event. Id: %u, time: %u", msg->event_id, msg->time_hp)
#ifdef CONF_USE_LED_NOTIFICATION
led_res_e res = Led_set(LED_ID, LED_OFF);
if (res != LED_RES_OK)
{
LOG(LVL_ERROR, "Led set failed with led_id: %d", LED_ID)
}
#endif
}
static poslib_ret_e App_start_positioning(void)
{
/* Retrieve the default | persistent settings */
PosApp_Settings_get(&settings);
res = PosLib_setConfig(&settings);
if (res != POS_RET_OK)
{
LOG(LVL_ERROR, "PosLib configuration fail. res: %d", res);
return res;
}
res = PosLib_eventRegister(event, App_posLib_event, &m_event_config_change_id);
if (res != POS_RET_OK)
{
LOG(LVL_ERROR, "PosLib cannot register event %u failed. res: %d", event, res);
return res;
}
res = PosLib_eventRegister(event_led_on, App_posLib_led_on_event, &m_event_led_on_id);
if (res != POS_RET_OK)
{
LOG(LVL_ERROR, "PosLib cannot register event %u failed. res: %d", event, res);
return res;
}
res = PosLib_eventRegister(event_led_off, App_posLib_led_off_event, &m_event_led_off_id);
if (res != POS_RET_OK)
{
LOG(LVL_ERROR, "PosLib cannot register event %u failed. res: %d", event, res);
return res;
}
#ifdef MOTION_SUPPORTED
update_motion(&settings.motion);
#endif
if (res !=POS_RET_OK)
{
LOG(LVL_ERROR, "PosLib start periodic failed. res: %d", res);
}
return res;
}
void App_init(const app_global_functions_t* functions)
{
PosApp_Settings_configureNode();
enable_button();
#ifdef CONF_USE_LED_NOTIFICATION
#endif
/* Initialize motion sensor if enabled */
#ifdef MOTION_SUPPORTED
PosAppMotion_init();
#endif
#ifdef CONF_VOLTAGE_REPORT
/* Initialize voltage measurement. */
#endif
// start the stack
lib_state->startStack();
//Start positioning
App_start_positioning();
}
poslib_motion_mon_settings_t
position library motion settings.
Definition: poslib.h:257
LVL_INFO
#define LVL_INFO
Definition: debug_log.h:83
Led_set
led_res_e Led_set(uint8_t led_id, bool state)
Turn the given LED on or off.
POSLIB_FLAG_EVENT_info_t
Definition: poslib.h:310
POSLIB_FLAG_EVENT_SUBSCRIBERS_MAX
#define POSLIB_FLAG_EVENT_SUBSCRIBERS_MAX
Definition: poslib.h:116
led_res_e
led_res_e
List of return code.
Definition: led.h:17
shared_neighbors.h
LVL_ERROR
#define LVL_ERROR
Definition: debug_log.h:85
PosLib_motion
poslib_ret_e PosLib_motion(poslib_motion_mode_e mode)
Used to indicate that the tag is static or dynamic. The update rate used will be adjusted according t...
APP_SCHEDULER_STOP_TASK
#define APP_SCHEDULER_STOP_TASK
Value to return from task to remove it.
Definition: app_scheduler.h:37
POSLIB_FLAG_EVENT_CONFIG_CHANGE
@ POSLIB_FLAG_EVENT_CONFIG_CHANGE
Definition: poslib.h:139
BOARD_BUTTON_PIN_LIST
#define BOARD_BUTTON_PIN_LIST
Definition: board.h:33
stack_state.h
POS_RET_OK
@ POS_RET_OK
Definition: poslib.h:40
LED_RES_OK
@ LED_RES_OK
Definition: led.h:20
App_Scheduler_addTask_execTime
app_scheduler_res_e App_Scheduler_addTask_execTime(task_cb_f cb, uint32_t delay_ms, uint32_t exec_time_us)
Add a task.
poslib_motion_mode_e
poslib_motion_mode_e
defines the position motion mode.
Definition: poslib.h:75
POSLIB_FLAG_EVENT_LED_OFF
@ POSLIB_FLAG_EVENT_LED_OFF
Definition: poslib.h:143
poslib_ret_e
poslib_ret_e
Return codes of positioning functions.
Definition: poslib.h:37
PosLib_startOneshot
poslib_ret_e PosLib_startOneshot(void)
Triggers one position update. If PosLib is started the scheduled operation will continue after the on...
PosLib_setConfig
poslib_ret_e PosLib_setConfig(poslib_settings_t *settings)
Sets PosLib configuration.
shared_offline.h
PosLib_getConfig
poslib_ret_e PosLib_getConfig(poslib_settings_t *settings)
Gets PosLib configuration.
BOARD_BUTTON_ACTIVE_LOW
#define BOARD_BUTTON_ACTIVE_LOW
Definition: board.h:36
poslib_events_e
poslib_events_e
Defines the events triggered by PosLib.
Definition: poslib.h:122
app_scheduler.h
app_global_functions_t
List of global functions, passed to App_entrypoint()
Definition: wms_app.h:157
poslib_settings_t::motion
poslib_motion_mon_settings_t motion
Definition: poslib.h:291
debug_log.h
app_persistent.h
led.h
Board-independent LED functions.
Mcu_voltageInit
void Mcu_voltageInit(void)
Initialize voltage measurement mechanisms. Need to do once before measurements.
shared_appconfig.h
PosLib_eventRegister
poslib_ret_e PosLib_eventRegister(poslib_events_e event, poslib_events_listen_info_f cb, uint8_t *id)
Register an event subscriber.
LVL_DEBUG
#define LVL_DEBUG
Macros to define several log levels: Debug, Info, Warning, Error.
Definition: debug_log.h:82
POSLIB_FLAG_EVENT_info_t::event_id
uint16_t event_id
Definition: poslib.h:312
BOARD_BUTTON_INTERNAL_PULL
#define BOARD_BUTTON_INTERNAL_PULL
Definition: board.h:39
POSLIB_FLAG_EVENT_info_t::time_hp
app_lib_time_timestamp_hp_t time_hp
Definition: poslib.h:313
shared_beacon.h
poslib_settings_t
position library settings.
Definition: poslib.h:276
Led_init
void Led_init(void)
Initialize Led module.
LOG_INIT
#define LOG_INIT()
Definition: debug_log.h:66
shared_data.h
PosLib_startPeriodic
poslib_ret_e PosLib_startPeriodic(void)
Start the positioning updates according to provided configuration. Calls PosLib initialization functi...
voltage.h
Voltage measurement examples for various processors.
LOG
#define LOG(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition: debug_log.h:172
poslib.h
poslib_motion_mon_settings_t::enabled
bool enabled
Definition: poslib.h:259
APP_SCHEDULER_SCHEDULE_ASAP
#define APP_SCHEDULER_SCHEDULE_ASAP
Value to return from task or as initial time to be executed ASAP.
Definition: app_scheduler.h:42
poslib_motion_mon_settings_t::threshold_mg
uint16_t threshold_mg
Definition: poslib.h:260
api.h
poslib_motion_mon_settings_t::duration_ms
uint16_t duration_ms
Definition: poslib.h:261
POSLIB_FLAG_EVENT_LED_ON
@ POSLIB_FLAG_EVENT_LED_ON
Definition: poslib.h:141