if 2 != len(sys.argv): print usageLine exit(1) # Grab the first argument. (Not argv[0], that's the command name.) arg1 = int(sys.argv[1]) # range() is a little unintuitive, to me. range(0, 256) excludes 256, meaning # it includes numbers 0 - 255, inclusive. if not arg1 in range(0, 256): print usageLine exit(1) # Get the i2c bus number, then create a blinkm object with that bus number. bus = GFDITools.guessBus() blinkm = BlinkM(bus=bus) # Issue the command to the I2C blinkm.reset() blinkm.go_to(arg1, arg1, arg1) sys.exit(0) # BlinkM makes some scripts available, which flash the lights in various # patterns. They're included here in commented form. Copy lines as needed, or # uncomment the whole block if you want to throw a rave. ''' raw_input("Press Enter to continue...") print "STARTUP\n" blinkm.play_script(Scripts.STARTUP)
def Execute_Action_Button(root): # conditionally import BlinkM if (Config.i2c_demo()): from pyblinkm import BlinkM, Scripts # find the interface object type objectServerID = root.find("./OBJECTSERVERID").text objectName = root.find("./OBJECTNAME").text objectFlags = root.find("./OBJECTFLAGS").text validate = Validate.checkForValidate(root) if (Config.debug()): print "VALIDATE=%s" % validate outgoingXMLData = BuildResponse.buildHeader(root) if (Config.debug()): print("objectServerID = %s" % objectServerID) # we have the objectServerID so now we can choose the correct # program # FB-1 just does a toggle from on to off from the button name if (objectServerID == "FB-1"): # do a toggle #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData responseData = "XXX" lowername = objectName.lower() if (lowername.count(" off") > 0): lowername = lowername.replace(" off", " on") responseData = lowername.title() elif (lowername.count(" on") > 0): lowername = lowername.replace(" on", " off") responseData = lowername.title() else: responseData = objectName outgoingXMLData += BuildResponse.buildResponse(responseData) # B-1 does a toggle on a BlinkM module on I2C bus address 0xb (11) elif (objectServerID == "B-1"): # do a toggle #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData if (Config.debug()): print "Config.i2c_demo=%i" % Config.i2c_demo() if (Config.i2c_demo()): blinkm = BlinkM(1,0xb) blinkm.reset() try: blinkm.go_to(0, 0, 255) time.sleep(0.2) blinkm.go_to(0, 255, 0) responseData = "OK" except IOError as e: #blinkm.reset() print "I/O error({0}): {1}".format(e.errno, e.strerror) responseData = "FAILED" except: blinkm.reset() print "Unexpected error:", sys.exc_info()[0] raise responseData = "OK" outgoingXMLData += BuildResponse.buildResponse(responseData) else: # invalid RaspiConnect Code outgoingXMLData += Validate.buildValidateResponse("NO") outgoingXMLData += BuildResponse.buildFooter() if (Config.debug()): print outgoingXMLData return outgoingXMLData # End of ExecuteActionButton.py
def Generate_Remote_WebView(root, LOCALURL): if (Config.i2c_demo()): from pyblinkm import BlinkM, Scripts objectServerID = root.find("./OBJECTSERVERID").text objectFlags = root.find("./OBJECTFLAGS").text validate = Validate.checkForValidate(root) if (Config.debug()): print "VALIDATE=%s" % validate outgoingXMLData = BuildResponse.buildHeader(root) if (objectServerID == "W-1"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # normal response requested responseData = "" # check to see if i2c_demo is turned on if (Config.i2c_demo()): if (Config.debug()): print "Config.i2c_demo passed as True" # Yes, it is on # Initialise the BMP085 and use STANDARD mode (default value) # bmp = BMP085(0x77, debug=True) # bmp = BMP085(0x77) # To specify a different operating mode, uncomment one of the following: # bmp = BMP085(0x77, 0) # ULTRALOWPOWER Mode # bmp = BMP085(0x77, 1) # STANDARD Mode # bmp = BMP085(0x77, 2) # HIRES Mode bmp = BMP085(0x77, 3) # ULTRAHIRES Mode count = 0 exceptionCount = 0 exceptionCountBMP = 0 blinkm = BlinkM(1,0xc) blinkm.reset() try: temp = bmp.readTemperature() pressure = bmp.readPressure() altitude = bmp.readAltitude() tempData = "%.2f C" % temp pressureData = "%.2f hPa" % (pressure / 100.0) except IOError as e: exceptionCountBMP = exceptionCountBMP + 1 print "I/O error({0}): {1}".format(e.errno, e.strerror) except: exceptionCountBMP = exceptionCountBMP + 1 print "Unexpected error:", sys.exc_info()[0] raise else: # now set some values for display since we don't have i2C tempData = "xx.x C (no i2c enabled)" pressureData = "xxxx.x hPa (no i2c enabled)" # read an HTML template into aw string with open ("./Templates/W-1.html", "r") as myfile: responseData += myfile.read().replace('\n', '') # replace the URL so it will point to static responseData = responseData.replace("XXX", LOCALURL) # now replace the AAA, BBB, etc with the right data responseData = responseData.replace("AAA", subprocess.check_output(["date", ""], shell=True)) # split uptime at first blank, then at first , uptimeString = subprocess.check_output(["uptime", ""]) uptimeType = uptimeString.split(",") uptimeCount = len(uptimeType) if (uptimeCount == 6): # over 24 hours uptimeSplit = uptimeString.split(",") uptimeSplit = uptimeSplit[0]+uptimeSplit[1] uptimeSplit = uptimeSplit.split(" ", 1) uptimeData = uptimeSplit[1] else: # under 24 hours uptimeSplit = uptimeString.split(" ", 2) uptimeSplit = uptimeSplit[2].split(",", 1) uptimeData = uptimeSplit[0] responseData = responseData.replace("BBB", uptimeData) usersString = subprocess.check_output(["who", "-q"], shell=False, stderr=subprocess.STDOUT,) responseData = responseData.replace("CCC", usersString) freeString = subprocess.check_output(["free", "-mh"]) freeSplit = freeString.split("cache: ", 1) freeSplit = freeSplit[1].split(" ", 2) freeSplit = freeSplit[2].split("\nSwap:", 1) freeData = freeSplit[0] responseData = responseData.replace("DDD", freeData) responseData = responseData.replace("EEE", tempData) responseData = responseData.replace("FFF", pressureData) output = subprocess.check_output(["cat", "/sys/class/thermal/thermal_zone0/temp"]) cpuTemp = "%3.2f C" % (float(output)/1000.0) responseData = responseData.replace("GGG", cpuTemp) try: freeString = subprocess.check_output(["ifconfig", "eth0"]) freeSplit = freeString.split("inet addr:", 1) if (len(freeSplit) > 1): freeSplit = freeSplit[1].split(" ", 1) freeData = freeSplit[0] else: freeData = "" except: freeData = "" responseData = responseData.replace("HHH", freeData) responseData = responseData.replace("III", Config.localURL()) # responseData = responseData.replace("III", "'your external address here'") responseData = responseData.replace("JJJ", Config.version_number()) # read latest data from ST-1 SendText control on RasPiConnect try: with open ("./local/ST-1.txt", "r") as myfile: sendTextData = myfile.read().replace('\n', '') except IOError: sendTextData = "" responseData = responseData.replace("KKK", sendTextData) # check to see if i2c_demo is turned on if (Config.i2c_demo()): time.sleep(0.2) try: blinkm.go_to(255, 0, 0) time.sleep(0.2) blinkm.go_to(0, 255, 0) except IOError as e: #blinkm.reset() exceptionCount = exceptionCount + 1 print "I/O error({0}): {1}".format(e.errno, e.strerror) except: blinkm.reset() exceptionCount = exceptionCount + 1 print "Unexpected error:", sys.exc_info()[0] raise #responseData += subprocess.check_output(["cat", "/proc/cpuinfo"]) #responseData += subprocess.check_output(["cat", "/proc/meminfo"]) outgoingXMLData += BuildResponse.buildResponse(responseData) if (Config.debug()): print outgoingXMLData elif (objectServerID == "W-2"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # normal response requested imageName = "RovioImage.jpg" responseData = "<html><head>" responseData += "<title></title><style>body,html,iframe{margin:0;padding:0;}</style>" responseData += "</head>" responseData += "<body><img src=\"" responseData += LOCALURL responseData += "static/" responseData += imageName responseData += "\" type=\"jpg\" width=\"300\" height=\"300\">" responseData += "<BR>Picture<BR>" responseData +="</body>" responseData += "</html>" outgoingXMLData += BuildResponse.buildResponse(responseData) if (Config.debug()): print outgoingXMLData else: # invalid RaspiConnect Code outgoingXMLData += Validate.buildValidateResponse("NO") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # End of ExecuteWebView.py