update test code, indent code and move away from legacy commands
This commit is contained in:
82
ibuddy.py
82
ibuddy.py
@@ -1,11 +1,15 @@
|
||||
import usb
|
||||
#!/usr/bin/python
|
||||
import usb.core
|
||||
import usb.util
|
||||
from time import sleep
|
||||
|
||||
class DeviceNotFoundException(Exception): pass
|
||||
class DeviceNotFoundException(Exception):
|
||||
|
||||
pass
|
||||
|
||||
class ibuddy:
|
||||
USB_VENDOR = 0x1130
|
||||
USB_PRODUCT = 0x0001
|
||||
USB_PRODUCT = 1
|
||||
SETUP = (0x22, 0x09, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00)
|
||||
MESS = (0x55, 0x53, 0x42, 0x43, 0x00, 0x40, 0x02)
|
||||
|
||||
@@ -15,7 +19,7 @@ class ibuddy:
|
||||
RIGHT = 1
|
||||
UP = 0
|
||||
DOWN = 1
|
||||
OFF = 0
|
||||
Off = 0
|
||||
ON = 1
|
||||
|
||||
BLUE = (0, 0, 1)
|
||||
@@ -26,29 +30,23 @@ class ibuddy:
|
||||
WHITE = (1, 1, 1)
|
||||
YELLOW = (1, 1, 0)
|
||||
|
||||
CLEAR = 0xFF
|
||||
CLEAR = 0xff
|
||||
finalMess = CLEAR
|
||||
command = CLEAR
|
||||
|
||||
|
||||
def __init__(self, product=None):
|
||||
if product is None:
|
||||
product = self.USB_PRODUCT
|
||||
busses = usb.busses()
|
||||
for bus in busses:
|
||||
for dev in bus.devices:
|
||||
if dev.idVendor == self.USB_VENDOR and dev.idProduct == product:
|
||||
self.dev = dev
|
||||
if not hasattr(self, 'dev'):
|
||||
dev = usb.core.find(idVendor = self.USB_VENDOR, idProduct = self.USB_PRODUCT)
|
||||
if dev is None:
|
||||
raise DeviceNotFoundException()
|
||||
self.handle = self.dev.open()
|
||||
self.dev = dev
|
||||
|
||||
def __send(self, inp):
|
||||
""" send your command to the device """
|
||||
try:
|
||||
self.handle.controlMsg(0x21, 0x09, self.SETUP, 0x02, 0x01)
|
||||
self.handle.controlMsg(0x21, 0x09, self.MESS+(inp,), 0x02, 0x01)
|
||||
except usb.USBError:
|
||||
self.dev.ctrl_transfer(0x21, 0x09, 0x02, 1, self.SETUP)
|
||||
self.dev.ctrl_transfer(0x21, 0x09, 0x02, 1, self.MESS + (inp, ))
|
||||
except usb.USBError as e:
|
||||
print(e)
|
||||
self.__init__()
|
||||
|
||||
def pumpMessage(self):
|
||||
@@ -68,10 +66,10 @@ class ibuddy:
|
||||
|
||||
def setReverseBitValue(self, num, value):
|
||||
""" commands are sent as disabled bits """
|
||||
if (value==1):
|
||||
temp = 0xFF - (1<<num)
|
||||
if value == 1:
|
||||
temp = 0xff - (1 << num)
|
||||
self.command = self.command & temp
|
||||
elif (value==0):
|
||||
elif value == 0:
|
||||
temp = 1 << num
|
||||
self.command = self.command | temp
|
||||
|
||||
@@ -79,7 +77,7 @@ class ibuddy:
|
||||
""" what was that bit set to again? """
|
||||
temp = self.command
|
||||
temp = temp >> num
|
||||
res = not(temp&1)
|
||||
res = not temp & 1
|
||||
return res
|
||||
|
||||
def setHeart(self, status):
|
||||
@@ -87,23 +85,24 @@ class ibuddy:
|
||||
self.setReverseBitValue(7, status)
|
||||
|
||||
def flick(self, direction):
|
||||
if (direction == self.RIGHT):
|
||||
if direction == self.RIGHT:
|
||||
self.setReverseBitValue(1, 1)
|
||||
self.setReverseBitValue(0, 0)
|
||||
elif(direction == self.LEFT):
|
||||
elif direction == self.LEFT:
|
||||
self.setReverseBitValue(1, 0)
|
||||
self.setReverseBitValue(0, 1)
|
||||
|
||||
def wing(self, direction):
|
||||
if (direction == self.UP):
|
||||
if direction == self.UP:
|
||||
self.setReverseBitValue(3, 1)
|
||||
self.setReverseBitValue(2,0)
|
||||
elif(direction == self.DOWN):
|
||||
self.setReverseBitValue(0x02, 0)
|
||||
elif direction == self.DOWN:
|
||||
self.setReverseBitValue(3, 0)
|
||||
self.setReverseBitValue(2,1)
|
||||
self.setReverseBitValue(0x02, 1)
|
||||
|
||||
def getColors(self):
|
||||
return self.getReverseBitValue(4), self.getReverseBitValue(5), self.getReverseBitValue(6)
|
||||
return (self.getReverseBitValue(4), self.getReverseBitValue(5),
|
||||
self.getReverseBitValue(6))
|
||||
|
||||
def getHeart(self):
|
||||
""" returns heart-light status of on (1) or off (0) """
|
||||
@@ -111,15 +110,15 @@ class ibuddy:
|
||||
|
||||
def getWing(self):
|
||||
""" returns wing status of BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """
|
||||
return self.getReverseBitValue(2)
|
||||
return self.getReverseBitValue(0x02)
|
||||
|
||||
def getDirection(self):
|
||||
return self.getReverseBitValue(1)
|
||||
|
||||
def send(self, inp):
|
||||
try:
|
||||
self.dev.handle.controlMsg(0x21, 0x09, self.SETUP, 0x02, 0x01)
|
||||
self.dev.handle.controlMsg(0x21, 0x09, self.MESS+(inp,), 0x02, 0x01)
|
||||
self.dev.ctrl_transfer(0x21, 0x09, 0x02, 1, self.SETUP)
|
||||
self.dev.handle.controlMsg(0x21, 0x09, 0x02, 1, self.MESS + inp)
|
||||
except usb.USBError:
|
||||
self.__init__(self.battery, self.product)
|
||||
|
||||
@@ -131,7 +130,8 @@ class ibuddy:
|
||||
|
||||
def getHeadColors(self):
|
||||
""" returns color status as tuple representing (red, green, blue) as on (1) or off (0) """
|
||||
return self.getReverseBitValue(4), self.getReverseBitValue(5), self.getReverseBitValue(6)
|
||||
return (self.getReverseBitValue(4), self.getReverseBitValue(5),
|
||||
self.getReverseBitValue(6))
|
||||
|
||||
def setHeart(self, status):
|
||||
""" heart-light can be on (1) or off (0) """
|
||||
@@ -143,23 +143,23 @@ class ibuddy:
|
||||
|
||||
def setWing(self, direction):
|
||||
""" move the wings BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """
|
||||
if (direction == self.UP):
|
||||
if direction == self.UP:
|
||||
self.setReverseBitValue(3, 1)
|
||||
self.setReverseBitValue(2,0)
|
||||
elif(direction == self.DOWN):
|
||||
self.setReverseBitValue(0x02, 0)
|
||||
elif direction == self.DOWN:
|
||||
self.setReverseBitValue(3, 0)
|
||||
self.setReverseBitValue(2,1)
|
||||
self.setReverseBitValue(0x02, 1)
|
||||
|
||||
def getWing(self):
|
||||
""" returns wing status of BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """
|
||||
return self.getReverseBitValue(2)
|
||||
return self.getReverseBitValue(0x02)
|
||||
|
||||
def setSwivel(self, direction):
|
||||
""" swivel the body BuddyDevice.LEFT (0) or BuddyDevice.RIGHT (1) """
|
||||
if (direction == self.RIGHT):
|
||||
if direction == self.RIGHT:
|
||||
self.setReverseBitValue(1, 1)
|
||||
self.setReverseBitValue(0, 0)
|
||||
elif(direction == self.LEFT):
|
||||
elif direction == self.LEFT:
|
||||
self.setReverseBitValue(1, 0)
|
||||
self.setReverseBitValue(0, 1)
|
||||
|
||||
@@ -193,7 +193,7 @@ class ibuddy:
|
||||
for i in range(times):
|
||||
self.setHeart(self.ON)
|
||||
self.doCmd(seconds)
|
||||
self.setHeart(self.OFF)
|
||||
self.setHeart(self.Off)
|
||||
self.doCmd(seconds)
|
||||
|
||||
def doColorRGB(self, r, g, b, seconds=WAITTIME):
|
||||
|
||||
18
test.py
18
test.py
@@ -17,14 +17,16 @@ if __name__ == '__main__':
|
||||
print('No iBuddy device found!')
|
||||
exit(1)
|
||||
|
||||
ibuddy.doColorName(ibuddy.PURPLE, 0.5)
|
||||
ibuddy.doColorName(ibuddy.BLUE, 0.5)
|
||||
ibuddy.doColorName(ibuddy.LTBLUE, 0.5)
|
||||
ibuddy.doColorName(ibuddy.YELLOW, 0.5)
|
||||
ibuddy.doColorName(ibuddy.GREEN, 0.5)
|
||||
ibuddy.doColorName(ibuddy.RED, 0.5)
|
||||
ibuddy.doColorName(ibuddy.WHITE, 0.5)
|
||||
ibuddy.doFlap()
|
||||
for x in range(0, 10):
|
||||
ibuddy.doColorName(ibuddy.PURPLE, 0.1)
|
||||
ibuddy.doColorName(ibuddy.BLUE, 0.1)
|
||||
ibuddy.doColorName(ibuddy.LTBLUE, 0.1)
|
||||
ibuddy.doColorName(ibuddy.YELLOW, 0.1)
|
||||
ibuddy.doColorName(ibuddy.GREEN, 0.1)
|
||||
ibuddy.doColorName(ibuddy.RED, 0.1)
|
||||
ibuddy.doColorName(ibuddy.WHITE, 0.1)
|
||||
sleep(0.1)
|
||||
ibuddy.doFlap() # does not work!
|
||||
sleep(1)
|
||||
ibuddy.doWiggle() # does not work!
|
||||
sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user