From b440c55d7a4433bbbd3b0135bf90f18e839cbf5e Mon Sep 17 00:00:00 2001 From: Miller Puckette Date: Thu, 30 Dec 2021 21:14:06 -0800 Subject: [PATCH] add mutex to protect bluetooth to Pd message passing --- main/espd.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++---- main/pdmain.c | 55 +++++++++++++-------------------------------- 2 files changed, 73 insertions(+), 44 deletions(-) diff --git a/main/espd.c b/main/espd.c index 448c682..8a02be2 100644 --- a/main/espd.c +++ b/main/espd.c @@ -36,6 +36,7 @@ static const char *TAG = "ESPD"; extern void pdmain_tick( void); void pdmain_init( void); +void pd_bt_poll( void); void bt_init( void); void sd_init( void); @@ -89,6 +90,11 @@ void pdmain_print( const char *s) void trymem(int foo); + /* allow deprecated form if new one unavailable */ +#ifndef I2S_COMM_FORMAT_STAND_I2S +#define I2S_COMM_FORMAT_STAND_I2S I2S_COMM_FORMAT_I2S +#endif + void app_main(void) { i2s_config_t i2s_cfg = { @@ -97,7 +103,7 @@ void app_main(void) .bits_per_sample = 16, /* .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, */ .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, - .communication_format = I2S_COMM_FORMAT_I2S, + .communication_format = I2S_COMM_FORMAT_STAND_I2S, .dma_buf_count = 3, .dma_buf_len = 300, .use_apll = 1, @@ -134,15 +140,59 @@ void app_main(void) ESP_LOGI(TAG, "tick"); } */ + pd_bt_poll(); pdmain_tick(); senddacs(); } - } /* -------------------- bluetooth and SD card ------------------ */ +/* queue from bluetooth. Need to make this a proper RTOS queue */ +static char *pd_bt_buf; +static int pd_bt_size; +static SemaphoreHandle_t pd_bt_mutex; -void pd_bt_dispatch(char *data, size_t size); +void *getbytes(size_t nbytes); +void freebytes(void *x, size_t nbytes); +void *resizebytes(void *x, size_t oldsize, size_t newsize); +#include +void pd_sendmsg(char *buf, int bufsize); + +void pd_bt_dispatch(char *data, size_t size) +{ + if (!pd_bt_buf) + pd_bt_buf = getbytes(0); + if (!pd_bt_mutex) + pd_bt_mutex = xSemaphoreCreateMutex(); + while (xSemaphoreTake(pd_bt_mutex, 1) != pdTRUE) + ; + pd_bt_buf = (char *)resizebytes(pd_bt_buf, pd_bt_size, pd_bt_size+size); + memcpy(pd_bt_buf + pd_bt_size, data, size); + pd_bt_size += size; + xSemaphoreGive(pd_bt_mutex); +} + +void pd_bt_poll( void) +{ + int lastchar; + if (!pd_bt_mutex) + pd_bt_mutex = xSemaphoreCreateMutex(); + if (xSemaphoreTake(pd_bt_mutex, 0) != pdTRUE) + return; + + /* only interpret this text if terminated by a semicolon */ + lastchar = pd_bt_size-1; + while (lastchar >= 0 && isspace((int)(pd_bt_buf[lastchar]))) + lastchar--; + if (lastchar >= 3 && pd_bt_buf[lastchar] == ';' && + pd_bt_buf[lastchar-1] != '\\') + { + pd_sendmsg(pd_bt_buf, pd_bt_size); + pd_bt_buf = (char *)resizebytes(pd_bt_buf, pd_bt_size, 0); + pd_bt_size = 0; + } + xSemaphoreGive(pd_bt_mutex); +} static uint32_t pd_bt_writehandle; @@ -168,7 +218,11 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) case ESP_SPP_INIT_EVT: ESP_LOGI(TAG, "ESP_SPP_INIT_EVT"); esp_bt_dev_set_device_name("pure_data"); +#ifdef ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); +#else + esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); +#endif esp_spp_start_srv(sec_mask,role_slave, 0, "pd_server"); break; case ESP_SPP_DISCOVERY_COMP_EVT: @@ -189,10 +243,10 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) break; case ESP_SPP_DATA_IND_EVT: +#if 0 if (param->data_ind.len > 2) ESP_LOGI(TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d", param->data_ind.len, param->data_ind.handle); -#if 0 { char foo[80]; int len = (param->data_ind.len > 79 ? 79 : param->data_ind.len); diff --git a/main/pdmain.c b/main/pdmain.c index bfa3080..8f670c1 100644 --- a/main/pdmain.c +++ b/main/pdmain.c @@ -28,7 +28,6 @@ canvas 274 279 752 643 12;\n\ #X connect 0 0 1 0;\n\ #X connect 1 0 2 0;\n\ "; -#endif static const char patchfile[] = "\ canvas 0 50 450 300 12;\n\ @@ -39,6 +38,7 @@ canvas 0 50 450 300 12;\n\ #X connect 0 0 1 0;\n\ #X connect 3 0 2 0;\n\ "; +#endif void trymem(int foo) { @@ -57,48 +57,22 @@ void trymem(int foo) #endif } -/* queue from bluetooth. Need to make this a proper RTOS queue */ -static char *pd_bt_buf; -static int pd_bt_size; - -void pd_bt_dispatch(char *data, size_t size) +void pd_sendmsg(char *buf, int bufsize) { - if (!pd_bt_buf) - pd_bt_buf = getbytes(0); - pd_bt_buf = (char *)resizebytes(pd_bt_buf, pd_bt_size, pd_bt_size+size); - memcpy(pd_bt_buf + pd_bt_size, data, size); - pd_bt_size += size; -} - -void pd_bt_poll( void) -{ - int lastchar; static t_binbuf *b; if (!b) { b = binbuf_new(); post("new binbuf %x", b); } - /* only interpret this text if terminated by a semicolon */ - lastchar = pd_bt_size-1; - while (lastchar >= 0 && isspace((int)(pd_bt_buf[lastchar]))) - lastchar--; - if (lastchar >= 3 && pd_bt_buf[lastchar] == ';' && - pd_bt_buf[lastchar-1] != '\\') - { - binbuf_text(b, pd_bt_buf, pd_bt_size); - binbuf_eval(b, 0, 0, 0); - pd_bt_buf = (char *)resizebytes(pd_bt_buf, pd_bt_size, 0); - pd_bt_size = 0; - } + binbuf_text(b, buf, bufsize); + binbuf_eval(b, 0, 0, 0); } extern float soundin[], soundout[]; void canvas_start_dsp( void); void pdmain_init( void) { - t_binbuf *b; - sys_printhook = pdmain_print; trymem(1); pd_init(); @@ -108,14 +82,16 @@ void pdmain_init( void) STUFF->st_soundin = soundin; #if 0 - b = binbuf_new(); - glob_setfilename(0, gensym("main-patch"), gensym(".")); - binbuf_text(b, patchfile, strlen(patchfile)); - binbuf_eval(b, &pd_canvasmaker, 0, 0); - canvas_loadbang((t_canvas *)s__X.s_thing); - vmess(s__X.s_thing, gensym("pop"), "i", 0); - glob_setfilename(0, &s_, &s_); - binbuf_free(b); + { + t_binbuf *b = binbuf_new(); + glob_setfilename(0, gensym("main-patch"), gensym(".")); + binbuf_text(b, patchfile, strlen(patchfile)); + binbuf_eval(b, &pd_canvasmaker, 0, 0); + canvas_loadbang((t_canvas *)s__X.s_thing); + vmess(s__X.s_thing, gensym("pop"), "i", 0); + glob_setfilename(0, &s_, &s_); + binbuf_free(b); + } #endif } @@ -123,7 +99,6 @@ void pdmain_init( void) void pdmain_tick( void) { memset(soundout, 0, 64*sizeof(float)); - pd_bt_poll(); sched_tick(); } @@ -765,7 +740,7 @@ static void sys_expandpath(const char *from, char *to, int bufsize) int sys_open(const char *path, int oflag, ...) { - int i, fd; + int fd; char pathbuf[MAXPDSTRING]; sys_bashfilename(path, pathbuf); if (oflag & O_CREAT)