Skip to content

Commit 4d227c4

Browse files
committed
BLE handler
1 parent c42a527 commit 4d227c4

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
UART based message handler for CircuitPython logging.
3+
4+
Adafruit invests time and resources providing this open source code.
5+
Please support Adafruit and open source hardware by purchasing
6+
products from Adafruit!
7+
8+
Written by Dave Astels for Adafruit Industries
9+
Copyright (c) 2018 Adafruit Industries
10+
Licensed under the MIT license.
11+
12+
All text above must be included in any redistribution.
13+
"""
14+
15+
16+
#pylint:disable=missing-super-argument
17+
18+
# Example:
19+
#
20+
# import board
21+
# import busio
22+
# from uart_handler import UartHandler
23+
# import adafruit_logging as logging
24+
#
25+
# uart = busio.UART(board.TX, board.RX, baudrate=115200)
26+
# logger = logging.getLogger('uart')
27+
# logger.addHandler(UartHandler(uart))
28+
# logger.level = logging.INFO
29+
# logger.info('testing')
30+
31+
from adafruit_logging import LoggingHandler
32+
from adafruit_ble.uart import UARTServer
33+
34+
class BLEHandler(LoggingHandler):
35+
"""Send logging output to the BLE uart port."""
36+
37+
def __init__(self):
38+
"""Create an instance.
39+
40+
:param uart: the busio.UART instance to which to write messages
41+
42+
"""
43+
self._advertising_now = False
44+
self._uart = UARTServer()
45+
self._uart.start_advertising()
46+
47+
def format(self, level, msg):
48+
"""Generate a string to log.
49+
50+
:param level: The level at which to log
51+
:param msg: The core message
52+
53+
"""
54+
return super().format(level, msg) + '\r\n'
55+
56+
def emit(self, level, msg):
57+
"""Generate the message and write it to the UART.
58+
59+
:param level: The level at which to log
60+
:param msg: The core message
61+
62+
"""
63+
while not self._uart.connected:
64+
pass
65+
66+
data = bytes(self.format(level, msg), 'utf-8')
67+
for i in range(len(data)):
68+
self._uart.write(data[i:i+1])

CircuitPython_Logger/ble_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
import random
3+
from ble_handler import BLEHandler
4+
import adafruit_logging as logging
5+
6+
l = logging.getLogger('ble')
7+
8+
l.addHandler(BLEHandler())
9+
10+
def go():
11+
while True:
12+
t = random.randint(1, 5)
13+
if t == 1:
14+
print('debug')
15+
l.debug("debug message: %d", random.randint(0, 1000))
16+
elif t == 2:
17+
print('info')
18+
l.info("info message: %d", random.randint(0, 1000))
19+
elif t == 3:
20+
print('warning')
21+
l.warning("warning message: %d", random.randint(0, 1000))
22+
elif t == 4:
23+
print('error')
24+
l.error("error message: %d", random.randint(0, 1000))
25+
elif t == 5:
26+
print('critical')
27+
l.critical("critical message: %d", random.randint(0, 1000))
28+
time.sleep(5.0 + (random.random() * 5.0))

0 commit comments

Comments
 (0)