gpio.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef KBF_GPIO_H
  2. #define KBF_GPIO_H
  3. #include <driver/gpio.h>
  4. #include "task.h"
  5. /**
  6. * General-Purpose Input/Output pin functions
  7. */
  8. namespace kbf::gpio {
  9. static constexpr const char *TAG = "kbf::gpio";
  10. class Output {
  11. public:
  12. /**
  13. * Initialises GPIO pin by resetting it and setting it's direction to GPIO_MODE_OUTPUT.
  14. *
  15. * @param pin GPIO pin number
  16. */
  17. explicit Output(int pin);
  18. /**
  19. * GPIO pin number
  20. */
  21. const gpio_num_t pin;
  22. /**
  23. * Sets pin output to HIGH.
  24. */
  25. void high() const;
  26. /**
  27. * Sets pin output to LOW.
  28. */
  29. void low() const;
  30. /**
  31. * Checks the current state of the pin.
  32. *
  33. * @return true if pin is HIGH; false if pin is LOW
  34. */
  35. [[nodiscard]] bool isHigh() const;
  36. /**
  37. * Alternates between HIGH and LOW states, sleeping delay_ms milliseconds in between. Starts with HIGH.
  38. *
  39. * @note If the limit is negative (default), it will continue indefinitely.
  40. *
  41. * @note This method uses kbf::sleep() which in turn uses vTaskDelay() from RTOS which is fine for
  42. * blinking an LED but is probably not good enough for high precision stuff.
  43. *
  44. * @param delay_ms delay in milliseconds
  45. * @param limit number of times to loop; if negative, loop indefinitely.
  46. */
  47. void startPulse(int delay_ms, int limit = -1);
  48. /**
  49. * Stop pulse loop.
  50. *
  51. * @note Will ABORT if pulse loop not running
  52. */
  53. void stopPulse();
  54. private:
  55. kbf::Task *pulseTask = nullptr;
  56. int pulseDelay = 0;
  57. int pulseLimit = 0;
  58. /**
  59. * Callback for pulse task.
  60. *
  61. * @param arg Output instance
  62. */
  63. static void pulseCallback(void *arg);
  64. };
  65. }
  66. #endif //KBF_GPIO_H