Commit c0c27bd9 authored by unknown's avatar unknown
Browse files

Cleaned up code

parent e4659eb4
Loading
Loading
Loading
Loading
+81 −67
Original line number Diff line number Diff line
@@ -82,23 +82,26 @@
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

/* Defines exclusive per PCB ---------------------------------------------------------------- */
#define SECTION_NUMBER 8
#define DEVICE_NAME "NeokompSeksjon8" /**< Name of device. Will be included in the advertising data. */

/*----------------------------------------------------------------*/

#define NOTIFICATION_LED 6 /**< LED to be toggled with the help of the LED Button Service. */
#define LEDBUTTON_BUTTON0 BSP_BUTTON_0 /**< Button that will trigger the notification event with the LED Button Service */
#define LEDBUTTON_BUTTON1 BSP_BUTTON_1 /**< Button that will trigger the notification event with the LED Button Service */
#define LEDBUTTON_BUTTON2 BSP_BUTTON_2 /**< Button that will trigger the notification event with the LED Button Service */
#define LEDBUTTON_BUTTON3 31           /**< Button that will trigger the notification event with the LED Button Service */
#define BUTTON1 31         /**< Button that will trigger the notification event with the LED Button Service */
#define ENCODER1_A 25
#define ENCODER1_B 26
#define ENCODER2_A 27
#define ENCODER2_B 28
#define ENCODER3_A 29
#define ENCODER3_B 30
#define ENCODER4_A 31
#define ENCODER4_B 32

#define LCD_SCREEN_LED 2
#define LCD_SCREEN_DC 0

#define SECTION_NUMBER 8
#define DEVICE_NAME "Testurd"                            /**< Name of device. Will be included in the advertising data. */
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */

#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
@@ -116,7 +119,9 @@
#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(1) /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
#define BUTTON_DETECTION_DELAY_MS APP_TIMER_TICKS(1) /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
#define SLEEP_DELAY_MS APP_TIMER_TICKS(60000)        //60 Seconds
#define SEND_TIMEOUT_MS APP_TIMER_TICKS(10000)       //10 Seconds

#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */

@@ -133,14 +138,10 @@
static uint32_t background_color = WHITE;

#define MAX_SCROLL 20 //Max value of both shelf, row, column anbd depth

#define LINE_STEP 10

#define CIRCLE_RADIUS 10
#define CIRCLE_STEP ((2 * CIRCLE_RADIUS) + 1)

#define BORDER 2

BLE_LBS_DEF(m_lbs);                               /**< LED Button Service instance. */
NRF_BLE_GATT_DEF(m_gatt);                         /**< GATT module instance. */
NRF_BLE_QWR_DEF(m_qwr);                           /**< Context for the Queued Write module.*/
@@ -177,10 +178,12 @@ static int state;
#define NORMAL_STATE 2
#define SLEEP_STATE 3

//Forward declaration
//Forward declarations
void text_print_large(char *test_text, uint16_t x, uint16_t y, uint16_t color, uint8_t side);
void start_sleep_state();

APP_TIMER_DEF(screen_timer_id);
APP_TIMER_DEF(send_timer_id);

/**@brief Struct that contains pointers to the encoded advertising data. */
static ble_gap_adv_data_t m_adv_data =
@@ -219,6 +222,8 @@ static void leds_init(void) {
  bsp_board_init(BSP_INIT_LEDS);
}

static void timer_timeout(void *p_context); //Forward declaration

/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module.
@@ -227,6 +232,8 @@ static void timers_init(void) {
  // Initialize timer module, making it use the scheduler
  ret_code_t err_code = app_timer_init();
  APP_ERROR_CHECK(err_code);
  app_timer_create(&screen_timer_id, APP_TIMER_MODE_SINGLE_SHOT, timer_timeout);
  app_timer_create(&send_timer_id, APP_TIMER_MODE_SINGLE_SHOT, timer_timeout);
}

/**@brief Function for the GAP initialization.
@@ -351,7 +358,6 @@ static void nus_data_handler(ble_nus_evt_t *p_evt) {
  switch (p_evt->type) {
  case BLE_NUS_EVT_RX_DATA:


    NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
    NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);

@@ -380,7 +386,6 @@ static void nus_data_handler(ble_nus_evt_t *p_evt) {
    nrf_gpio_pin_set(NOTIFICATION_LED); //Will send on notification enable.
    send_nus();
    break;

  }
}

@@ -417,7 +422,6 @@ static void services_init(void) {

  nus_init.data_handler = nus_data_handler;


  err_code = ble_nus_init(&m_nus, &nus_init);
  APP_ERROR_CHECK(err_code);
}
@@ -504,7 +508,6 @@ static void advertising_stop(void) {
  }
}


/**@brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
@@ -640,12 +643,12 @@ void int_print_large(int integer, uint16_t x, uint16_t y, uint16_t color, uint8_

static void button_event_handler(uint8_t pin_no, uint8_t button_action) {

  if (pin_no == LEDBUTTON_BUTTON3) {
  if (pin_no == BUTTON1) {
    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);
    app_timer_start(screen_timer_id, SLEEP_DELAY_MS, NULL);
    ret_code_t err_code;
    char sting_shelf[3];
    char sting_row[3];
@@ -682,6 +685,18 @@ static void button_event_handler(uint8_t pin_no, uint8_t button_action) {
        int_print_large(counter_imaginary_shelf, nrf_gfx_width_get(p_lcd) / 8, nrf_gfx_height_get(p_lcd) / 8 - 3, BLUE, SCREEN_PRINT_LEFT);
        int_print_large(counter_encoder_depth, nrf_gfx_width_get(p_lcd) / 2 + 6, nrf_gfx_height_get(p_lcd) / 2 + 3, RED, SCREEN_PRINT_RIGHT);
      }
      break;

    case ENCODER4_A:
      if (button_action) {
        if (nrf_gpio_pin_read(ENCODER4_B)) {
          counter_inc(&counter_encoder_depth);
        } else {
          counter_dec(&counter_encoder_depth);
        }
        int_print_large(counter_imaginary_shelf, nrf_gfx_width_get(p_lcd) / 8, nrf_gfx_height_get(p_lcd) / 8 - 3, BLUE, SCREEN_PRINT_LEFT);
        int_print_large(counter_encoder_depth, nrf_gfx_width_get(p_lcd) / 2 + 6, nrf_gfx_height_get(p_lcd) / 2 + 3, RED, SCREEN_PRINT_RIGHT);
      }

      break;
    default:
@@ -695,6 +710,15 @@ static void button_event_handler(uint8_t pin_no, uint8_t button_action) {
 */
static void buttons_init(void) {
  ret_code_t err_code;
  app_button_enable();
  nrf_gpio_cfg_output(LCD_SCREEN_LED);
  nrf_gpio_cfg_output(LCD_SCREEN_DC);
  nrf_gpio_cfg_output(NOTIFICATION_LED);
  nrf_gpio_cfg_input(ENCODER1_B, NRF_GPIO_PIN_NOPULL);
  nrf_gpio_cfg_input(ENCODER2_B, NRF_GPIO_PIN_NOPULL);
  nrf_gpio_cfg_input(ENCODER3_B, NRF_GPIO_PIN_NOPULL);
  nrf_gpio_pin_clear(LCD_SCREEN_LED);
  nrf_gpio_pin_set(LCD_SCREEN_DC);

  //The array must be static because a pointer to it will be saved in the button handler module.
  static app_button_cfg_t buttons[] =
@@ -702,12 +726,13 @@ static void buttons_init(void) {
          {ENCODER1_A, false, NRF_GPIO_PIN_NOPULL, button_event_handler},
          {ENCODER2_A, false, NRF_GPIO_PIN_NOPULL, button_event_handler},
          {ENCODER3_A, false, NRF_GPIO_PIN_NOPULL, button_event_handler},
          {LEDBUTTON_BUTTON3, false, BUTTON_PULL, button_event_handler}
          {ENCODER4_A, false, NRF_GPIO_PIN_NOPULL, button_event_handler},
          {BUTTON1, false, BUTTON_PULL, button_event_handler}

      };

  err_code = app_button_init(buttons, ARRAY_SIZE(buttons),
      BUTTON_DETECTION_DELAY);
      BUTTON_DETECTION_DELAY_MS);
  APP_ERROR_CHECK(err_code);
}

@@ -866,7 +891,7 @@ void start_fault_state() {
    background_color = WHITE;
    nrf_gpio_pin_set(LCD_SCREEN_LED);
    app_timer_stop(screen_timer_id);
    app_timer_start(screen_timer_id, APP_TIMER_TICKS(10000), NULL);
    app_timer_start(screen_timer_id, SLEEP_DELAY_MS, NULL);
  }
}

@@ -891,7 +916,8 @@ void start_advertising_state() {
    text_print("Please wait...", nrf_gfx_width_get(p_lcd) / 16, 50, BLACK);
    nrf_gpio_pin_set(LCD_SCREEN_LED);
    app_timer_stop(screen_timer_id);
    app_timer_start(screen_timer_id, APP_TIMER_TICKS(30000), NULL);
    app_timer_stop(send_timer_id);
    app_timer_start(send_timer_id, SEND_TIMEOUT_MS, NULL);
  }
}

@@ -906,7 +932,8 @@ void start_normal_state() {
    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);
    app_timer_stop(send_timer_id);
    app_timer_start(screen_timer_id, SLEEP_DELAY_MS, NULL);
  }
}

@@ -915,6 +942,7 @@ void start_sleep_state() {
    state = SLEEP_STATE;
    advertising_stop();
    nrf_gpio_pin_clear(LCD_SCREEN_LED);
    idle_state_handle();
  }
}

@@ -941,22 +969,8 @@ int main(void) {
  services_init();
  advertising_init();
  conn_params_init();

  // Start execution.
  app_button_enable();
  nrf_gpio_cfg_output(LCD_SCREEN_LED);
  nrf_gpio_cfg_output(LCD_SCREEN_DC);
  nrf_gpio_cfg_output(NOTIFICATION_LED);
  nrf_gpio_cfg_input(ENCODER1_B, NRF_GPIO_PIN_NOPULL);
  nrf_gpio_cfg_input(ENCODER2_B, NRF_GPIO_PIN_NOPULL);
  nrf_gpio_cfg_input(ENCODER3_B, NRF_GPIO_PIN_NOPULL);
  nrf_gpio_pin_clear(LCD_SCREEN_LED);
  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
  nrf_gpio_pin_set(LCD_SCREEN_LED);
  start_sleep_state();
  // Enter main loop.
  for (;;) {
+612 B (993 KiB)

File changed.

No diff preview for this file type.

Loading