move quadrature tasks from notification to queue

This commit is contained in:
demik 2021-08-21 10:52:54 +02:00
parent 435965954b
commit 4d64e67602
6 changed files with 76 additions and 46 deletions

View File

@ -23,6 +23,7 @@
#include "driver/gpio.h" #include "driver/gpio.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h" #include "esp_system.h"
@ -43,7 +44,8 @@
rmt_config_t adb_rmt_rx = RMT_DEFAULT_CONFIG_RX(GPIO_ADB, RMT_RX_CHANNEL); rmt_config_t adb_rmt_rx = RMT_DEFAULT_CONFIG_RX(GPIO_ADB, RMT_RX_CHANNEL);
extern TaskHandle_t t_adb2hid; extern TaskHandle_t t_adb2hid;
extern TaskHandle_t t_green, t_blue, t_yellow, t_red; extern TaskHandle_t t_green, t_blue, t_yellow, t_red;
extern TaskHandle_t t_click, t_qx, t_qy; extern TaskHandle_t t_click;
extern QueueHandle_t q_qx, q_qy;
/* static defines */ /* static defines */
static void adb_handle_button(bool action); static void adb_handle_button(bool action);
@ -234,7 +236,7 @@ void adb_task_host(void *pvParameters) {
} }
if (move) if (move)
xTaskNotify(t_qx, move, eSetValueWithOverwrite); xQueueSendToBack(q_qx, &move, 0);
move = (data & ADB_CMP_MY) >> 8; move = (data & ADB_CMP_MY) >> 8;
if (move & 0x40) { if (move & 0x40) {
@ -244,7 +246,7 @@ void adb_task_host(void *pvParameters) {
} }
if (move) if (move)
xTaskNotify(t_qy, move, eSetValueWithOverwrite); xQueueSendToBack(q_qy, &move, 0);
} }
else { else {
last++; last++;

View File

@ -54,10 +54,13 @@ static const char *TAG = "blue";
/* globals */ /* globals */
TaskHandle_t t_adb2hid; TaskHandle_t t_adb2hid;
extern TaskHandle_t t_click, t_qx, t_qy; extern TaskHandle_t t_click;
static esp_hidd_dev_t *hid_dev = NULL; static esp_hidd_dev_t *hid_dev = NULL;
extern QueueHandle_t q_qx, q_qy;
/* private functions */ /* private functions */
static void blue_d_connect(void);
static void blue_d_disconnect(esp_hidd_event_data_t *dev);
static void blue_d_init(void); static void blue_d_init(void);
static void blue_d_start(void); static void blue_d_start(void);
void blue_h_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data); void blue_h_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data);
@ -83,8 +86,7 @@ static void blue_d_callback(void *handler_args, esp_event_base_t base, int32_t i
break; break;
} }
case ESP_HIDD_CONNECT_EVENT: { case ESP_HIDD_CONNECT_EVENT: {
ESP_LOGI(TAG, "CONNECT"); blue_d_connect();
xTaskNotify(t_blue, LED_ON, eSetValueWithOverwrite);
break; break;
} }
case ESP_HIDD_PROTOCOL_MODE_EVENT: { case ESP_HIDD_PROTOCOL_MODE_EVENT: {
@ -106,9 +108,7 @@ static void blue_d_callback(void *handler_args, esp_event_base_t base, int32_t i
break; break;
} }
case ESP_HIDD_DISCONNECT_EVENT: { case ESP_HIDD_DISCONNECT_EVENT: {
ESP_LOGI(TAG, "DISCONNECT: %s", esp_hid_disconnect_reason_str(esp_hidd_dev_transport_get(param->disconnect.dev), param->disconnect.reason)); blue_d_disconnect(param);
esp_hid_ble_gap_adv_start();
xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite);
break; break;
} }
case ESP_HIDD_STOP_EVENT: { case ESP_HIDD_STOP_EVENT: {
@ -121,6 +121,18 @@ static void blue_d_callback(void *handler_args, esp_event_base_t base, int32_t i
return; return;
} }
static void blue_d_connect() {
ESP_LOGI(TAG, "Host connected");
xTaskNotify(t_blue, LED_ON, eSetValueWithOverwrite);
}
static void blue_d_disconnect(esp_hidd_event_data_t *dev) {
ESP_LOGI(TAG, "Host disconnected, reason: %s",
esp_hid_disconnect_reason_str(esp_hidd_dev_transport_get(dev->disconnect.dev), dev->disconnect.reason));
esp_hid_ble_gap_adv_start();
xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite);
}
static void blue_d_init() { static void blue_d_init() {
esp_err_t ret; esp_err_t ret;
@ -393,17 +405,24 @@ void blue_h_input(esp_hidh_dev_t *dev, uint8_t *data, uint16_t length) {
x = data[1]; x = data[1];
y = data[2]; y = data[2];
/* reduce bluetooth movement speed as well before notifying */
if (x != 0) { if (x != 0) {
if (x == 1 || x == -1) if (x == 1 || x == -1) {
xTaskNotify(t_qx, x, eSetValueWithOverwrite); xQueueSendToBack(q_qx, &x, 0);
else }
xTaskNotify(t_qx, x / 2, eSetValueWithOverwrite); else {
x /= 2;
xQueueSendToBack(q_qx, &x, 0);
}
} }
if (y != 0) { if (y != 0) {
if (y == 1 || y == -1) if (y == 1 || y == -1) {
xTaskNotify(t_qy, y, eSetValueWithOverwrite); xQueueSendToBack(q_qy, &y, 0);
else }
xTaskNotify(t_qy, y / 2, eSetValueWithOverwrite); else {
y /= 2;
xQueueSendToBack(q_qy, &y, 0);
}
} }
} }

View File

@ -24,9 +24,16 @@
/* prototypes */ /* prototypes */
void led_dispatch(void *pvParameters); void led_dispatch(void *pvParameters);
const char *led_gpio_name(uint8_t id);
void led_init(void); void led_init(void);
void led_task(void *pvParameters); void led_task(void *pvParameters);
/* structures */
typedef struct led_names_s {
const uint8_t num;
const char *name;
} led_names_t;
/* defines */ /* defines */
#define LED_OFF (1 << 0) #define LED_OFF (1 << 0)
#define LED_ON (1 << 1) #define LED_ON (1 << 1)

View File

@ -23,6 +23,7 @@
#include "driver/gpio.h" #include "driver/gpio.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_err.h" #include "esp_err.h"
@ -37,6 +38,8 @@
TaskHandle_t t_click, t_qx, t_qy; TaskHandle_t t_click, t_qx, t_qy;
esp_timer_handle_t quad_qx, quad_qy; esp_timer_handle_t quad_qx, quad_qy;
QueueHandle_t q_qx, q_qy;
/* static functions */ /* static functions */
static void IRAM_ATTR quad_timer(void* arg); static void IRAM_ATTR quad_timer(void* arg);
@ -63,15 +66,18 @@ void quad_init(void) {
gpio_set_level(GPIO_QY2, q2[0]); gpio_set_level(GPIO_QY2, q2[0]);
/* create timers for quadrature phases */ /* create timers for quadrature phases */
args.callback = &quad_timer; args.callback = quad_timer;
args.arg = t_qx; args.arg = t_qx;
args.name = "quad_qx"; args.name = "quad_qx";
ESP_ERROR_CHECK(esp_timer_create(&args, &quad_qx)); ESP_ERROR_CHECK(esp_timer_create(&args, &quad_qx));
args.callback = &quad_timer; args.callback = quad_timer;
args.arg = t_qy; args.arg = t_qy;
args.name = "quad_qy"; args.name = "quad_qy";
ESP_ERROR_CHECK(esp_timer_create(&args, &quad_qy)); ESP_ERROR_CHECK(esp_timer_create(&args, &quad_qy));
q_qx = xQueueCreate(4, sizeof(int8_t));
q_qy = xQueueCreate(4, sizeof(int8_t));
ESP_LOGI("quad", "Quadrature tasks started on core %d", xPortGetCoreID()); ESP_LOGI("quad", "Quadrature tasks started on core %d", xPortGetCoreID());
} }
@ -97,16 +103,14 @@ void quad_click(void *pvParameters) {
} }
void IRAM_ATTR quad_move_x(void *pvParameters) { void IRAM_ATTR quad_move_x(void *pvParameters) {
unsigned int value = 0; int8_t move = 0;
int move = 0;
uint8_t i = 0; uint8_t i = 0;
(void)pvParameters; (void)pvParameters;
while (true) { while (true) {
xTaskNotifyWait(0, 0, &value, portMAX_DELAY); xQueueReceive(q_qx, &move, portMAX_DELAY);
assert(value != 0); assert(move != 0);
move = (signed)value;
if (move > 0) { if (move > 0) {
while (move--) { while (move--) {
@ -114,7 +118,7 @@ void IRAM_ATTR quad_move_x(void *pvParameters) {
gpio_set_level(GPIO_QX1, q1[i % 4]); gpio_set_level(GPIO_QX1, q1[i % 4]);
gpio_set_level(GPIO_QX2, q2[i % 4]); gpio_set_level(GPIO_QX2, q2[i % 4]);
ESP_ERROR_CHECK(esp_timer_start_once(quad_qx, QUAD_INTERVAL)); ESP_ERROR_CHECK(esp_timer_start_once(quad_qx, QUAD_INTERVAL));
vTaskSuspend(NULL); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} }
} }
else { else {
@ -123,23 +127,21 @@ void IRAM_ATTR quad_move_x(void *pvParameters) {
gpio_set_level(GPIO_QX1, q1[i % 4]); gpio_set_level(GPIO_QX1, q1[i % 4]);
gpio_set_level(GPIO_QX2, q2[i % 4]); gpio_set_level(GPIO_QX2, q2[i % 4]);
ESP_ERROR_CHECK(esp_timer_start_once(quad_qx, QUAD_INTERVAL)); ESP_ERROR_CHECK(esp_timer_start_once(quad_qx, QUAD_INTERVAL));
vTaskSuspend(NULL); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} }
} }
} }
} }
void IRAM_ATTR quad_move_y(void *pvParameters) { void IRAM_ATTR quad_move_y(void *pvParameters) {
unsigned int value = 0; int8_t move = 0;
int move = 0;
uint8_t i = 0; uint8_t i = 0;
(void)pvParameters; (void)pvParameters;
while (true) { while (true) {
xTaskNotifyWait(0, 0, &value, portMAX_DELAY); xQueueReceive(q_qy, &move, portMAX_DELAY);
assert(value != 0); assert(move != 0);
move = (signed)value;
if (move > 0) { if (move > 0) {
while (move--) { while (move--) {
@ -147,7 +149,7 @@ void IRAM_ATTR quad_move_y(void *pvParameters) {
gpio_set_level(GPIO_QY1, q1[i % 4]); gpio_set_level(GPIO_QY1, q1[i % 4]);
gpio_set_level(GPIO_QY2, q2[i % 4]); gpio_set_level(GPIO_QY2, q2[i % 4]);
ESP_ERROR_CHECK(esp_timer_start_once(quad_qy, QUAD_INTERVAL)); ESP_ERROR_CHECK(esp_timer_start_once(quad_qy, QUAD_INTERVAL));
vTaskSuspend(NULL); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} }
} }
else { else {
@ -156,18 +158,18 @@ void IRAM_ATTR quad_move_y(void *pvParameters) {
gpio_set_level(GPIO_QY1, q1[i % 4]); gpio_set_level(GPIO_QY1, q1[i % 4]);
gpio_set_level(GPIO_QY2, q2[i % 4]); gpio_set_level(GPIO_QY2, q2[i % 4]);
ESP_ERROR_CHECK(esp_timer_start_once(quad_qy, QUAD_INTERVAL)); ESP_ERROR_CHECK(esp_timer_start_once(quad_qy, QUAD_INTERVAL));
vTaskSuspend(NULL); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} }
} }
} }
} }
/* simple ISR function. Resume task that called the oneshot timer */ /* simple ISR function. Resume the task that called the oneshot timer */
static void IRAM_ATTR quad_timer(void* arg) { static void IRAM_ATTR quad_timer(void* arg) {
BaseType_t xYieldRequired = pdFALSE; BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
xYieldRequired = xTaskResumeFromISR(arg); vTaskNotifyGiveFromISR(arg, &pxHigherPriorityTaskWoken);
/* switch context of needed */ /* switch context of needed */
if( xYieldRequired == pdTRUE ) if( pxHigherPriorityTaskWoken == pdTRUE )
portYIELD_FROM_ISR(); portYIELD_FROM_ISR();
} }

View File

@ -29,7 +29,7 @@ void quad_move_x(void *pvParameters);
void quad_move_y(void *pvParameters); void quad_move_y(void *pvParameters);
/* defines */ /* defines */
#define QUAD_INTERVAL 400 #define QUAD_INTERVAL 200
#endif #endif

View File

@ -533,9 +533,9 @@ CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_0=y
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_7 is not set # CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_7 is not set
CONFIG_ESP32_BROWNOUT_DET_LVL=0 CONFIG_ESP32_BROWNOUT_DET_LVL=0
# CONFIG_ESP32_REDUCE_PHY_TX_POWER is not set # CONFIG_ESP32_REDUCE_PHY_TX_POWER is not set
CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y # CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 is not set
# CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set # CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set
# CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set CONFIG_ESP32_TIME_SYSCALL_USE_FRC1=y
# CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set # CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set
CONFIG_ESP32_RTC_CLK_SRC_INT_RC=y CONFIG_ESP32_RTC_CLK_SRC_INT_RC=y
# CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS is not set # CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS is not set
@ -655,7 +655,7 @@ CONFIG_HTTPD_PURGE_BUF_LEN=32
CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL=120 CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL=120
CONFIG_ESP_NETIF_TCPIP_LWIP=y CONFIG_ESP_NETIF_TCPIP_LWIP=y
# CONFIG_ESP_NETIF_LOOPBACK is not set # CONFIG_ESP_NETIF_LOOPBACK is not set
CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y # CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER is not set
# end of ESP NETIF Adapter # end of ESP NETIF Adapter
# #
@ -670,7 +670,7 @@ CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y
# #
# High resolution timer (esp_timer) # High resolution timer (esp_timer)
# #
# CONFIG_ESP_TIMER_PROFILING is not set CONFIG_ESP_TIMER_PROFILING=y
CONFIG_ESP_TIMER_TASK_STACK_SIZE=3584 CONFIG_ESP_TIMER_TASK_STACK_SIZE=3584
# CONFIG_ESP_TIMER_IMPL_FRC2 is not set # CONFIG_ESP_TIMER_IMPL_FRC2 is not set
CONFIG_ESP_TIMER_IMPL_TG0_LAC=y CONFIG_ESP_TIMER_IMPL_TG0_LAC=y
@ -788,7 +788,7 @@ CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y
# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set # CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set
CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY=y CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY=y
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set # CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set
CONFIG_FREERTOS_INTERRUPT_BACKTRACE=y # CONFIG_FREERTOS_INTERRUPT_BACKTRACE is not set
CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=1 CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=1
CONFIG_FREERTOS_ASSERT_FAIL_ABORT=y CONFIG_FREERTOS_ASSERT_FAIL_ABORT=y
# CONFIG_FREERTOS_ASSERT_FAIL_PRINT_CONTINUE is not set # CONFIG_FREERTOS_ASSERT_FAIL_PRINT_CONTINUE is not set