Merge pull request #7465 from tannewt/ble_workflow_ctrl_c

Watch for ctrl-c over BLE workflow serial
This commit is contained in:
Dan Halbert 2023-01-19 15:43:16 -05:00 committed by GitHub
commit 28afed91b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 8 deletions

View File

@ -49,7 +49,19 @@ STATIC int characteristic_buffer_on_ble_evt(struct ble_gap_event *event, void *p
event->notify_rx.attr_handle == self->characteristic->handle) { event->notify_rx.attr_handle == self->characteristic->handle) {
const struct os_mbuf *m = event->notify_rx.om; const struct os_mbuf *m = event->notify_rx.om;
while (m != NULL) { while (m != NULL) {
ringbuf_put_n(&self->ringbuf, m->om_data, m->om_len); const uint8_t *data = m->om_data;
uint16_t len = m->om_len;
if (self->watch_for_interrupt_char) {
for (uint16_t i = 0; i < len; i++) {
if (data[i] == mp_interrupt_char) {
mp_sched_keyboard_interrupt();
} else {
ringbuf_put(&self->ringbuf, data[i]);
}
}
} else {
ringbuf_put_n(&self->ringbuf, data, len);
}
m = SLIST_NEXT(m, om_next); m = SLIST_NEXT(m, om_next);
} }
} }
@ -69,9 +81,11 @@ void _common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buff
bleio_characteristic_obj_t *characteristic, bleio_characteristic_obj_t *characteristic,
mp_float_t timeout, mp_float_t timeout,
uint8_t *buffer, size_t buffer_size, uint8_t *buffer, size_t buffer_size,
void *static_handler_entry) { void *static_handler_entry,
bool watch_for_interrupt_char) {
self->characteristic = characteristic; self->characteristic = characteristic;
self->timeout_ms = timeout * 1000; self->timeout_ms = timeout * 1000;
self->watch_for_interrupt_char = watch_for_interrupt_char;
ringbuf_init(&self->ringbuf, buffer, buffer_size); ringbuf_init(&self->ringbuf, buffer, buffer_size);
if (static_handler_entry != NULL) { if (static_handler_entry != NULL) {
@ -87,7 +101,7 @@ void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffe
mp_float_t timeout, mp_float_t timeout,
size_t buffer_size) { size_t buffer_size) {
uint8_t *buffer = m_malloc(buffer_size, true); uint8_t *buffer = m_malloc(buffer_size, true);
_common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL); _common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL, false);
} }
uint32_t common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) { uint32_t common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) {

View File

@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H #ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H #define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
#include <stdbool.h>
#include "py/ringbuf.h" #include "py/ringbuf.h"
#include "shared-bindings/_bleio/Characteristic.h" #include "shared-bindings/_bleio/Characteristic.h"
@ -36,6 +38,7 @@ typedef struct {
uint32_t timeout_ms; uint32_t timeout_ms;
// Ring buffer storing consecutive incoming values. // Ring buffer storing consecutive incoming values.
ringbuf_t ringbuf; ringbuf_t ringbuf;
bool watch_for_interrupt_char;
} bleio_characteristic_buffer_obj_t; } bleio_characteristic_buffer_obj_t;
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H #endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H

View File

@ -45,7 +45,17 @@
STATIC void write_to_ringbuf(bleio_characteristic_buffer_obj_t *self, uint8_t *data, uint16_t len) { STATIC void write_to_ringbuf(bleio_characteristic_buffer_obj_t *self, uint8_t *data, uint16_t len) {
uint8_t is_nested_critical_region; uint8_t is_nested_critical_region;
sd_nvic_critical_region_enter(&is_nested_critical_region); sd_nvic_critical_region_enter(&is_nested_critical_region);
ringbuf_put_n(&self->ringbuf, data, len); if (self->watch_for_interrupt_char) {
for (uint16_t i = 0; i < len; i++) {
if (data[i] == mp_interrupt_char) {
mp_sched_keyboard_interrupt();
} else {
ringbuf_put(&self->ringbuf, data[i]);
}
}
} else {
ringbuf_put_n(&self->ringbuf, data, len);
}
sd_nvic_critical_region_exit(is_nested_critical_region); sd_nvic_critical_region_exit(is_nested_critical_region);
} }
@ -85,10 +95,12 @@ void _common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buff
bleio_characteristic_obj_t *characteristic, bleio_characteristic_obj_t *characteristic,
mp_float_t timeout, mp_float_t timeout,
uint8_t *buffer, size_t buffer_size, uint8_t *buffer, size_t buffer_size,
void *static_handler_entry) { void *static_handler_entry,
bool watch_for_interrupt_char) {
self->characteristic = characteristic; self->characteristic = characteristic;
self->timeout_ms = timeout * 1000; self->timeout_ms = timeout * 1000;
self->watch_for_interrupt_char = watch_for_interrupt_char;
ringbuf_init(&self->ringbuf, buffer, buffer_size); ringbuf_init(&self->ringbuf, buffer, buffer_size);
@ -105,7 +117,7 @@ void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffe
mp_float_t timeout, mp_float_t timeout,
size_t buffer_size) { size_t buffer_size) {
uint8_t *buffer = m_malloc(buffer_size, true); uint8_t *buffer = m_malloc(buffer_size, true);
_common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL); _common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL, false);
} }
uint32_t common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) { uint32_t common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) {

View File

@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H #ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H #define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
#include <stdbool.h>
#include "nrf_soc.h" #include "nrf_soc.h"
#include "py/ringbuf.h" #include "py/ringbuf.h"
@ -38,6 +40,7 @@ typedef struct {
uint32_t timeout_ms; uint32_t timeout_ms;
// Ring buffer storing consecutive incoming values. // Ring buffer storing consecutive incoming values.
ringbuf_t ringbuf; ringbuf_t ringbuf;
bool watch_for_interrupt_char;
} bleio_characteristic_buffer_obj_t; } bleio_characteristic_buffer_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H

View File

@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H #define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H
#include <stdbool.h>
#include "common-hal/_bleio/CharacteristicBuffer.h" #include "common-hal/_bleio/CharacteristicBuffer.h"
extern const mp_obj_type_t bleio_characteristic_buffer_type; extern const mp_obj_type_t bleio_characteristic_buffer_type;
@ -35,7 +37,8 @@ void _common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buff
bleio_characteristic_obj_t *characteristic, bleio_characteristic_obj_t *characteristic,
mp_float_t timeout, mp_float_t timeout,
uint8_t *buffer, size_t buffer_size, uint8_t *buffer, size_t buffer_size,
void *static_handler_entry); void *static_handler_entry,
bool watch_for_interrupt_char);
void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self, void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self,
bleio_characteristic_obj_t *characteristic, bleio_characteristic_obj_t *characteristic,
mp_float_t timeout, mp_float_t timeout,

View File

@ -146,7 +146,8 @@ void supervisor_start_bluetooth_serial(void) {
&supervisor_ble_circuitpython_rx_characteristic, &supervisor_ble_circuitpython_rx_characteristic,
0.1f, 0.1f,
(uint8_t *)_incoming, sizeof(_incoming) * sizeof(uint32_t), (uint8_t *)_incoming, sizeof(_incoming) * sizeof(uint32_t),
&rx_static_handler_entry); &rx_static_handler_entry,
true /* watch for interrupt character */);
_enabled = true; _enabled = true;
} }