-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpico_observing_led.py
75 lines (60 loc) · 2.17 KB
/
pico_observing_led.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
""" pico_observer_led.py
Sample application to test reading advertismentdata from LEOG hub.
Run this script on your Rapsberry Pi Pico.
Use with script hub_advertiser_led.py on your LEGO hub.
Pico setup:
- connect LED to pin GP15. Don't forget to add a resistor to the circuit.
- Upload file ble_pybricks.py together with this file to your Pico.
LEGO Hub setup:
- Make sure your LEGO hub is running Pybricks.
- Attach motor to port A.
- Attach a technic beam to the motor to make it easy to turn manualy.
Usage:
- start script hub_advertiser_led.py on your LEGO hub.
- start script pico_observer_led.py on your Pico.
- Turn motor A on your LEGO hub to adjust the brightness of the led.
"""
import bluetooth
import machine
import ustruct
from micropython import const
from ble_pybricks import check_lego_manufacturer_id, get_list_of_values
_IRQ_SCAN_RESULT = const(5)
_IRQ_SCAN_DONE = const(6)
#event handler function
def bt_irq(event, data):
global receivedNumber, dataReceivedFlag, led_value
values = []
if event == _IRQ_SCAN_RESULT:
# A single scan result.
addr_type, addr, adv_type, rssi, adv_data = data
address = bytes(addr)
if check_lego_manufacturer_id(adv_data) and not dataReceivedFlag:
values = get_list_of_values(adv_data)
led_value = values[0]
print(led_value)
receivedNumber=ustruct.unpack('<i',bytes(adv_data))[0]
dataReceivedFlag = True
elif event == _IRQ_SCAN_DONE:
print('scan finished.')
ble = bluetooth.BLE()
ble.active(True)
ble.irq(bt_irq)
led15 = machine.PWM(machine.Pin(15))
led15.freq(1000)
scanDuration_ms = 0 # 0 means indefenitely
interval_us = 100 # Interval 100ms is Pybricks condition
window_us = 100 # The same window as interval, means continuous scan
active = False # Do not care for a reply for a scan from the transmitter
receivedNumber = 0
dataReceivedFlag = False
led_value = 0
ble.gap_scan(scanDuration_ms, interval_us, window_us, active)
f = 65535 / 360
while True:
if dataReceivedFlag:
# print(receivedNumber)
dataReceivedFlag = False
if led_value < 0:
led_value = 0
led15.duty_u16(int(f*led_value))