FastLED 3.9.7
Loading...
Searching...
No Matches
digital_pin.cpp
1
2#include <stdint.h>
3
4#include "fl/ui.h"
5#include "fl/ptr.h"
6
7#include "fl/namespace.h"
8#include "digital_pin.h"
9
10
11
12#if !defined(USE_ARDUINO) && __has_include(<Arduino.h>)
13#define USE_ARDUINO 1
14#else
15#define USE_ARDUINO 0
16#endif
17
18
19#if USE_ARDUINO
20#include <Arduino.h> // ok include
21#else
22// Fallback
23#define FASTLED_INTERNAL
24#include "FastLED.h"
25#include "fastpin.h"
26#endif
27
28
29namespace fl {
30
31
32#if USE_ARDUINO
33class DigitalPinImpl : public Referent {
34 public:
35 DigitalPinImpl(int DigitalPin) : mDigitalPin(DigitalPin) {}
36 ~DigitalPinImpl() = default;
37
38 void setPinMode(DigitalPin::Mode mode) {
39 switch (mode) {
40 case DigitalPin::kInput:
41 ::pinMode(mDigitalPin, INPUT);
42 break;
43 case DigitalPin::kOutput:
44 ::pinMode(mDigitalPin, OUTPUT);
45 break;
46 }
47 }
48 bool high() { return HIGH == ::digitalRead(mDigitalPin); }
49 void write(bool value) { ::digitalWrite(mDigitalPin, value ? HIGH : LOW); }
50
51 private:
52 int mDigitalPin;
53};
54
55#else
56class DigitalPinImpl : public Referent {
57 public:
58 DigitalPinImpl(int pin) : mPin(pin) {}
59 ~DigitalPinImpl() = default;
60
61 void setPinMode(DigitalPin::Mode mode) {
62 switch (mode) {
63 case DigitalPin::kInput:
64 mPin.setInput();
65 break;
66 case DigitalPin::kOutput:
67 mPin.setOutput();
68 break;
69 }
70 }
71
72 bool high() { return mPin.hival(); }
73 void write(bool value) { value ? mPin.hi(): mPin.lo(); }
74 // define pin
75 Pin mPin;
76};
77#endif
78
79
80DigitalPin::DigitalPin(int DigitalPin) {
81 mImpl = DigitalPinImplPtr::New(DigitalPin);
82}
83DigitalPin::~DigitalPin() = default;
84DigitalPin::DigitalPin(const DigitalPin &other) = default;
85
86DigitalPin& DigitalPin::operator=(const DigitalPin &other) = default;
87
88void DigitalPin::setPinMode(Mode mode) {
89 mImpl->setPinMode(mode);
90}
91
92bool DigitalPin::high() const {
93 return mImpl->high();
94}
95
96void DigitalPin::write(bool is_high) {
97 mImpl->write(is_high);
98}
99
100} // namespace fl
central include file for FastLED, defines the CFastLED class/object
Naive fallback solution for low level pin access.
Definition fastpin.h:47
void setOutput()
Set the pin mode as OUTPUT
Definition fastpin.h:73
void setInput()
Set the pin mode as INPUT
Definition fastpin.h:76
void hi()
Set the pin state to HIGH
Definition fastpin.h:79
void lo()
Set the pin state to LOW
Definition fastpin.h:81
port_t hival()
Gets the state of the port with this pin HIGH
Definition fastpin.h:106
Class base definitions for defining fast pin access.
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16