Skip to content

Instantly share code, notes, and snippets.

@rustyeddy
Created May 19, 2019 20:35
Show Gist options
  • Save rustyeddy/da838496344c327869488602975ba437 to your computer and use it in GitHub Desktop.
Save rustyeddy/da838496344c327869488602975ba437 to your computer and use it in GitHub Desktop.
esp32 C++ Program
#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);
};
#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 ...
}
#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);
}
#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