Skip to content

Commit

Permalink
add example control with buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefichen5 committed Mar 21, 2020
1 parent 323c101 commit 7c6fe45
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ESP32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
idf_component_register(
SRCS "main.cpp" "app/autonomous_controller.cpp" "hw_interface/uart_device.cpp"
SRCS "main.cpp" "app/autonomous_controller.cpp" "hw_interface/uart_device.cpp" "hw_interface/gpio_pin.cpp"
INCLUDE_DIRS ""
)
13 changes: 12 additions & 1 deletion ESP32/main/hw_interface/gpio_pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ gpio_pin::~gpio_pin() {
gpio_set_level(pin_nr, off);
}

bool gpio_pin::set_mode(gpio_mode_t const mode) {
bool gpio_pin::set_direction(gpio_mode_t const mode) {
auto retval = gpio_set_direction(pin_nr, mode);

return retval == ESP_OK;
Expand All @@ -38,3 +38,14 @@ bool gpio_pin::set_mode(gpio_mode_t const mode) {
gpio_num_t gpio_pin::get_nr() const {
return pin_nr;
}

bool gpio_pin::enable_pullup(bool enable) {
esp_err_t ret = ESP_OK;

if(enable){
ret = gpio_pullup_en(pin_nr);
} else{
ret = gpio_pullup_dis(pin_nr);
}
return ret == ESP_OK;
}
4 changes: 3 additions & 1 deletion ESP32/main/hw_interface/gpio_pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class gpio_pin {
gpio_pin_state get_state() const;
bool set_state(gpio_pin_state const state);

bool set_mode(gpio_mode_t const mode);
bool set_direction(gpio_mode_t const mode);

bool enable_pullup(bool enable);

gpio_num_t get_nr() const;

Expand Down
28 changes: 25 additions & 3 deletions ESP32/main/main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
#include <iostream>
#include <driver/gpio.h>
#include "hw_interface/uart_device.h"
#include "hw_interface/gpio_pin.h"
#include "app/autonomous_controller.h"

extern "C" void app_main(void)
{
std::cout << "Hello world" << std::endl;

uart_device uart(UART_NUM_2, 9600, 23, 22);
uart_device uart(UART_NUM_2, 9600, 23, 22);
autonomous_controller controller(&uart);

controller.get_height();
controller.go_to_height(0x4D);
gpio_pin up_button(GPIO_NUM_32);
gpio_pin down_button(GPIO_NUM_27);

up_button.set_direction(GPIO_MODE_INPUT);
down_button.set_direction(GPIO_MODE_INPUT);
up_button.enable_pullup(true);
down_button.enable_pullup(true);

//controller.get_height();
//controller.go_to_height(0x4D);

while (true){
vTaskDelay(200 / portTICK_PERIOD_MS);
if(up_button.get_state() == gpio_pin::off){
controller.go_up();
//std::cout << "up" << std::endl;
} else if(down_button.get_state() == gpio_pin::off){
//std::cout << "down" << std::endl;
controller.go_down();
}
std::cout << controller.get_height_inch() << std::endl;
}
}

0 comments on commit 7c6fe45

Please sign in to comment.