From d9173145de6d3bd167f29a7c95781860fca1c9da Mon Sep 17 00:00:00 2001 From: demik Date: Wed, 16 Dec 2020 21:00:00 +0100 Subject: [PATCH] added led management tasks --- main/blue.c | 14 +++++++-- main/blue.h | 2 ++ main/gpio.c | 44 +++++++++++++++++++++----- main/gpio.h | 12 +++---- main/led.c | 91 ++++++++++++++++++++++++++++++++++++++++++++--------- main/led.h | 7 +++++ main/main.c | 31 +++++++++++++----- 7 files changed, 164 insertions(+), 37 deletions(-) diff --git a/main/blue.c b/main/blue.c index 459ccb1..d3818c5 100644 --- a/main/blue.c +++ b/main/blue.c @@ -28,12 +28,14 @@ #include "esp_hid_gap.h" #include "blue.h" +#include "led.h" static const char *TAG = "blue"; #define BTMOUSE_BUTTON1 (1 << 0) #define BTMOUSE_BUTTON2 (1 << 1) #define BTMOUSE_BUTTON3 (1 << 2) +#define BTMOUSE_BUTTONE 248 void hidh_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data) { @@ -63,6 +65,7 @@ void hidh_callback(void *handler_args, esp_event_base_t base, int32_t id, void * else ESP_LOGI(TAG, "opened connection with device: " ESP_BD_ADDR_STR, ESP_BD_ADDR_HEX(bda)); //esp_hidh_dev_dump(param->open.dev, stdout); + xTaskNotify(t_blue, LED_ON, eSetValueWithOverwrite); break; } case ESP_HIDH_BATTERY_EVENT: { @@ -74,6 +77,7 @@ void hidh_callback(void *handler_args, esp_event_base_t base, int32_t id, void * const uint8_t *bda = esp_hidh_dev_bda_get(param->input.dev); ESP_LOGD(TAG, ESP_BD_ADDR_STR " INPUT: %8s, MAP: %2u, ID: %3u, Len: %d, Data:", ESP_BD_ADDR_HEX(bda), esp_hid_usage_str(param->input.usage), param->input.map_index, param->input.report_id, param->input.length); ESP_LOG_BUFFER_HEX(TAG, param->input.data, param->input.length); + xTaskNotify(t_yellow, LED_ONCE, eSetValueWithOverwrite); memcpy (&click, param->input.data, sizeof(uint8_t)); click = click & (BTMOUSE_BUTTON1 | BTMOUSE_BUTTON2 | BTMOUSE_BUTTON3); if (click) @@ -90,6 +94,7 @@ void hidh_callback(void *handler_args, esp_event_base_t base, int32_t id, void * const uint8_t *bda = esp_hidh_dev_bda_get(param->close.dev); ESP_LOGI(TAG, ESP_BD_ADDR_STR " CLOSE: '%s' %s", ESP_BD_ADDR_HEX(bda), esp_hidh_dev_name_get(param->close.dev), esp_hid_disconnect_reason_str(esp_hidh_dev_transport_get(param->close.dev), param->close.reason)); esp_hidh_dev_free(param->close.dev); + xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite); break; } default: @@ -159,8 +164,13 @@ void blue_init(void) .callback = hidh_callback, }; - ESP_ERROR_CHECK( esp_hidh_init(&config) ); + ESP_ERROR_CHECK(esp_hidh_init(&config)); - /* keep scanning until a device is found */ + /* at this point, everything but bluetooth is started. + * put green steady, start blinking blue and keep scanning until a device is found + */ + + xTaskNotify(t_green, LED_ON, eSetValueWithOverwrite); + xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite); xTaskCreatePinnedToCore(&hid_demo_task, "hid_task", 6 * 1024, NULL, 2, NULL, 0); } diff --git a/main/blue.h b/main/blue.h index 914165b..7ced702 100644 --- a/main/blue.h +++ b/main/blue.h @@ -28,6 +28,8 @@ /* prototypes */ void blue_init(void); +/* global variables for tasks handles */ +extern TaskHandle_t t_green, t_blue, t_yellow, t_red; #endif diff --git a/main/gpio.c b/main/gpio.c index 1f5461b..5b32e9f 100644 --- a/main/gpio.c +++ b/main/gpio.c @@ -1,11 +1,27 @@ -/* Hello World Example - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. +/* + * gpio.c + * quack + * + * Created by Michel DEPEIGE on 14/12/2020. + * Copyright (c) 2020 Michel DEPEIGE. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (see the file COPYING); if not, write to the + * Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * */ + #include #include "driver/gpio.h" #include "sdkconfig.h" @@ -26,4 +42,18 @@ void gpio_init(void) { gpio_set_direction(GPIO_BLUELED, GPIO_MODE_OUTPUT); gpio_set_direction(GPIO_YELLOWLED, GPIO_MODE_OUTPUT); gpio_set_direction(GPIO_REDLED, GPIO_MODE_OUTPUT); + + /* Quadrature mouse */ + gpio_reset_pin(GPIO_CLICK); + gpio_reset_pin(GPIO_QX1); + gpio_reset_pin(GPIO_QX2); + gpio_reset_pin(GPIO_QY1); + gpio_reset_pin(GPIO_QY2); + gpio_set_direction(GPIO_CLICK, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_QX1, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_QX2, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_QY1, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_QY2, GPIO_MODE_OUTPUT); + + gpio_set_level(GPIO_CLICK, 1); } diff --git a/main/gpio.h b/main/gpio.h index f0ddaf5..e1466a5 100644 --- a/main/gpio.h +++ b/main/gpio.h @@ -49,12 +49,12 @@ void gpio_init(void); * - GPIO17 */ -#define ADB_GPIO 4 -#define CLICK_HPIO 2 -#define QX1_GPIO 12 -#define QX2_GPIO 13 -#define QY1_GPIO 14 -#define QY2_GPIO 15 +#define GPIO_ADB 4 +#define GPIO_CLICK 2 +#define GPIO_QX1 12 +#define GPIO_QX2 13 +#define GPIO_QY1 14 +#define GPIO_QY2 15 #define GPIO_GREENLED 21 #define GPIO_BLUELED 25 diff --git a/main/led.c b/main/led.c index 61ed8cc..171d6de 100644 --- a/main/led.c +++ b/main/led.c @@ -1,11 +1,27 @@ -/* Hello World Example - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. +/* + * led.c + * quack + * + * Created by Michel DEPEIGE on 15/12/2020. + * Copyright (c) 2020 Michel DEPEIGE. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (see the file COPYING); if not, write to the + * Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * */ + #include #include "driver/gpio.h" #include "sdkconfig.h" @@ -17,10 +33,11 @@ #include "led.h" #include "gpio.h" -void led_init(void) { - BaseType_t r; - TaskHandle_t green, blue, yellow, red; +/* global variables for tasks handles */ +TaskHandle_t t_green, t_blue, t_yellow, t_red; +/* init all leds, blink everything once and start background tasks */ +void led_init(void) { /* blink allds LEDs once */ gpio_set_level(GPIO_GREENLED, 1); gpio_set_level(GPIO_BLUELED, 1); @@ -32,14 +49,58 @@ void led_init(void) { gpio_set_level(GPIO_YELLOWLED, 0); gpio_set_level(GPIO_REDLED, 0); - r = xTaskCreate(led_task, "GREEN", 1024, (void *) GPIO_GREENLED, tskIDLE_PRIORITY, &green); + xTaskCreate(led_task, "GREEN", 1024, (void *) GPIO_GREENLED, tskIDLE_PRIORITY, &t_green); + xTaskCreate(led_task, "BLUE", 1024, (void *) GPIO_BLUELED, tskIDLE_PRIORITY, &t_blue); + xTaskCreate(led_task, "YELLOW", 1024, (void *) GPIO_YELLOWLED, tskIDLE_PRIORITY, &t_yellow); + xTaskCreate(led_task, "RED", 1024, (void *) GPIO_REDLED, tskIDLE_PRIORITY, &t_red); + + /* blink green led fast (we are booting...) */ + xTaskNotify(t_green, LED_FAST, eSetValueWithOverwrite); } void led_task(void *pvParameters) { + unsigned int color = (unsigned int)pvParameters; + unsigned int mode = LED_OFF; + TickType_t wait = portMAX_DELAY; + + /* start only if there is a led specified */ + configASSERT(((uint32_t) pvParameters) > 0); + while (1) { - gpio_set_level(GPIO_GREENLED, 1); - vTaskDelay(75 / portTICK_PERIOD_MS); - gpio_set_level(GPIO_GREENLED, 0); - vTaskDelay(75 / portTICK_PERIOD_MS); + xTaskNotifyWait(0, 0, &mode, wait); + switch (mode) + { + case LED_OFF: + gpio_set_level(color, 0); + wait = portMAX_DELAY; + break; + case LED_ON: + gpio_set_level(color, 1); + wait = portMAX_DELAY; + break; + case LED_ONCE: + gpio_set_level(color, 1); + vTaskDelay(40 / portTICK_PERIOD_MS); + gpio_set_level(color, 0); + vTaskDelay(40 / portTICK_PERIOD_MS); + wait = portMAX_DELAY; + break; + case LED_FAST: + gpio_set_level(color, 1); + vTaskDelay(40 / portTICK_PERIOD_MS); + gpio_set_level(color, 0); + wait = 40 / portTICK_PERIOD_MS; + break; + case LED_SLOW: + gpio_set_level(color, 1); + vTaskDelay(500 / portTICK_PERIOD_MS); + gpio_set_level(color, 0); + wait = 500 / portTICK_PERIOD_MS; + break; + default: + break; + } } + + gpio_set_level(color, 1); } diff --git a/main/led.h b/main/led.h index 5c508f6..8405bc2 100644 --- a/main/led.h +++ b/main/led.h @@ -29,5 +29,12 @@ void led_init(void); void led_task(void *pvParameters); +/* defines */ +#define LED_OFF (1 << 0) +#define LED_ON (1 << 1) +#define LED_ONCE (1 << 2) +#define LED_SLOW (1 << 3) +#define LED_FAST (1 << 4) + #endif diff --git a/main/main.c b/main/main.c index a23c817..187c8ba 100644 --- a/main/main.c +++ b/main/main.c @@ -1,11 +1,27 @@ -/* Hello World Example - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. +/* + * main.c + * quack + * + * Created by Michel DEPEIGE on 14/12/2020. + * Copyright (c) 2020 Michel DEPEIGE. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (see the file COPYING); if not, write to the + * Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * */ + #include #include "driver/gpio.h" #include "sdkconfig.h" @@ -19,6 +35,7 @@ #include "led.h" #include "gpio.h" +/* global TAG for ESP_LOG */ static const char* TAG = "quack"; void app_main(void)