Open FFBoard
Open source force feedback firmware
GPIOPin.h
Go to the documentation of this file.
1/*
2 * GPIOPin.h
3 *
4 * Created on: 30.12.2020
5 * Author: willson556
6 */
7#include "cppmain.h"
8
9#ifndef GPIOPIN_H_
10#define GPIOPIN_H_
13
14class GpioPin{
15protected:
16 GPIO_TypeDef *port;
17 uint16_t pin;
18public:
19 GpioPin(GPIO_TypeDef &port, uint16_t pin) // const std::string name = ""
20 : port{&port}, pin{pin} {}
21
22
23 bool operator==(const GpioPin& b){
24 return(this->port == b.port && this->pin == b.pin);
25 }
26 const GPIO_TypeDef* getPort() const {return port;}
27 uint16_t getPin() const {return pin;}
28
29};
30
31
32class OutputPin : public GpioPin {
33public:
34 OutputPin(GPIO_TypeDef &port, uint16_t pin) // const std::string name = ""
35 : GpioPin(port,pin) {}
36
37 //OutputPin(const OutputPin& p): port{p.port}, pin{p.pin} {};
38 void set() const {
39 write(true);
40 }
41
42 void reset() const {
43 write(false);
44 }
45
46 void write(bool state) const {
47 HAL_GPIO_WritePin(port, pin, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
48 }
49
50
54 void configureOutput(uint32_t pull = GPIO_NOPULL,bool opendrain = false, uint32_t speed = GPIO_SPEED_FREQ_LOW){
55 GPIO_InitTypeDef GPIO_InitStruct = {0};
56 GPIO_InitStruct.Pin = pin;
57 GPIO_InitStruct.Mode = opendrain ? GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP;
58 GPIO_InitStruct.Pull = pull;
59 GPIO_InitStruct.Speed = speed;
60 HAL_GPIO_Init(port, &GPIO_InitStruct);
61 }
62
63 //const std::string getName(){return name;}
64
65};
66
67
68class InputPin : public GpioPin {
69public:
70 InputPin(GPIO_TypeDef &port, uint16_t pin) // const std::string name = ""
71 : GpioPin(port,pin) {}
72 InputPin(GPIO_TypeDef* port, uint16_t pin) // const std::string name = ""
73 : GpioPin(*port,pin) {}
74
75 //OutputPin(const OutputPin& p): port{p.port}, pin{p.pin} {};
76 bool read() const{
77 return HAL_GPIO_ReadPin(port,pin);
78 }
79
80
81 //const std::string getName(){return name;}
82
83};
84#endif
GpioPin(GPIO_TypeDef &port, uint16_t pin)
Definition: GPIOPin.h:19
uint16_t pin
Definition: GPIOPin.h:17
uint16_t getPin() const
Definition: GPIOPin.h:27
GPIO_TypeDef * port
Definition: GPIOPin.h:16
bool operator==(const GpioPin &b)
Definition: GPIOPin.h:23
const GPIO_TypeDef * getPort() const
Definition: GPIOPin.h:26
InputPin(GPIO_TypeDef *port, uint16_t pin)
Definition: GPIOPin.h:72
InputPin(GPIO_TypeDef &port, uint16_t pin)
Definition: GPIOPin.h:70
bool read() const
Definition: GPIOPin.h:76
void write(bool state) const
Definition: GPIOPin.h:46
OutputPin(GPIO_TypeDef &port, uint16_t pin)
Definition: GPIOPin.h:34
void set() const
Definition: GPIOPin.h:38
void reset() const
Definition: GPIOPin.h:42
void configureOutput(uint32_t pull=GPIO_NOPULL, bool opendrain=false, uint32_t speed=GPIO_SPEED_FREQ_LOW)
Definition: GPIOPin.h:54