-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuimo-onkyo.py
executable file
·101 lines (80 loc) · 3.13 KB
/
nuimo-onkyo.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
from nuimo import Nuimo
from bluepy.btle import UUID, DefaultDelegate, Peripheral, BTLEException
import sys
import os
import time
from onkyo import Onkyo
class NuimoOnkyoDelegate(DefaultDelegate):
def __init__(self, nuimoOnkyo):
DefaultDelegate.__init__(self)
self.nuimo = nuimoOnkyo
def handleNotification(self, cHandle, data):
if int(cHandle) == self.nuimo.characteristicValueHandles['BATTERY']:
print('BATTERY', ord(data[0]))
#nuimoOnkyo.battery(ord(data[0]))
elif int(cHandle) == self.nuimo.characteristicValueHandles['FLY']:
print('FLY', ord(data[0]), ord(data[1]))
#nuimoOnkyo.fly(ord(data[0]), ord(data[1]))
elif int(cHandle) == self.nuimo.characteristicValueHandles['SWIPE']:
self.nuimo.swipe(ord(data[0]))
elif int(cHandle) == self.nuimo.characteristicValueHandles['ROTATION']:
value = ord(data[0]) + (ord(data[1]) << 8)
if value >= 1 << 15:
value = value - (1 << 16)
self.nuimo.rotate(value)
elif int(cHandle) == self.nuimo.characteristicValueHandles['BUTTON']:
self.nuimo.button(ord(data[0]))
class NuimoOnkyo(Nuimo):
def __init__(self, macAddress):
Nuimo.__init__(self, macAddress)
self.onkyo = Onkyo()
print("Onkyo Host: %s" % self.onkyo.Host)
print("Onkyo power state is %s" % self.onkyo.PowerState())
print("Onkyo volume level is %s" % self.onkyo.VolumeLevel())
def fly(self, flyValue):
print(flyValue)
def swipe(self, swipeValue):
print(swipeValue)
def rotate(self, rotateValue):
print(int(rotateValue))
self.onkyo.rotateVolume(rotateValue)
def button(self, buttonValue):
if buttonValue == 0:
# check power state of Onkyo
print("Button pressed. Onkyo PowerState is %s" % self.onkyo.PowerState())
self.onkyo.TogglePower()
def getNuimoBatteryLevel(self):
return
if __name__ == "__main__":
######## Nuimo MAC Address #########
nuimomac = "F6:B2:90:F2:DF:08"
####################################
nuimoOnkyo = NuimoOnkyo(nuimomac)
nuimoOnkyo.set_delegate(NuimoOnkyoDelegate(nuimoOnkyo))
print("Trying to connect to %s. Press Ctrl+C to cancel." % nuimomac)
try:
nuimoOnkyo.connect()
except BTLEException as e:
print("Bluetooth exception occurred:", str(e))
sys.exit()
print("Connected. Waiting for input events...")
# Display some LEDs matrices and wait for notifications
nuimoOnkyo.displayLedMatrix(
" " +
" *** " +
" * * * " +
" * * " +
" *** * " +
" * * " +
" * * " +
" * * " +
" ", 2.0)
time.sleep(2)
try:
while True:
nuimoOnkyo.waitForNotifications()
except BTLEException as e:
print("Connection error:", str(e))
except KeyboardInterrupt:
print("Program aborted")