ESPHome  2024.4.0
gpio_arduino.cpp
Go to the documentation of this file.
1 #ifdef USE_LIBRETINY
2 
3 #include "gpio_arduino.h"
4 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace libretiny {
8 
9 static const char *const TAG = "lt.gpio";
10 
11 static int IRAM_ATTR flags_to_mode(gpio::Flags flags) {
12  if (flags == gpio::FLAG_INPUT) {
13  return INPUT;
14  } else if (flags == gpio::FLAG_OUTPUT) {
15  return OUTPUT;
16  } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
17  return INPUT_PULLUP;
18  } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLDOWN)) {
19  return INPUT_PULLDOWN;
20  } else if (flags == (gpio::FLAG_OUTPUT | gpio::FLAG_OPEN_DRAIN)) {
21  return OUTPUT_OPEN_DRAIN;
22  } else {
23  return 0;
24  }
25 }
26 
27 struct ISRPinArg {
28  uint8_t pin;
29  bool inverted;
30 };
31 
33  auto *arg = new ISRPinArg{}; // NOLINT(cppcoreguidelines-owning-memory)
34  arg->pin = pin_;
35  arg->inverted = inverted_;
36  return ISRInternalGPIOPin((void *) arg);
37 }
38 
39 void ArduinoInternalGPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const {
40  PinStatus arduino_mode = (PinStatus) 255;
41  switch (type) {
43  arduino_mode = inverted_ ? FALLING : RISING;
44  break;
46  arduino_mode = inverted_ ? RISING : FALLING;
47  break;
49  arduino_mode = CHANGE;
50  break;
52  arduino_mode = inverted_ ? HIGH : LOW;
53  break;
55  arduino_mode = inverted_ ? LOW : HIGH;
56  break;
57  }
58 
59  attachInterruptParam(pin_, func, arduino_mode, arg);
60 }
61 
63  pinMode(pin_, flags_to_mode(flags)); // NOLINT
64 }
65 
67  char buffer[32];
68  snprintf(buffer, sizeof(buffer), "%u", pin_);
69  return buffer;
70 }
71 
73  return bool(digitalRead(pin_)) ^ inverted_; // NOLINT
74 }
76  digitalWrite(pin_, value ^ inverted_); // NOLINT
77 }
79  detachInterrupt(pin_); // NOLINT
80 }
81 
82 } // namespace libretiny
83 
84 using namespace libretiny;
85 
86 bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
87  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
88  return bool(digitalRead(arg->pin)) ^ arg->inverted; // NOLINT
89 }
90 void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
91  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
92  digitalWrite(arg->pin, value ^ arg->inverted); // NOLINT
93 }
94 void IRAM_ATTR ISRInternalGPIOPin::clear_interrupt() {
95  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
96  detachInterrupt(arg->pin);
97 }
98 void IRAM_ATTR ISRInternalGPIOPin::pin_mode(gpio::Flags flags) {
99  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
100  pinMode(arg->pin, flags_to_mode(flags)); // NOLINT
101 }
102 
103 } // namespace esphome
104 
105 #endif // USE_LIBRETINY
void digital_write(bool value) override
ISRInternalGPIOPin to_isr() const override
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:66
void pin_mode(gpio::Flags flags)
Definition: gpio.cpp:128
uint8_t type
std::string dump_summary() const override
const uint32_t flags
Definition: stm32flash.h:85
InterruptType
Definition: gpio.h:40
void pin_mode(gpio::Flags flags) override
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
void digital_write(bool value)
Definition: gpio.cpp:121