update test code, indent code and move away from legacy commands

This commit is contained in:
2016-08-20 11:55:25 +01:00
parent fac1d8c9eb
commit d080fa9ecd
2 changed files with 180 additions and 178 deletions

338
ibuddy.py
View File

@@ -1,207 +1,207 @@
import usb #!/usr/bin/python
import usb.core
import usb.util
from time import sleep from time import sleep
class DeviceNotFoundException(Exception): pass class DeviceNotFoundException(Exception):
pass
class ibuddy: class ibuddy:
USB_VENDOR = 0x1130 USB_VENDOR = 0x1130
USB_PRODUCT = 0x0001 USB_PRODUCT = 1
SETUP = (0x22, 0x09, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00) SETUP = (0x22, 0x09, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00)
MESS = (0x55, 0x53, 0x42, 0x43, 0x00, 0x40, 0x02) MESS = (0x55, 0x53, 0x42, 0x43, 0x00, 0x40, 0x02)
WAITTIME = 0.1 WAITTIME = 0.1
LEFT = 0 LEFT = 0
RIGHT = 1 RIGHT = 1
UP = 0 UP = 0
DOWN = 1 DOWN = 1
OFF = 0 Off = 0
ON = 1 ON = 1
BLUE = (0,0,1) BLUE = (0, 0, 1)
GREEN = (0,1,0) GREEN = (0, 1, 0)
LTBLUE = (0,1,1) LTBLUE = (0, 1, 1)
PURPLE = (1,0,1) PURPLE = (1, 0, 1)
RED = (1,0,0) RED = (1, 0, 0)
WHITE = (1,1,1) WHITE = (1, 1, 1)
YELLOW = (1,1,0) YELLOW = (1, 1, 0)
CLEAR = 0xFF CLEAR = 0xff
finalMess = CLEAR finalMess = CLEAR
command = CLEAR command = CLEAR
def __init__(self, product=None):
dev = usb.core.find(idVendor = self.USB_VENDOR, idProduct = self.USB_PRODUCT)
if dev is None:
raise DeviceNotFoundException()
self.dev = dev
def __init__(self, product=None): def __send(self, inp):
if product is None: """ send your command to the device """
product = self.USB_PRODUCT try:
busses = usb.busses() self.dev.ctrl_transfer(0x21, 0x09, 0x02, 1, self.SETUP)
for bus in busses: self.dev.ctrl_transfer(0x21, 0x09, 0x02, 1, self.MESS + (inp, ))
for dev in bus.devices: except usb.USBError as e:
if dev.idVendor == self.USB_VENDOR and dev.idProduct == product: print(e)
self.dev = dev self.__init__()
if not hasattr(self, 'dev'):
raise DeviceNotFoundException()
self.handle = self.dev.open()
def __send(self, inp): def pumpMessage(self):
""" send your command to the device """ self.send(self.finalMess)
try:
self.handle.controlMsg(0x21, 0x09, self.SETUP, 0x02, 0x01)
self.handle.controlMsg(0x21, 0x09, self.MESS+(inp,), 0x02, 0x01)
except usb.USBError:
self.__init__()
def pumpMessage(self): def resetMessage(self):
self.send(self.finalMess) self.finalMess = self.CLEAR
def resetMessage(self): def doCmd(self, seconds=WAITTIME):
self.finalMess = self.CLEAR """ send the command specified by the current command """
self.__send(self.command)
sleep(seconds)
def doCmd(self, seconds=WAITTIME): def resetCmd(self):
""" send the command specified by the current command """ """ reset command to default (must pump to take effect) """
self.__send(self.command) self.command = self.CLEAR
sleep(seconds)
def resetCmd(self): def setReverseBitValue(self, num, value):
""" reset command to default (must pump to take effect) """ """ commands are sent as disabled bits """
self.command = self.CLEAR if value == 1:
temp = 0xff - (1 << num)
self.command = self.command & temp
elif value == 0:
temp = 1 << num
self.command = self.command | temp
def setReverseBitValue(self,num,value): def getReverseBitValue(self, num):
""" commands are sent as disabled bits """ """ what was that bit set to again? """
if (value==1): temp = self.command
temp = 0xFF - (1<<num) temp = temp >> num
self.command = self.command & temp res = not temp & 1
elif (value==0): return res
temp = 1 << num
self.command = self.command | temp
def getReverseBitValue(self,num): def setHeart(self, status):
""" what was that bit set to again? """ """ heart-light can be on (1) or off (0) """
temp = self.command self.setReverseBitValue(7, status)
temp = temp >> num
res = not(temp&1)
return res
def setHeart(self, status): def flick(self, direction):
""" heart-light can be on (1) or off (0) """ if direction == self.RIGHT:
self.setReverseBitValue(7, status) self.setReverseBitValue(1, 1)
self.setReverseBitValue(0, 0)
elif direction == self.LEFT:
self.setReverseBitValue(1, 0)
self.setReverseBitValue(0, 1)
def flick(self, direction): def wing(self, direction):
if (direction == self.RIGHT): if direction == self.UP:
self.setReverseBitValue(1,1) self.setReverseBitValue(3, 1)
self.setReverseBitValue(0,0) self.setReverseBitValue(0x02, 0)
elif(direction == self.LEFT): elif direction == self.DOWN:
self.setReverseBitValue(1,0) self.setReverseBitValue(3, 0)
self.setReverseBitValue(0,1) self.setReverseBitValue(0x02, 1)
def wing(self, direction): def getColors(self):
if (direction == self.UP): return (self.getReverseBitValue(4), self.getReverseBitValue(5),
self.setReverseBitValue(3,1) self.getReverseBitValue(6))
self.setReverseBitValue(2,0)
elif(direction == self.DOWN):
self.setReverseBitValue(3,0)
self.setReverseBitValue(2,1)
def getColors (self): def getHeart(self):
return self.getReverseBitValue(4), self.getReverseBitValue(5), self.getReverseBitValue(6) """ returns heart-light status of on (1) or off (0) """
return self.getReverseBitValue(7)
def getHeart(self): def getWing(self):
""" returns heart-light status of on (1) or off (0) """ """ returns wing status of BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """
return self.getReverseBitValue(7) return self.getReverseBitValue(0x02)
def getWing(self): def getDirection(self):
""" returns wing status of BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """ return self.getReverseBitValue(1)
return self.getReverseBitValue(2)
def getDirection(self): def send(self, inp):
return self.getReverseBitValue(1) try:
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)
def send(self, inp): def setHeadColors(self, red, green, blue):
try: """ colors as (red, green, blue) can be on (1) or off (0) """
self.dev.handle.controlMsg(0x21, 0x09, self.SETUP, 0x02, 0x01) self.setReverseBitValue(4, red)
self.dev.handle.controlMsg(0x21, 0x09, self.MESS+(inp,), 0x02, 0x01) self.setReverseBitValue(5, green)
except usb.USBError: self.setReverseBitValue(6, blue)
self.__init__(self.battery, self.product)
def setHeadColors(self, red, green, blue): def getHeadColors(self):
""" colors as (red, green, blue) can be on (1) or off (0) """ """ returns color status as tuple representing (red, green, blue) as on (1) or off (0) """
self.setReverseBitValue(4, red) return (self.getReverseBitValue(4), self.getReverseBitValue(5),
self.setReverseBitValue(5, green) self.getReverseBitValue(6))
self.setReverseBitValue(6, blue)
def getHeadColors(self): def setHeart(self, status):
""" returns color status as tuple representing (red, green, blue) as on (1) or off (0) """ """ heart-light can be on (1) or off (0) """
return self.getReverseBitValue(4), self.getReverseBitValue(5), self.getReverseBitValue(6) self.setReverseBitValue(7, status)
def setHeart(self, status): def getHeart(self):
""" heart-light can be on (1) or off (0) """ """ returns heart-light status of on (1) or off (0) """
self.setReverseBitValue(7,status) return self.getReverseBitValue(7)
def getHeart(self): def setWing(self, direction):
""" returns heart-light status of on (1) or off (0) """ """ move the wings BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """
return self.getReverseBitValue(7) if direction == self.UP:
self.setReverseBitValue(3, 1)
self.setReverseBitValue(0x02, 0)
elif direction == self.DOWN:
self.setReverseBitValue(3, 0)
self.setReverseBitValue(0x02, 1)
def setWing(self, direction): def getWing(self):
""" move the wings BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """ """ returns wing status of BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """
if (direction == self.UP): return self.getReverseBitValue(0x02)
self.setReverseBitValue(3,1)
self.setReverseBitValue(2,0)
elif(direction == self.DOWN):
self.setReverseBitValue(3,0)
self.setReverseBitValue(2,1)
def getWing(self): def setSwivel(self, direction):
""" returns wing status of BuddyDevice.UP (0) or BuddyDevice.DOWN (1) """ """ swivel the body BuddyDevice.LEFT (0) or BuddyDevice.RIGHT (1) """
return self.getReverseBitValue(2) if direction == self.RIGHT:
self.setReverseBitValue(1, 1)
self.setReverseBitValue(0, 0)
elif direction == self.LEFT:
self.setReverseBitValue(1, 0)
self.setReverseBitValue(0, 1)
def setSwivel(self, direction): def getSwivel(self):
""" swivel the body BuddyDevice.LEFT (0) or BuddyDevice.RIGHT (1) """ """ returns current swivel direction as BuddyDevice.LEFT (0) or BuddyDevice.RIGHT (1) """
if (direction == self.RIGHT): return self.getReverseBitValue(1)
self.setReverseBitValue(1,1)
self.setReverseBitValue(0,0)
elif(direction == self.LEFT):
self.setReverseBitValue(1,0)
self.setReverseBitValue(0,1)
def getSwivel(self): def doReset(self, seconds=WAITTIME):
""" returns current swivel direction as BuddyDevice.LEFT (0) or BuddyDevice.RIGHT (1) """ """ reset to default positions/off, run command immediately """
return self.getReverseBitValue(1) self.resetCmd()
def doReset(self, seconds=WAITTIME):
""" reset to default positions/off, run command immediately """
self.resetCmd()
self.doCmd(seconds)
def doFlap(self, times=3, seconds=0.2):
""" flap wings X times with Y seconds pause in between, run command immediately """
for i in range(times):
self.setWing(self.UP)
self.doCmd(seconds)
self.setWing(self.DOWN)
self.doCmd(seconds) self.doCmd(seconds)
def doWiggle(self, times=3, seconds=0.2): def doFlap(self, times=3, seconds=0.2):
""" wiggle back and forth X times with Y seconds pauses, run command immediately """ """ flap wings X times with Y seconds pause in between, run command immediately """
for i in range(times): for i in range(times):
self.setSwivel(self.LEFT) self.setWing(self.UP)
self.doCmd(seconds) self.doCmd(seconds)
self.setSwivel(self.RIGHT) self.setWing(self.DOWN)
self.doCmd(seconds)
def doWiggle(self, times=3, seconds=0.2):
""" wiggle back and forth X times with Y seconds pauses, run command immediately """
for i in range(times):
self.setSwivel(self.LEFT)
self.doCmd(seconds)
self.setSwivel(self.RIGHT)
self.doCmd(seconds)
def doHeartbeat(self, times=3, seconds=0.3):
""" blink heart X times with Y seconds' pause in betwee, run command immediately """
for i in range(times):
self.setHeart(self.ON)
self.doCmd(seconds)
self.setHeart(self.Off)
self.doCmd(seconds)
def doColorRGB(self, r, g, b, seconds=WAITTIME):
""" set head color by red/green/blue values 0 or 1, run command immediately """
self.setHeadColors(r, g, b)
self.doCmd(seconds) self.doCmd(seconds)
def doHeartbeat(self, times=3, seconds=0.3): def doColorName(self, rgb, seconds=WAITTIME):
""" blink heart X times with Y seconds' pause in betwee, run command immediately """ """ set head color with color name tuples, run command immediately """
for i in range(times): self.setHeadColors(*rgb)
self.setHeart(self.ON)
self.doCmd(seconds) self.doCmd(seconds)
self.setHeart(self.OFF)
self.doCmd(seconds)
def doColorRGB(self, r, g, b, seconds=WAITTIME):
""" set head color by red/green/blue values 0 or 1, run command immediately """
self.setHeadColors(r, g, b)
self.doCmd(seconds)
def doColorName(self, rgb, seconds=WAITTIME):
""" set head color with color name tuples, run command immediately """
self.setHeadColors(*rgb)
self.doCmd(seconds)

18
test.py
View File

@@ -17,14 +17,16 @@ if __name__ == '__main__':
print('No iBuddy device found!') print('No iBuddy device found!')
exit(1) exit(1)
ibuddy.doColorName(ibuddy.PURPLE, 0.5) for x in range(0, 10):
ibuddy.doColorName(ibuddy.BLUE, 0.5) ibuddy.doColorName(ibuddy.PURPLE, 0.1)
ibuddy.doColorName(ibuddy.LTBLUE, 0.5) ibuddy.doColorName(ibuddy.BLUE, 0.1)
ibuddy.doColorName(ibuddy.YELLOW, 0.5) ibuddy.doColorName(ibuddy.LTBLUE, 0.1)
ibuddy.doColorName(ibuddy.GREEN, 0.5) ibuddy.doColorName(ibuddy.YELLOW, 0.1)
ibuddy.doColorName(ibuddy.RED, 0.5) ibuddy.doColorName(ibuddy.GREEN, 0.1)
ibuddy.doColorName(ibuddy.WHITE, 0.5) ibuddy.doColorName(ibuddy.RED, 0.1)
ibuddy.doFlap() ibuddy.doColorName(ibuddy.WHITE, 0.1)
sleep(0.1)
ibuddy.doFlap() # does not work!
sleep(1) sleep(1)
ibuddy.doWiggle() # does not work! ibuddy.doWiggle() # does not work!
sleep(1) sleep(1)