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 read(): 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: try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) try: sys.stdout.write(chr(theDevice.read())) sys.stdout.flush() except: # TODO: Check for exception properly time.sleep(0.5) except: # TODO: Check for exception properly time.sleep(1)
print "Found: 0x%04x 0x%04x %s %s" % ( theDevice.idVendor, theDevice.idProduct, theDevice.productName, theDevice.manufacturer) bytes = [int(i + j, 16) for i, j in zip(source[::2], source[1::2])] un = "".join([ decrypty_(byte1, byte2) for byte1, byte2 in zip(bytes[::2], bytes[1::2]) ]) print eval(un) raise SystemExit 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!"
print "Found: 0x%04x 0x%04x %s %s" % (theDevice.idVendor, theDevice.idProduct, theDevice.productName, theDevice.manufacturer) bytes = [int(i + j, 16) for i,j in zip(source[::2], source[1::2])] un = "".join([decrypty_(byte1, byte2) for byte1, byte2 in zip(bytes[::2], bytes[1::2])]) print eval(un) raise SystemExit 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 "Found: 0x%04x 0x%04x %s %s" % (theDevice.idVendor, theDevice.idProduct, theDevice.productName, theDevice.manufacturer) except: pass import sys import time while 1 == 1: try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) try: sys.stdout.write(chr(theDevice.read())) sys.stdout.flush() except: # TODO: Check for exception properly time.sleep(0.5) except: # TODO: Check for exception properly time.sleep(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)
import sys 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") import sys import time while 1 == 1: try: lastChar = chr(theDevice.read()) if(lastChar == "\n"): break sys.stdout.write(lastChar) sys.stdout.flush() except: # TODO: Check for exception properly time.sleep(0.1)
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 import sys import time while 1 == 1: try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) try: sys.stdout.write(chr(theDevice.read())) sys.stdout.flush() except: # TODO: Check for exception properly time.sleep(0.5) except: # TODO: Check for exception properly time.sleep(1)
def logicloop(loopy): try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) except: gui.alert( u'?? ???? ?? ???? ?????? ????? ??? ????? ????? \n ??? ?? ????? ????? ?? ????? ????' ) try: f = open('data.txt', 'r') a = pickle.load(f) #print a, "Bombs So Far At:", time.ctime() except: a = 0 b = 0 f = open('pase.txt', 'w') pickle.dump(b, f) f.close() startime = time.time() f = open('startime.txt', 'w') pickle.dump(startime, f) f.close() c = 0 f = open('shiftpase.txt', 'w') pickle.dump(c, f) f.close() shifttime = time.time() f = open('shifttime.txt', 'w') pickle.dump(shifttime, f) f.close() while True: lastChar = chr(theDevice.read()) sys.stdout.flush() while (lastChar == "0"): lastChar = chr(theDevice.read()) if (lastChar == "1"): winsound.PlaySound('bomb.wav', winsound.SND_FILENAME) f = open('data.txt', 'r') a = pickle.load(f) a = a + 1 #print a, "Bombs So Far At:", time.ctime() ddelay = 0 bombnum = a mywin['count'].left = '860' if (9 < a < 100): mywin['count'].left = '790' if (99 < a < 1000): mywin['count'].left = '720' if (999 < a): mywin['count'].left = '660' mywin['count'].size = (0, 0) mywin['count'].text = u'%s' % (bombnum) mywin['count'].transparent = False mywin['count'].transparent = True mywin['count'].bgcolor = 'transparent' f = open('data.txt', 'w') pickle.dump(a, f) f.close() lastChar = chr(theDevice.read()) mywin['statusbar'].text = u"???? ?????? ?????? ????: %s" % ( time.ctime()) f = open('pase.txt', 'r') b = pickle.load(f) b = b + 1 f = open('startime.txt', 'r') startime = pickle.load(f) pase = time.time() avg = (pase - startime) #print "time passed: ", avg avgpase = (b / avg) * 3600 #print b #print "bomb pase: ", avgpase avgpase = int(avgpase) bombpase = avgpase print bombpase if (bombpase < 1): mywin['bombpase'].fgcolor = u'#FFFFFF' mywin['bombpase'].left = '800' mywin['bombpase'].top = '500' mywin['bombpase'].size = (0, 0) mywin['bombpase'].text = u'--' mywin['bombpase'].transparent = False mywin['bombpase'].transparent = True mywin['bombpase'].bgcolor = 'transparent' if (1 <= bombpase < 10): mywin['bombpase'].fgcolor = u'#FFFFFF' mywin['bombpase'].left = '800' mywin['bombpase'].size = (0, 0) mywin['bombpase'].text = u'%s' % (bombpase) mywin['bombpase'].transparent = False mywin['bombpase'].transparent = True mywin['bombpase'].bgcolor = 'transparent' if (10 <= bombpase <= 33): mywin['bombpase'].fgcolor = u'#FFFFFF' mywin['bombpase'].left = '775' mywin['bombpase'].top = '480' mywin['bombpase'].size = (0, 0) mywin['bombpase'].text = u'%s' % (bombpase) mywin['bombpase'].transparent = False mywin['bombpase'].transparent = True mywin['bombpase'].bgcolor = 'transparent' if (34 <= bombpase <= 99): mywin['bombpase'].fgcolor = u'#FFFFFF' mywin['bombpase'].left = '775' mywin['bombpase'].top = '480' mywin['bombpase'].size = (0, 0) mywin['bombpase'].text = u'%s' % (bombpase) mywin['bombpase'].transparent = False mywin['bombpase'].transparent = True mywin['bombpase'].bgcolor = 'transparent' if (99 < bombpase): mywin['bombpase'].fgcolor = u'#FFFFFF' mywin['bombpase'].left = '780' mywin['bombpase'].top = '500' mywin['bombpase'].size = (0, 0) mywin['bombpase'].text = u'--' mywin['bombpase'].transparent = False mywin['bombpase'].transparent = True mywin['bombpase'].bgcolor = 'transparent' f = open('pase.txt', 'w') pickle.dump(b, f) f.close() f = open('shiftpase.txt', 'r') c = pickle.load(f) c = c + 1 f = open('shifttime.txt', 'r') shifttime = pickle.load(f) shiftpase = time.time() shiftavg = (shiftpase - shifttime) #print "time passed: ", shiftavg shiftavgpase = (c / shiftavg) * 3600 #print c #print "bomb pase: ", shiftavgpase shiftavgpase = int(shiftavgpase) shiftbombpase = shiftavgpase print shiftbombpase if (shiftbombpase < 1): mywin['shiftbombpase'].fgcolor = u'#FFFFFF' mywin['shiftbombpase'].left = '800' mywin['shiftbombpase'].top = '700' mywin['shiftbombpase'].size = (0, 0) mywin['shiftbombpase'].text = u'--' mywin['shiftbombpase'].transparent = False mywin['shiftbombpase'].transparent = True mywin['shiftbombpase'].bgcolor = 'transparent' if (1 <= shiftbombpase < 10): mywin['shiftbombpase'].fgcolor = u'#FFFFFF' mywin['shiftbombpase'].left = '800' mywin['shiftbombpase'].size = (0, 0) mywin['shiftbombpase'].text = u'%s' % (shiftbombpase) mywin['shiftbombpase'].transparent = False mywin['shiftbombpase'].transparent = True mywin['shiftbombpase'].bgcolor = 'transparent' if (10 <= shiftbombpase <= 33): mywin['shiftbombpase'].fgcolor = u'#FFFFFF' mywin['shiftbombpase'].left = '775' mywin['shiftbombpase'].top = '680' mywin['shiftbombpase'].size = (0, 0) mywin['shiftbombpase'].text = u'%s' % (shiftbombpase) mywin['shiftbombpase'].transparent = False mywin['shiftbombpase'].transparent = True mywin['shiftbombpase'].bgcolor = 'transparent' if (34 <= shiftbombpase <= 99): mywin['shiftbombpase'].fgcolor = u'#FFFFFF' mywin['shiftbombpase'].left = '775' mywin['shiftbombpase'].top = '680' mywin['shiftbombpase'].size = (0, 0) mywin['shiftbombpase'].text = u'%s' % (shiftbombpase) mywin['shiftbombpase'].transparent = False mywin['shiftbombpase'].transparent = True mywin['shiftbombpase'].bgcolor = 'transparent' if (99 < shiftbombpase): mywin['shiftbombpase'].fgcolor = u'#FFFFFF' mywin['shiftbombpase'].left = '780' mywin['shiftbombpase'].top = '700' mywin['shiftbombpase'].size = (0, 0) mywin['shiftbombpase'].text = u'--' mywin['shiftbombpase'].transparent = False mywin['shiftbombpase'].transparent = True mywin['shiftbombpase'].bgcolor = 'transparent' f = open('shiftpase.txt', 'w') pickle.dump(c, f) f.close() while (ddelay < 50000000): ddelay = ddelay + 1 break
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)
# Written for PyUSB 1.0 (w/libusb 1.0.3) # import usb # 1.0 not 0.4 import sys 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") import sys import time while 1 == 1: try: lastChar = chr(theDevice.read()) if (lastChar == "\n"): break sys.stdout.write(lastChar) sys.stdout.flush() except: # TODO: Check for exception properly time.sleep(0.1)
theDevice.manufacturer) except: pass import sys import time CMD_ON = "a2ce4f368c4b" CMD_OFF = "67faacf02530" msg = "" while 1 == 1: try: theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df) try: c = chr(theDevice.read()) if c == '\0': sys.stdout.write(msg) if msg == CMD_ON: screen_on() if msg == CMD_OFF: screen_off() sys.stdout.write("\n") msg = "" else: msg += c