def write(): user_input = raw_input("DigiUSB#") user_input = user_input+"\n" theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) for c in user_input: theDevice.write(ord(c)) time.sleep(0.1)
def send_to_digi(m_str): m_str += '\n' try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) except: #sys.exit("No DigiUSB Device Found") return "ERROR: No DigiUSB Device Found" for c in m_str: theDevice.write(ord(c))
class LED(object): def __init__(self): self._digispark = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) self.off() def on(self, color_name): rgb_values = webcolors.name_to_rgb(color_name) self._digispark.write(ord("s")) for v in rgb_values: self._digispark.write(v) def off(self): rgb_values = [0, 0, 0] self._digispark.write(ord("s")) for v in rgb_values: self._digispark.write(v) def blink(self, color_name, blink_count): self.off() for _ in xrange(blink_count): self.on(color_name) time.sleep(.75) self.off() time.sleep(.75)
class DigisparkTemperature(): def __init__(self): try: self.device = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) except: raise rospy.ROSException("No DigiUSB Device Found") rospy.init_node('temperature_sensor_publisher') self.pub = rospy.Publisher('temp', Temperature, queue_size=50) self.rate = 1.0 def handle(self): self.device.write(ord('\n')) temp = Temperature() temp.header.stamp = self.current_time temp.header.frame_id = 'temp_frame' buf = StringIO() while True: try: c = chr(self.device.read()) if c == '\n': break buf.write(c) except Exception: continue try: rospy.loginfo(buf.getvalue()) v = json.loads(buf.getvalue()) temp.temperature = v['temp'] except ValueError as e: rospy.loginfo(e) return except Exception as e: rospy.logerror(e) return self.pub.publish(temp) def spin(self): r = rospy.Rate(self.rate) while not rospy.is_shutdown(): self.current_time = rospy.Time.now() self.handle() r.sleep()
def write(): try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) print "Found: 0x%04x 0x%04x %s %s" % ( theDevice.idVendor, theDevice.idProduct, theDevice.productName, theDevice.manufacturer) except: pass while True: user_input = raw_input("DigiUSB#") user_input = user_input + "\n" theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) for c in user_input: theDevice.write(ord(c)) time.sleep(0.1)
def blink(): theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) red = request.query.get('red') green = request.query.get('green') blue = request.query.get('blue') print("red:{}, green:{}, blue:{}".format(red, green, blue)) red = int(mapping(red)) green = int(mapping(green)) blue = int(mapping(blue)) theDevice.write(ord("s")) theDevice.write(red) theDevice.write(green) theDevice.write(blue) print("mred:{}, mgreen:{}, mblue:{}".format(red, green, blue))
try: print "Read: 0x%02x" % theDevice.read() except: # TODO: Check for exception properly print "No data read." import sys import time import random if sys.argv[1:]: sequence = sys.argv[1:] else: sequence = [11, 12, 13] * 20 random.shuffle(sequence) print "Look over there, flashing lights!" for pin in sequence: pin = int(pin) theDevice.write(pin) if pin != theDevice.read(): print "Pin response didn't match." time.sleep(0.2) print
# TODO: Check for exception properly print "No data read." import sys import time import random if sys.argv[1:]: sequence = sys.argv[1:] else: sequence = [11,12,13]* 20 random.shuffle(sequence) print "Look over there, flashing lights!" for pin in sequence: pin = int(pin) theDevice.write(pin) if pin != theDevice.read(): print "Pin response didn't match." time.sleep(0.2) print
def updateColor(setColor): global color if (color != setColor): color = setColor global taskbar_icon try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) icon = color if (icon == "black"): icon = "white" taskbar_icon.set_icon("icons/"+icon+".png") # print "Color:"+color global red,green,blue,cyan,white,warmwhite,purple,magenta,yellow,orange,black if color == 'red': color_list = red elif color == 'green': color_list = green elif color == 'blue': color_list = blue elif color == 'cyan': color_list = cyan elif color == 'white': color_list = white elif color == 'warmwhite': color_list = warmwhite elif color == 'purple': color_list = purple elif color == 'magenta': color_list = magenta elif color == 'yellow': color_list = yellow elif color == 'orange': color_list = orange elif color == 'black': color_list = black # while 1: #pin = int(pin) #if output == "\r": # print line # line ="" #else: # line += output #else: # print "Pin response didn't match." #byte val = sys.argv[1] print color_list theDevice.write(ord("s")) if color_list[0] == 0: theDevice.write(0) else: theDevice.write(int(color_list[0])) if color_list[1] == 0: theDevice.write(0) else: theDevice.write(int(color_list[1])) if color_list[2] == 0: theDevice.write(0) else: theDevice.write(int(color_list[2])) except: print "No device found"
#if output == "\r": # print line # line ="" #else: # line += output #else: # print "Pin response didn't match." #byte val = sys.argv[1] print color_list theDevice.write(ord("s")) if color_list[1] == 0: theDevice.write(0) else: theDevice.write(int(color_list[1])) if color_list[2] == 0: theDevice.write(0) else: theDevice.write(int(color_list[2])) if color_list[3] == 0: theDevice.write(0) else: theDevice.write(int(color_list[3]))
# /usr/bin/python # # Written for PyUSB 1.0 (w/libusb 1.0.3) # import usb, sys # 1.0 not 0.4 sys.path.append("..") from arduino.usbdevice import ArduinoUsbDevice if __name__ == "__main__": try: theDevice = ArduinoUsbDevice(idVendor=0x16C0, idProduct=0x05DF) except: sys.exit("No DigiUSB Device Found") try: user_input = sys.argv[1] + "\n" except: exit("No data to send") for c in user_input: theDevice.write(ord(c))
class OpenWestKit: def __init__(self, debug=False): self.DEBUG = debug self.connect() self.brightness(255) time.sleep(1) self.brightness(64) self.clear() def connect(self): if self.DEBUG: print "Connecting Device" deviceBound = False while not deviceBound: try: self.device = ArduinoUsbDevice(idVendor=0x16C0, idProduct=0x05DF) deviceBound = True except Exception as e: print "Problem connecting:", e time.sleep(1) print "retrying..." # Sends a byte to the device def sendByteToDevice(self, data): try: self.device.write(data) except Exception as e: print "Unable to write to device:", e self.connect() # Try to rebind the device return 0 return 1 # Read all data off the bus. Multiple codes may be waiting so we keep # reading until there is no more data. We then return it all in an array. # Codes: # z - Acknowledgement of LED set command # a - Button 1 pressed # b - Button 2 pressed def readData(self): result = [] while True: # keep reading while we have data try: result.append(str(unichr(self.device.read()))) except: break return result # Sets the master brightness on the device (0-255) def brightness(self, bright): if self.DEBUG: print "Setting master brightness to: ", bright while True: if self.sendByteToDevice(42): # LED control code if self.sendByteToDevice(255): # Brightness control code if self.sendByteToDevice(bright): # brightness amount break # Sets a Pixel to RGB value (0-3, 0-255, 0-255, 0-255) def setPixel(self, pixelNum, redByte, greenByte, blueByte): if self.DEBUG: print "Setting pixel [ %s ] to values: %s, %s, %s" % (pixelNum, redByte, greenByte, blueByte) while True: if self.sendByteToDevice(42): # LED control code if self.sendByteToDevice(pixelNum): # LED position control code if self.sendByteToDevice(redByte): if self.sendByteToDevice(greenByte): if self.sendByteToDevice(blueByte): break # Reset board to starting state with Red, Green, Blue, White LED def reset(self): self.brightness(10) self.setPixel(0, 255, 0, 0) self.setPixel(1, 0, 255, 0) self.setPixel(2, 0, 0, 255) self.setPixel(3, 255, 255, 255) # Helper method to turn all LEDs off def clear(self): self.setPixel(0, 0, 0, 0) self.setPixel(1, 0, 0, 0) self.setPixel(2, 0, 0, 0) self.setPixel(3, 0, 0, 0)
sys.path.append("..") from arduino.usbdevice import ArduinoUsbDevice if __name__ == "__main__": try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) print "Found: 0x%04x 0x%04x %s %s" % ( theDevice.idVendor, theDevice.idProduct, theDevice.productName, theDevice.manufacturer) except: print("Couldn't find device.") sys.exit(0) # Switch mode (PlayPingPong). theDevice.write(ord('p')) time.sleep(0.1) while True: key = getch() if ord(key) == 3: sys.exit(0) elif key == 'a': theDevice.write(ord('a')) elif key == 'd': theDevice.write(ord('d')) elif key == 'j': theDevice.write(ord('j')) elif key == 'l': theDevice.write(ord('l'))
class USBLedController(object): def __init__(self): self.blink = False self.on_time = 0 self.off_time = 0 self.color = (0, 0, 0) self.work_thread = threading.Thread(target=self._main_loop) self.work_thread.setDaemon(True) self.work_thread.start() def set_constant(self, color): self.blink = False self.color = color def set_blink(self, color, on_time, off_time): self.on_time = on_time self.off_time = off_time self.color = color self.blink = True def _main_loop(self): while True: try: self.led_usb = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) led_usb = self.led_usb print("Found: 0x%04x 0x%04x %s %s" % (led_usb.idVendor, led_usb.idProduct, led_usb.productName, led_usb.manufacturer)) while True: if not self.blink: self._set_color(self.color) time.sleep(1) else: self._set_color(self.color) time.sleep(self.on_time) self._set_color((0, 0, 0)) time.sleep(self.off_time) except USBFailed: print("USB communication problem, retrying later") time.sleep(2) except USBDeviceNotFound: print("No USB device found, retrying later") time.sleep(2) def _set_color(self, color): r, g, b = color for retry in range(5): try: if retry > 0: # If retrying, flush any previous transfer for _ in range(3): self.led_usb.write(0) self.led_usb.write(ord("s")) self.led_usb.write(r) self.led_usb.write(g) self.led_usb.write(b) return except usb.core.USBError as exp: print(f"USB error retrying({retry})...") print(exp) raise USBFailed("USB communication failed")
# # Assumes 'UsbStreamDemo1.pde' is loaded on Arduino and # LEDs are present on pins 11, 12 and 13. # import usb # 1.0 not 0.4 import sys, time sys.path.append("..") from arduino.usbdevice import ArduinoUsbDevice if __name__ == "__main__": try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) print "Found: 0x%04x 0x%04x %s %s" % ( theDevice.idVendor, theDevice.idProduct, theDevice.productName, theDevice.manufacturer) except: pass while 1 == 1: user_input = raw_input("DigiUSB#") user_input = user_input + "\n" theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) for c in user_input: theDevice.write(ord(c)) time.sleep(0.1)
class OpenWestKit: def __init__(self, debug=False): self.DEBUG = debug self.connect() self.brightness(255) time.sleep(1) self.brightness(64) self.clear() def connect(self): if self.DEBUG: print "Connecting Device" deviceBound = False while (not deviceBound): try: self.device = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) deviceBound = True except Exception as e: print "Problem connecting:", e time.sleep(1) print 'retrying...' # Sends a byte to the device def sendByteToDevice(self, data): try: self.device.write(data) except Exception as e: print "Unable to write to device:", e self.connect() # Try to rebind the device return 0 return 1 # Read all data off the bus. Multiple codes may be waiting so we keep # reading until there is no more data. We then return it all in an array. # Codes: # z - Acknowledgement of LED set command # a - Button 1 pressed # b - Button 2 pressed def readData(self): result = [] while (True): # keep reading while we have data try: result.append(str(unichr(self.device.read()))) except: break return result # Sets the master brightness on the device (0-255) def brightness(self, bright): if self.DEBUG: print "Setting master brightness to: ", bright while (True): if (self.sendByteToDevice(42)): # LED control code if (self.sendByteToDevice(255)): # Brightness control code if (self.sendByteToDevice(bright)): # brightness amount break # Sets a Pixel to RGB value (0-3, 0-255, 0-255, 0-255) def setPixel(self, pixelNum, redByte, greenByte, blueByte): if self.DEBUG: print "Setting pixel [ %s ] to values: %s, %s, %s" % ( pixelNum, redByte, greenByte, blueByte) while (True): if (self.sendByteToDevice(42)): # LED control code if (self.sendByteToDevice(pixelNum) ): # LED position control code if (self.sendByteToDevice(redByte)): if (self.sendByteToDevice(greenByte)): if (self.sendByteToDevice(blueByte)): break # Reset board to starting state with Red, Green, Blue, White LED def reset(self): self.brightness(10) self.setPixel(0, 255, 0, 0) self.setPixel(1, 0, 255, 0) self.setPixel(2, 0, 0, 255) self.setPixel(3, 255, 255, 255) # Helper method to turn all LEDs off def clear(self): self.setPixel(0, 0, 0, 0) self.setPixel(1, 0, 0, 0) self.setPixel(2, 0, 0, 0) self.setPixel(3, 0, 0, 0)
def updateColor(setColor): global color if color != setColor: color = setColor global taskbar_icon try: theDevice = ArduinoUsbDevice(idVendor=0x16C0, idProduct=0x05DF) icon = color if icon == "black": icon = "white" taskbar_icon.set_icon("icons/" + icon + ".png") # print "Color:" + color global red, green, blue, cyan, white, warmwhite, purple, magenta, yellow, orange, black if color == "red": color_list = red elif color == "green": color_list = green elif color == "blue": color_list = blue elif color == "cyan": color_list = cyan elif color == "white": color_list = white elif color == "warmwhite": color_list = warmwhite elif color == "purple": color_list = purple elif color == "magenta": color_list = magenta elif color == "yellow": color_list = yellow elif color == "orange": color_list = orange elif color == "black": color_list = black # while 1: # pin = int(pin) # if output == "\r": # print line # line ="" # else: # line += output # else: # print "Pin response didn't match." # byte val = sys.argv[1] print color_list theDevice.write(ord("s")) if color_list[0] == 0: theDevice.write(0) else: theDevice.write(int(color_list[0])) if color_list[1] == 0: theDevice.write(0) else: theDevice.write(int(color_list[1])) if color_list[2] == 0: theDevice.write(0) else: theDevice.write(int(color_list[2])) except: print "No device found"