add mutex to protect bluetooth to Pd message passing

This commit is contained in:
Miller Puckette 2021-12-30 21:14:06 -08:00
parent 2ed3cbdf92
commit b440c55d7a
2 changed files with 73 additions and 44 deletions

View file

@ -36,6 +36,7 @@ static const char *TAG = "ESPD";
extern void pdmain_tick( void); extern void pdmain_tick( void);
void pdmain_init( void); void pdmain_init( void);
void pd_bt_poll( void);
void bt_init( void); void bt_init( void);
void sd_init( void); void sd_init( void);
@ -89,6 +90,11 @@ void pdmain_print( const char *s)
void trymem(int foo); 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) void app_main(void)
{ {
i2s_config_t i2s_cfg = { i2s_config_t i2s_cfg = {
@ -97,7 +103,7 @@ void app_main(void)
.bits_per_sample = 16, .bits_per_sample = 16,
/* .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, */ /* .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, */
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, .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_count = 3,
.dma_buf_len = 300, .dma_buf_len = 300,
.use_apll = 1, .use_apll = 1,
@ -134,15 +140,59 @@ void app_main(void)
ESP_LOGI(TAG, "tick"); ESP_LOGI(TAG, "tick");
} }
*/ */
pd_bt_poll();
pdmain_tick(); pdmain_tick();
senddacs(); senddacs();
} }
} }
/* -------------------- bluetooth and SD card ------------------ */ /* -------------------- 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 <ctype.h>
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; 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: case ESP_SPP_INIT_EVT:
ESP_LOGI(TAG, "ESP_SPP_INIT_EVT"); ESP_LOGI(TAG, "ESP_SPP_INIT_EVT");
esp_bt_dev_set_device_name("pure_data"); 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); 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"); esp_spp_start_srv(sec_mask,role_slave, 0, "pd_server");
break; break;
case ESP_SPP_DISCOVERY_COMP_EVT: 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; break;
case ESP_SPP_DATA_IND_EVT: case ESP_SPP_DATA_IND_EVT:
#if 0
if (param->data_ind.len > 2) if (param->data_ind.len > 2)
ESP_LOGI(TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d", ESP_LOGI(TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d",
param->data_ind.len, param->data_ind.handle); param->data_ind.len, param->data_ind.handle);
#if 0
{ {
char foo[80]; char foo[80];
int len = (param->data_ind.len > 79 ? 79 : param->data_ind.len); int len = (param->data_ind.len > 79 ? 79 : param->data_ind.len);

View file

@ -28,7 +28,6 @@ canvas 274 279 752 643 12;\n\
#X connect 0 0 1 0;\n\ #X connect 0 0 1 0;\n\
#X connect 1 0 2 0;\n\ #X connect 1 0 2 0;\n\
"; ";
#endif
static const char patchfile[] = "\ static const char patchfile[] = "\
canvas 0 50 450 300 12;\n\ 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 0 0 1 0;\n\
#X connect 3 0 2 0;\n\ #X connect 3 0 2 0;\n\
"; ";
#endif
void trymem(int foo) void trymem(int foo)
{ {
@ -57,48 +57,22 @@ void trymem(int foo)
#endif #endif
} }
/* queue from bluetooth. Need to make this a proper RTOS queue */ void pd_sendmsg(char *buf, int bufsize)
static char *pd_bt_buf;
static int pd_bt_size;
void pd_bt_dispatch(char *data, size_t size)
{ {
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; static t_binbuf *b;
if (!b) if (!b)
{ {
b = binbuf_new(); b = binbuf_new();
post("new binbuf %x", b); post("new binbuf %x", b);
} }
/* only interpret this text if terminated by a semicolon */ binbuf_text(b, buf, bufsize);
lastchar = pd_bt_size-1; binbuf_eval(b, 0, 0, 0);
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;
}
} }
extern float soundin[], soundout[]; extern float soundin[], soundout[];
void canvas_start_dsp( void); void canvas_start_dsp( void);
void pdmain_init( void) void pdmain_init( void)
{ {
t_binbuf *b;
sys_printhook = pdmain_print; sys_printhook = pdmain_print;
trymem(1); trymem(1);
pd_init(); pd_init();
@ -108,14 +82,16 @@ void pdmain_init( void)
STUFF->st_soundin = soundin; STUFF->st_soundin = soundin;
#if 0 #if 0
b = binbuf_new(); {
glob_setfilename(0, gensym("main-patch"), gensym(".")); t_binbuf *b = binbuf_new();
binbuf_text(b, patchfile, strlen(patchfile)); glob_setfilename(0, gensym("main-patch"), gensym("."));
binbuf_eval(b, &pd_canvasmaker, 0, 0); binbuf_text(b, patchfile, strlen(patchfile));
canvas_loadbang((t_canvas *)s__X.s_thing); binbuf_eval(b, &pd_canvasmaker, 0, 0);
vmess(s__X.s_thing, gensym("pop"), "i", 0); canvas_loadbang((t_canvas *)s__X.s_thing);
glob_setfilename(0, &s_, &s_); vmess(s__X.s_thing, gensym("pop"), "i", 0);
binbuf_free(b); glob_setfilename(0, &s_, &s_);
binbuf_free(b);
}
#endif #endif
} }
@ -123,7 +99,6 @@ void pdmain_init( void)
void pdmain_tick( void) void pdmain_tick( void)
{ {
memset(soundout, 0, 64*sizeof(float)); memset(soundout, 0, 64*sizeof(float));
pd_bt_poll();
sched_tick(); 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 sys_open(const char *path, int oflag, ...)
{ {
int i, fd; int fd;
char pathbuf[MAXPDSTRING]; char pathbuf[MAXPDSTRING];
sys_bashfilename(path, pathbuf); sys_bashfilename(path, pathbuf);
if (oflag & O_CREAT) if (oflag & O_CREAT)