10000 Added extra ble characteristic in remote controle ble service · kitlab-io/micropython@6762664 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6762664

Browse files
Added extra ble characteristic in remote controle ble service
1 parent 9f06949 commit 6762664

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

jem/ble_uart_remote_control.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class BLEUARTREMOTECONTROL:
99
def __init__(self, tmr, uart, sync_uuid=0xCF33):
1010
self._uart = uart
1111
self._tx_buf = bytearray()
12+
self._tx_extra_buf = bytearray()
1213
self.tx_max_len = 100
1314
self.tx_delay_ms = 20
1415
self.cmd_delay_ms = 1
@@ -22,6 +23,20 @@ def __init__(self, tmr, uart, sync_uuid=0xCF33):
2223
self.sync_char = self._uart.service.characteristic(uuid=sync_uuid, buf_size=20)
2324
self.sync_char.callback(None, self.sync_callback)
2425

26+
# add new characteristic to uart service
27+
# extra ble char that user kit can use to asynchronously send data to app
28+
self.extra_char = self._uart.service.characteristic(uuid=0xCD33, buf_size=200)
29+
self.extra_char.callback(None, self.extra_callback)
30+
31+
def extra_callback(self, chr, data=None):
32+
# we use this char to send data to user
33+
# but if we want to update and use it for both tx and rx then update this callback
34+
try:
35+
if IRQ_GATTS_WRITE == chr.event():
36+
pass
37+
except Exception as e:
38+
print("extra_callback failed %s" % e)
39+
2540
def sync_callback(self, chr, data=None):
2641
try:
2742
if IRQ_GATTS_WRITE == chr.event():
@@ -99,10 +114,19 @@ def _execute_next_cmd(self, eval_cmd):
99114

100115
def _flush(self):
101116
try:
102-
data = self._tx_buf[0:self.tx_max_len]
103-
self._tx_buf = self._tx_buf[self.tx_max_len:]
104-
self._uart.write(data)
117+
# check default tx
105118
if self._tx_buf:
119+
data = self._tx_buf[0:self.tx_max_len]
120+
self._tx_buf = self._tx_buf[self.tx_max_len:]
121+
self._uart.write(data)
122+
123+
# check extra tx
124+
if self._tx_extra_buf:
125+
data = self._tx_extra_buf[0:self.tx_max_len]
126+
self._tx_extra_buf = self._tx_extra_buf[self.tx_max_len:]
127+
self._uart.ble.write(self.extra_char, data)
128+
129+
if self._tx_buf or self._tx_extra_buf:
106130
schedule_in(self._timer, self._wrap_flush, self.tx_delay_ms)
107131
except Exception as e:
108132
print("_flush failed: %s" % e)
@@ -113,5 +137,12 @@ def write(self, buf):
113137
if empty:
114138
schedule_in(self._timer, self._wrap_flush, self.tx_delay_ms)
115139

140+
def write_extra(self, buf):
141+
empty = not self._tx_extra_buf
142+
self._tx_extra_buf += buf
143+
if empty:
144+
schedule_in(self._timer, self._wrap_flush, self.tx_delay_ms)
145+
116146
def is_connected(self):
117147
return self._uart.is_connected()
148+

0 commit comments

Comments
 (0)
0