Skip to content

Commit

Permalink
Add sleep_enabled flag to config, and fix subsequent writing of confi…
Browse files Browse the repository at this point in the history
…g to flash
  • Loading branch information
ps2 committed Apr 27, 2017
1 parent 9a214f8 commit 5901461
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ RBC_MESH := rbc_mesh
LINKER_SCRIPT := $(SIMBLEE_BASE)/variants/Simblee/linker_scripts/gcc/Simblee.ld
RFD_LOADER := $(SIMBLEE_BASE)/RFDLoader_osx
#SERIAL_PORT := /dev/cu.usbserial-DN00D34P # left
#SERIAL_PORT := /dev/cu.usbserial-DN00CSZ7 # right
SERIAL_PORT := /dev/cu.usbserial-DN00CSZ7 # right
#SERIAL_PORT := /dev/cu.usbserial-A105RB12
#SERIAL_PORT := /dev/cu.usbserial-FTZ86FTC # tag-connect
SERIAL_PORT := /dev/cu.usbserial-DO00C2G2 # Breadboard setup
#SERIAL_PORT := /dev/cu.usbserial-DO00C2G2 # Breadboard setup

ifeq ($(USE_RBC_MESH_SERIAL), "yes")
SERIAL_STRING := "_serial"
Expand Down
4 changes: 2 additions & 2 deletions pyaci/interactive_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def runCommand(self, command):
def setTime(self):
self.runCommand(sensei_cmd.SetTime())

def setConfig(self, sensor_id, serial_enabled, mesh_channel):
self.runCommand(sensei_cmd.SetConfig(sensor_id, serial_enabled, mesh_channel))
def setConfig(self, sensor_id, serial_enabled, mesh_channel, sleep_enabled):
self.runCommand(sensei_cmd.SetConfig(sensor_id, serial_enabled, mesh_channel, sleep_enabled))

def getConfig(self):
self.runCommand(sensei_cmd.GetConfig())
Expand Down
8 changes: 5 additions & 3 deletions pyaci/sensei_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ def serialize(self):
class SetConfig(object):
OpCode = 0x03

def __init__(self, sensor_id=0, serial_enabled=False, mesh_channel=38):
def __init__(self, sensor_id=0, serial_enabled=False, mesh_channel=38, sleep_enabled=True):
self.sensor_id = sensor_id
self.serial_enabled = serial_enabled
self.mesh_channel=mesh_channel
self.mesh_channel = mesh_channel
self.sleep_enabled = sleep_enabled


def serialize(self):
return struct.pack("BBBB", self.OpCode, self.sensor_id, self.serial_enabled, self.mesh_channel)
return struct.pack("BBBBB", self.OpCode, self.sensor_id, self.serial_enabled, self.mesh_channel, self.sleep_enabled)

class GetConfig(object):
OpCode = 0x04
Expand Down
21 changes: 17 additions & 4 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@
#define APP_CONFIG_BLOCK 0
#define APP_CONFIG_OFFSET 0

#define APP_CONFIG_STORAGE_SIZE ((sizeof(app_config_t)/16 + 1) * 16)

static pstorage_handle_t m_storage_handle;
static app_config_t m_config;
static bool m_loaded = false;

static void m_storage_callback(pstorage_handle_t *p_handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len) {
if (op_code == PSTORAGE_STORE_OP_CODE && result == NRF_SUCCESS) {
toggle_led(LED_GREEN);
static pstorage_handle_t block;
switch (op_code) {
case PSTORAGE_STORE_OP_CODE:
if (result == NRF_SUCCESS) {
toggle_led(LED_BLUE);
}
break;
case PSTORAGE_CLEAR_OP_CODE:
// After clearing, we write the config
if (pstorage_block_identifier_get(&m_storage_handle, APP_CONFIG_BLOCK, &block) == NRF_SUCCESS) {
pstorage_store(&block, (uint8_t*)&m_config, APP_CONFIG_STORAGE_SIZE, APP_CONFIG_OFFSET);
}
break;
}
}

Expand All @@ -41,6 +54,7 @@ static void init_config_to_defaults() {
m_config.sensor_id = 0;
m_config.serial_enabled = 1;
m_config.mesh_channel = 38;
m_config.sleep_enabled = 1;
}

static bool ensure_config_loaded() {
Expand Down Expand Up @@ -78,8 +92,7 @@ uint32_t set_config(app_config_t *config) {
if (error_code != NRF_SUCCESS) {
return error_code;
}
// Size must be word-aligned
error_code = pstorage_store(&block, (uint8_t*)&m_config, ((sizeof(app_config_t)/16 + 1) * 16), APP_CONFIG_OFFSET);
error_code = pstorage_clear(&block, APP_CONFIG_STORAGE_SIZE);
return error_code;
}

Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef __packed_armcc struct
uint8_t sensor_id;
uint8_t serial_enabled;
uint8_t mesh_channel;
uint8_t sleep_enabled;
} __packed_gcc app_config_t;

// Returns true on success
Expand Down
1 change: 1 addition & 0 deletions src/heartbeat.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void send_heartbeat_packet(uint8_t sensor_id, uint32_t epoch_seconds, uint16_t e
void received_heartbeat(heartbeat_ad_t *p_heartbeat_ad, uint8_t rssi) {
app_evt_t event;
set_clock_time(p_heartbeat_ad->epoch_seconds, p_heartbeat_ad->epoch_ms, CLOCK_SOURCE_RF, p_heartbeat_ad->clock_version);
DBG_TICK_PIN(6);
proximity_add_entry(p_heartbeat_ad->sensor_id, rssi);

event.opcode = APP_EVT_OPCODE_HEARTBEAT;
Expand Down
2 changes: 2 additions & 0 deletions src/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define LED_BLUE 2

#define DBG_TICK_PIN(x) NRF_GPIO->OUTSET = (1 << (x)); \
__NOP();\
__NOP();\
__NOP();\
__NOP();\
NRF_GPIO->OUTCLR = (1 << (x))
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ int main(void)
tc_radio_params_set(MESH_ACCESS_ADDR, app_config.mesh_channel);
}

scheduler_init(); // Initializes, but does not start, timer
scheduler_init(app_config.sleep_enabled); // Initializes, but does not start, clock

heartbeat_init(); // Inits structures for sending heartbeat

/* Initialize serial ACI */
Expand Down
13 changes: 8 additions & 5 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ static app_timer_id_t m_clock_sync_timer_ID;
static app_timer_id_t m_offset_timer_ID;
static app_timer_id_t m_periodic_timer_ID;
static scheduler_state_t m_scheduler_state;
static bool m_sleep_enabled = true;
static prng_t m_rand;
static uint32_t m_clock_second_start_counter_value;


#define MS_TO_TICKS(MS) ((TICKS_PER_100ms * (MS)) / 100)
#define TICKS_TO_MS(TICKS) (100 * (TICKS) / TICKS_PER_100ms)

Expand All @@ -50,10 +50,12 @@ static void report_debug_register() {
static void periodic_timer_cb(void * p_context)
{
m_current_time += 1;
DBG_TICK_PIN(6);


if (m_current_time % 10 == 0) {
//report_debug_register();
led_config(LED_GREEN, 1);
//led_config(LED_GREEN, 1);
app_timer_cnt_get(&m_clock_second_start_counter_value);
m_scheduler_state = SCHEDULER_STATE_BEFORE_HB;
rbc_mesh_start();
Expand Down Expand Up @@ -110,7 +112,7 @@ static void delay_to_sleep() {

static void do_sleep() {
rbc_mesh_stop();
led_config(LED_GREEN, 0);
//led_config(LED_GREEN, 0);
}

static void offset_timer_cb(void * p_context) {
Expand All @@ -128,7 +130,7 @@ static void offset_timer_cb(void * p_context) {
delay_to_sleep();
break;
case SCHEDULER_STATE_REPORTING:
if (clock_is_synchronized()) {
if (m_sleep_enabled && clock_is_synchronized()) {
do_sleep();
}
m_scheduler_state = SCHEDULER_STATE_SLEEP;
Expand Down Expand Up @@ -158,7 +160,8 @@ void start_clock(uint16_t start_delay) {
}
}

void scheduler_init() {
void scheduler_init(bool sleep_enabled) {
m_sleep_enabled = sleep_enabled;
rand_prng_seed(&m_rand);
m_scheduler_state = SCHEDULER_STATE_STOPPED;
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef enum {
#define HEARTBEAT_WINDOW_MS (40)
#define TOTAL_RADIO_WINDOW_MS (900)

void scheduler_init();
void scheduler_init(bool sleep_enabled);

void start_clock(uint16_t start_delay);

Expand Down

0 comments on commit 5901461

Please sign in to comment.