Commit e31366c9 authored by siguhe's avatar siguhe

Added timeout to module.

parent 206aeb6f
......@@ -115,7 +115,7 @@
#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (5 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
#define BUTTON_DETECTION_DELAY APP_TIMER_TICKS(4) /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
#define BUTTON_DETECTION_DELAY APP_TIMER_TICKS(1) /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
......@@ -152,7 +152,15 @@ extern const nrf_lcd_t nrf_lcd_st7735;
static const nrf_gfx_font_desc_t *p_font = &orkney_8ptFontInfo;
static const nrf_lcd_t *p_lcd = &nrf_lcd_st7735;
static const app_timer_id_t screen_timer_id;
static int state;
#define FAULT_STATE 0
#define ADVERTISING_STATE 1
#define NORMAL_STATE 2
#define SLEEP_STATE 3
APP_TIMER_DEF(screen_timer_id);
/**@brief Struct that contains pointers to the encoded advertising data. */
static ble_gap_adv_data_t m_adv_data =
......@@ -379,23 +387,23 @@ static void conn_params_init(void) {
/**@brief Function for starting advertising.
*/
static void advertising_start(void) {
ret_code_t err_code;
advertising_now = 1;
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
bsp_board_led_on(ADVERTISING_LED);
if(!advertising_now){
ret_code_t err_code;
advertising_now = 1;
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
}
}
/**@brief Function for stopping advertising.
*/
static void advertising_stop(void) {
ret_code_t err_code;
advertising_now = 0;
err_code = sd_ble_gap_adv_stop(m_adv_handle);
APP_ERROR_CHECK(err_code);
bsp_board_led_off(ADVERTISING_LED);
if(advertising_now){
ret_code_t err_code;
advertising_now = 0;
err_code = sd_ble_gap_adv_stop(m_adv_handle);
APP_ERROR_CHECK(err_code);
}
}
/**@brief Function for handling BLE events.
......@@ -433,12 +441,9 @@ static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) {
case BLE_GAP_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected");
//nrf_gpio_pin_clear(2);
bsp_board_led_off(CONNECTED_LED);
m_conn_handle = BLE_CONN_HANDLE_INVALID;
//err_code = app_button_disable();
//APP_ERROR_CHECK(err_code);
advertising_stop();
start_sleep_state();
break;
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
......@@ -520,79 +525,69 @@ static void ble_stack_init(void) {
*/
static void button_event_handler(uint8_t pin_no, uint8_t button_action) {
if (!advertising_now) {
nrf_gpio_pin_set(LCD_SCREEN_LED);
ret_code_t err_code;
switch (pin_no) {
case ENCODER1_A:
if (button_action) {
if (nrf_gpio_pin_read(ENCODER1_B)) {
counter_encoder_row+=1;
} else {
counter_encoder_row -= 1;
}
}
break;
case ENCODER2_A:
if (button_action) {
if (nrf_gpio_pin_read(ENCODER2_B)) {
counter_encoder_column += 1;
} else {
counter_encoder_column -= 1;
}
}
break;
case ENCODER3_A:
if (button_action) {
if (nrf_gpio_pin_read(ENCODER3_B)) {
if(counter_encoder_depth>=10){
counter_imaginary_shelf+=1;
if (pin_no==LEDBUTTON_BUTTON3) {
start_advertising_state();
}
else if(state!=ADVERTISING_STATE){
start_normal_state();
app_timer_stop(screen_timer_id);
app_timer_start(screen_timer_id,APP_TIMER_TICKS(10000), NULL);
ret_code_t err_code;
switch (pin_no) {
case ENCODER1_A:
if (button_action) {
if (nrf_gpio_pin_read(ENCODER1_B)) {
counter_encoder_row+=1;
} else {
counter_encoder_depth += 1;
counter_encoder_row -= 1;
}
}
else {
if(counter_encoder_depth<=0){
counter_imaginary_shelf-=1;
break;
case ENCODER2_A:
if (button_action) {
if (nrf_gpio_pin_read(ENCODER2_B)) {
counter_encoder_column += 1;
} else {
counter_encoder_depth -= 1;
counter_encoder_column -= 1;
}
}
}
break;
case LEDBUTTON_BUTTON3:
if (button_action) {
if (!advertising_now) {
advertising_start();
app_timer_start(screen_timer_id,1, NULL);
}
}
break;
break;
case ENCODER3_A:
if (button_action) {
if (nrf_gpio_pin_read(ENCODER3_B)) {
if(counter_encoder_depth>=10){
counter_imaginary_shelf+=1;
} else {
counter_encoder_depth += 1;
}
}
default:
APP_ERROR_HANDLER(pin_no);
break;
}
char char_arr1[4];
char char_arr2[4];
char char_arr3[4];
char char_arr4[4];
sprintf(char_arr1, "%d", counter_imaginary_shelf);
sprintf(char_arr2, "%d", counter_encoder_row);
sprintf(char_arr3, "%d", counter_encoder_column);
sprintf(char_arr4, "%d", counter_encoder_depth);
nrf_gfx_screen_fill_custom(p_lcd, GRAY);
text_print(&char_arr1, 100, 15);
text_print(&char_arr2, 100, 30);
text_print(&char_arr3, 100, 45);
text_print(&char_arr4, 100, 60);
} else {
screen_clear();
text_print("Sending to hub...", 2, 30);
text_print("Please wait.", 2, 45);
else {
if(counter_encoder_depth<=0){
counter_imaginary_shelf-=1;
} else {
counter_encoder_depth -= 1;
}
}
}
break;
default:
APP_ERROR_HANDLER(pin_no);
break;
}
char char_arr1[4];
char char_arr2[4];
char char_arr3[4];
char char_arr4[4];
sprintf(char_arr1, "%d", counter_imaginary_shelf);
sprintf(char_arr2, "%d", counter_encoder_row);
sprintf(char_arr3, "%d", counter_encoder_column);
sprintf(char_arr4, "%d", counter_encoder_depth);
nrf_gfx_screen_fill_custom(p_lcd, GRAY);
text_print(&char_arr1, 100, 15);
text_print(&char_arr2, 100, 30);
text_print(&char_arr3, 100, 45);
text_print(&char_arr4, 100, 60);
}
}
......@@ -742,10 +737,67 @@ static void rect_draw(void) {
nrf_gfx_rotation_set(p_lcd, NRF_LCD_ROTATE_0);
}
void start_fault_state(){
if(state!=FAULT_STATE){
state=FAULT_STATE;
advertising_stop();
screen_clear();
text_print("Error", 50, 15);
text_print("Please contact staff.", 2, 30);
nrf_gpio_pin_set(LCD_SCREEN_LED);
app_timer_stop(screen_timer_id);
app_timer_start(screen_timer_id,APP_TIMER_TICKS(10000), NULL);
}
}
void start_advertising_state(){
if(state!=ADVERTISING_STATE){
state=ADVERTISING_STATE;
advertising_start();
screen_clear();
text_print("Sending to hub...", 2, 30);
text_print("Please wait.", 2, 45);
nrf_gpio_pin_set(LCD_SCREEN_LED);
app_timer_stop(screen_timer_id);
app_timer_start(screen_timer_id,APP_TIMER_TICKS(10000), NULL);
void cancel_operations_on_timeout(){
nrf_gpio_pin_clear(LCD_SCREEN_LED);
advertising_stop();
}
}
void start_normal_state(){
if(state!=NORMAL_STATE){
advertising_stop();
state=NORMAL_STATE;
screen_clear();
text_print("POSTITION:", 2, 0);
text_print("HYLLE:", 2, 15);
text_print("RAD:", 2, 30);
text_print("KOLONNE:", 2, 45);
text_print("DYBDE:", 2, 60);
nrf_gpio_pin_set(LCD_SCREEN_LED);
counter_encoder_row = 0;
counter_encoder_column = 0;
counter_encoder_depth = 0;
counter_imaginary_shelf = 0;
app_timer_stop(screen_timer_id);
app_timer_start(screen_timer_id,APP_TIMER_TICKS(10000), NULL);
}
}
void start_sleep_state(){
if(state!=SLEEP_STATE){
state=SLEEP_STATE;
advertising_stop();
nrf_gpio_pin_clear(LCD_SCREEN_LED);
}
}
static void timer_timeout(void * p_context){
if(state==ADVERTISING_STATE){
start_fault_state();
} else {
start_sleep_state();
}
}
/**@brief Function for application main entry.
......@@ -775,16 +827,11 @@ int main(void) {
nrf_gpio_cfg_output(LCD_SCREEN_DC);
nrf_gpio_pin_set(LCD_SCREEN_DC);
gfx_initialization();
app_timer_create(&screen_timer_id,APP_TIMER_MODE_SINGLE_SHOT,timer_timeout);
// Set up screen default text
screen_clear();
text_print("POSTITION:", 2, 0);
text_print("HYLLE:", 2, 15);
text_print("RAD:", 2, 30);
text_print("KOLONNE:", 2, 45);
text_print("DYBDE:", 2, 60);
app_timer_create(screen_timer_id,APP_TIMER_MODE_SINGLE_SHOT, &cancel_operations_on_timeout);
nrf_gpio_pin_set(LCD_SCREEN_LED);
start_sleep_state();
// Enter main loop.
for (;;) {
......
......@@ -58,7 +58,7 @@
<Files>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="175" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/ble/nrf_ble_qwr/nrf_ble_qwr.c" selected="0" top="159" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="0" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/softdevice/s132/headers/ble.h" selected="0" top="0" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="569" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../main.c" selected="1" top="527" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="2" y="833" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../main.c" selected="1" top="772" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="1845" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/softdevice/s132/headers/ble_gap.h" selected="0" top="1819" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="124" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/ble/ble_services/ble_lbs/ble_lbs.c" selected="0" top="116" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="154" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/boards/boards.h" selected="0" top="142" codecName="Default"/>
......@@ -70,8 +70,8 @@
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="118" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../integration/nrfx/legacy/nrf_drv_spi.h" selected="0" top="110" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="313" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/drivers_ext/st7735/st7735.c" selected="0" top="299" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="5435" useTextEdit="1" useBinaryEdit="0" left="0" path="../config/sdk_config.h" selected="0" top="5426" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="60" y="130" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/libraries/timer/app_timer.h" selected="0" top="117" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="0" y="1011" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/libraries/timer/app_timer.c" selected="0" top="985" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="8" y="118" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/libraries/timer/app_timer.h" selected="0" top="110" codecName="Default"/>
<SessionOpenFile windowGroup="DockEditLeft" x="11" y="987" useTextEdit="1" useBinaryEdit="0" left="0" path="../../../../../../components/libraries/timer/app_timer.c" selected="0" top="1058" codecName="Default"/>
</Files>
<ARMCrossStudioWindow activeProject="ble_app_blinky_pca10040_s132" fileDialogDefaultFilter="*.c" autoConnectTarget="J-Link" buildConfiguration="Release" debugSearchFileMap="" fileDialogInitialDirectory="/home/bruk/nRF5_SDK_15.3.0_59ac345/integration/nrfx/legacy" debugSearchPath="" autoConnectCapabilities="3711"/>
</session>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment