This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Assign action/event to custom button installed in other PIN

Is there a way to achieve what bsp_event_to_button_action_assign(button_id, action, event) does but using my own button?

The functions requires a button_id like 0,1,2,3 not a custom GPIO pin, but what if I have a button connected to a custom pin like P0.06

  • Hello Carlos Villavicencio

    The bsp module initializes the number of pins and buttons defined for your board, in your case the pca10040. It is therefore limited to the 4 buttons present on the board. If you look at the bsp_init or bsp_event_to_button_action_assign functions they are limited by the BUTTONS_NUMBER define defined in pca10040.h.

    The pin numbers are further defined in the array app_buttons[BUTTONS_NUMBER] in bsp.c, which uses the button defines from pca10040.h.

    If you want to change which pins are used by the bsp module, or add additional ones, you can make changes to the pca10040.h file. If you want to simply change which pins are used you can change the defines for BUTTON_START, BUTTON_1, BUTTON_2, BUTTON_3, BUTTON_4 and BUTTON_STOP as needed. BUTTON_START and BUTTON_STOP is not necessarily used in the project, but you should do a project wide search to make sure changing their values does not interfere with anything.

    If you want to add additional buttons, you can make new defines for the pins you need. You will need to change the BUTTONS_NUMBER define, make both the BUTTON_# and BSP_BUTTON_# defines and add your new button to the BUTTONS_LIST array in the pca10040.h file. You also need to change the number of low power gpiote events defined in sdk_config.h to accommodate for the number of buttons you want. It is important you follow the same naming convention already used in pca10040.h for this to work properly.

    Once defined you can customize it like any other button defined by bsp.h, and events can be handled in the bsp_event_handler defined when executing bsp_init.

    One thing to note, if you change the pca10040.h file, you will change it for ALL examples within the SDK folder, and it can potentially interfere with the functionality of the other examples. If you only want to change it for one project either, create a separate version of the pca10040.h file that your project uses (and change the path of the include), or create a separate SDK folder.

    Best regards

    Jørn Frøysa

Related