Added and tested GPIO class
This commit is contained in:
+57
-3
@@ -17,9 +17,9 @@
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <gpio.h>
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
@@ -50,14 +50,68 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
bool GPIO::GPIO(int number, GPIO_Direction_t direction)
|
||||
|
||||
GPIO::GPIO(int number, GPIO_Direction_t direction)
|
||||
{
|
||||
this->number = number;
|
||||
this->direction = direction;
|
||||
|
||||
return true;
|
||||
|
||||
//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));
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool GPIO::SetOutput(GPIO_Value_t value)
|
||||
{
|
||||
bool returnValue = false;
|
||||
|
||||
if (this->direction == GPIO_DIRECTION_OUTPUT)
|
||||
{
|
||||
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t)this->number, (uint32_t)value));
|
||||
this->value = value;
|
||||
returnValue = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = false;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
GPIO_Value_t GPIO::GetInput(void)
|
||||
{
|
||||
if (gpio_get_level((gpio_num_t)this->number))
|
||||
{
|
||||
this->value = GPIO_VALUE_HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->value = GPIO_VALUE_LOW;
|
||||
}
|
||||
|
||||
return this->value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user