def rtc(): from pytronics import i2cRead, i2cWrite import json if 'command' in request.form: command = request.form['command'] if command == 'set_rtc': set_rtc() elif command == 'set_rascal': set_rascal() elif command == 'start': i2cWrite(0x68, 0, i2cRead(0x68, 0) & 0x7f) elif command == 'stop': i2cWrite(0x68, 0, i2cRead(0x68, 0) | 0x80) elif command == 'reset': i2cWrite(0x68, 0, [0x80, 0, 0], 'I') return json.dumps(i2cRead(0x68, 0, 'I', 7))
def headlines(): global headlines, lastFeed, lastSlot, lastUpdate import time, json try: feed = int(request.form['feed']) except KeyError: feed = 0 try: temp = pytronics.i2cRead(0x48, 0, 'I', 2) strTemp = '{0:0.1f}{1}C'.format( ((temp[0] << 4) | (temp[1] >> 4)) * 0.0625, unichr(176)) except: strTemp = '' now = time.localtime() # Update headlines every five minutes slot = now.tm_min / 5 if feed != lastFeed or slot != lastSlot: headlines = getHeadlines(feed) lastFeed = feed lastSlot = slot lastUpdate = time.strftime('%H:%M %Z', now) updated = True else: updated = False data = { 'date': time.strftime('%a, %d %b %Y %H:%M %Z', now), 'temp': strTemp, 'headlines': headlines, 'updated': updated, 'lastUpdate': lastUpdate } return json.dumps(data)
def init(): global initialised mode1 = i2cRead(0x40, PCA_INC_NONE | 0, 'B') if mode1 != 0: print '## dimmer ## Initialising (MODE1 was 0x{0:02X})'.format(mode1) settings = json.loads(jsettings) i2cWrite(0x40, PCA_INC_ALL | 0, settings, 'I') initialised = True
def read_sensor(address): import pytronics try: data = pytronics.i2cRead(address, size='W') except: print 'Couldn\'t get reading from sensor' return 0.0 return ((data % 0x0100 * 16) + (data / 0x1000)) * 0.0625
def read_sensor(address): import pytronics try: data = pytronics.i2cRead(address, size="W") except: print "Couldn't get reading from sensor" return 0.0 return ((data % 0x0100 * 16) + (data / 0x1000)) * 0.0625
def set_rascal (): from pytronics import i2cRead import subprocess try: ar = i2cRead(0x68, 0, 'I', 7) args = '20{0:02x}.{1:02x}.{2:02x}-{3:02x}:{4:02x}:{5:02x}'.format(ar[6], ar[5], ar[4], ar[2], ar[1], ar[0]) subp = subprocess.Popen([ 'date', args], stdout=subprocess.PIPE) print '## set_rascal ## ' + subp.communicate()[0].strip() except Exception, e: print '## set_rascal ## Unexpected error: %s' % str(e)
def i2cget(addr, reg, mode): iaddr = int(addr, 0) ireg = int(reg, 0) try: res = pytronics.i2cRead(iaddr, ireg, mode) print '## i2cget ## {0}'.format(res) return str(res) except (OSError, IOError) as e: import errno print '## i2cget ## Error: [{0}] {1}'.format(errno.errorcode[e.errno], e.strerror) return str(-1) except Exception as e: return 'Internal server error', 500
def headlines(): global headlines, headlinesTag, headlinesLastMod, weather, weatherTag, weatherLastMod, lastFeed, lastForecast, lastSlot, lastUpdate import time, json try: feed = int(request.form['feed']) except KeyError: feed = 0 try: forecast = int(request.form['forecast']) except KeyError: forecast = 0 locationID = getLocationID() try: temp = pytronics.i2cRead(0x48, 0, 'I', 2) strTemp = '{0:0.1f}{1}C'.format( ((temp[0] << 4) | (temp[1] >> 4)) * 0.0625, unichr(176)) except: strTemp = '' try: now = time.localtime() # Update headlines every five minutes slot = now.tm_min / 5 updated = False if feed != lastFeed or forecast != lastForecast or slot != lastSlot: try: if feed != lastFeed: headlinesTag = '""' headlinesLastMod = '' result = getHeadlines(feed, headlinesLastMod) if result['status'] == 200: headlinesTag = result['tag'] headlinesLastMod = result['lastMod'] headlines = result['data'] lastUpdate = time.strftime('%H:%M %Z', now) updated = True except Exception, e: print '## getHeadlines ## Unexpected error: %s' % str(e) try: result = getWeather(locationID, forecast, weatherTag) if result['status'] == 200: weatherTag = result['tag'] weatherLastMod = result['lastMod'] weather = result['data'] if not updated: lastUpdate = time.strftime('%H:%M %Z', now) updated = True except Exception, e: print '## getWeather ## Unexpected error: %s' % str(e) lastFeed = feed lastForecast = forecast lastSlot = slot
def dimmer(): try: if not initialised: init() if 'command' in request.form: command = request.form['command'] if command == 'read_status': return json.dumps(i2cRead(0x40, PCA_INC_ALL | 0, 'I', 28)) elif command == 'set_brightness': ireg = int(request.form['register'], 0) ival = int(request.form['value'], 0) i2cWrite(0x40, ireg, ival, 'B') return 'OK', 200 except Exception as e: print '## dimmer ## Unexpected error {0}'.format(e) return 'Bad request', 400
def i2c_read(): import json try: params = json.loads(request.form['params']) print '## i2c_read ## ' + str(params) value = pytronics.i2cRead(params['addr'], params['reg'], params['size'], params['length']) result = {'success': True, 'value': value} return json.dumps(result) except (OSError, IOError) as e: import errno print '## i2c_read ## Error: [{0}] {1}'.format( errno.errorcode[e.errno], e.strerror) result = { 'success': False, 'errorCode': errno.errorcode[e.errno], 'errorMessage': e.strerror } return json.dumps(result) except Exception as e: return 'Internal server error', 500
def i2c_read(): import json try: params = json.loads(request.form['params']) print '## i2c_read ## ' + str(params) value = pytronics.i2cRead(params['addr'], params['reg'], params['size'], params['length']) result = { 'success': True, 'value': value } return json.dumps(result) except (OSError, IOError) as e: import errno print '## i2c_read ## Error: [{0}] {1}'.format(errno.errorcode[e.errno], e.strerror) result = { 'success': False, 'errorCode': errno.errorcode[e.errno], 'errorMessage': e.strerror } return json.dumps(result) except Exception as e: return 'Internal server error', 500
def blinkm_get_rgb(address): rgb = pytronics.i2cRead(int(address), 0x67, 'I', 3) return json.dumps(zip(['red', 'green', 'blue'], rgb))
def blinkm_get_hsb(address): rgb = pytronics.i2cRead(int(address), 0x67, 'I', 3) return json.dumps( zip(['hue', 'saturation', 'brightness'], colorsys.rgb_to_hsv(rgb[0] / 255.0, rgb[1] / 255.0, rgb[2] / 255.0)))
def log_value(num): import datalogger artemp = pytronics.i2cRead(0x48, 0, 'I', 2) ftemp = ((artemp[0] << 4) | (artemp[1] >> 4)) * 0.0625 # print '## temp_log ## ' + str(ftemp) datalogger.log(ftemp)
def blinkm_get_hsb(address): rgb = pytronics.i2cRead(int(address), 0x67, 'I', 3) return json.dumps(zip(['hue', 'saturation', 'brightness'], colorsys.rgb_to_hsv(rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0)))
def log_value(num): import datalogger artemp = pytronics.i2cRead(0x48, 0, 'I', 2) ftemp = ((artemp[0] << 4) | (artemp[1] >> 4)) * 0.0625 # print('## temp_log ## ' + str(ftemp)) datalogger.log(ftemp)