Skip to content

Commit

Permalink
Merge pull request #4 from niwciu/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
niwciu authored Aug 7, 2024
2 parents ad6a928 + 52e1187 commit 64abe37
Show file tree
Hide file tree
Showing 159 changed files with 713 additions and 598 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 niwciu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
193 changes: 36 additions & 157 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,160 +1,39 @@
# PUSHBUTTON_SWITCH_LIB - simple cross platform library
# PUSHBUTTON_SWITCH_LIB - simple cross platform library

![GitHub License](https://img.shields.io/github/license/niwciu/PUSHBUTTON_SWITCH_LIB)
![GitHub top language](https://img.shields.io/github/languages/top/niwciu/PUSHBUTTON_SWITCH_LIB)
![GitHub Release](https://img.shields.io/github/v/release/niwciu/PUSHBUTTON_SWITCH_LIB)
![GitHub branch check runs](https://img.shields.io/github/check-runs/niwciu/LCD_HD44780/main)
![Unit Tests](https://github.com/niwciu/PUSHBUTTON_SWITCH_LIB/actions/workflows/run_library_unit_test.yml/badge.svg)
![cppcheck](https://github.com/niwciu/PUSHBUTTON_SWITCH_LIB/actions/workflows/run_cppcheck.yml/badge.svg)
![clang-format](https://github.com/niwciu/PUSHBUTTON_SWITCH_LIB/actions/workflows/clang-format_check.yml/badge.svg)
![code complexity](https://github.com/niwciu/PUSHBUTTON_SWITCH_LIB/actions/workflows/run_lizard_lib_check.yml/badge.svg)

The `PUSHBUTTON_SWITCH_LIB` is a simple cross-platform C library providing interfaces for pushbuttons and two-state switches (two-state input signals).

## Features
### Push Buttons:
- 3 trigger modes:
- TRIGGER_ON_PUSH
- TRIGGER_ON_RELEASE
- TRIGGER_ON_SHORT_PUSH_AND_LONG_PUSH
- Option to enable repetition
- Fully configurable time slots for:
- debounce time
- time to the first repetition
- time between continuous repetitions
- time defining the difference between short and long push
- Option to dynamically register callbacks for each trigger source
- Driver interface that is fully separated from the driver layer

### Switches:
- Debounce functionality based on a repeat count of the same state (no timer needed)
- Fully configurable debounce counter
- Dynamically register callbacks for switch ON and OFF states
- Driver interface that is fully separated from the driver layer

## Examples, how to use library etc.
For more information about usage, examples, colaboration and all features please refer to [project wiki](https://github.com/niwciu/PUSHBUTTON_SWITCH_LIB/wiki)

The `PUSHBUTTON_SWITCH_LIB` is a simple cross platform library providing interfaces for pushbutton switches and two-state switches (two-state input signals). It includes features such as debouncing, repetition functionality for pushbuttons, and state management for switches.

## Usage - simplest cases

### Pushbutton Module

1. Include the necessary files in your project:
- `pushbutton.h`
- `pushbutton.c`
- `pushbutton_GPIO_interface.h`
- `pushbutton_TIM_interface.h`

2. Initialize the pushbutton(s) using `init_pushbutton` function. Example:

```c
PUSHBUTTON_TypDef myButton;
init_pushbutton(&myButton, REPETITION_ON, TRIGGER_ON_PUSH, pushButton_GPIO_interface_get);
```

3. Register callbacks for push events:

```c
register_button_push_callback(&myButton, myPushCallback);
```

4. Periodically check the pushbutton state using `check_pushbutton`:

```c
check_pushbutton(&myButton);
```

### Switch Module

1. Include the necessary files in your project:
- `switch.h`
- `switch.c`
- `switch_GPIO_interface.h`
2. Initialize the switch using `init_switch` function. Example:

```c
SWITCH_TypDef mySwitch;
init_switch(&mySwitch, switch_ON_callback, switch_OFF_callback, switch_GPIO_interface_get);
```

3. Periodically check the switch state using `check_switch`:

```c
check_switch(&mySwitch);
```

4. Implement your callback functions for switch events.

## Notes

- Make sure to include the necessary dependencies for GPIO and timer interfaces as per your hardware platform.
- Adjust debounce and repetition settings based on your application requirements.

## Examples
### STM32G071_NUCLEO_LL_1_KEY_UI
Example of using the library to create a User Interface Keyboard module in the project. The example includes the initialization of one pushbutton keyboard with short-push and long-push with repetition functionality.
- Short-push triggers the LED ON function,
- Long-push triggers the LED TOGGLE function.
#### Requirements for compiling and running the example:
1. CMake installed
2. Make or Ninja installed
3. ARM GNU Toolchain (gcc-arm-none-eabi) installed
4. STM32_Programmer_CLI installed
5. ST-link (placed on Nucleo Board) installed
#### Hardware requirements, configuration, and connections:
1. STM32G071 Nucleo-64<br>
<img src="https://www.st.com/bin/ecommerce/api/image.PF265386.en.feature-description-include-personalized-no-cpn-medium.jpg" width="400"><br> https://www.st.com/en/evaluation-tools/nucleo-g071rb.html <br>
#### How to build and run the example:
1. Open the location you want to clone the repository to in your terminal
2. Clone the repository to your preferred localization
```bash
git clone https://github.com/niwciu/PUSHBUTTON_SWITCH_LIB.git
```
3. Enter to PUSHBUTTON_SWITCH_LIB/examples/STM32G071_NUCLEO_LL_1_KEY_UI
```bash
cd ./PUSHBUTTON_SWITCH_LIB/examples/STM32G071_NUCLEO_LL_1_KEY_UI
```
4. For Make type:
```bash
cmake -S ./ -B Debug -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug
```
```bash
cd Debug
```
```bash
make all
```
```bash
make flash
```
5. For Ninja type:
```bash
cmake -S ./ -B Debug -G"Ninja" -DCMAKE_BUILD_TYPE=Debug
```
```bash
cd Debug
```
```bash
ninja
```
```bash
ninja flash
```
6. In some cases Nucleo board requires plugging out and in a USB port to run the program.
### STM32G071_NUCLEO_LL_2_STATE_SWITCH
Example of using the library to create an input signals module in the project. The example includes the initialization of one two-state switch that calls functions when transitioning to ON and OFF states.
- ON state triggers the LED ON function,
- OFF state triggers the LED OFF function.
#### Requirements for compiling and running the example:
1. CMake installed
2. Make or Ninja installed
3. ARM GNU Toolchain (gcc-arm-none-eabi) installed
4. STM32_Programmer_CLI installed
5. ST-link (placed on Nucleo Board) installed
#### Hardware requirements, configuration, and connections:
1. STM32G071 Nucleo-64<br>
<img src="https://www.st.com/bin/ecommerce/api/image.PF265386.en.feature-description-include-personalized-no-cpn-medium.jpg" width="400"><br> https://www.st.com/en/evaluation-tools/nucleo-g071rb.html <br>
#### How to build and run the example
1. Open the location you want to clone the repository to in your terminal
2. Clone the repository to your preferred localization
```bash
git clone https://github.com/niwciu/PUSHBUTTON_SWITCH_LIB.git
```
3. Enter to PUSHBUTTON_SWITCH_LIB/examples/STM32G071_NUCLEO_LL_2_STATE_SWITCH/
```bash
cd ./PUSHBUTTON_SWITCH_LIB/examples/STM32G071_NUCLEO_LL_2_STATE_SWITCH
```
4. For Make type:
```bash
cmake -S ./ -B Debug -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug
```
```bash
cd Debug
```
```bash
make all
```
```bash
make flash
```
5. For Ninja type:
```bash
cmake -S ./ -B Debug -G"Ninja" -DCMAKE_BUILD_TYPE=Debug
```
```bash
cd Debug
```
```bash
ninja
```
```bash
ninja flash
```
6. In some cases Nucleo board requires plugging out and in a USB port to run the program.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ set(INCLUDE_DIRS
./Core
./Core/Inc
./Core/Src/LED
./Core/Src/UI_KEYBOARD
./Core/Src/UI_KEYPAD
# Core/Src
# Core/Startup

Expand All @@ -53,8 +53,8 @@ set(C_SRCS
Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_utils.c

Core/Src/LED/LED_GPIO_driver.c
Core/Src/UI_KEYBOARD/keyboard.c
Core/Src/UI_KEYBOARD/pushbutton_GPIO_interface.c
Core/Src/UI_KEYPAD/keypad.c
Core/Src/PB_GPIO_DRIVER_INTERFACE/pushbutton_GPIO_interface.c
Core/Src/main.c
Core/Src/stm32g0xx_it.c
Core/Src/syscalls.c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,45 @@
* @copyright Copyright (c) 2024
*/

#include "keyboard.h"
#include "keypad.h"
#include "keypad_TIM_interface.h"
#include "pushbutton.h"
#include "LED_GPIO_driver.h"

/** @brief Structure representing KEY_1 pushbutton. */
PUSHBUTTON_TypDef KEY_1;
/** @brief Structure representing PUSHBUTTON_1 pushbutton. */
PUSHBUTTON_TypDef PUSHBUTTON_1;

/** @brief Pointer to the KEY_1 pushbutton structure. */
PUSHBUTTON_TypDef *ENT_ESC = &KEY_1;
/** @brief Pointer to the PUSHBUTTON_1 pushbutton structure. */
PUSHBUTTON_TypDef *ENT_ESC = &PUSHBUTTON_1;

static void update_button_timers(void);

/**
* @brief Initializes the keyboard pushbutton components.
* @details This function initializes the necessary pushbuttons, sets their configurations,
* and registers corresponding callback functions. It also subscribes to the SysTick timer
* for periodic updates of pushbuttonbutton timers.
*/
void init_keyboard(void)
void init_keypad(void)
{
// Key ENT_ESC Initialization
init_pushbutton(ENT_ESC, REPETITION_ON, TRIGGER_ON_SHORT_PUSH_AND_LONG_PUSH, pushbutton_1_GPIO_interface_get);
register_button_short_push_long_push_callbacks(ENT_ESC, LED_on, LED_toggle);
subscribe_SysTick_callback(update_button_timers);

// KEY_2 Initialization
// ...
// KEY_3 Initialization
// ...

subscribe_keypad_timers_update_callback(update_button_timers);
}

/**
* @brief Checks the state of pushbuttons in the keyboard.
* @details This function checks the state of the pushbuttons in the keyboard,
* processes any events, and updates the button timers.
*/
void check_keyboard(void)
void check_keypad(void)
{
check_pushbutton(ENT_ESC);
//check_pushbutton(&KEY_2);
Expand All @@ -56,9 +62,10 @@ void check_keyboard(void)
* @details This function is called periodically by the SysTick timer to update
* the debounce and repetition timers for the pushbuttons in the keyboard.
*/
void update_button_timers(void)
static void update_button_timers(void)
{
dec_pushbutton_deb_rep_timer(ENT_ESC);
// dec_pushbutton_deb_rep_timer(&KEY_2);
// dec_pushbutton_deb_rep_timer(&KEY_3);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
extern "C" {
#endif

void init_keyboard(void);
void check_keyboard(void);
void update_button_timers (void);
void init_keypad(void);
void check_keypad(void);
// void update_button_timers(void);


#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file keypad_TIM_interface.h
* @author niwciu (niwciu@gmail.com)
* @brief Defines the interface for integrating pushbutton layer with a timer (TIM) module. It's part of this library but in project this file should come
* from Timer implementation layer side where "observer" design patter is implemented.
* @date 2024-03-02
* @copyright Copyright (c) 2024
*/
#ifndef _KEYPAD_TIM_INTERFACE_H_
#define _KEYPAD_TIM_INTERFACE_H_

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

/**
* @brief Callback function type for 1 ms timer events.
*
* This function type is used for callback functions that are triggered every 1ms by timer events.
*/
typedef void (*keypad_timers_update_callback_t)(void);

/**
* @brief Subscribes a callback function to timer events fired with 1ms resolution.
*
* This function allows subscribing a callback function for keypad (all pusbuttons) timers update.
* subscribed callback should be called with time resolution of 1ms.
*
* @param timers_update_callback The callback function to be subscribed.
*/
void subscribe_keypad_timers_update_callback(keypad_timers_update_callback_t timers_update_callback);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _KEYPAD_TIM_INTERFACE_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "keyboard.h"
#include "keypad.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -91,7 +91,7 @@ int main(void)
MX_GPIO_Init();
/* USER CODE BEGIN 2 */

init_keyboard();
init_keypad();
LL_SYSTICK_EnableIT();

// LL_GPIO_SetOutputPin(LED_GPIO_Port,LED_Pin);
Expand All @@ -102,7 +102,7 @@ int main(void)
while (1)
{

check_keyboard();
check_keypad();
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
Expand Down
Loading

0 comments on commit 64abe37

Please sign in to comment.