Created
May 19, 2019 20:35
-
-
Save rustyeddy/da838496344c327869488602975ba437 to your computer and use it in GitHub Desktop.
esp32 C++ Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class LED { | |
private: | |
gpio_num_t _pin = GPIO_NUM_0; | |
gpio_mode_t _mode = GPIO_MODE_OUTPUT; | |
int _state = 0; | |
public: | |
LED(gpio_num_t p); | |
void set(int val); | |
void on(); | |
void off(); | |
void toggle(); | |
void blink(int ondur, int offdir, int count); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#include "driver/gpio.h" | |
#include "led.h" | |
// LED initializes and new LED at the given pin number | |
LED::LED(gpio_num_t p) | |
{ | |
_pin = p; | |
gpio_pad_select_gpio(_pin); | |
gpio_set_direction(_pin, _mode); | |
} | |
// set the LED value to the given parameter | |
void LED::set(int val) | |
{ | |
_state = val; | |
gpio_set_level(_pin, val); | |
} | |
void LED::on() | |
{ | |
_state = 1; | |
gpio_set_level(_pin, 1); | |
} | |
void LED::off() | |
{ | |
_state = 0; | |
gpio_set_level(_pin, _state); | |
} | |
void LED::toggle() | |
{ | |
// switch state from 0 -> or 1 -> 0 | |
_state = (_state) ? 0 : 1; | |
gpio_set_level(_pin, _state); | |
} | |
void LED::blink(int onduration, int offduration, int count) | |
{ | |
// TODO ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#include "driver/gpio.h" | |
#include "sdkconfig.h" | |
#include "main.h" | |
#include "led.h" | |
// ==================================================================== | |
const int STACK_SIZE = 256; | |
const gpio_num_t LED_RED = GPIO_NUM_22; | |
const gpio_num_t LED_GREEN = GPIO_NUM_23; | |
// GLobal LEDs | |
// ==================================================================== | |
LED red = LED(LED_RED); | |
LED green = LED(LED_GREEN); | |
void app_main() { | |
int keep_going = 1; | |
int delay = 100; // micro-seconds (1/100) | |
printf("Starting the stinky blinker \n"); | |
red.on(); | |
green.off(); | |
printf(" lights are on ...\n"); | |
unsigned int loop; | |
unsigned int overflows = -1; | |
for (loop = 1; keep_going; loop++) { | |
// we don't care about no sinking overflows! | |
if (loop == 1) overflows++; // overflows will start as | |
printf(" loop[%d - %d]\n", overflows, loop); | |
red.toggle(); | |
green.toggle(); | |
vTaskDelay(delay); | |
} | |
printf("Goodbye Cruel Loop!\n"); | |
vTaskDelete(NULL); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
void app_main(); | |
#ifdef __cplusplus | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment