|
12 | 12 | import time
|
13 | 13 | import board
|
14 | 14 | import digitalio
|
15 |
| - |
| 15 | +import neopixel |
16 | 16 | import adafruit_ble
|
17 | 17 | from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
|
18 | 18 | import adafruit_ble_apple_notification_center as ancs
|
19 | 19 |
|
20 |
| -butA = digitalio.DigitalInOut(board.D4) # Circuit Playground Bluefruit A button |
21 |
| -butB = digitalio.DigitalInOut(board.D5) # Circuit Playground Bluefruit A button |
22 |
| -butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH |
| 20 | +# Circuit Playground Bluefruit buttons and LED setup |
| 21 | +butA = digitalio.DigitalInOut(board.D4) # CPB "A" button |
| 22 | +butB = digitalio.DigitalInOut(board.D5) # CPB "B" button |
| 23 | +butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH |
23 | 24 | butB.switch_to_input(digitalio.Pull.DOWN)
|
24 | 25 |
|
| 26 | +leds = neopixel.NeoPixel(board.D8, 10, brightness=0.1) |
| 27 | + |
| 28 | +(coff, cred, cgrn, cblu, cgra) = (0x000000, 0xff0000, 0x00ff00, 0x0000ff, 0x111111 ) |
| 29 | +leds_off = ( coff,coff,coff,coff,coff, coff,coff,coff,coff,coff ) |
| 30 | +leds_idle = ( cgra,cgra,cgra,cgra,cgra, cgra,cgra,cgra,cgra,cgra ) |
| 31 | +leds_incoming_call = ( coff,cgrn,cgrn,cgrn,coff, coff,cred,cred,cred,coff ) |
| 32 | +leds_active_call = ( cgrn,coff,coff,coff,cgrn, cgrn,cred,cred,cred,cgrn ) |
| 33 | + |
| 34 | +print("starting...") |
25 | 35 | radio = adafruit_ble.BLERadio() # pylint: disable=no-member
|
26 | 36 | a = SolicitServicesAdvertisement()
|
| 37 | +#a.complete_name = "CIRPYCALLHANDLER" # this crashes things? |
27 | 38 | a.solicited_services.append(ancs.AppleNotificationCenterService)
|
28 | 39 | radio.start_advertising(a)
|
29 | 40 |
|
30 |
| -while not radio.connected: |
31 |
| - pass |
| 41 | +last_display_time = time.monotonic() |
32 | 42 |
|
33 |
| -print("connected") |
| 43 | +while True: |
| 44 | + while not radio.connected: |
| 45 | + print("not connected") |
| 46 | + time.sleep(1) |
34 | 47 |
|
35 |
| -while radio.connected: |
36 | 48 | for connection in radio.connections:
|
37 | 49 | if not connection.paired:
|
38 | 50 | connection.pair()
|
39 | 51 | print("paired")
|
40 | 52 |
|
41 | 53 | ans = connection[ancs.AppleNotificationCenterService]
|
| 54 | + |
42 | 55 | for notification in ans.wait_for_new_notifications():
|
43 |
| - print("new notification:", notification) |
| 56 | + print("New Notification:\n- ", notification) |
| 57 | + |
| 58 | + leds[:] = leds_idle |
44 | 59 |
|
45 |
| - print("Notifications:", len(ans.active_notifications)) |
46 |
| - for nid, n in ans.active_notifications.items(): |
47 |
| - print( |
48 |
| - "uid:", |
49 |
| - n.id, |
50 |
| - "cat:", |
51 |
| - n.category_id, |
52 |
| - "title:", |
53 |
| - n.title, |
54 |
| - "msg:", |
55 |
| - n.message, |
56 |
| - ) |
57 |
| - if n.category_id == 1: # incoming call, has positive & negative actions |
| 60 | + for notification in ans.active_notifications.values(): |
| 61 | + # incoming call category, has positive & negative actions |
| 62 | + if notification.category_id == 1: |
| 63 | + leds[:] = leds_incoming_call |
58 | 64 | if butA.value:
|
59 |
| - print("accepting call") |
60 |
| - n.send_positive_action() |
| 65 | + print("Action: accepting call") |
| 66 | + notification.send_positive_action() |
| 67 | + time.sleep(1) # simple debounce |
61 | 68 | if butB.value:
|
62 |
| - print("declining call") |
63 |
| - n.send_negative_action() |
64 |
| - if n.category_id == 12: # active call, only has negative action |
| 69 | + print("Action: declining call") |
| 70 | + notification.send_negative_action() |
| 71 | + time.sleep(1) # simple debounce |
| 72 | + # active call category, only has negative action |
| 73 | + if notification.category_id == 12: |
| 74 | + leds[:] = leds_active_call |
65 | 75 | if butB.value:
|
66 |
| - print("hanging up call") |
67 |
| - n.send_negative_action() |
68 |
| - time.sleep(1) |
| 76 | + print("Action: hanging up call") |
| 77 | + notification.send_negative_action() |
| 78 | + time.sleep(1) # simple debounce |
69 | 79 |
|
70 |
| -print("disconnected") |
| 80 | + if time.monotonic() - last_display_time > 3.0: |
| 81 | + last_display_time = time.monotonic() |
| 82 | + print("Current Notifications:", len(ans.active_notifications), time.monotonic()) |
| 83 | + for nid, n in ans.active_notifications.items(): |
| 84 | + print("- uid:",n.id, "catid:", n.category_id, "title:", n.title, "msg:", n.message) |
0 commit comments