Commit e31366c9 authored by siguhe's avatar siguhe
Browse files

Added timeout to module.

parent 206aeb6f
Loading
Loading
Loading
Loading
+144 −97
Original line number Diff line number Diff line
@@ -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) {
  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);

  bsp_board_led_on(ADVERTISING_LED);
  }
}

/**@brief Function for stopping advertising.
 */
static void advertising_stop(void) {
  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);

  bsp_board_led_off(ADVERTISING_LED);
   }
}

/**@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,8 +525,13 @@ 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);
  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:
@@ -542,7 +552,6 @@ static void button_event_handler(uint8_t pin_no, uint8_t button_action) {
            }
          }
          break;

        case ENCODER3_A:
          if (button_action) {
            if (nrf_gpio_pin_read(ENCODER3_B)) {
@@ -562,16 +571,6 @@ static void button_event_handler(uint8_t pin_no, uint8_t button_action) {
            }
          }
          break;

      case LEDBUTTON_BUTTON3:
        if (button_action) {
          if (!advertising_now) {
            advertising_start();
            app_timer_start(screen_timer_id,1, NULL);
          }
        }
        break;

        default:
          APP_ERROR_HANDLER(pin_no);
          break;
@@ -589,10 +588,6 @@ static void button_event_handler(uint8_t pin_no, uint8_t button_action) {
      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);
  }
}

@@ -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 cancel_operations_on_timeout(){
    nrf_gpio_pin_clear(LCD_SCREEN_LED);
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 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 (;;) {

+3.71 KiB (965 KiB)

File changed.

No diff preview for this file type.

Loading