Skip to content

Commit 315fdd4

Browse files
committed
Added gpio input pin class & extended local buttons to 16
1 parent 9b9bfcc commit 315fdd4

7 files changed

Lines changed: 105 additions & 23 deletions

File tree

Firmware/FFBoard/Inc/CAN.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "main.h"
1414
#include <vector>
1515
#include "semaphore.hpp"
16-
#include "OutputPin.h"
16+
#include <GPIOPin.h>
1717
#include "PersistentStorage.h"
1818
#include "CommandHandler.h"
1919

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
/*
2-
* SPI.h
2+
* GPIOPin.h
33
*
44
* Created on: 30.12.2020
55
* Author: willson556
66
*/
77

88
#include "stm32f4xx_hal.h"
9-
#ifndef OUTPUTPIN_H_
10-
#define OUTPUTPIN_H_
9+
#ifndef GPIOPIN_H_
10+
#define GPIOPIN_H_
1111
/// For now this class only works with pre-configured output pins but it could be
1212
/// easily extended to cover input pins as well as expose various configuration API's.
13-
class OutputPin {
13+
14+
class GpioPin{
15+
public:
16+
GpioPin(GPIO_TypeDef &port, uint16_t pin) // const std::string name = ""
17+
: port{&port}, pin{pin} {}
18+
19+
20+
bool operator==(const GpioPin& b){
21+
return(this->port == b.port && this->pin == b.pin);
22+
}
1423
protected:
1524
GPIO_TypeDef *port;
1625
uint16_t pin;
17-
//const std::string name;
26+
};
27+
1828

29+
class OutputPin : public GpioPin {
1930
public:
2031
OutputPin(GPIO_TypeDef &port, uint16_t pin) // const std::string name = ""
21-
: port{&port}, pin{pin} {}
32+
: GpioPin(port,pin) {}
2233

2334
//OutputPin(const OutputPin& p): port{p.port}, pin{p.pin} {};
2435
void set() const {
@@ -33,9 +44,6 @@ class OutputPin {
3344
HAL_GPIO_WritePin(port, pin, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
3445
}
3546

36-
bool operator==(const OutputPin& b){
37-
return(this->port == b.port && this->pin == b.pin);
38-
}
3947

4048
/**
4149
* Sets a pin into output mode in case it was previously reconfigured
@@ -51,6 +59,23 @@ class OutputPin {
5159

5260
//const std::string getName(){return name;}
5361

62+
};
63+
64+
65+
class InputPin : public GpioPin {
66+
public:
67+
InputPin(GPIO_TypeDef &port, uint16_t pin) // const std::string name = ""
68+
: GpioPin(port,pin) {}
69+
InputPin(GPIO_TypeDef* port, uint16_t pin) // const std::string name = ""
70+
: GpioPin(*port,pin) {}
71+
72+
//OutputPin(const OutputPin& p): port{p.port}, pin{p.pin} {};
73+
bool read() const{
74+
return HAL_GPIO_ReadPin(port,pin);
75+
}
76+
77+
78+
//const std::string getName(){return name;}
5479

5580
};
5681
#endif

Firmware/FFBoard/Inc/SPI.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
#ifndef SPI_H_
99
#define SPI_H_
1010

11+
#include <GPIOPin.h>
1112
#include <vector>
1213
#include "cppmain.h"
1314

1415
#include "stm32f4xx_hal.h"
1516

1617
#include "SpiHandler.h"
17-
#include "OutputPin.h"
18-
1918
#include "semaphore.hpp"
2019

2120
struct SPIConfig {

Firmware/FFBoard/Inc/constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* For more settings see target_constants.h in a target specific folder
99
*/
1010

11-
static const uint8_t SW_VERSION_INT[3] = {1,12,1}; // Version as array. 8 bit each!
11+
static const uint8_t SW_VERSION_INT[3] = {1,12,2}; // Version as array. 8 bit each!
1212
#define MAX_AXIS 2 // ONLY USE 2 for now else screws HID Reports
1313
#define FLASH_VERSION 0 // Counter to increase whenever a full flash erase is required.
1414

Firmware/FFBoard/UserExtensions/Inc/LocalButtons.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212
#include "ChoosableClass.h"
1313
#include "CommandHandler.h"
1414
#include "constants.h"
15+
#include <array>
16+
#include "GPIOPin.h"
1517

1618
class LocalButtons: public ButtonSource,CommandHandler{
1719
enum class LocalButtons_commands : uint32_t{
1820
mask,polarity,pins,values,
1921
};
22+
private:
23+
uint32_t mask = 0xff;
24+
void setMask(uint32_t mask);
25+
bool polarity = false;
26+
static const std::array<InputPin,BUTTON_PINS> button_pins;
27+
2028
public:
2129
LocalButtons();
2230
virtual ~LocalButtons();
@@ -39,15 +47,10 @@ class LocalButtons: public ButtonSource,CommandHandler{
3947
if (button_num >= maxButtons || button_num < 0) {
4048
return false;
4149
}
42-
return HAL_GPIO_ReadPin(button_ports[button_num], button_pins[button_num]);
50+
// return HAL_GPIO_ReadPin(button_ports[button_num], button_pins[button_num]);
51+
return button_pins[button_num].read();
4352
}
4453
const ClassType getClassType() {return ClassType::Buttonsource;};
45-
private:
46-
uint32_t mask = 0xff;
47-
void setMask(uint32_t mask);
48-
bool polarity = false;
49-
static constexpr uint16_t button_pins[8] {DIN0_Pin,DIN1_Pin,DIN2_Pin,DIN3_Pin,DIN4_Pin,DIN5_Pin,DIN6_Pin,DIN7_Pin};
50-
static constexpr GPIO_TypeDef* button_ports[8] {DIN0_GPIO_Port,DIN1_GPIO_Port,DIN2_GPIO_Port,DIN3_GPIO_Port,DIN4_GPIO_Port,DIN5_GPIO_Port,DIN6_GPIO_Port,DIN7_GPIO_Port};
5154
};
5255

5356
#endif /* LOCALBUTTONS_H_ */

Firmware/FFBoard/UserExtensions/Inc/TMC4671.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "TimerHandler.h"
2424

2525
#include "semaphore.hpp"
26-
#include "OutputPin.h"
26+
#include <GPIOPin.h>
2727
#include "cpp_target_config.h"
2828
#include "Filters.h"
2929

Firmware/FFBoard/UserExtensions/Src/LocalButtons.cpp

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ uint8_t LocalButtons::readButtons(uint64_t* buf){
5858
}
5959

6060
void LocalButtons::saveFlash(){
61-
uint16_t dat = this->mask & 0xff;
61+
uint16_t dat = this->mask & 0xffff;
6262
Flash_Write(ADR_LOCAL_BTN_CONF, dat);
6363

6464
uint16_t dat2 = this->polarity & 0x01;
@@ -68,7 +68,7 @@ void LocalButtons::saveFlash(){
6868
void LocalButtons::restoreFlash(){
6969
uint16_t dat = 0;
7070
if(Flash_Read(ADR_LOCAL_BTN_CONF,&dat)){
71-
this->setMask(dat & 0xff);
71+
this->setMask(dat & 0xffff);
7272
}
7373

7474
if(Flash_Read(ADR_LOCAL_BTN_CONF_2,&dat)){
@@ -125,3 +125,58 @@ CommandStatus LocalButtons::command(const ParsedCommand& cmd,std::vector<Command
125125

126126
}
127127

128+
/**
129+
* All DIN pins must be defined as inputs.
130+
* Not more than 16 pins can be defined
131+
*/
132+
const std::array<InputPin,BUTTON_PINS> LocalButtons::button_pins {
133+
#if BUTTON_PINS > 0
134+
InputPin(DIN0_GPIO_Port, DIN0_Pin),
135+
#endif
136+
#if BUTTON_PINS > 1
137+
InputPin(DIN1_GPIO_Port, DIN1_Pin),
138+
#endif
139+
#if BUTTON_PINS > 2
140+
InputPin(DIN2_GPIO_Port, DIN2_Pin),
141+
#endif
142+
#if BUTTON_PINS > 3
143+
InputPin(DIN3_GPIO_Port, DIN3_Pin),
144+
#endif
145+
#if BUTTON_PINS > 4
146+
InputPin(DIN4_GPIO_Port, DIN4_Pin),
147+
#endif
148+
#if BUTTON_PINS > 5
149+
InputPin(DIN5_GPIO_Port, DIN5_Pin),
150+
#endif
151+
#if BUTTON_PINS > 6
152+
InputPin(DIN6_GPIO_Port, DIN6_Pin),
153+
#endif
154+
#if BUTTON_PINS > 7
155+
InputPin(DIN7_GPIO_Port, DIN7_Pin),
156+
#endif
157+
#if BUTTON_PINS > 8
158+
InputPin(DIN8_GPIO_Port, DIN8_Pin),
159+
#endif
160+
#if BUTTON_PINS > 9
161+
InputPin(DIN9_GPIO_Port, DIN9_Pin),
162+
#endif
163+
#if BUTTON_PINS > 10
164+
InputPin(DIN10_GPIO_Port, DIN10_Pin),
165+
#endif
166+
#if BUTTON_PINS > 11
167+
InputPin(DIN11_GPIO_Port, DIN11_Pin),
168+
#endif
169+
#if BUTTON_PINS > 12
170+
InputPin(DIN12_GPIO_Port, DIN12_Pin),
171+
#endif
172+
#if BUTTON_PINS > 13
173+
InputPin(DIN13_GPIO_Port, DIN13_Pin),
174+
#endif
175+
#if BUTTON_PINS > 14
176+
InputPin(DIN14_GPIO_Port, DIN14_Pin),
177+
#endif
178+
#if BUTTON_PINS > 15
179+
InputPin(DIN15_GPIO_Port, DIN15_Pin),
180+
#endif
181+
182+
};

0 commit comments

Comments
 (0)