12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #include "kbf/gpio.h"
- #include <esp_log.h>
- #include <esp_sleep.h>
- #include "kbf.h"
- #include "kbf/macros.h"
- using namespace kbf;
- gpio::Output::Output(int pin) : pin(static_cast<gpio_num_t>(pin)) {
- ESP_LOGI(TAG, "setting GPIO %d to OUTPUT mode", pin);
- CHECK(gpio_reset_pin(this->pin));
- CHECK(gpio_set_direction(this->pin, GPIO_MODE_OUTPUT));
- }
- void gpio::Output::high() const {
- gpio_set_level(pin, 1);
- }
- void gpio::Output::low() const {
- gpio_set_level(pin, 0);
- }
- bool gpio::Output::isHigh() const {
- return gpio_get_level(pin);
- }
- void gpio::Output::startPulse(int delay_ms, int limit) {
- ESP_LOGI(TAG, "starting pulse on GPIO %d; delay = %d, limit = %d", pin, delay_ms, limit);
- string taskName = "kbf_gpio_p" + std::to_string(pin);
- if (pulseTask != nullptr && pulseTask->isRunning()) {
- ESP_LOGE(TAG, "pulse task already running");
- ABORT("fix me");
- }
- pulseDelay = delay_ms;
- pulseLimit = limit;
- delete pulseTask;
- pulseTask = new Task(taskName, pulseCallback, this);
- }
- void gpio::Output::stopPulse() {
- ESP_LOGI(TAG, "stopping pulse on GPIO %d", pin);
- if (pulseTask == nullptr) {
- ESP_LOGE(TAG, "pulse task not running");
- ABORT("fix me");
- }
- pulseTask->stop();
- low();
- }
- void gpio::Output::pulseCallback(void *arg_ptr) {
- auto output = static_cast<Output *>(arg_ptr);
- int count = 0;
- while (output->pulseLimit < 0 || count++ < output->pulseLimit) {
- output->high();
- kbf::sleep(output->pulseDelay);
- output->low();
- kbf::sleep(output->pulseDelay);
- }
- }
|