@@ -86,13 +86,17 @@ def value(self):
86
86
return self .v
87
87
88
88
class BleCharacteristic :
89
- def __init__ (self , uuid , buf_size = None ):
89
+ def __init__ (self , name , uuid , buf_size = None ):
90
90
self .uuid = bluetooth .UUID (uuid )
91
91
self .handler = None
92
92
self .trigger = None
93
93
self .value_handle = None # this is what we get back from ble after register services / chars
94
94
self .buf_size = buf_size
95
+ self ._name = name
95
96
97
+ def get_name (self ):
98
+ return self ._name
99
+
96
100
def callback (self , trigger , handler ):
97
101
# ex: rx_callback = rx_char.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.rx_cb_handler)
98
102
self .trigger = trigger
@@ -104,13 +108,17 @@ def irq(self, event, value):
104
108
class BleService :
105
109
# ex: uuid = "6E400003-B5A3-F393-E0A9-E50E24DCCA77"
106
110
DEFAULT_MTU = 23
107
- def __init__ (self , uuid , isPrimary = False ):
111
+ def __init__ (self , name , uuid , isPrimary = False ):
108
112
self .is_primary = isPrimary
109
113
self .uuid = bluetooth .UUID (uuid )
110
114
self .chr_uuids = []
111
115
self .chrs = []
112
116
self ._mtu = BleService .DEFAULT_MTU
117
+ self ._name = name
113
118
119
+ def get_name (self ):
120
+ return self ._name
121
+
114
122
def set_mtu (self , mtu_size ):
115
123
if mtu_size and (mtu_size >= BleService .DEFAULT_MTU ):
116
124
self ._mtu = mtu_size
@@ -125,12 +133,18 @@ def get(self):
125
133
#ex: service = ( _UART_UUID, (_UART_TX, _UART_RX), )
126
134
return service
127
135
128
- def characteristic (self , uuid , buf_size = None ):
136
+ def characteristic (self , uuid , buf_size = None , name = "none" ):
129
137
if uuid not in self .chr_uuids :
130
- ble_char = BleCharacteristic (uuid , buf_size )
138
+ ble_char = BleCharacteristic (name , uuid , buf_size )
131
139
self .chrs .append (ble_char )
132
140
self .chr_uuids .append (uuid )
133
141
return ble_char
142
+
143
+ def get_char_by_name (self , name ):
144
+ for chr in self .chrs :
145
+ if chr .get_name () == name :
146
+ return chr
147
+ return None
134
148
135
149
class BLE :
136
150
def __init__ (self , esp32_ble , name = "JEM-BLE" ):
@@ -158,16 +172,22 @@ def connect_callback(self, connected):
158
172
def get_mtu (self ):
159
173
return self ._ble .config ('mtu' )
160
174
161
- def service (self , uuid , isPrimary = False , nbr_chars = 0 ):
175
+ def service (self , name , uuid , isPrimary = False , nbr_chars = 0 ):
162
176
if uuid not in self .service_uuids :
163
177
self .service_uuids .append (uuid )
164
- service = BleService (uuid = uuid , isPrimary = isPrimary )
178
+ service = BleService (name = name , uuid = uuid , isPrimary = isPrimary )
165
179
if isPrimary :
166
180
self .primary_uuid = uuid
167
181
self .services .append (service )
168
182
return service
169
183
else :
170
184
print ("oops uuid %d already setup" % uuid )
185
+
186
+ def get_service_by_name (self , name ):
187
+ for sr in self .services :
188
+ if sr .get_name () == name :
189
+ return sr
190
+ return None
171
191
172
192
def get_chr_handles (self ):
173
193
char_handles = []
@@ -256,10 +276,24 @@ def _irq(self, event, data):
256
276
elif event == IRQ_MTU_EXCHANGED :
257
277
print ("IRQ_MTU_EXCHANGED: %s" % data [1 ])
258
278
279
+ class JEMBLE (object ):
280
+ _instance = None
281
+ def __new__ (cls , ble_drv = None , name = "JEM_BLE" ):
282
+ # ble_drv should not be None if this is first time creating jem ble
283
+ if cls ._instance is None :
284
+ print ('Creating the JEMBLE object' )
285
+ cls ._instance = super (JEMBLE , cls ).__new__ (cls )
286
+ print ("JEMBLE: init start" )
287
+
288
+ cls ._instance .ble = BLE (ble_drv , name = name ) #ble drive is esp32 mp ble module, for example
289
+ print ("JEMBLE: init done" )
290
+ return cls ._instance .ble
291
+
292
+
259
293
class BLEUART :
260
- def __init__ (self , jem_ble , service_uuid , tx_chr_uuid , rx_chr_uuid , rxbuf = 100 , primary = False ):
294
+ def __init__ (self , jem_ble , service_uuid , tx_chr_uuid , rx_chr_uuid , rxbuf = 100 , primary = False , name = "uart" ):
261
295
self .ble = jem_ble # jem ble wrapper
262
- self .service = self .ble .service (uuid = service_uuid , isPrimary = primary )
296
+ self .service = self .ble .service (name , uuid = service_uuid , isPrimary = primary )
263
297
self .tx_char = self .service .characteristic (uuid = tx_chr_uuid , buf_size = rxbuf )
264
298
self .rx_char = self .service .characteristic (uuid = rx_chr_uuid , buf_size = rxbuf )
265
299
self .rx_char .callback (None , self .rx_cbk )
0 commit comments