Skip to content

Commit

Permalink
Some minor adaptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehaenger committed Sep 30, 2024
1 parent a730298 commit 7003fb1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 113 deletions.
65 changes: 30 additions & 35 deletions Firmware/CoverUI/YardForce/Buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
* @file Buttons.cpp
* @author Apehaenger ([email protected])
* @brief YardForce CoverUI Buttons class for OpenMower https://github.com/ClemensElflein/OpenMower
* @version 0.6
* @date 2023-11-05
* @version 0.7
* @date 2024-09-30
*
* @copyright Copyright (c) 2023
* @copyright Copyright (c) 2023, 2024
*
*/
#include <Arduino.h>
#include <stdint.h>

#include <map>
#include "include/Buttons.hpp"

#include "../BttnCtl.h"
#include "include/main.h"

#ifdef MCU_STM32
#define DIGITAL_PIN_TO_PORT_NR(p) (STM_PORT(digitalPinToPinName(p)))
#else // MCU_GD32
#else // MCU_GD32
#define DIGITAL_PIN_TO_PORT_NR(p) (GD_PORT_GET(DIGITAL_TO_PINNAME(p)))
#endif

Expand All @@ -26,15 +28,14 @@ extern void sendMessage(void *message, size_t size);
* @brief Setup GPIOs
*
*/
void Buttons::setup()
{
for (auto const &it : kBtnDefByNumMap) // Loop over Button-Num -> button pin map
void Buttons::setup() {
for (auto const &it : kBtnDefByNumMap) // Loop over Button-Num -> button pin map
{
// Create debouncer if not already exists for this Pin's GPIO_Port_Nr
// Create debouncer if not already exist for this Pin's GPIO_Port_Nr
uint32_t gpio_port_nr = DIGITAL_PIN_TO_PORT_NR(it.second.pin);
auto debouncer = debouncer_by_gpio_port_nr_map.find(gpio_port_nr);
if (debouncer == debouncer_by_gpio_port_nr_map.end())
debouncer_by_gpio_port_nr_map.insert(std::pair<uint32_t, ButtonDebouncer>(gpio_port_nr, ButtonDebouncer()));
debouncer_by_gpio_port_nr_map.insert(etl::pair<uint32_t, ButtonDebouncer>(gpio_port_nr, ButtonDebouncer()));

pinMode(it.second.pin, INPUT_PULLUP);
}
Expand All @@ -44,10 +45,9 @@ void Buttons::setup()
* @brief Process GPIO states by debouncer. Has to get called regulary i.e. by timer (5ms)
*
*/
void Buttons::process_states()
{
for (std::map<uint32_t, ButtonDebouncer>::iterator it = debouncer_by_gpio_port_nr_map.begin(); it != debouncer_by_gpio_port_nr_map.end(); ++it)
it->second.process_state(it->first);
void Buttons::process_states() {
for (auto &it : debouncer_by_gpio_port_nr_map)
it.second.process_state(it.first);
};

/**
Expand All @@ -56,9 +56,8 @@ void Buttons::process_states()
* @param button_nr
* @return uint8_t LED num. -1 of not exists.
*/
int8_t Buttons::get_led(uint8_t button_nr)
{
auto btn_def_it = kBtnDefByNumMap.find(button_nr); // Find button_nr and get iterator pair
int8_t Buttons::get_led(uint8_t button_nr) {
auto btn_def_it = kBtnDefByNumMap.find(button_nr); // Find button_nr and get iterator pair
if (btn_def_it != kBtnDefByNumMap.end())
return btn_def_it->second.led_num;

Expand All @@ -72,19 +71,17 @@ int8_t Buttons::get_led(uint8_t button_nr)
* @param uint8_t button_nr
* @return true if pressed, false if not pressed
*/
bool Buttons::is_pressed(uint8_t button_nr)
{
auto btn_def_it = kBtnDefByNumMap.find(button_nr); // Find button_nr and get iterator pair
if (btn_def_it != kBtnDefByNumMap.end())
{
uint32_t gpio_port_nr = DIGITAL_PIN_TO_PORT_NR(btn_def_it->second.pin);
auto debouncer_it = debouncer_by_gpio_port_nr_map.find(gpio_port_nr); // Find debouncer and get iterator pair
if (debouncer_it != debouncer_by_gpio_port_nr_map.end())
{
return debouncer_it->second.is_pressed(btn_def_it->second.pin);
}
}
return false;
bool Buttons::is_pressed(uint8_t button_nr) {
auto btn_def_it = kBtnDefByNumMap.find(button_nr); // Find button_nr and get iterator pair
if (btn_def_it == kBtnDefByNumMap.end())
return false;

uint32_t gpio_port_nr = DIGITAL_PIN_TO_PORT_NR(btn_def_it->second.pin);
auto debouncer_it = debouncer_by_gpio_port_nr_map.find(gpio_port_nr); // Find debouncer and get iterator pair
if (debouncer_it == debouncer_by_gpio_port_nr_map.end())
return false;

return debouncer_it->second.is_pressed(btn_def_it->second.pin);
};

/**
Expand All @@ -93,9 +90,8 @@ bool Buttons::is_pressed(uint8_t button_nr)
*
* @return uint8_t 0 = none pressed, >0 = ButtonNum
*/
uint8_t Buttons::is_pressed()
{
for (auto const &it : kBtnDefByNumMap) // Loop over Button-Num -> button pin map
uint8_t Buttons::is_pressed() {
for (auto const &it : kBtnDefByNumMap) // Loop over Button-Num -> button pin map
if (is_pressed(it.first))
return it.first;

Expand All @@ -106,8 +102,7 @@ uint8_t Buttons::is_pressed()
* @brief Send 'rain' message via COBS with last read rain-sensor- value (together with (currently static) threshold)
*
*/
void Buttons::send(uint16_t button_id, uint8_t press_duration)
{
void Buttons::send(uint16_t button_id, uint8_t press_duration) {
msg_event_button msg = {
.type = Get_Button,
.button_id = button_id,
Expand Down
2 changes: 1 addition & 1 deletion Firmware/CoverUI/YardForce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ check section [usage](#usage) about the [LED](#meaning-of-the-leds) and [Button]

| Version [^4] | Changes | Date |
| ------- | ------- | ---- |
| 2.06 | - Add MOD_HALL support to YardForce RM-ECOW-V1.0.0 | 2024-09-29
| 2.06 | - Add MOD_HALL support to YardForce RM-ECOW-V1.0.0<br>- Fix heap overflow which could happen when an action button got pressed multiple (10-20) times | 2024-09-29
| 2.05 | - YardForce RM-ECOW-V1.1.0 support<br>- Backside alive LED is now software driven (hwtimer independent)<br>- Fix emergency-clear LED-countdown for RM-ECOW-V1.0.0| 2024-06-30
| 2.04 | - YardForce RM-ECOW-V1.0.0 support<br>- Dropped separate versioning<br>- Fully refactored | 2023-11-14
| 1.00 | - YardForce SAxPRO (Rev6) Dot-Matrix-Display support | 2023-09-27
Expand Down
71 changes: 37 additions & 34 deletions Firmware/CoverUI/YardForce/include/Buttons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
* @file Buttons.hpp
* @author Apehaenger ([email protected])
* @brief YardForce CoverUI Buttons header for OpenMower https://github.com/ClemensElflein/OpenMower
* @version 0.6
* @date 2023-11-05
* @version 0.7
* @date 2024-09-30
*
* @copyright Copyright (c) 2023
* @copyright Copyright (c) 2023, 2024
*
*/
#ifndef YARDFORCE_BUTTONS_HPP
#define YARDFORCE_BUTTONS_HPP

#include <Arduino.h>
#include <etl/map.h>
#include <stdint.h>

#include <map>

#include "ButtonDebouncer.hpp"

// Logic button numbers. Take attention that OM known buttons need to have the same logic number!
// 0 is reserved for no-button return
#define BTN_CLK_NUM 1
#define BTN_HOME_NUM 2
#define BTN_PLAY_NUM 3 // or Start
#define BTN_PLAY_NUM 3 // or Start
#define BTN_S1_NUM 4
#define BTN_S2_NUM 5
#define BTN_LOCK_NUM 6
#define BTN_OK_NUM 7 // or Enter
#define BTN_OK_NUM 7 // or Enter
#define BTN_MON_NUM 8
#define BTN_TUE_NUM 9
#define BTN_WED_NUM 10
Expand All @@ -33,39 +36,39 @@
#define BTN_SAT_NUM 13
#define BTN_SUN_NUM 14
// (yet) unknown OM buttons. Let's start from 0x20 to have some free space for more (future) OM supported buttons
#define BTN_UP_NUM 32 // i.e. SAxPRO display
#define BTN_DOWN_NUM 33 // i.e. SAxPRO display
#define BTN_BACK_NUM 34 // i.e. SAxPRO display
#define BTN_4H_NUM 35 // i.e. RM-ECOW-V100
#define BTN_6H_NUM 36 // i.e. RM-ECOW-V100
#define BTN_8H_NUM 37 // i.e. RM-ECOW-V100
#define BTN_10H_NUM 38 // i.e. RM-ECOW-V100
#define BTN_SETUP_NUM 39 // i.e. RM-ECOW-V100 (WLAN-Setup button)
#define BTN_MENU_NUM 40 // i.e. RM-EC3-V11
#define BTN_0_NUM 48 // ASCII 0, i.e. RM-EC3-V11
#define BTN_1_NUM 49 // ASCII 1, i.e. RM-EC3-V11
#define BTN_2_NUM 50 // ASCII 2, i.e. RM-EC3-V11
#define BTN_3_NUM 51 // ASCII 3, i.e. RM-EC3-V11
#define BTN_4_NUM 52 // ASCII 4, i.e. RM-EC3-V11
#define BTN_5_NUM 53 // ASCII 5, i.e. RM-EC3-V11
#define BTN_6_NUM 54 // ASCII 6, i.e. RM-EC3-V11
#define BTN_7_NUM 55 // ASCII 7, i.e. RM-EC3-V11
#define BTN_8_NUM 56 // ASCII 8, i.e. RM-EC3-V11
#define BTN_9_NUM 57 // ASCII 9, i.e. RM-EC3-V11
#define BTN_UP_NUM 32 // i.e. SAxPRO display
#define BTN_DOWN_NUM 33 // i.e. SAxPRO display
#define BTN_BACK_NUM 34 // i.e. SAxPRO display
#define BTN_4H_NUM 35 // i.e. RM-ECOW-V100
#define BTN_6H_NUM 36 // i.e. RM-ECOW-V100
#define BTN_8H_NUM 37 // i.e. RM-ECOW-V100
#define BTN_10H_NUM 38 // i.e. RM-ECOW-V100
#define BTN_SETUP_NUM 39 // i.e. RM-ECOW-V100 (WLAN-Setup button)
#define BTN_MENU_NUM 40 // i.e. RM-EC3-V11
#define BTN_0_NUM 48 // ASCII 0, i.e. RM-EC3-V11
#define BTN_1_NUM 49 // ASCII 1, i.e. RM-EC3-V11
#define BTN_2_NUM 50 // ASCII 2, i.e. RM-EC3-V11
#define BTN_3_NUM 51 // ASCII 3, i.e. RM-EC3-V11
#define BTN_4_NUM 52 // ASCII 4, i.e. RM-EC3-V11
#define BTN_5_NUM 53 // ASCII 5, i.e. RM-EC3-V11
#define BTN_6_NUM 54 // ASCII 6, i.e. RM-EC3-V11
#define BTN_7_NUM 55 // ASCII 7, i.e. RM-EC3-V11
#define BTN_8_NUM 56 // ASCII 8, i.e. RM-EC3-V11
#define BTN_9_NUM 57 // ASCII 9, i.e. RM-EC3-V11

#define MAX_BTN_GPIO_PORTS 6 // Our MCUs have GPIOA-F as a max = 6

class Buttons
{
public:
struct ButtonDef
{
class Buttons {
public:
struct ButtonDef {
uint8_t pin;
int8_t led_num; // Corresponding LED num. -1 is none.
int8_t led_num; // Corresponding LED num. -1 is none.
};

const std::map<uint8_t, ButtonDef> &kBtnDefByNumMap; // Map of Button-Num -> ButtonDef (pin & corresponding LED num)
std::map<uint32_t, ButtonDebouncer> debouncer_by_gpio_port_nr_map; // Map of GPIO Port Nr -> debouncer object
const std::map<uint8_t, ButtonDef> &kBtnDefByNumMap; // Map of Button-Num -> ButtonDef (pin & corresponding LED num)
etl::map<uint32_t, ButtonDebouncer, MAX_BTN_GPIO_PORTS> debouncer_by_gpio_port_nr_map; // Map of GPIO Port Nr -> debouncer object

Buttons(const std::map<uint8_t, ButtonDef> &t_kBtnDefByNumMap) : kBtnDefByNumMap(t_kBtnDefByNumMap){};
Buttons(const std::map<uint8_t, ButtonDef> &t_kBtnDefByNumMap) : kBtnDefByNumMap(t_kBtnDefByNumMap) {};

virtual ~Buttons() = default;

Expand All @@ -79,4 +82,4 @@ class Buttons
void send(uint16_t button_id, uint8_t press_duration);
};

#endif // YARDFORCE_BUTTONS_HPP
#endif // YARDFORCE_BUTTONS_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define YARDFORCE_BUTTONS_RMECOWV100_H

#include <Arduino.h>

#include "../Buttons.hpp"

#define BTN_S1_PIN PA4
Expand All @@ -30,4 +31,4 @@ extern Buttons buttons;
#define HAS_MAGIC_BUTTONS
extern void magic_buttons();

#endif // YARDFORCE_BUTTONS_RMECOWV100_H
#endif // YARDFORCE_BUTTONS_RMECOWV100_H
Loading

0 comments on commit 7003fb1

Please sign in to comment.