-
Notifications
You must be signed in to change notification settings - Fork 0
2. How to use SWITCH lib
Include the necessary files in your project and add them to the project configuration to ensure they are properly integrated.
Below you can find a list of the files required for the drive switch (2 state input):
switch.h
switch.c
switch_GPIO_interface.h
Depending on your project needs you can reconfigure the switch debounce repetition counts.
To do this, open switch.h
and edit the following section of the code:
/**
* @brief Number of debounce repetitions for the switch.
*/
#define SWITCH_DEBOUNCE_REPETITIONS 1000U
Define the driver interface for the library in your project's driver layer.
The driver interface declaration can be found in switch_GPIO_interface.h
.
Go to the interface_template_files where you will find ready to use templates of driver interface.
For more details and additional guidance about how to use driver interface templates and define driver interface, you can consult the ready-to-compile and run examples.
Steps listed below describes general approach which was taken to create provided template:
-
Initialization of driver interface structure based on it's declaration from
switch_GPIO_interface.h
file:SWITCH_driver_interface_t mySwitch_GPIO_interface_struct = { mySwitch_gpio_init, get_mySwitch_state, };
-
Definition of all driver interface functions:
static void mySwitch_gpio_init(void) { // init mySwitch // Here place code for init GPIO pin associated with switch input } static SWITCH_input_state_t get_mySwitch_state(void) { SWITCH_input_state_t test=SWITCH_INPUT_UNKNOWN; //Here place code that will check switch GPIO pin state and return proper state according to project assumptions return test; }
-
Defining a function that returns the address of the driver interface structure to the library:
const SWITCH_driver_interface_t *switch_1_GPIO_interface_get(void) { return &SWITCH_1_GPIO_interface_struct; }
-
Above steps define driver interface for one switch input signal. Each switch input signal require it's own driver interface. All interfaces can de defined in one file by repeating all steps and renaming structure and function names accordingly to the new switch signals name.
In interface_template_files you can find example where implementation of second switch input signal was commented out but where this implementation is shown. -
Keep in mind that each switch input signal required also declaration of
switch_x_GPIO_interface_get()
inswitch_GPIO_interface.h
-
Initialize the switch using
init_switch
function:SWITCH_TypDef mySwitch; init_switch(&mySwitch, switch_ON_callback, switch_OFF_callback, switch_GPIO_interface_get);
-
Periodically check the switch state in main loop using
check_switch
function:check_switch(&mySwitch);
-
For more switch input signals all above operation need to be conducted for each switch input signal.