a0d13f08c1
- Added GPIO handling - Added a logger class with additional static debug log handling Tested, functional
128 lines
3.9 KiB
C++
128 lines
3.9 KiB
C++
// --------------------------------------------------------------------------------------------------------------------
|
|
/// \file gpio.c
|
|
/// \brief Description
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
//
|
|
// vbchaos software design
|
|
//
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
/// $Revision: $
|
|
/// $Author: $
|
|
/// $Date: $
|
|
// (c) 2023 vbchaos
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
// Include files
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
#include <gpio.h>
|
|
#include "driver/gpio.h"
|
|
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
// Constant and macro definitions
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
// Type definitions
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
// File-scope variables
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
// Function declarations
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
// Function definitions
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
gpio::gpio(uint32_t number, Direction_t direction, Value_t initialValue)
|
|
{
|
|
this->number = number;
|
|
this->direction = direction;
|
|
|
|
//zero-initialize the config structure.
|
|
gpio_config_t io_conf = {};
|
|
//disable interrupt
|
|
io_conf.intr_type = GPIO_INTR_DISABLE;
|
|
//set as output mode
|
|
io_conf.mode = (this->direction == GPIO_DIRECTION_OUTPUT) ? GPIO_MODE_OUTPUT : GPIO_MODE_INPUT;
|
|
//bit mask of the pins that you want to set
|
|
io_conf.pin_bit_mask = (1ULL << this->number);
|
|
//disable pull-down mode
|
|
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
|
//disable pull-up mode
|
|
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
|
|
//configure GPIO with the given settings
|
|
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
|
|
|
this->currentState = GPIO_VALUE_LOW;
|
|
|
|
set(initialValue);
|
|
|
|
}
|
|
|
|
|
|
FunctionStatus gpio::set(Value_t value)
|
|
{
|
|
FunctionStatus returnValue = FUNCTION_STATUS_ERROR;
|
|
|
|
if (this->direction == GPIO_DIRECTION_OUTPUT)
|
|
{
|
|
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t)this->number, (uint32_t)value));
|
|
this->currentState = value;
|
|
returnValue = FUNCTION_STATUS_OK;
|
|
}
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
|
|
FunctionStatus gpio::get(Value_t* value)
|
|
{
|
|
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
|
|
|
if (gpio_get_level((gpio_num_t)this->number))
|
|
{
|
|
this->currentState = GPIO_VALUE_HIGH;
|
|
}
|
|
else
|
|
{
|
|
this->currentState = GPIO_VALUE_LOW;
|
|
}
|
|
|
|
*value = this->currentState;
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
|
|
FunctionStatus gpio::toogle()
|
|
{
|
|
FunctionStatus returnValue = FUNCTION_STATUS_ERROR;
|
|
|
|
if (this->currentState == GPIO_VALUE_LOW)
|
|
{
|
|
// Going to HIGH
|
|
set(GPIO_VALUE_HIGH);
|
|
}
|
|
else
|
|
{
|
|
// Going to LOW
|
|
set(GPIO_VALUE_LOW);
|
|
}
|
|
|
|
return returnValue;
|
|
}
|