Commit b15810de authored by siguhe's avatar siguhe
Browse files

Encoders functional

parent 8c5f9cc2
Loading
Loading
Loading
Loading
+59 −59
Original line number Diff line number Diff line
@@ -76,6 +76,13 @@
#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               BSP_BUTTON_3                            /**< 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 DEVICE_NAME                     "Testurd"                         /**< Name of device. Will be included in the advertising data. */

@@ -95,7 +102,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(50)                     /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
#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 DEAD_BEEF                       0xDEADBEEF                              /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */

@@ -110,6 +117,10 @@ static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];                    /**< Buffer for storing an encoded advertising set. */
static uint8_t m_enc_scan_response_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];         /**< Buffer for storing an encoded scan data. */
static uint8_t advertising_now=0;
static uint8_t counter_encoder1=0;
static uint8_t counter_encoder2=0;
static uint8_t counter_encoder3=0;



/**@brief Struct that contains pointers to the encoded advertising data. */
@@ -405,7 +416,7 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
            NRF_LOG_INFO("Connected");
            bsp_board_led_on(CONNECTED_LED);
            bsp_board_led_off(ADVERTISING_LED);
            //nrf_gpio_pin_set(2);
            nrf_gpio_pin_set(2);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
            APP_ERROR_CHECK(err_code);
@@ -416,7 +427,7 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected");
            bsp_board_led_off(CONNECTED_LED);
            //nrf_gpio_pin_clear(2);
            nrf_gpio_pin_clear(2);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            //err_code = app_button_disable();
            //APP_ERROR_CHECK(err_code);
@@ -512,66 +523,50 @@ static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    
    ret_code_t err_code;
    uint8_t position[4];
    switch (pin_no)
    {
        case LEDBUTTON_BUTTON0:
            bsp_board_led_on(BSP_BOARD_LED_3);
            position[0]=2;
            position[1]=1;
            position[2]=0;
    position[0]=counter_encoder1;
    position[1]=counter_encoder2;
    position[2]=counter_encoder3;
    position[3]=0;

            
            NRF_LOG_INFO("Send button state change.");
            err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, position);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE &&
                err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    switch (pin_no)
    {
                APP_ERROR_CHECK(err_code);
        case ENCODER1_A:
            if(button_action){
                if(nrf_gpio_pin_read(ENCODER1_B)){
                    counter_encoder1+=1;
                }
                else{
                    counter_encoder1-=1;
                }
            }
            break;

        case LEDBUTTON_BUTTON1:
            bsp_board_led_off(BSP_BOARD_LED_3);
            position[0]=2;
            position[1]=1;
            position[2]=1;
            position[3]=0;
        case ENCODER2_A:
            if(button_action){
                if(nrf_gpio_pin_read(ENCODER2_B)){
                    counter_encoder2+=1;
                }
                
                else{
                    counter_encoder2-=1;
                }

            NRF_LOG_INFO("Send button state change.");
            err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, position);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE &&
                err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
            {
                APP_ERROR_CHECK(err_code);
            }
            break;
        case LEDBUTTON_BUTTON2:
            position[0]=2;
            position[1]=1;
            position[2]=2;
            position[3]=0;

            NRF_LOG_INFO("Send button state change.");
            err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, position);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE &&
                err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
            {
                APP_ERROR_CHECK(err_code);
        case ENCODER3_A:
            if(button_action){
                if(nrf_gpio_pin_read(ENCODER3_B)){
                    counter_encoder3+=1;
                }
                
                else{
                    counter_encoder3-=1;
                }
            }
            break;

        case LEDBUTTON_BUTTON3:
            position[0]=2;
            position[1]=1;
            position[2]=3;
            position[3]=button_action;

            NRF_LOG_INFO("Send button state change.");
            err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, position);
            if (err_code != NRF_SUCCESS &&
@@ -587,6 +582,7 @@ static void button_event_handler(uint8_t pin_no, uint8_t button_action)
            APP_ERROR_HANDLER(pin_no);
            break;
    }

}


@@ -599,10 +595,11 @@ static void buttons_init(void)
    //The array must be static because a pointer to it will be saved in the button handler module.
    static app_button_cfg_t buttons[] =
    {
        {LEDBUTTON_BUTTON0, false, BUTTON_PULL, button_event_handler},
        {LEDBUTTON_BUTTON1, false, BUTTON_PULL, button_event_handler},
        {LEDBUTTON_BUTTON2, false, BUTTON_PULL, button_event_handler},
        {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}
        
    };

    err_code = app_button_init(buttons, ARRAY_SIZE(buttons),
@@ -666,6 +663,9 @@ int main(void)
    advertising_start();
    nrf_gpio_cfg_output(20);
    nrf_gpio_cfg_output(2);
    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(2);
//    nrf_gpio_pin_toggle(6);

+2.91 KiB (801 KiB)

File changed.

No diff preview for this file type.

Loading