-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_control.py
56 lines (45 loc) · 1.32 KB
/
easy_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import atexit
import time
import signal
import sys
import Adafruit_BluefruitLE
from Adafruit_BluefruitLE.services import UART
ble = Adafruit_BluefruitLE.get_provider()
def main():
ble.clear_cached_data()
global device
adapter = ble.get_default_adapter()
adapter.power_on()
print('Using adapter: {0}'.format(adapter.name))
print('Searching for UART device...')
try:
adapter.start_scan()
device = UART.find_device()
if device is None:
raise RuntimeError('Failed to find UART device!')
finally:
adapter.stop_scan()
print('Connecting to device...')
device.connect()
print('Discovering services...')
UART.discover(device)
uart = UART(device)
line = ''
timer = time.time()
while True:
received = uart.read(timeout_sec=1)
if received is not None:
timer = time.time()
line = ''.join([line, received])
else:
if (time.time() - timer) > 1 and line != '':
print('{0} {1}'.format('line is: ', line))
line = ''
def stop_easy_control(signal = None, frame = None):
global device
device.disconnect()
sys.exit(0)
atexit.register(stop_easy_control)
signal.signal(signal.SIGINT, stop_easy_control)
ble.initialize()
ble.run_mainloop_with(main)