diff --git a/pyaci/aci/AciCommand.py b/pyaci/aci/AciCommand.py index b1188d6..04b6f83 100644 --- a/pyaci/aci/AciCommand.py +++ b/pyaci/aci/AciCommand.py @@ -48,7 +48,7 @@ def serialize(self): return pkt def __repr__(self): - return str.format("I am %s and my Lenght is %d, OpCode is 0x%02x and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, self.Data)) + return str.format("I am %s and my Length is %d, OpCode is 0x%02x and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, self.Data)) class AciEcho(AciCommandPkt): OpCode = 0x02 diff --git a/pyaci/aci/AciEvent.py b/pyaci/aci/AciEvent.py index dc9697a..b7526cd 100644 --- a/pyaci/aci/AciEvent.py +++ b/pyaci/aci/AciEvent.py @@ -57,7 +57,7 @@ def __init__(self, pkt): logging.error('Packet size must be > 1, packet contents: %s', str(pkt)) def __repr__(self): - return str.format("I am %s and my Lenght is %d, OpCode is 0x%02x and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, self.Data)) + return str.format("I am %s and my Length is %d, OpCode is 0x%02x and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, self.Data)) class AciDeviceStarted(AciEventPkt): #OpCode = 0x81 @@ -71,7 +71,7 @@ def __init__(self,pkt): self.DataCreditAvailable = pkt[4] def __repr__(self): - return str.format("I am %s and my Lenght is %d, OpCode is 0x%02x, OperatingMode is 0x%02x, HWError is 0x%02x, and DataCreditAvailable is 0x%02x" %(self.__class__.__name__, self.Len, self.OpCode, self.OperatingMode, self.HWError, self.DataCreditAvailable)) + return str.format("I am %s and my Length is %d, OpCode is 0x%02x, OperatingMode is 0x%02x, HWError is 0x%02x, and DataCreditAvailable is 0x%02x" %(self.__class__.__name__, self.Len, self.OpCode, self.OperatingMode, self.HWError, self.DataCreditAvailable)) class AciEchoRsp(AciEventPkt): #OpCode = 0x82 @@ -90,7 +90,7 @@ def __init__(self,pkt): self.Data = pkt[4:] def __repr__(self): - return str.format("I am %s and my Lenght is %d, OpCode is 0x%02x, CommandOpCode is %s, StatusCode is %s, and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, AciCommand.AciCommandLookUp(self.CommandOpCode), AciStatusLookUp(self.StatusCode), self.Data)) + return str.format("I am %s and my Length is %d, OpCode is 0x%02x, CommandOpCode is %s, StatusCode is %s, and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, AciCommand.AciCommandLookUp(self.CommandOpCode), AciStatusLookUp(self.StatusCode), self.Data)) class AciEventNew(AciEventPkt): #OpCode = 0xB3 @@ -103,7 +103,7 @@ def __init__(self,pkt): self.Data = pkt[3:] def __repr__(self): - return str.format("I am %s and my Lenght is %d, OpCode is 0x%02x, ValueHandle is 0x%04x, and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, self.ValueHandle, self.Data)) + return str.format("I am %s and my Length is %d, OpCode is 0x%02x, ValueHandle is 0x%04x, and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, self.ValueHandle, self.Data)) class AciEventUpdate(AciEventNew): #OpCode = 0xB4 diff --git a/pyaci/interactive_console.py b/pyaci/interactive_console.py index 2cb7159..3c4ea82 100644 --- a/pyaci/interactive_console.py +++ b/pyaci/interactive_console.py @@ -4,6 +4,8 @@ from traitlets import config from aci import AciCommand from aci_serial import AciUart +import time +import sensei_cmd class Interactive(object): def __init__(self, acidev): @@ -25,7 +27,7 @@ def Echo(self, Data): def RadioReset(self): self.acidev.write_aci_cmd(AciCommand.AciRadioReset()) - def AppCommand(self, data=[], length=1): + def AppCommand(self, data=[]): self.acidev.write_aci_cmd(AciCommand.AciAppCommand(data=data,length=len(data)+1)) def Init(self, AccessAddress=0x8E89BED6, MinInterval=100, Channel=39): @@ -70,6 +72,14 @@ def ChannelGet(self): def MinIntervalGet(self): self.acidev.write_aci_cmd(AciCommand.AciIntervalMinMsGet()) + # Experimental: to be removed + def runCommand(self, command): + print("Running + {data}".format(data=command.serialize())) + self.AppCommand(command.serialize()) + + def setTime(self): + self.runCommand(sensei_cmd.SetTime()) + def get_ipython_config(device): # import os, sys, IPython diff --git a/src/app_cmd.c b/src/app_cmd.c index f64e6fe..dc6918f 100644 --- a/src/app_cmd.c +++ b/src/app_cmd.c @@ -15,15 +15,11 @@ uint16_t app_cmd_handler(uint8_t *data, uint8_t len, uint8_t *response, uint8_t { set_epoch_time(cmd->params.set_epoch_time.epoch, cmd->params.set_epoch_time.ms); error_code = NRF_SUCCESS; + *response_length = 0; } break; default: error_code = NRF_ERROR_NOT_SUPPORTED; } - - toggle_led(LED_RED); - response[0] = 0xfa; - response[1] = 0xba; - *response_length = 2; return error_code; } diff --git a/src/epoch.c b/src/epoch.c index a917aa4..4ba15b7 100644 --- a/src/epoch.c +++ b/src/epoch.c @@ -1,10 +1,39 @@ #include "epoch.h" +#include "leds.h" +#include "sensor.h" +#include "app_timer.h" + +// Timer settings +#define APP_TIMER_PRESCALER 63 // Wraparound value -1 +#define APP_TIMER_MAX_TIMERS 1 +#define APP_TIMER_OP_QUEUE_SIZE 1 + static int32_t epoch_offset; +static bool started_timer = false; +static app_timer_id_t timer_ID; + + +static void timeout_cb(void * p_context) +{ + sensor_update(); + toggle_led(LED_RED); +} + +static void start_timer() { + started_timer = true; + APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false); + app_timer_create(&timer_ID, APP_TIMER_MODE_REPEATED, timeout_cb); + app_timer_start(timer_ID, 512, NULL); +} void set_epoch_time(int32_t epoch, uint16_t ms) { epoch_offset = epoch; //uint16_t sleep_time = 1000 - ms; // Need to wake in sleep time ms + // then start 1s timer + if (!started_timer) { + start_timer(); + } } diff --git a/src/main.c b/src/main.c index 45f235c..df65486 100644 --- a/src/main.c +++ b/src/main.c @@ -1,32 +1,4 @@ -/*********************************************************************************** -Copyright (c) Nordic Semiconductor ASA -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright notice, this - list of conditions and the following disclaimer in the documentation and/or - other materials provided with the distribution. - - 3. Neither the name of Nordic Semiconductor ASA nor the names of other - contributors to this software may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -************************************************************************************/ + #include "rbc_mesh.h" #include "mesh_aci.h" @@ -36,7 +8,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "nrf_gpio.h" #include "boards.h" #include "leds.h" -#include "app_timer.h" #include "pstorage_platform.h" #include "app_cmd.h" #include "config.h" @@ -53,20 +24,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define MESH_CLOCK_SRC (NRF_CLOCK_LFCLKSRC_XTAL_75_PPM) -// Timer settings -#define APP_TIMER_PRESCALER 327 -#define APP_TIMER_MAX_TIMERS 1 -#define APP_TIMER_OP_QUEUE_SIZE 1 - -static app_timer_id_t timer_ID; - - -void timeOut(void * p_context) -{ - sensor_update(); - //toggle_led(LED_RED); -} - /** @brief General error handler. */ static inline void error_loop(void) { @@ -92,7 +49,7 @@ void sd_assert_handler(uint32_t pc, uint16_t line_num, const uint8_t* p_file_nam /** @brief Hardware fault handler. */ void HardFault_Handler(void) { - error_loop(); + error_loop(); } /** @@ -102,27 +59,27 @@ void HardFault_Handler(void) */ static void rbc_mesh_event_handler(rbc_mesh_event_t* p_evt) { - toggle_led(LED_GREEN); + toggle_led(LED_GREEN); - switch (p_evt->type) - { - case RBC_MESH_EVENT_TYPE_CONFLICTING_VAL: - case RBC_MESH_EVENT_TYPE_NEW_VAL: - case RBC_MESH_EVENT_TYPE_UPDATE_VAL: - if (p_evt->params.rx.value_handle == 1) { - led_config(LED_BLUE, p_evt->params.rx.p_data[0]); - } - break; - case RBC_MESH_EVENT_TYPE_TX: - case RBC_MESH_EVENT_TYPE_INITIALIZED: - case RBC_MESH_EVENT_TYPE_DFU_NEW_FW_AVAILABLE: - case RBC_MESH_EVENT_TYPE_DFU_RELAY_REQ: - case RBC_MESH_EVENT_TYPE_DFU_SOURCE_REQ: - case RBC_MESH_EVENT_TYPE_DFU_START: - case RBC_MESH_EVENT_TYPE_DFU_END: - case RBC_MESH_EVENT_TYPE_DFU_BANK_AVAILABLE: - break; - } + switch (p_evt->type) + { + case RBC_MESH_EVENT_TYPE_CONFLICTING_VAL: + case RBC_MESH_EVENT_TYPE_NEW_VAL: + case RBC_MESH_EVENT_TYPE_UPDATE_VAL: + if (p_evt->params.rx.value_handle == 1) { + led_config(LED_BLUE, p_evt->params.rx.p_data[0]); + } + break; + case RBC_MESH_EVENT_TYPE_TX: + case RBC_MESH_EVENT_TYPE_INITIALIZED: + case RBC_MESH_EVENT_TYPE_DFU_NEW_FW_AVAILABLE: + case RBC_MESH_EVENT_TYPE_DFU_RELAY_REQ: + case RBC_MESH_EVENT_TYPE_DFU_SOURCE_REQ: + case RBC_MESH_EVENT_TYPE_DFU_START: + case RBC_MESH_EVENT_TYPE_DFU_END: + case RBC_MESH_EVENT_TYPE_DFU_BANK_AVAILABLE: + break; + } } /* dispatch system events to interested modules. */ @@ -143,78 +100,82 @@ void clock_initialization() { // Do nothing. } + + NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; } int main(void) { - nrf_gpio_cfg_input(BUTTON_1, NRF_GPIO_PIN_PULLDOWN); - nrf_gpio_cfg_input(BUTTON_2, NRF_GPIO_PIN_PULLDOWN); + //clock_initialization(); + + nrf_gpio_cfg_input(BUTTON_1, NRF_GPIO_PIN_PULLDOWN); + nrf_gpio_cfg_input(BUTTON_2, NRF_GPIO_PIN_PULLDOWN); - /* Enable Softdevice (including sd_ble before framework */ - SOFTDEVICE_HANDLER_INIT(MESH_CLOCK_SRC, NULL); - softdevice_ble_evt_handler_set(rbc_mesh_ble_evt_handler); + /* Enable Softdevice (including sd_ble before framework */ + SOFTDEVICE_HANDLER_INIT(MESH_CLOCK_SRC, NULL); + softdevice_ble_evt_handler_set(rbc_mesh_ble_evt_handler); - // Register with the SoftDevice handler module for system events. - softdevice_sys_evt_handler_set(sys_evt_dispatch); + // Register with the SoftDevice handler module for system events. + softdevice_sys_evt_handler_set(sys_evt_dispatch); - LEDS_CONFIGURE(LEDS_MASK); + LEDS_CONFIGURE(LEDS_MASK); - /* Initialize mesh. */ - rbc_mesh_init_params_t init_params; - init_params.access_addr = MESH_ACCESS_ADDR; - init_params.interval_min_ms = MESH_INTERVAL_MIN_MS; - init_params.channel = MESH_CHANNEL; - init_params.lfclksrc = MESH_CLOCK_SRC; - init_params.tx_power = RBC_MESH_TXPOWER_0dBm; + // if (NRF_CLOCK->LFCLKSRC == (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos)) { + // toggle_led(LED_GREEN); + // } - uint32_t error_code; - error_code = rbc_mesh_init(init_params); - APP_ERROR_CHECK(error_code); + /* Initialize mesh. */ + rbc_mesh_init_params_t init_params; + init_params.access_addr = MESH_ACCESS_ADDR; + init_params.interval_min_ms = MESH_INTERVAL_MIN_MS; + init_params.channel = MESH_CHANNEL; + init_params.lfclksrc = MESH_CLOCK_SRC; + init_params.tx_power = RBC_MESH_TXPOWER_0dBm; - config_init(); + uint32_t error_code; + error_code = rbc_mesh_init(init_params); + APP_ERROR_CHECK(error_code); - /* Initialize serial ACI */ + config_init(); + + /* Initialize serial ACI */ #ifdef RBC_MESH_SERIAL - mesh_aci_init(); - mesh_aci_app_cmd_handler_set(app_cmd_handler); + mesh_aci_init(); + mesh_aci_app_cmd_handler_set(app_cmd_handler); #endif - /* Enable handle 1 */ - error_code = rbc_mesh_value_enable(1); - APP_ERROR_CHECK(error_code); + /* Enable handle 1 */ + error_code = rbc_mesh_value_enable(1); + APP_ERROR_CHECK(error_code); - APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false); - app_timer_create(&timer_ID, APP_TIMER_MODE_REPEATED, timeOut); - app_timer_start(timer_ID, 100, NULL); + rbc_mesh_event_t evt; - rbc_mesh_event_t evt; + while (true) + { - while (true) + for (uint32_t pin = BUTTON_START; pin <= BUTTON_STOP; ++pin) { - - for (uint32_t pin = BUTTON_START; pin <= BUTTON_STOP; ++pin) + if(nrf_gpio_pin_read(pin) == 1) { - if(nrf_gpio_pin_read(pin) == 1) - { - while(nrf_gpio_pin_read(pin) == 1); - uint8_t mesh_data[1]; - uint32_t led_status = !!((pin - BUTTON_START) & 0x01); /* even buttons are OFF, odd buttons are ON */ - - mesh_data[0] = led_status; - if (rbc_mesh_value_set(1, mesh_data, 1) == NRF_SUCCESS) - { - led_config(LED_BLUE, led_status); - } - } - } - - if (rbc_mesh_event_get(&evt) == NRF_SUCCESS) - { - rbc_mesh_event_handler(&evt); - rbc_mesh_event_release(&evt); + while(nrf_gpio_pin_read(pin) == 1); + uint8_t mesh_data[1]; + uint32_t led_status = !!((pin - BUTTON_START) & 0x01); /* even buttons are OFF, odd buttons are ON */ + + mesh_data[0] = led_status; + if (rbc_mesh_value_set(1, mesh_data, 1) == NRF_SUCCESS) + { + led_config(LED_BLUE, led_status); + } } + } - sd_app_evt_wait(); + if (rbc_mesh_event_get(&evt) == NRF_SUCCESS) + { + rbc_mesh_event_handler(&evt); + rbc_mesh_event_release(&evt); } + + sd_app_evt_wait(); + } }