pin_registry.h 873 B

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. #include <vector>
  3. #include "esphome/core/gpio.h"
  4. namespace esphome::gxepd2 {
  5. // Maps GxEPD2's plain int16_t pin numbers back to the real esphome::GPIOPin
  6. // each one stands for. IDs are assigned in Python at codegen time, not via
  7. // any runtime allocation call, so there's no construction-order hazard.
  8. class PinRegistry {
  9. public:
  10. static void set(int16_t id, GPIOPin *pin) {
  11. if (id < 0 || pin == nullptr)
  12. return;
  13. if (static_cast<size_t>(id) >= pins().size())
  14. pins().resize(id + 1, nullptr);
  15. pins()[id] = pin;
  16. pin->setup();
  17. }
  18. static GPIOPin *get(int16_t id) {
  19. if (id < 0 || static_cast<size_t>(id) >= pins().size())
  20. return nullptr;
  21. return pins()[id];
  22. }
  23. protected:
  24. static std::vector<GPIOPin *> &pins() {
  25. static std::vector<GPIOPin *> pins;
  26. return pins;
  27. }
  28. };
  29. } // namespace esphome::gxepd2