Skip to content

Commit

Permalink
Added LED and compilation define to exlude event for older IDF
Browse files Browse the repository at this point in the history
  • Loading branch information
teuteuguy committed Oct 15, 2019
1 parent 6e2786b commit 4087a6c
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/m5stickc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" {
#include "m5button.h"
#include "m5display.h"
#include "m5event.h"
#include "m5led.h"

typedef struct {
m5power_config_t power;
Expand Down
25 changes: 21 additions & 4 deletions m5button.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ esp_err_t m5button_enable(m5button_t * button)
return ESP_FAIL;
}

#if defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
button->evt_queue = xQueueCreate(1, sizeof(m5button_event_id_t));
#endif // ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY

return ESP_OK;
}

Expand All @@ -139,6 +143,9 @@ esp_err_t m5button_disable(m5button_t * button)

vEventGroupDelete(button->event_group);
vTaskDelete(button->task);
#if defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
vQueueDelete(button->evt_queue);
#endif // ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY

return ESP_OK;
}
Expand Down Expand Up @@ -183,7 +190,7 @@ void m5button_task(void * pvParameter)
EventBits_t event;
m5button_t * button = (m5button_t *) pvParameter;

ESP_LOGD(TAG, "Button task started");
ESP_LOGD(TAG, "Button %u task started", button->gpio);

while(1) {
event = xEventGroupWaitBits(button->event_group, M5BUTTON_BUTTON_PUSH_BIT, pdTRUE, pdFALSE, portMAX_DELAY);
Expand All @@ -192,11 +199,21 @@ void m5button_task(void * pvParameter)
xEventGroupClearBits(button->event_group, M5BUTTON_BUTTON_POP_BIT);
event = xEventGroupWaitBits(button->event_group, M5BUTTON_BUTTON_POP_BIT, pdTRUE, pdFALSE, button->hold_time / portTICK_PERIOD_MS);
if((event & M5BUTTON_BUTTON_POP_BIT) != 0) {
#if defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
button->event = M5BUTTON_BUTTON_CLICK_EVENT;
xQueueSend(button->evt_queue, &button->event, 0);
#else
esp_event_post_to(m5_event_loop, button->esp_event_base, M5BUTTON_BUTTON_CLICK_EVENT, NULL, 0, portMAX_DELAY);
ESP_LOGD(TAG, "BUTTON_CLICK event");
} else {
#endif // ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY
ESP_LOGD(TAG, "BUTTON_CLICK %u event", button->gpio);
} else {
#if defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
button->event = M5BUTTON_BUTTON_HOLD_EVENT;
xQueueSend(button->evt_queue, &button->event, 0);
#else
esp_event_post_to(m5_event_loop, button->esp_event_base, M5BUTTON_BUTTON_HOLD_EVENT, NULL, 0, portMAX_DELAY);
ESP_LOGD(TAG, "BUTTON_HOLD event");
#endif // ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY
ESP_LOGD(TAG, "BUTTON_HOLD %u event", button->gpio);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions m5button.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ typedef struct {
uint32_t debounce_time; /*!< Button debounce time */
uint32_t hold_time; /*!< Button hold time */
esp_event_base_t esp_event_base; /*!< Button event base */
#if defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
xQueueHandle evt_queue; /*!< Button event queue */
m5button_event_id_t event; /*!< Button event */
#endif // ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY
EventGroupHandle_t event_group; /*!< Event group handle */
TaskHandle_t task; /*!< Button task handle */
#if defined(CONFIG_SUPPORT_STATIC_ALLOCATION)
Expand Down
6 changes: 6 additions & 0 deletions m5display.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ esp_err_t m5display_on()

esp_err_t m5display_timeout(uint32_t timeout)
{
#if !defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
esp_err_t e;

e = esp_event_handler_register_with(m5_event_loop, M5BUTTON_A_EVENT_BASE, ESP_EVENT_ANY_ID, m5display_event_handler, NULL);
Expand Down Expand Up @@ -162,16 +163,21 @@ esp_err_t m5display_timeout(uint32_t timeout)
ESP_LOGE(TAG, "[FAIL] Error starting display timeout timer");
return ESP_FAIL;
}
#else
return ESP_OK;
#endif // ESP_EVENT_DECLARE_BASE
}

void m5display_wakeup()
{
m5display_on();
#if !defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
if(xTimerReset(m5display_timer, 0) == pdTRUE) {
ESP_LOGD(TAG, "[ OK ] Display timeout timer reset");
} else {
ESP_LOGE(TAG, "[FAIL] Error resetting display timeout timer");
}
#endif // ESP_EVENT_DECLARE_BASE
}

void m5display_sleep()
Expand Down
8 changes: 8 additions & 0 deletions m5display.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ extern "C" {
#define M5DISPLAY_WIDTH 160 /*!< Display width in pixels after rotation */
#define M5DISPLAY_HEIGHT 80 /*!< Display height in pixels after rotation */

// Renames of TFT Library
#define TFT_FONT_ROTATE font_rotate
#define TFT_TEXT_WRAP text_wrap
#define TFT_FONT_TRANSPARENT font_transparent
#define TFT_FONT_FORCEFIXED font_forceFixed
#define TFT_GRAY_SCALE gray_scale
#define TFT_FONT_BACKGROUND _bg
#define TFT_FONT_FOREGROUND _fg

extern spi_lobo_device_handle_t m5display_spi; /*!< SPI device handle */

Expand Down
6 changes: 6 additions & 0 deletions m5event.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ esp_event_loop_handle_t m5_event_loop;

esp_err_t m5event_init()
{
#if !defined(ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY)
esp_event_loop_args_t loop_args = {
.queue_size = 5,
.task_name = "m5_event_loop",
Expand All @@ -29,4 +30,9 @@ esp_err_t m5event_init()
ESP_LOGE(TAG, "Error creating event loop: %s", esp_err_to_name(e));
return ESP_FAIL;
}
#else
ESP_LOGD(TAG, "Event loop created");
return ESP_OK;
#endif // ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY

}
14 changes: 14 additions & 0 deletions m5event.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ extern "C" {
#endif

#include "esp_log.h"
#include "esp_err.h"

#if defined(ESP_EVENT_DECLARE_BASE)
#include "esp_event.h"
#else
#define ESP_EVENT_DECLARE_BASE(id) extern esp_event_base_t id
#define ESP_EVENT_DEFINE_BASE(id) esp_event_base_t id = #id
#define ESP_IDF_DOES_NOT_HAVE_EVENTS_LIBRARY
typedef const char* esp_event_base_t; /**< unique pointer to a subsystem that exposes events */
typedef void* esp_event_loop_handle_t; /**< a number that identifies an event with respect to a base */
typedef void (*esp_event_handler_t)(void* event_handler_arg,
esp_event_base_t event_base,
int32_t event_id,
void* event_data); /**< function called when an event is posted to the queue */
#endif // ESP_EVENT_DECLARE_BASE

extern esp_event_loop_handle_t m5_event_loop; /*!< Event loop for M5 device-specific events */

Expand Down
72 changes: 72 additions & 0 deletions m5led.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* m5led.c
*
* (C) 2019 - Teuteuguy
* This code is licensed under the MIT License.
*/

#include "m5led.h"

static const char * TAG = "m5led";

static bool current_state = M5LED_DEFAULT_STATE;

esp_err_t m5led_init()
{
esp_err_t e;

gpio_config_t io_conf;
// Setup the LED
io_conf.intr_type = GPIO_PIN_INTR_DISABLE; //disable interrupt
io_conf.mode = GPIO_MODE_OUTPUT; //set as output mode
io_conf.pin_bit_mask = ((1ULL << M5LED_GPIO)); // bit mask of the pins that you want to set, e.g.GPIO10
io_conf.pull_down_en = 0; //disable pull-down mode
io_conf.pull_up_en = 0; //disable pull-up mode
e = gpio_config(&io_conf); //configure GPIO with the given settings
if (e != ESP_OK)
{
ESP_LOGE(TAG, "Error setting up LED: %u", e);
return e;
}

e = gpio_set_level(M5LED_GPIO, M5LED_DEFAULT_STATE);
if (e != ESP_OK)
{
return ESP_FAIL;
}

ESP_LOGD(TAG, "LED G10 enabled");
return ESP_OK;
}

bool m5led_is_on(void)
{
return current_state;
}

esp_err_t m5led_set(bool state)
{
esp_err_t e;

current_state = state;
e = gpio_set_level(M5LED_GPIO, current_state);
if (e != ESP_OK)
{
return ESP_FAIL;
}

return ESP_OK;
}

esp_err_t m5led_toggle(void)
{
esp_err_t e;

e = m5led_set(1 - current_state);
if (e != ESP_OK)
{
return ESP_FAIL;
}

return ESP_OK;
}
62 changes: 62 additions & 0 deletions m5led.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* m5led.h
*
* (C) 2019 - Teuteuguy
* This code is licensed under the MIT License.
*/

#ifndef _M5LED_H_
#define _M5LED_H_

#ifdef __cplusplus
extern "C" {
#endif
#include "esp_system.h"
#include "esp_log.h"

#define M5LED_ON 0
#define M5LED_OFF 1

#define M5LED_DEFAULT_STATE M5LED_OFF
#define M5LED_GPIO GPIO_NUM_10

/**
* @brief Initialize led
* *
* @return ESP_OK success
* ESP_FAIL failed
*/
esp_err_t m5led_init();

/**
* @brief Check if led is on
*
* @return false not on
* true otherwise on
*/
bool m5led_is_on(void);

/**
* @brief Set led.
*
* @param state led state to set
* @return ESP_OK success
* ESP_FAIL failed
* ESP_ERR_INVALID_ARG led null or state is invalid
*/
esp_err_t m5led_set(bool state);

/**
* @brief Toggle led.
*
* @return ESP_OK success
* ESP_FAIL failed
* ESP_ERR_INVALID_ARG led null or state is invalid
*/
esp_err_t m5led_toggle(void);

#ifdef __cplusplus
}
#endif

#endif // _M5BUTTON_H_
12 changes: 12 additions & 0 deletions m5stickc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ esp_err_t m5_init(m5stickc_config_t * config) {
++error_count;
}

// Init led
e = m5led_init();
if (e == ESP_OK)
{
ESP_LOGD(TAG, "Led initialized");
}
else
{
ESP_LOGE(TAG, "Error initializing led");
++error_count;
}

// Init display
e = m5display_init();
if(e == ESP_OK) {
Expand Down

0 comments on commit 4087a6c

Please sign in to comment.