12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #ifndef KBF_GPIO_H
- #define KBF_GPIO_H
- #include <driver/gpio.h>
- #include "task.h"
- /**
- * General-Purpose Input/Output pin functions
- */
- namespace kbf::gpio {
- static constexpr const char *TAG = "kbf::gpio";
- class Output {
- public:
- /**
- * Initialises GPIO pin by resetting it and setting it's direction to GPIO_MODE_OUTPUT.
- *
- * @param pin GPIO pin number
- */
- explicit Output(int pin);
- /**
- * GPIO pin number
- */
- const gpio_num_t pin;
- /**
- * Sets pin output to HIGH.
- */
- void high() const;
- /**
- * Sets pin output to LOW.
- */
- void low() const;
- /**
- * Checks the current state of the pin.
- *
- * @return true if pin is HIGH; false if pin is LOW
- */
- [[nodiscard]] bool isHigh() const;
- /**
- * Alternates between HIGH and LOW states, sleeping delay_ms milliseconds in between. Starts with HIGH.
- *
- * @note If the limit is negative (default), it will continue indefinitely.
- *
- * @note This method uses kbf::sleep() which in turn uses vTaskDelay() from RTOS which is fine for
- * blinking an LED but is probably not good enough for high precision stuff.
- *
- * @param delay_ms delay in milliseconds
- * @param limit number of times to loop; if negative, loop indefinitely.
- */
- void startPulse(int delay_ms, int limit = -1);
- /**
- * Stop pulse loop.
- *
- * @note Will ABORT if pulse loop not running
- */
- void stopPulse();
- private:
- kbf::Task *pulseTask = nullptr;
- int pulseDelay = 0;
- int pulseLimit = 0;
- /**
- * Callback for pulse task.
- *
- * @param arg Output instance
- */
- static void pulseCallback(void *arg);
- };
- }
- #endif //KBF_GPIO_H
|