PWM based motor driver added from other project
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @file gpioPort.cpp
|
||||
* @author Gustice
|
||||
* @brief C++ Wrapper for GPIO port
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*/
|
||||
|
||||
#include "gpioPort.hpp"
|
||||
#include "esp_log.h"
|
||||
|
||||
///
|
||||
/// Gpio - Implementation
|
||||
///
|
||||
uint64_t GpioPort::_reservedPorts = 0;
|
||||
|
||||
GpioPort::GpioPort(gpio_num_t port) : _port(port) {
|
||||
uint64_t p = 1ULL << port;
|
||||
if ((_reservedPorts & p) != 0) {
|
||||
ESP_LOGW("HAL", "GpioPort Port %d is already in use!", port);
|
||||
}
|
||||
|
||||
_reservedPorts |= p;
|
||||
}
|
||||
|
||||
GpioPort::~GpioPort() {
|
||||
uint64_t p = 1ULL << _port;
|
||||
_reservedPorts &= ~p;
|
||||
}
|
||||
|
||||
///
|
||||
/// InputPort - Implementation
|
||||
///
|
||||
InputPort::InputPort(gpio_num_t port, GpioPort::PullResistor resistor) : GpioPort(port) {
|
||||
gpio_config_t config = {
|
||||
.pin_bit_mask = 1ULL << port,
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = resistor == PullUp ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = resistor == PullDown ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(gpio_config(&config));
|
||||
}
|
||||
|
||||
bool InputPort::read(void) {
|
||||
auto value = gpio_get_level(_port);
|
||||
return ((value ^ _logic) & 0x01) == 1;
|
||||
}
|
||||
|
||||
///
|
||||
/// TriggeredInput - Implementation
|
||||
///
|
||||
bool TriggeredInput::_isrServiceInitiated = false;
|
||||
|
||||
TriggeredInput::TriggeredInput(gpio_num_t port, GpioPort::PullResistor resistor,
|
||||
TriggeredInput::Interrupt edge)
|
||||
: InputPort(port, resistor) {
|
||||
assert(gpio_set_intr_type(_port, (gpio_int_type_t)edge) == ESP_OK);
|
||||
|
||||
if (!_isrServiceInitiated) {
|
||||
_isrServiceInitiated = true;
|
||||
esp_err_t err = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL2);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI("HAL", "ISR-service installed successfully for gpio-port");
|
||||
} else {
|
||||
ESP_LOGE("HAL", "ISR-service failed to install. Code '%s'", esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TriggeredInput::RegisterIsr(gpio_isr_t pIsrHandle) {
|
||||
ESP_LOGI("HAL", "Hooking ISR-Function to gpio-port %d", _port);
|
||||
gpio_isr_handler_add(_port, pIsrHandle, nullptr);
|
||||
}
|
||||
|
||||
///
|
||||
/// OutputPort - Implementation
|
||||
///
|
||||
OutputPort::OutputPort(gpio_num_t port, GpioPort::Logic mode) : GpioPort(port), _mode(mode) {
|
||||
gpio_config_t io_conf = {
|
||||
.pin_bit_mask = 1ULL << port,
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
gpio_config(&io_conf);
|
||||
}
|
||||
|
||||
void OutputPort::write(int active) {
|
||||
if (_mode == GpioPort::Normal) {
|
||||
gpio_set_level(_port, active & 1);
|
||||
} else {
|
||||
active ^= 1;
|
||||
gpio_set_level(_port, active & 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user