Added and tested GPIO class
This commit is contained in:
@@ -2,9 +2,13 @@
|
||||
# for more information about component CMakeLists.txt files.
|
||||
|
||||
idf_component_register(
|
||||
SRCS main.c # list the source files of this component
|
||||
SRCS # list the source files of this component
|
||||
"main.cpp"
|
||||
"src/gpio.cpp"
|
||||
INCLUDE_DIRS # optional, add here public include directories
|
||||
"inc"
|
||||
PRIV_INCLUDE_DIRS # optional, add here private include directories
|
||||
REQUIRES # optional, list the public requirements (component names)
|
||||
PRIV_REQUIRES # optional, list the private requirements
|
||||
)
|
||||
|
||||
|
||||
@@ -60,11 +60,12 @@ class GPIO
|
||||
{
|
||||
public:
|
||||
|
||||
bool GPIO(int number, GPIO_Direction_t direction);
|
||||
GPIO(int number, GPIO_Direction_t direction);
|
||||
|
||||
bool GPIO_setOutput(GPIO_Value_t value);
|
||||
bool SetOutput(GPIO_Value_t value);
|
||||
|
||||
GPIO_Value_t GetInput(void);
|
||||
|
||||
GPIO_Value_t GPIO_getInput(void);
|
||||
|
||||
private:
|
||||
|
||||
@@ -72,10 +73,11 @@ class GPIO
|
||||
|
||||
GPIO_Direction_t direction;
|
||||
|
||||
GPIO_Value_t value;
|
||||
GPIO_Value_t value = GPIO_VALUE_LOW;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
#endif /* MAIN_INC_GPIO_H_ */
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
while (true) {
|
||||
printf("Hello from app_main!\n");
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <cstdlib>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "inc/gpio.h"
|
||||
|
||||
static TaskHandle_t devTaskHandle = NULL;
|
||||
static void devTask(void* parameters);
|
||||
static GPIO gpio0(0, GPIO_DIRECTION_OUTPUT);
|
||||
static GPIO gpio1(1, GPIO_DIRECTION_OUTPUT);
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
|
||||
// Create the development task
|
||||
if(xTaskCreate(devTask, "DevTask", 2048, NULL, 3, &devTaskHandle) != pdPASS)
|
||||
{
|
||||
printf("Task not created");
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
vTaskDelay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void devTask(void* parameters)
|
||||
{
|
||||
uint32_t counter = 0;
|
||||
|
||||
printf("DevTask created");
|
||||
while (true)
|
||||
{
|
||||
(void)gpio0.SetOutput((GPIO_Value_t)(counter % 2));
|
||||
(void)gpio1.SetOutput((GPIO_Value_t)(counter % 7));
|
||||
counter++;
|
||||
vTaskDelay(100);
|
||||
}
|
||||
}
|
||||
+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