@@ -9,6 +9,7 @@ class BLEUARTREMOTECONTROL:
9
9
def __init__ (self , tmr , uart , sync_uuid = 0xCF33 ):
10
10
self ._uart = uart
11
11
self ._tx_buf = bytearray ()
12
+ self ._tx_extra_buf = bytearray ()
12
13
self .tx_max_len = 100
13
14
self .tx_delay_ms = 20
14
15
self .cmd_delay_ms = 1
@@ -22,6 +23,20 @@ def __init__(self, tmr, uart, sync_uuid=0xCF33):
22
23
self .sync_char = self ._uart .service .characteristic (uuid = sync_uuid , buf_size = 20 )
23
24
self .sync_char .callback (None , self .sync_callback )
24
25
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
+
25
40
def sync_callback (self , chr , data = None ):
26
41
try :
27
42
if IRQ_GATTS_WRITE == chr .event ():
@@ -99,10 +114,19 @@ def _execute_next_cmd(self, eval_cmd):
99
114
100
115
def _flush (self ):
101
116
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
105
118
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 :
106
130
schedule_in (self ._timer , self ._wrap_flush , self .tx_delay_ms )
107
131
except Exception as e :
108
132
print ("_flush failed: %s" % e )
@@ -113,5 +137,12 @@ def write(self, buf):
113
137
if empty :
114
138
schedule_in (self ._timer , self ._wrap_flush , self .tx_delay_ms )
115
139
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
+
116
146
def is_connected (self ):
117
147
return self ._uart .is_connected ()
148
+
0 commit comments