-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pin.h
55 lines (43 loc) · 1.12 KB
/
Pin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef PIN_H
#define PIN_H
#include <pico/stdlib.h>
#include <hardware/pwm.h>
#include <map>
#include <functional>
using IsrCallback_t = std::function<void(int, uint32_t)>;
enum PinFunction
{
FN_OUTPUT,
FN_PWM,
FN_INPUT,
FN_INPUT_ISR,
};
struct PwmParams
{
int wrap_value;
float clock_divider;
};
class Pin
{
public:
Pin(int pin, PinFunction function);
Pin(int pin, PinFunction function, PwmParams pwmParams);
Pin(int pin, PinFunction function, uint32_t events);
~Pin();
void setCallback(IsrCallback_t callback) const
{
assert(this->_function == FN_INPUT_ISR);
Pin::callbackMap[this->_pin] = callback;
}
int pin() const { return this->_pin; }
PinFunction function() const { return this->_function; }
void set(int value) const { gpio_put(this->_pin, value); }
void set_duty(int value) const { pwm_set_gpio_level(this->_pin, value); }
bool get() const { return gpio_get(this->_pin); }
static std::map<int, IsrCallback_t> callbackMap;
private:
const int _pin;
const PinFunction _function;
const PwmParams _pwmParams;
};
#endif // PIN_H