diff --git a/Adafruit_CharLCD.py b/Adafruit_CharLCD.py index 22f3d7d..71be2c2 100755 --- a/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD.py @@ -8,58 +8,58 @@ from time import sleep + class Adafruit_CharLCD: # commands - LCD_CLEARDISPLAY = 0x01 - LCD_RETURNHOME = 0x02 - LCD_ENTRYMODESET = 0x04 - LCD_DISPLAYCONTROL = 0x08 - LCD_CURSORSHIFT = 0x10 - LCD_FUNCTIONSET = 0x20 - LCD_SETCGRAMADDR = 0x40 - LCD_SETDDRAMADDR = 0x80 + LCD_CLEARDISPLAY = 0x01 + LCD_RETURNHOME = 0x02 + LCD_ENTRYMODESET = 0x04 + LCD_DISPLAYCONTROL = 0x08 + LCD_CURSORSHIFT = 0x10 + LCD_FUNCTIONSET = 0x20 + LCD_SETCGRAMADDR = 0x40 + LCD_SETDDRAMADDR = 0x80 # flags for display entry mode - LCD_ENTRYRIGHT = 0x00 - LCD_ENTRYLEFT = 0x02 - LCD_ENTRYSHIFTINCREMENT = 0x01 - LCD_ENTRYSHIFTDECREMENT = 0x00 + LCD_ENTRYRIGHT = 0x00 + LCD_ENTRYLEFT = 0x02 + LCD_ENTRYSHIFTINCREMENT = 0x01 + LCD_ENTRYSHIFTDECREMENT = 0x00 # flags for display on/off control - LCD_DISPLAYON = 0x04 - LCD_DISPLAYOFF = 0x00 - LCD_CURSORON = 0x02 - LCD_CURSOROFF = 0x00 - LCD_BLINKON = 0x01 - LCD_BLINKOFF = 0x00 + LCD_DISPLAYON = 0x04 + LCD_DISPLAYOFF = 0x00 + LCD_CURSORON = 0x02 + LCD_CURSOROFF = 0x00 + LCD_BLINKON = 0x01 + LCD_BLINKOFF = 0x00 # flags for display/cursor shift - LCD_DISPLAYMOVE = 0x08 - LCD_CURSORMOVE = 0x00 + LCD_DISPLAYMOVE = 0x08 + LCD_CURSORMOVE = 0x00 # flags for display/cursor shift - LCD_DISPLAYMOVE = 0x08 - LCD_CURSORMOVE = 0x00 - LCD_MOVERIGHT = 0x04 - LCD_MOVELEFT = 0x00 + LCD_DISPLAYMOVE = 0x08 + LCD_CURSORMOVE = 0x00 + LCD_MOVERIGHT = 0x04 + LCD_MOVELEFT = 0x00 # flags for function set - LCD_8BITMODE = 0x10 - LCD_4BITMODE = 0x00 - LCD_2LINE = 0x08 - LCD_1LINE = 0x00 - LCD_5x10DOTS = 0x04 - LCD_5x8DOTS = 0x00 - - - - def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 22], GPIO = None): - # Emulate the old behavior of using RPi.GPIO if we haven't been given - # an explicit GPIO interface to use - if not GPIO: - import RPi.GPIO as GPIO - self.GPIO = GPIO + LCD_8BITMODE = 0x10 + LCD_4BITMODE = 0x00 + LCD_2LINE = 0x08 + LCD_1LINE = 0x00 + LCD_5x10DOTS = 0x04 + LCD_5x8DOTS = 0x00 + + def __init__(self, pin_rs=25, pin_e=24, + pins_db=[23, 17, 27, 22], GPIO=None): + # Emulate the old behavior of using RPi.GPIO if we haven't been given + # an explicit GPIO interface to use + if not GPIO: + import RPi.GPIO as GPIO + self.GPIO = GPIO self.pin_rs = pin_rs self.pin_e = pin_e self.pins_db = pins_db @@ -71,141 +71,128 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 22], GPIO = None): for pin in self.pins_db: self.GPIO.setup(pin, GPIO.OUT) - self.write4bits(0x33) # initialization - self.write4bits(0x32) # initialization - self.write4bits(0x28) # 2 line 5x7 matrix - self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor - self.write4bits(0x06) # shift cursor right + self.write4bits(0x33) # initialization + self.write4bits(0x32) # initialization + self.write4bits(0x28) # 2 line 5x7 matrix + self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor + self.write4bits(0x06) # shift cursor right - self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF + self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF - self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS - self.displayfunction |= self.LCD_2LINE + self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS + self.displayfunction |= self.LCD_2LINE - """ Initialize to default text direction (for romance languages) """ - self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode + """ Initialize to default text direction (for romance languages) """ + self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) + # set the entry mode self.clear() - def begin(self, cols, lines): - if (lines > 1): - self.numlines = lines - self.displayfunction |= self.LCD_2LINE - self.currline = 0 - + if (lines > 1): + self.numlines = lines + self.displayfunction |= self.LCD_2LINE + self.currline = 0 def home(self): - self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero - self.delayMicroseconds(3000) # this command takes a long time! - + self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero + self.delayMicroseconds(3000) # this command takes a long time! def clear(self): - self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display - self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time - + self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display + self.delayMicroseconds( + 3000) # 3000 microsecond sleep, clearing the display takes a long time def setCursor(self, col, row): - self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ] - - if ( row > self.numlines ): - row = self.numlines - 1 # we count rows starting w/0 - - self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row])) + self.row_offsets = [0x00, 0x40, 0x14, 0x54] + if (row > self.numlines): + row = self.numlines - 1 # we count rows starting w/0 - def noDisplay(self): - """ Turn the display off (quickly) """ + self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row])) - self.displaycontrol &= ~self.LCD_DISPLAYON - self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) + def noDisplay(self): + """ Turn the display off (quickly) """ + self.displaycontrol &= ~self.LCD_DISPLAYON + self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def display(self): - """ Turn the display on (quickly) """ - - self.displaycontrol |= self.LCD_DISPLAYON - self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) + """ Turn the display on (quickly) """ + self.displaycontrol |= self.LCD_DISPLAYON + self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noCursor(self): - """ Turns the underline cursor on/off """ - - self.displaycontrol &= ~self.LCD_CURSORON - self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) + """ Turns the underline cursor on/off """ + self.displaycontrol &= ~self.LCD_CURSORON + self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def cursor(self): - """ Cursor On """ - - self.displaycontrol |= self.LCD_CURSORON - self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) + """ Cursor On """ + self.displaycontrol |= self.LCD_CURSORON + self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noBlink(self): - """ Turn on and off the blinking cursor """ - - self.displaycontrol &= ~self.LCD_BLINKON - self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) + """ Turn on and off the blinking cursor """ + self.displaycontrol &= ~self.LCD_BLINKON + self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noBlink(self): - """ Turn on and off the blinking cursor """ - - self.displaycontrol &= ~self.LCD_BLINKON - self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) + """ Turn on and off the blinking cursor """ + self.displaycontrol &= ~self.LCD_BLINKON + self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def DisplayLeft(self): - """ These commands scroll the display without changing the RAM """ - - self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT) + """ These commands scroll the display without changing the RAM """ + self.write4bits( + self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT) def scrollDisplayRight(self): - """ These commands scroll the display without changing the RAM """ - - self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT); + """ These commands scroll the display without changing the RAM """ + self.write4bits( + self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT) def leftToRight(self): - """ This is for text that flows Left to Right """ - - self.displaymode |= self.LCD_ENTRYLEFT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode); + """ This is for text that flows Left to Right """ + self.displaymode |= self.LCD_ENTRYLEFT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def rightToLeft(self): - """ This is for text that flows Right to Left """ - self.displaymode &= ~self.LCD_ENTRYLEFT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) - + """ This is for text that flows Right to Left """ + self.displaymode &= ~self.LCD_ENTRYLEFT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def autoscroll(self): - """ This will 'right justify' text from the cursor """ + """ This will 'right justify' text from the cursor """ - self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) + self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) + def noAutoscroll(self): + """ This will 'left justify' text from the cursor """ - def noAutoscroll(self): - """ This will 'left justify' text from the cursor """ - - self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) - + self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def write4bits(self, bits, char_mode=False): """ Send command to LCD """ - self.delayMicroseconds(1000) # 1000 microsecond sleep + self.delayMicroseconds(1000) # 1000 microsecond sleep - bits=bin(bits)[2:].zfill(8) + bits = bin(bits)[2:].zfill(8) self.GPIO.output(self.pin_rs, char_mode) @@ -216,40 +203,40 @@ def write4bits(self, bits, char_mode=False): if bits[i] == "1": self.GPIO.output(self.pins_db[::-1][i], True) - self.pulseEnable() + self.pulseEnable() for pin in self.pins_db: self.GPIO.output(pin, False) - for i in range(4,8): + for i in range(4, 8): if bits[i] == "1": - self.GPIO.output(self.pins_db[::-1][i-4], True) - - self.pulseEnable() + self.GPIO.output(self.pins_db[::-1][i - 4], True) + self.pulseEnable() def delayMicroseconds(self, microseconds): - seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds - sleep(seconds) - + seconds = microseconds / \ + float(1000000) # divide microseconds by 1 million for seconds + sleep(seconds) def pulseEnable(self): - self.GPIO.output(self.pin_e, False) - self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns - self.GPIO.output(self.pin_e, True) - self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns - self.GPIO.output(self.pin_e, False) - self.delayMicroseconds(1) # commands need > 37us to settle - + self.GPIO.output(self.pin_e, False) + self.delayMicroseconds( + 1) # 1 microsecond pause - enable pulse must be > 450ns + self.GPIO.output(self.pin_e, True) + self.delayMicroseconds( + 1) # 1 microsecond pause - enable pulse must be > 450ns + self.GPIO.output(self.pin_e, False) + self.delayMicroseconds(1) # commands need > 37us to settle def message(self, text): """ Send string to LCD. Newline wraps to second line""" for char in text: if char == '\n': - self.write4bits(0xC0) # next line + self.write4bits(0xC0) # next line else: - self.write4bits(ord(char),True) + self.write4bits(ord(char), True) if __name__ == '__main__': @@ -258,4 +245,3 @@ def message(self, text): lcd.clear() lcd.message(" Adafruit 16x2\n Standard LCD") - diff --git a/Adafruit_CharLCD_IPclock_example.py b/Adafruit_CharLCD_IPclock_example.py index 8330d2b..e569098 100755 --- a/Adafruit_CharLCD_IPclock_example.py +++ b/Adafruit_CharLCD_IPclock_example.py @@ -1,7 +1,7 @@ #!/usr/bin/python from Adafruit_CharLCD import Adafruit_CharLCD -from subprocess import * +from subprocess import * from time import sleep, strftime from datetime import datetime @@ -10,18 +10,19 @@ cmd0 = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1" cmd1 = "ip addr show wlan0 | grep inet | awk '{print $2}' | cut -d/ -f1" -lcd.begin(16,1) +lcd.begin(16, 1) + def run_cmd(cmd): - p = Popen(cmd, shell=True, stdout=PIPE) - output = p.communicate()[0] - return output + p = Popen(cmd, shell=True, stdout=PIPE) + output = p.communicate()[0] + return output -while 1: - lcd.clear() - eth0ipaddr = run_cmd(cmd0) - wlan0ipaddr = run_cmd(cmd1) - #lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) - lcd.message('eth:%s' % ( eth0ipaddr ) ) - lcd.message('wln:%s' % ( wlan0ipaddr ) ) - sleep(2) +while True: + lcd.clear() + eth0ipaddr = run_cmd(cmd0) + wlan0ipaddr = run_cmd(cmd1) + # lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) + lcd.message('eth:%s' % (eth0ipaddr)) + lcd.message('wln:%s' % (wlan0ipaddr)) + sleep(2) diff --git a/Adafruit_I2C.py b/Adafruit_I2C.py index 3423461..ce79c39 100755 --- a/Adafruit_I2C.py +++ b/Adafruit_I2C.py @@ -6,165 +6,167 @@ # Adafruit_I2C Class # =========================================================================== -class Adafruit_I2C : - @staticmethod - def getPiRevision(): - "Gets the version number of the Raspberry Pi board" - # Courtesy quick2wire-python-api - # https://github.com/quick2wire/quick2wire-python-api - try: - with open('/proc/cpuinfo','r') as f: - for line in f: - if line.startswith('Revision'): - return 1 if line.rstrip()[-1] in ['1','2'] else 2 - except: - return 0 - - @staticmethod - def getPiI2CBusNumber(): - # Gets the I2C bus number /dev/i2c# - return 1 if Adafruit_I2C.getPiRevision() > 1 else 0 - - def __init__(self, address, busnum=-1, debug=False): - self.address = address - # By default, the correct I2C bus is auto-detected using /proc/cpuinfo - # Alternatively, you can hard-code the bus version below: - # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's) - # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's) - self.bus = smbus.SMBus( - busnum if busnum >= 0 else Adafruit_I2C.getPiI2CBusNumber()) - self.debug = debug - - def reverseByteOrder(self, data): - "Reverses the byte order of an int (16-bit) or long (32-bit) value" - # Courtesy Vishal Sapre - byteCount = len(hex(data)[2:].replace('L','')[::2]) - val = 0 - for i in range(byteCount): - val = (val << 8) | (data & 0xff) - data >>= 8 - return val - - def errMsg(self): - print "Error accessing 0x%02X: Check your I2C address" % self.address - return -1 - - def write8(self, reg, value): - "Writes an 8-bit value to the specified register/address" - try: - self.bus.write_byte_data(self.address, reg, value) - if self.debug: - print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg) - except IOError, err: - return self.errMsg() - - def write16(self, reg, value): - "Writes a 16-bit value to the specified register/address pair" - try: - self.bus.write_word_data(self.address, reg, value) - if self.debug: - print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % - (value, reg, reg+1)) - except IOError, err: - return self.errMsg() - - def writeList(self, reg, list): - "Writes an array of bytes using I2C format" - try: - if self.debug: - print "I2C: Writing list to register 0x%02X:" % reg - print list - self.bus.write_i2c_block_data(self.address, reg, list) - except IOError, err: - return self.errMsg() - - def readList(self, reg, length): - "Read a list of bytes from the I2C device" - try: - results = self.bus.read_i2c_block_data(self.address, reg, length) - if self.debug: - print ("I2C: Device 0x%02X returned the following from reg 0x%02X" % - (self.address, reg)) - print results - return results - except IOError, err: - return self.errMsg() - - def readU8(self, reg): - "Read an unsigned byte from the I2C device" - try: - result = self.bus.read_byte_data(self.address, reg) - if self.debug: - print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % - (self.address, result & 0xFF, reg)) - return result - except IOError, err: - return self.errMsg() - - def readS8(self, reg): - "Reads a signed byte from the I2C device" - try: - result = self.bus.read_byte_data(self.address, reg) - if result > 127: result -= 256 - if self.debug: - print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % - (self.address, result & 0xFF, reg)) - return result - except IOError, err: - return self.errMsg() - - def readU16(self, reg): - "Reads an unsigned 16-bit value from the I2C device" - try: - hibyte = self.readU8(reg) - lobyte = self.readU8(reg+1) - result = (hibyte << 8) + lobyte - if (self.debug): - print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) - return result - except IOError, err: - return self.errMsg() - - def readS16(self, reg): - "Reads a signed 16-bit value from the I2C device" - try: - hibyte = self.readS8(reg) - lobyte = self.readU8(reg+1) - result = (hibyte << 8) + lobyte - if (self.debug): - print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) - return result - except IOError, err: - return self.errMsg() - - def readU16Rev(self, reg): - "Reads an unsigned 16-bit value from the I2C device with rev byte order" - try: - lobyte = self.readU8(reg) - hibyte = self.readU8(reg+1) - result = (hibyte << 8) + lobyte - if (self.debug): - print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) - return result - except IOError, err: - return self.errMsg() - - def readS16Rev(self, reg): - "Reads a signed 16-bit value from the I2C device with rev byte order" - try: - lobyte = self.readS8(reg) - hibyte = self.readU8(reg+1) - result = (hibyte << 8) + lobyte - if (self.debug): - print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) - return result - except IOError, err: - return self.errMsg() +class Adafruit_I2C: + + @staticmethod + def getPiRevision(): + "Gets the version number of the Raspberry Pi board" + # Courtesy quick2wire-python-api + # https://github.com/quick2wire/quick2wire-python-api + try: + with open('/proc/cpuinfo', 'r') as f: + for line in f: + if line.startswith('Revision'): + return 1 if line.rstrip()[-1] in ['1', '2'] else 2 + except: + return 0 + + @staticmethod + def getPiI2CBusNumber(): + # Gets the I2C bus number /dev/i2c# + return 1 if Adafruit_I2C.getPiRevision() > 1 else 0 + + def __init__(self, address, busnum=-1, debug=False): + self.address = address + # By default, the correct I2C bus is auto-detected using /proc/cpuinfo + # Alternatively, you can hard-code the bus version below: + # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's) + # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's) + self.bus = smbus.SMBus( + busnum if busnum >= 0 else Adafruit_I2C.getPiI2CBusNumber()) + self.debug = debug + + def reverseByteOrder(self, data): + "Reverses the byte order of an int (16-bit) or long (32-bit) value" + # Courtesy Vishal Sapre + byteCount = len(hex(data)[2:].replace('L', '')[::2]) + val = 0 + for i in range(byteCount): + val = (val << 8) | (data & 0xff) + data >>= 8 + return val + + def errMsg(self): + print "Error accessing 0x%02X: Check your I2C address" % self.address + return -1 + + def write8(self, reg, value): + "Writes an 8-bit value to the specified register/address" + try: + self.bus.write_byte_data(self.address, reg, value) + if self.debug: + print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg) + except IOError as err: + return self.errMsg() + + def write16(self, reg, value): + "Writes a 16-bit value to the specified register/address pair" + try: + self.bus.write_word_data(self.address, reg, value) + if self.debug: + print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % + (value, reg, reg + 1)) + except IOError as err: + return self.errMsg() + + def writeList(self, reg, list): + "Writes an array of bytes using I2C format" + try: + if self.debug: + print "I2C: Writing list to register 0x%02X:" % reg + print list + self.bus.write_i2c_block_data(self.address, reg, list) + except IOError as err: + return self.errMsg() + + def readList(self, reg, length): + "Read a list of bytes from the I2C device" + try: + results = self.bus.read_i2c_block_data(self.address, reg, length) + if self.debug: + print ("I2C: Device 0x%02X returned the following from reg 0x%02X" % + (self.address, reg)) + print results + return results + except IOError as err: + return self.errMsg() + + def readU8(self, reg): + "Read an unsigned byte from the I2C device" + try: + result = self.bus.read_byte_data(self.address, reg) + if self.debug: + print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % + (self.address, result & 0xFF, reg)) + return result + except IOError as err: + return self.errMsg() + + def readS8(self, reg): + "Reads a signed byte from the I2C device" + try: + result = self.bus.read_byte_data(self.address, reg) + if result > 127: + result -= 256 + if self.debug: + print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % + (self.address, result & 0xFF, reg)) + return result + except IOError as err: + return self.errMsg() + + def readU16(self, reg): + "Reads an unsigned 16-bit value from the I2C device" + try: + hibyte = self.readU8(reg) + lobyte = self.readU8(reg + 1) + result = (hibyte << 8) + lobyte + if (self.debug): + print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) + return result + except IOError as err: + return self.errMsg() + + def readS16(self, reg): + "Reads a signed 16-bit value from the I2C device" + try: + hibyte = self.readS8(reg) + lobyte = self.readU8(reg + 1) + result = (hibyte << 8) + lobyte + if (self.debug): + print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) + return result + except IOError as err: + return self.errMsg() + + def readU16Rev(self, reg): + "Reads an unsigned 16-bit value from the I2C device with rev byte order" + try: + lobyte = self.readU8(reg) + hibyte = self.readU8(reg + 1) + result = (hibyte << 8) + lobyte + if (self.debug): + print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) + return result + except IOError as err: + return self.errMsg() + + def readS16Rev(self, reg): + "Reads a signed 16-bit value from the I2C device with rev byte order" + try: + lobyte = self.readS8(reg) + hibyte = self.readU8(reg + 1) + result = (hibyte << 8) + lobyte + if (self.debug): + print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) + return result + except IOError as err: + return self.errMsg() if __name__ == '__main__': - try: - bus = Adafruit_I2C(address=0) - print "Default I2C bus is accessible" - except: - print "Error accessing default I2C bus" + try: + bus = Adafruit_I2C(address=0) + print "Default I2C bus is accessible" + except: + print "Error accessing default I2C bus" diff --git a/ECE118_CHAR.py b/ECE118_CHAR.py index 2542cc1..afaafb3 100755 --- a/ECE118_CHAR.py +++ b/ECE118_CHAR.py @@ -1,39 +1,41 @@ #!/usr/bin/python from Adafruit_CharLCD import Adafruit_CharLCD -from subprocess import * +from subprocess import * from time import sleep, strftime from datetime import datetime -execfile("/root/Scripts/ECE118_PHOTO.py") +exec(compile(open("/root/Scripts/ECE118_PHOTO.py") + .read(), "/root/Scripts/ECE118_PHOTO.py", 'exec')) lcd = Adafruit_CharLCD() cmd0 = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1" cmd1 = "ip addr show wlan0 | grep inet | awk '{print $2}' | cut -d/ -f1" -lcd.begin(16,1) +lcd.begin(16, 1) + def run_cmd(cmd): - p = Popen(cmd, shell=True, stdout=PIPE) - output = p.communicate()[0] - return output - -while 1: - lcd.clear() - readinfile = open("/root/Scripts/displaycmd.dat", 'r') - displaycmd = readinfile.read() - readinfile.close() - - if displaycmd == '' or displaycmd == '\n': - eth0ipaddr = run_cmd(cmd0) - wlan0ipaddr = run_cmd(cmd1) - #lcd.message(datetime.now().strftime('%b %d %H:%M:%S')) - lcd.message('eth:%s\n' % ( eth0ipaddr ) ) - lcd.message('wln:%s\n' % ( wlan0ipaddr ) ) - sleep(2) - - lcd.clear() - lcd.message('luminos:%i\n' % ( readadc(0) ) ) - else: - lcd.message(displaycmd) - sleep(2) + p = Popen(cmd, shell=True, stdout=PIPE) + output = p.communicate()[0] + return output + +while True: + lcd.clear() + readinfile = open("/root/Scripts/displaycmd.dat", 'r') + displaycmd = readinfile.read() + readinfile.close() + + if displaycmd == '' or displaycmd == '\n': + eth0ipaddr = run_cmd(cmd0) + wlan0ipaddr = run_cmd(cmd1) + # lcd.message(datetime.now().strftime('%b %d %H:%M:%S')) + lcd.message('eth:%s\n' % (eth0ipaddr)) + lcd.message('wln:%s\n' % (wlan0ipaddr)) + sleep(2) + + lcd.clear() + lcd.message('luminos:%i\n' % (readadc(0))) + else: + lcd.message(displaycmd) + sleep(2) diff --git a/ECE118_MOTOR.py b/ECE118_MOTOR.py index 353e129..e1f1941 100755 --- a/ECE118_MOTOR.py +++ b/ECE118_MOTOR.py @@ -1,5 +1,5 @@ -#IO18 -> 12 -#IO04 -> 7 +# IO18 -> 12 +# IO04 -> 7 import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, GPIO.HIGH) diff --git a/ECE118_PHOTO.py b/ECE118_PHOTO.py index be1cc68..c0b8a42 100755 --- a/ECE118_PHOTO.py +++ b/ECE118_PHOTO.py @@ -5,12 +5,14 @@ DEBUG = 0 spi = spidev.SpiDev() -spi.open(0,0) +spi.open(0, 0) # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) + + def readadc(adcnum): - if ((adcnum > 7) or (adcnum < 0)): - return -1 - r = spi.xfer2([1,(8+adcnum)<<4,0]) - adcout = ((r[1]&3) << 8) + r[2] - return adcout + if ((adcnum > 7) or (adcnum < 0)): + return -1 + r = spi.xfer2([1, (8 + adcnum) << 4, 0]) + adcout = ((r[1] & 3) << 8) + r[2] + return adcout