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 i2cset(addr, reg, val, mode): iaddr = int(addr, 0) ireg = int(reg, 0) ival = int(val, 0) try: pytronics.i2cWrite(iaddr, ireg, ival, mode) print '## i2cset ##' return str(0) except (OSError, IOError) as e: import errno print '## i2cset ## Error: [{0}] {1}'.format(errno.errorcode[e.errno], e.strerror) return str(-1) except Exception as e: return 'Internal server error', 500
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_write(): import json try: params = json.loads(request.form['params']) print '## i2c_write ## ' + str(params) pytronics.i2cWrite(params['addr'], params['reg'], params['value'], params['size']) result = { 'success': True } return json.dumps(result) except (OSError, IOError) as e: import errno print '## i2c_write ## 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_write(): import json try: params = json.loads(request.form['params']) print '## i2c_write ## ' + str(params) pytronics.i2cWrite(params['addr'], params['reg'], params['value'], params['size']) result = {'success': True} return json.dumps(result) except (OSError, IOError) as e: import errno print '## i2c_write ## 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 set_rtc (): from pytronics import i2cWrite import subprocess, time cmd = 'ntpdate uk.pool.ntp.org' subp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) try: data = subp.communicate()[0].strip() t = time.gmtime() rtc = [] rtc.append(BCD(t.tm_sec)) rtc.append(BCD(t.tm_min)) rtc.append(BCD(t.tm_hour)) rtc.append(BCD(t.tm_wday + 1)) rtc.append(BCD(t.tm_mday)) rtc.append(BCD(t.tm_mon)) rtc.append(BCD(t.tm_year - 2000)) i2cWrite(0x68, 0, rtc, 'I') # Reset OSF (oscillator stopped flag) i2cWrite(0x68, 0x09, 0, 'B') subp = subprocess.Popen([ 'date'], stdout=subprocess.PIPE) print '## set_rtc ## ' + subp.communicate()[0].strip() except Exception, e: print '## set_rtc ## Unexpected error: %s' % str(e)
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))