2015-06-08 08:09:06 +00:00
|
|
|
/* Respond to a button press.
|
|
|
|
*
|
|
|
|
* This code combines two ways of checking for a button press -
|
|
|
|
* busy polling (the bad way) and button interrupt (the good way).
|
|
|
|
*
|
|
|
|
* This sample code is in the public domain.
|
|
|
|
*/
|
|
|
|
#include "espressif/esp_common.h"
|
2015-10-06 12:04:19 +00:00
|
|
|
#include "esp/uart.h"
|
2015-06-08 08:09:06 +00:00
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "esp8266.h"
|
|
|
|
|
|
|
|
/* pin config */
|
|
|
|
const int gpio = 0; /* gpio 0 usually has "PROGRAM" button attached */
|
|
|
|
const int active = 0; /* active == 0 for active low */
|
2015-08-20 22:11:29 +00:00
|
|
|
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG;
|
2015-06-08 08:09:06 +00:00
|
|
|
#define GPIO_HANDLER gpio00_interrupt_handler
|
|
|
|
|
|
|
|
|
|
|
|
/* This task polls for the button and prints the tick
|
|
|
|
count when it's seen.
|
|
|
|
|
|
|
|
Debounced to 200ms with a simple vTaskDelay.
|
|
|
|
|
|
|
|
This is not a good example of how to wait for button input!
|
|
|
|
*/
|
|
|
|
void buttonPollTask(void *pvParameters)
|
|
|
|
{
|
|
|
|
printf("Polling for button press on gpio %d...\r\n", gpio);
|
|
|
|
while(1) {
|
2015-06-08 23:00:32 +00:00
|
|
|
while(gpio_read(gpio) != active)
|
|
|
|
{
|
|
|
|
taskYIELD();
|
|
|
|
}
|
2016-11-05 10:04:03 +00:00
|
|
|
printf("Polled for button press at %dms\r\n", xTaskGetTickCount()*portTICK_PERIOD_MS);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
2015-06-08 08:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This task configures the GPIO interrupt and uses it to tell
|
|
|
|
when the button is pressed.
|
|
|
|
|
|
|
|
The interrupt handler communicates the exact button press time to
|
|
|
|
the task via a queue.
|
|
|
|
|
|
|
|
This is a better example of how to wait for button input!
|
|
|
|
*/
|
|
|
|
void buttonIntTask(void *pvParameters)
|
|
|
|
{
|
|
|
|
printf("Waiting for button press interrupt on gpio %d...\r\n", gpio);
|
2016-11-05 10:04:03 +00:00
|
|
|
QueueHandle_t *tsqueue = (QueueHandle_t *)pvParameters;
|
2015-06-08 08:09:06 +00:00
|
|
|
gpio_set_interrupt(gpio, int_type);
|
|
|
|
|
|
|
|
uint32_t last = 0;
|
|
|
|
while(1) {
|
2015-06-08 23:00:32 +00:00
|
|
|
uint32_t button_ts;
|
|
|
|
xQueueReceive(*tsqueue, &button_ts, portMAX_DELAY);
|
2016-11-05 10:04:03 +00:00
|
|
|
button_ts *= portTICK_PERIOD_MS;
|
2015-06-08 23:00:32 +00:00
|
|
|
if(last < button_ts-200) {
|
2015-09-05 02:54:09 +00:00
|
|
|
printf("Button interrupt fired at %dms\r\n", button_ts);
|
2015-06-08 23:00:32 +00:00
|
|
|
last = button_ts;
|
|
|
|
}
|
2015-06-08 08:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 10:04:03 +00:00
|
|
|
static QueueHandle_t tsqueue;
|
2015-06-08 08:09:06 +00:00
|
|
|
|
|
|
|
void GPIO_HANDLER(void)
|
|
|
|
{
|
|
|
|
uint32_t now = xTaskGetTickCountFromISR();
|
|
|
|
xQueueSendToBackFromISR(tsqueue, &now, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void user_init(void)
|
|
|
|
{
|
2015-10-06 12:04:19 +00:00
|
|
|
uart_set_baud(0, 115200);
|
2015-06-08 08:09:06 +00:00
|
|
|
gpio_enable(gpio, GPIO_INPUT);
|
|
|
|
|
|
|
|
tsqueue = xQueueCreate(2, sizeof(uint32_t));
|
2016-10-21 09:40:36 +00:00
|
|
|
xTaskCreate(buttonIntTask, "buttonIntTask", 256, &tsqueue, 2, NULL);
|
|
|
|
xTaskCreate(buttonPollTask, "buttonPollTask", 256, NULL, 1, NULL);
|
2015-06-08 08:09:06 +00:00
|
|
|
}
|