8000 Merge pull request #78 from kitlab-io/jem-2-fixes · kitlab-io/micropython@a2effc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2effc3

Browse files
Merge pull request #78 from kitlab-io/jem-2-fixes
Add JEMBLE singleton class to manage access to BLE class
2 parents 6762664 + 6e9fbda commit a2effc3

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

jem/ble_info_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class BLEINFOService:
66
# service that contains mtu, jem version and other useful hw info
77
def __init__(self, jem_ble, service_uuid, rxbuf=2, primary=False):
88
self.ble = jem_ble # jem ble wrapper
9-
self.service = self.ble.service(uuid=service_uuid, isPrimary=primary)
9+
self.service = self.ble.service(name="info", uuid=service_uuid, isPrimary=primary)
1010
# mtu char can be used to tell client what mtu we agreed to
1111
self.mtu_char = self.service.characteristic(uuid=0x1234, buf_size=rxbuf)
1212
self.mtu_char.callback(None, self.mtu_cbk)

jem/ble_uart_peripheral.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ def value(self):
8686
return self.v
8787

8888
class BleCharacteristic:
89-
def __init__(self, uuid, buf_size=None):
89+
def __init__(self, name, uuid, buf_size=None):
9090
self.uuid = bluetooth.UUID(uuid)
9191
self.handler = None
9292
self.trigger = None
9393
self.value_handle = None # this is what we get back from ble after register services / chars
9494
self.buf_size = buf_size
95+
self._name = name
9596

97+
def get_name(self):
98+
return self._name
99+
96100
def callback(self, trigger, handler):
97101
# ex: rx_callback = rx_char.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.rx_cb_handler)
98102
self.trigger = trigger
@@ -104,13 +108,17 @@ def irq(self, event, value):
104108
class BleService:
105109
# ex: uuid = "6E400003-B5A3-F393-E0A9-E50E24DCCA77"
106110
DEFAULT_MTU = 23
107-
def __init__(self, uuid, isPrimary=False):
111+
def __init__(self, name, uuid, isPrimary=False):
108112
self.is_primary = isPrimary
109113
self.uuid = bluetooth.UUID(uuid)
110114
self.chr_uuids = []
111115
self.chrs = []
112116
self._mtu = BleService.DEFAULT_MTU
117+
self._name = name
113118

119+
def get_name(self):
120+
return self._name
121+
114122
def set_mtu(self, mtu_size):
115123
if mtu_size and (mtu_size >= BleService.DEFAULT_MTU):
116124
self._mtu = mtu_size
@@ -125,12 +133,18 @@ def get(self):
125133
#ex: service = ( _UART_UUID, (_UART_TX, _UART_RX), )
126134
return service
127135

128-
def characteristic(self, uuid, buf_size=None):
136+
def characteristic(self, uuid, buf_size=None, name="none"):
129137
if uuid not in self.chr_uuids:
130-
ble_char = BleCharacteristic(uuid, buf_size)
138+
ble_char = BleCharacteristic(name, uuid, buf_size)
131139
self.chrs.append(ble_char)
132140
self.chr_uuids.append(uuid)
133141
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
134148

135149
class BLE:
136150
def __init__(self, esp32_ble, name="JEM-BLE"):
@@ -158,16 +172,22 @@ def connect_callback(self, connected):
158172
def get_mtu(self):
159173
return self._ble.config('mtu')
160174

161-
def service(self, uuid, isPrimary=False, nbr_chars=0):
175+
def service(self, name, uuid, isPrimary=False, nbr_chars=0):
162176
if uuid not in self.service_uuids:
163177
self.service_uuids.append(uuid)
164-
service = BleService(uuid=uuid, isPrimary=isPrimary)
178+
service = BleService(name=name, uuid=uuid, isPrimary=isPrimary)
165179
if isPrimary:
166180
self.primary_uuid = uuid
167181
self.services.append(service)
168182
return service
169183
else:
170184
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
171191

172192
def get_chr_handles(self):
173193
char_handles = []
@@ -256,10 +276,24 @@ def _irq(self, event, data):
256276
elif event == IRQ_MTU_EXCHANGED:
257277
print("IRQ_MTU_EXCHANGED: %s" % data[1])
258278

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+
259293
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"):
261295
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)
263297
self.tx_char = self.service.characteristic(uuid=tx_chr_uuid, buf_size=rxbuf)
264298
self.rx_char = self.service.characteristic(uuid=rx_chr_uuid, buf_size=rxbuf)
265299
self.rx_char.callback(None, self.rx_cbk)

jem/ble_uart_remote_control.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, tmr, uart, sync_uuid=0xCF33):
2525

2626
# add new characteristic to uart service
2727
# 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)
28+
self.extra_char = self._uart.service.characteristic(name="extra", uuid=0xCD33, buf_size=200)
2929
self.extra_char.callback(None, self.extra_callback)
3030

3131
def extra_callback(self, chr, data=None):

jem/boot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#webrepl.start()
66

77
from ble_uart_repl import BLEUARTStream
8-
from ble_uart_peripheral import BLEUART, BLE
8+
from ble_uart_peripheral import BLEUART, JEMBLE
99
from ble_uart_ftp import BLEUARTFTP
1010
from ble_uart_remote_control import BLEUARTREMOTECONTROL
1111
from ble_info_service import BLEINFOService
@@ -21,11 +21,11 @@
2121
json_config = json.loads(json_str)
2222
name = json_config['ble']['name'] #get ble adv name
2323
except Exception as e:
24-
print("BLEMANAGER: Failed to load jem_config.json: %s" % e)
24+
print("Failed to load jem_config.json: %s" % e)
2525

2626
try:
2727
print("boot with ble name %s" % name)
28-
jem_ble = BLE(esp32_ble, name=name)
28+
jem_ble = JEMBLE(esp32_ble, name=name)#BLE(esp32_ble, name=name)
2929

3030
repl_uart = BLEUART(jem_ble, service_uuid="6E400001-B5A3-F393-E0A9-E50E24DCCA9E",
3131
tx_chr_uuid="6E400003-B5A3-F393-E0A9-E50E24DCCA9E",
@@ -38,7 +38,7 @@
3838
rxbuf=528)
3939
ftp = BLEUARTFTP(Timer(1), ftp_uart)
4040

41-
rc_uart = BLEUART(jem_ble, service_uuid = 0xCA33, rx_chr_uuid = 0xCB33, tx_chr_uuid = 0xCC33)
41+
rc_uart = BLEUART(jem_ble, service_uuid = 0xCA33, rx_chr_uuid = 0xCB33, tx_chr_uuid = 0xCC33, name="rc_uart")
4242
rc = BLEUARTREMOTECONTROL(Timer(2), rc_uart)
4343

4444

0 commit comments

Comments
 (0)
0