roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
gpio_setter.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5
7
8namespace roo_display {
9
10// An abstraction for non-performance-sensitive GPIO signals that might be wired
11// through port extenders, rather than direct MCU pins.
13 public:
14 // Gets called to initialize the pin for output.
15 using InitFn = std::function<void()>;
16
17 // Gets called with state = 0 to set low, and state = 1 to set high.
18 using SetterFn = std::function<void(uint8_t state)>;
19
20 // Creates an inatcive setter (setHigh and setLow are no-ops).
21 GpioSetter() : setter_(), init_() {}
22
23 // Implicit conversion from pin number, using DefaultGpio.
24 GpioSetter(int8_t pin) : setter_(nullptr), init_(nullptr) {
25 if (pin >= 0) {
26 setter_ = [pin](uint8_t state) {
27 if (state > 0) {
29 } else {
31 }
32 };
33 init_ = [pin]() { DefaultGpio::setOutput(pin); };
34 }
35 }
36
37 // Implicit conversion from a lambda setter function, in case no
38 // initialization is needed.
39 GpioSetter(SetterFn setter) : setter_(std::move(setter)), init_() {}
40
41 // Explicit constructor in cases when initialization is needed.
43 : setter_(std::move(setter)), init_(std::move(init)) {}
44
45 bool isDefined() const { return setter_ != nullptr; }
46
47 void init() {
48 if (init_ != nullptr) {
49 init_();
50 }
51 }
52
53 void setHigh() {
54 if (setter_ != nullptr) setter_(1);
55 }
56
57 void setLow() {
58 if (setter_ != nullptr) setter_(0);
59 }
60
61 private:
62 SetterFn setter_;
63 InitFn init_;
64};
65
66} // namespace roo_display
std::function< void(uint8_t state)> SetterFn
Definition gpio_setter.h:18
GpioSetter(SetterFn setter, InitFn init)
Definition gpio_setter.h:42
std::function< void()> InitFn
Definition gpio_setter.h:15
GpioSetter(SetterFn setter)
Definition gpio_setter.h:39
Defines 140 opaque HTML named colors.
static void setHigh()
Definition gpio.h:82
static void setOutput(int pin)
Definition gpio.h:66
static void setLow()
Definition gpio.h:75