diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..6d764cfa --- /dev/null +++ b/.gitignore @@ -0,0 +1,56 @@ +# Python specific .gitignore +# GitHub recommended entries from https://github.com/github/gitignore + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +bin/ +build/ +develop-eggs/ +dist/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject + +# Rope +.ropeproject + +# Django stuff: +*.log +*.pot + +# Sphinx documentation +docs/_build/ diff --git a/Adafruit_ADS1x15/ads1x15_ex_singleended.py b/Adafruit_ADS1x15/ads1x15_ex_singleended.py index 383927fa..2c4dee43 100644 --- a/Adafruit_ADS1x15/ads1x15_ex_singleended.py +++ b/Adafruit_ADS1x15/ads1x15_ex_singleended.py @@ -13,7 +13,7 @@ def signal_handler(signal, frame): ADS1115 = 0x01 # 16-bit ADC # Select the gain -# gain = 61 # +/- 6.144V +# gain = 6144 # +/- 6.144V gain = 4096 # +/- 4.096V # gain = 2048 # +/- 2.048V # gain = 1024 # +/- 1.024V diff --git a/Adafruit_BMP085/DEPRECATED.txt b/Adafruit_BMP085/DEPRECATED.txt new file mode 100644 index 00000000..4e7dadf4 --- /dev/null +++ b/Adafruit_BMP085/DEPRECATED.txt @@ -0,0 +1,5 @@ +This BMP085 sensor library has been deprecated in favor of a new version that supports both the Raspberry Pi and Beaglebone Black. + +You can find the new library with installation instructions at this github repository: https://github.com/adafruit/Adafruit_Python_BMP + +You can also find a tutorial on using this library at: https://learn.adafruit.com/using-the-bmp085-with-raspberry-pi/using-the-adafruit-bmp-python-library diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 22060c94..a4bb9a93 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -8,58 +8,58 @@ from time import sleep -class Adafruit_CharLCD: + +class Adafruit_CharLCD(object): # 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, 21, 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, 21, 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 + GPIO.setwarnings(False) + self.GPIO = GPIO self.pin_rs = pin_rs self.pin_e = pin_e self.pins_db = pins_db @@ -71,191 +71,140 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 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 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 - - self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row])) - - - def noDisplay(self): - """ Turn the display off (quickly) """ - - 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 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) - - - def noBlink(self): - """ Turn on and off the blinking cursor """ - - self.displaycontrol &= ~self.LCD_BLINKON - self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) - + """ Turns the underline 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 the blinking cursor off """ + self.displaycontrol &= ~self.LCD_BLINKON + self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) + def blink(self): + """ Turn the blinking cursor on """ + 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 """ - - self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) - - - def noAutoscroll(self): - """ This will 'left justify' text from the cursor """ - - self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) + """ This will 'right justify' text from the cursor """ + self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) + def noAutoscroll(self): + """ This will 'left justify' text from the cursor """ + 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 - - bits=bin(bits)[2:].zfill(8) - + self.delayMicroseconds(1000) # 1000 microsecond sleep + bits = bin(bits)[2:].zfill(8) self.GPIO.output(self.pin_rs, char_mode) - for pin in self.pins_db: self.GPIO.output(pin, False) - for i in range(4): 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.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__': - lcd = Adafruit_CharLCD() - lcd.clear() lcd.message(" Adafruit 16x2\n Standard LCD") - diff --git a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py b/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py index d3f6958f..f1be14cd 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py +++ b/Adafruit_CharLCD/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 @@ -9,16 +9,17 @@ cmd = "ip addr show eth0 | 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() - ipaddr = run_cmd(cmd) - lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) - lcd.message('IP %s' % ( ipaddr ) ) - sleep(2) + lcd.clear() + ipaddr = run_cmd(cmd) + lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) + lcd.message('IP %s' % (ipaddr)) + sleep(2) diff --git a/Adafruit_CharLCD/Adafruit_MCP230xx.py b/Adafruit_CharLCD/Adafruit_MCP230xx.py new file mode 120000 index 00000000..3df8faef --- /dev/null +++ b/Adafruit_CharLCD/Adafruit_MCP230xx.py @@ -0,0 +1 @@ +../Adafruit_MCP230xx/Adafruit_MCP230xx.py \ No newline at end of file diff --git a/Adafruit_CharLCD/LCD_MCP230XX_test.py b/Adafruit_CharLCD/LCD_MCP230XX_test.py new file mode 100644 index 00000000..cc1e7bcd --- /dev/null +++ b/Adafruit_CharLCD/LCD_MCP230XX_test.py @@ -0,0 +1,18 @@ +#!/usr/bin/python +# Example script to show usage of MCP230xx GPIO extender to drive character LCD. + +from Adafruit_CharLCD import Adafruit_CharLCD +from Adafruit_MCP230xx import MCP230XX_GPIO + +bus = 1 # Note you need to change the bus number to 0 if running on a revision 1 Raspberry Pi. +address = 0x20 # I2C address of the MCP230xx chip. +gpio_count = 8 # Number of GPIOs exposed by the MCP230xx chip, should be 8 or 16 depending on chip. + +# Create MCP230xx GPIO adapter. +mcp = MCP230XX_GPIO(bus, address, gpio_count) + +# Create LCD, passing in MCP GPIO adapter. +lcd = Adafruit_CharLCD(pin_rs=1, pin_e=2, pins_db=[3,4,5,6], GPIO=mcp) + +lcd.clear() +lcd.message(" Adafruit 16x2\n Standard LCD") diff --git a/Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py b/Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py index 06b9300c..8542e66b 100644 --- a/Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py +++ b/Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py @@ -11,6 +11,7 @@ from Adafruit_I2C import Adafruit_I2C from time import sleep + class Adafruit_CharLCDPlate(Adafruit_I2C): # ---------------------------------------------------------------------- @@ -72,17 +73,29 @@ class Adafruit_CharLCDPlate(Adafruit_I2C): LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00 + # Line addresses for up to 4 line displays. Maps line number to DDRAM address for line. + LINE_ADDRESSES = { 1: 0xC0, 2: 0x94, 3: 0xD4 } + + # Truncation constants for message function truncate parameter. + NO_TRUNCATE = 0 + TRUNCATE = 1 + TRUNCATE_ELLIPSIS = 2 # ---------------------------------------------------------------------- # Constructor - def __init__(self, busnum=-1, addr=0x20, debug=False): + def __init__(self, busnum=-1, addr=0x20, debug=False, backlight=ON): self.i2c = Adafruit_I2C(addr, busnum, debug) # I2C is relatively slow. MCP output port states are cached # so we don't need to constantly poll-and-change bit states. - self.porta, self.portb, self.ddrb = 0, 0, 0b00010000 + self.porta, self.portb, self.ddrb = 0, 0, 0b00000010 + + # Set initial backlight color. + c = ~backlight + self.porta = (self.porta & 0b00111111) | ((c & 0b011) << 6) + self.portb = (self.portb & 0b11111110) | ((c & 0b100) >> 2) # Set MCP23017 IOCON register to Bank 0 with sequential operation. # If chip is already set for Bank 0, this will just write to OLATB, @@ -116,8 +129,8 @@ def __init__(self, busnum=-1, addr=0x20, debug=False): 0b00000000, # INTCAPB self.porta, # GPIOA self.portb, # GPIOB - self.porta, # OLATA 0 on all outputs; side effect of - self.portb ]) # OLATB turning on R+G+B backlight LEDs. + self.porta, # OLATA + self.portb ]) # OLATB # Switch to Bank 1 and disable sequential operation. # From this point forward, the register addresses do NOT match @@ -187,7 +200,7 @@ def write(self, value, char_mode=False): """ Send command/data to LCD """ # If pin D7 is in input state, poll LCD busy flag until clear. - if self.ddrb & 0b00010000: + if self.ddrb & 0b00000010: lo = (self.portb & 0b00000001) | 0b01000000 hi = lo | 0b00100000 # E=1 (strobe) self.i2c.bus.write_byte_data( @@ -204,7 +217,7 @@ def write(self, value, char_mode=False): self.portb = lo # Polling complete, change D7 pin to output - self.ddrb &= 0b11101111 + self.ddrb &= 0b11111101 self.i2c.bus.write_byte_data(self.i2c.address, self.MCP23017_IODIRB, self.ddrb) @@ -249,7 +262,7 @@ def write(self, value, char_mode=False): # If a poll-worthy instruction was issued, reconfigure D7 # pin as input to indicate need for polling on next call. if (not char_mode) and (value in self.pollables): - self.ddrb |= 0b00010000 + self.ddrb |= 0b00000010 self.i2c.bus.write_byte_data(self.i2c.address, self.MCP23017_IODIRB, self.ddrb) @@ -260,6 +273,7 @@ def write(self, value, char_mode=False): def begin(self, cols, lines): self.currline = 0 self.numlines = lines + self.numcols = cols self.clear() @@ -404,13 +418,24 @@ def createChar(self, location, bitmap): self.write(self.LCD_SETDDRAMADDR) - def message(self, text): + def message(self, text, truncate=NO_TRUNCATE): """ Send string to LCD. Newline wraps to second line""" lines = str(text).split('\n') # Split at newline(s) for i, line in enumerate(lines): # For each substring... - if i > 0: # If newline(s), - self.write(0xC0) # set DDRAM address to 2nd line - self.write(line, True) # Issue substring + address = self.LINE_ADDRESSES.get(i, None) + if address is not None: # If newline(s), + self.write(address) # set DDRAM address to line + # Handle appropriate truncation if requested. + linelen = len(line) + if truncate == self.TRUNCATE and linelen > self.numcols: + # Hard truncation of line. + self.write(line[0:self.numcols], True) + elif truncate == self.TRUNCATE_ELLIPSIS and linelen > self.numcols: + # Nicer truncation with ellipses. + self.write(line[0:self.numcols-3] + '...', True) + else: + self.write(line, True) + def backlight(self, color): diff --git a/Adafruit_CharLCDPlate/Adafruit_I2C.py b/Adafruit_CharLCDPlate/Adafruit_I2C.py index f5fcefeb..77f06164 120000 --- a/Adafruit_CharLCDPlate/Adafruit_I2C.py +++ b/Adafruit_CharLCDPlate/Adafruit_I2C.py @@ -1 +1 @@ -../../Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C/Adafruit_I2C.py \ No newline at end of file +../Adafruit_I2C/Adafruit_I2C.py \ No newline at end of file diff --git a/Adafruit_CharLCDPlate/Adafruit_MCP230xx.py b/Adafruit_CharLCDPlate/Adafruit_MCP230xx.py index 4e89f685..3df8faef 120000 --- a/Adafruit_CharLCDPlate/Adafruit_MCP230xx.py +++ b/Adafruit_CharLCDPlate/Adafruit_MCP230xx.py @@ -1 +1 @@ -../../Adafruit-Raspberry-Pi-Python-Code/Adafruit_MCP230xx/Adafruit_MCP230xx.py \ No newline at end of file +../Adafruit_MCP230xx/Adafruit_MCP230xx.py \ No newline at end of file diff --git a/Adafruit_CharLCDPlate/LCDtest_20x4.py b/Adafruit_CharLCDPlate/LCDtest_20x4.py new file mode 100644 index 00000000..01fc8fe8 --- /dev/null +++ b/Adafruit_CharLCDPlate/LCDtest_20x4.py @@ -0,0 +1,156 @@ +#!/usr/bin/python + +#---------------------------------------------------------------- +# Author: Chris Crumpacker +# Date: May 2013 +# +# A demo of some of the built in helper functions of +# the Adafruit_CharLCDPlate.py This is 20x4 display specific. +# +# Using Adafruit_CharLCD code with the I2C and MCP230xx code aswell +#---------------------------------------------------------------- + +numcolumns = 20 +numrows = 4 + +from time import sleep +from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate + +lcd = Adafruit_CharLCDPlate() + +lcd.begin(numcolumns, numrows) + +lcd.backlight(lcd.ON) +lcd.message("LCD 20x4\nDemonstration") +sleep(2) + +while True: + #Text on each line alone. + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Line 1") + sleep(1) + + lcd.clear() + lcd.setCursor(0,1) + lcd.message("Line 2") + sleep(1) + + lcd.clear() + lcd.setCursor(0,2) + lcd.message("Line 3") + sleep(1) + + lcd.clear() + lcd.setCursor(0,3) + lcd.message("Line 4") + sleep(1) + + # Using the "\n" new line marker + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Line 1") + sleep(1) + + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Line 1\nLine 2") + sleep(1) + + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Line 1\nLine 2\nLine 3") + sleep(1) + + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Line 1\nLine 2\nLine 3\nLine 4") + sleep(1) + + # Auto line limiting by length as to not overflow the display + # This is line by line and does not to any caraige returns + lcd.clear() + lcd.setCursor(0,0) + lcd.message("This String is 33 Characters long", lcd.TRUNCATE) + sleep(2) + + lcd.clear() + lcd.setCursor(0,0) + lcd.message("This String has ellipsis", lcd.TRUNCATE_ELLIPSIS) + sleep(2) + + #Scroll text to the right + messageToPrint = "Scrolling Right" + i=0 + while i<20: + lcd.clear() + lcd.setCursor(0,0) + suffix = " " * i + lcd.message(suffix + messageToPrint, lcd.TRUNCATE) + sleep(.25) + i += 1 + + # Scroll test in from the Left + messageToPrint = "Scrolling Left" + i=20 + while i>=0: + lcd.clear() + lcd.setCursor(0,0) + suffix = " " * i + lcd.message(suffix + messageToPrint, lcd.TRUNCATE) + sleep(.25) + i -= 1 + sleep(2) + + # Printing text backwards, NOT right justified + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Right to left:") + lcd.setCursor(10,1) + lcd.rightToLeft() + lcd.message("Testing") + sleep(2) + + # Printing normally from the middle of the line + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Left to Right:") + lcd.setCursor(10,1) + lcd.message("Testing") + sleep(2) + + # Enabling the cursor and having it blink + lcd.clear() + lcd.setCursor(0,0) + lcd.cursor() + lcd.blink() + lcd.message("Cursor is blinking") + lcd.setCursor(0,1) + sleep(3) + lcd.noCursor() + lcd.noBlink() + + # Turning the backlight off and showing a simple count down + lcd.clear() + lcd.setCursor(0,0) + lcd.message("Backlight off in") + lcd.setCursor(0,3) + lcd.message("Back on in 3sec") + lcd.setCursor(17,0) #Reseting the cursor here keeps us from having to clear the screen, this over writes the previous character + lcd.message("3") + sleep(1) + + lcd.setCursor(17,0) + lcd.message("2") + sleep(1) + + lcd.setCursor(17,0) + lcd.message("1") + sleep(1) + + lcd.backlight(lcd.OFF) + lcd.clear() + lcd.setCursor(0,0) + sleep(3) + lcd.backlight(lcd.ON) + lcd.message("Backlight on") diff --git a/Adafruit_DHT_Driver/Adafruit_DHT.c b/Adafruit_DHT_Driver/Adafruit_DHT.c index 9d1746be..a35cf10d 100644 --- a/Adafruit_DHT_Driver/Adafruit_DHT.c +++ b/Adafruit_DHT_Driver/Adafruit_DHT.c @@ -105,7 +105,9 @@ int readDHT(int type, int pin) { } laststate = bcm2835_gpio_lev(pin); if (counter == 1000) break; +#ifdef DEBUG bits[bitidx++] = counter; +#endif if ((i>3) && (i%2 == 0)) { // shove each bit into the storage bytes diff --git a/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py b/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py index c13d3033..9c9f6318 100755 --- a/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py +++ b/Adafruit_DHT_Driver/Adafruit_DHT_googledocs.ex.py @@ -32,7 +32,7 @@ try: worksheet = gc.open(spreadsheet).sheet1 # Alternatively, open a spreadsheet using the spreadsheet's key - # worksheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE') + # worksheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE').sheet1 except: print "Unable to open the spreadsheet. Check your filename: %s" % spreadsheet sys.exit() diff --git a/Adafruit_DHT_Driver/DEPRECATED.txt b/Adafruit_DHT_Driver/DEPRECATED.txt new file mode 100644 index 00000000..41c4538a --- /dev/null +++ b/Adafruit_DHT_Driver/DEPRECATED.txt @@ -0,0 +1,5 @@ +This Adafruit_DHT_Driver code is deprecated and has been superseded by the Python library +at https://github.com/adafruit/Adafruit_Python_DHT You can see the new library's C code +for interfacing with the DHT sensors on either a Raspberry Pi or Beaglebone Black in the +source/Raspberry_Pi or source/Beaglebone_Black directory of the new library. See more +details on installing and using the library in the learning guide at: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview diff --git a/Adafruit_DHT_Driver_Python/DEPRECATED.txt b/Adafruit_DHT_Driver_Python/DEPRECATED.txt new file mode 100644 index 00000000..96dd74ec --- /dev/null +++ b/Adafruit_DHT_Driver_Python/DEPRECATED.txt @@ -0,0 +1,5 @@ +This Adafruit_DHT_Driver_Python code is deprecated and has been superseded by the Python +library at https://github.com/adafruit/Adafruit_Python_DHT You can see the new library's C +code for interfacing with the DHT sensors on either a Raspberry Pi or Beaglebone Black in the +source/Raspberry_Pi or source/Beaglebone_Black directory of the new library. See more details +on installing and using the library in the learning guide at: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview diff --git a/Adafruit_I2C/Adafruit_I2C.py b/Adafruit_I2C/Adafruit_I2C.py index 0c4a7d98..ba81def3 100755 --- a/Adafruit_I2C/Adafruit_I2C.py +++ b/Adafruit_I2C/Adafruit_I2C.py @@ -6,18 +6,19 @@ # Adafruit_I2C Class # =========================================================================== -class Adafruit_I2C : +class Adafruit_I2C(object): @staticmethod def getPiRevision(): "Gets the version number of the Raspberry Pi board" # Courtesy quick2wire-python-api # https://github.com/quick2wire/quick2wire-python-api + # Updated revision info from: http://elinux.org/RPi_HardwareHistory#Board_Revision_History 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 + return 1 if line.rstrip()[-1] in ['2','3'] else 2 except: return 0 @@ -25,15 +26,14 @@ def getPiRevision(): 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.bus = smbus.SMBus(busnum if busnum >= 0 else Adafruit_I2C.getPiI2CBusNumber()) self.debug = debug def reverseByteOrder(self, data): @@ -69,6 +69,15 @@ def write16(self, reg, value): except IOError, err: return self.errMsg() + def writeRaw8(self, value): + "Writes an 8-bit value on the bus" + try: + self.bus.write_byte(self.address, value) + if self.debug: + print "I2C: Wrote 0x%02X" % value + except IOError, err: + return self.errMsg() + def writeList(self, reg, list): "Writes an array of bytes using I2C format" try: @@ -114,22 +123,25 @@ def readS8(self, reg): except IOError, err: return self.errMsg() - def readU16(self, reg): + def readU16(self, reg, little_endian=True): "Reads an unsigned 16-bit value from the I2C device" try: result = self.bus.read_word_data(self.address,reg) + # Swap bytes if using big endian because read_word_data assumes little + # endian on ARM (little endian) systems. + if not little_endian: + result = ((result << 8) & 0xFF00) + (result >> 8) 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): + def readS16(self, reg, little_endian=True): "Reads a signed 16-bit value from the I2C device" try: - result = self.bus.read_word_data(self.address,reg) - if (self.debug): - print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) + result = self.readU16(reg,little_endian) + if result > 32767: result -= 65536 return result except IOError, err: return self.errMsg() diff --git a/Adafruit_LEDBackpack/Adafruit_Bargraph.py b/Adafruit_LEDBackpack/Adafruit_Bargraph.py new file mode 100644 index 00000000..804991fa --- /dev/null +++ b/Adafruit_LEDBackpack/Adafruit_Bargraph.py @@ -0,0 +1,55 @@ +#!/usr/bin/python + +import time +import datetime +from Adafruit_LEDBackpack import LEDBackpack + +# =========================================================================== +# Bargraph Display +# =========================================================================== + +# This class is meant to be used with the 24-LED bicolor bargraph +# displays available from Adafruit + +class Bargraph: + disp = None + + LED_OFF = 0 + LED_RED = 1 + LED_GREEN = 2 + LED_YELLOW = 3 + + # Constructor + def __init__(self, address=0x70, debug=False): + self.debug = debug + + if self.debug: + print "Initializing a new instance of LEDBackpack at 0x%02X" % address + self.disp = LEDBackpack(address=address, debug=debug) + + def setLed(self, bar, color): + if bar > 24: + return + if color > 3: + return + + if bar < 12: + c = bar / 4 + else: + c = (bar - 12) / 4 + + a = bar % 4; + if bar >= 12: + a += 4; + + if self.debug: + print "Ano = %d Cath %d" % (a, c) + + bufRow = self.disp.getBufferRow(c) & ~((1 << a) | (1 << (a+8))) # turn off the LED + + if color == self.LED_RED: + self.disp.setBufferRow(c, bufRow | (1 << a)) + elif color == self.LED_YELLOW: + self.disp.setBufferRow(c, bufRow | (1 << a) | (1 << (a+8))) + elif color == self.LED_GREEN: + self.disp.setBufferRow(c, bufRow | 1 << (a+8)) diff --git a/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py b/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py index a0c58295..b2a20065 100644 --- a/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py +++ b/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py @@ -64,6 +64,12 @@ def setBufferRow(self, row, value, update=True): if (update): self.writeDisplay() # Update the display + def getBufferRow(self, row): + "Returns a single 16-bit entry in the 8*16-bit buffer" + if (row > 7): + return + return self.__buffer[row] + def getBuffer(self): "Returns a copy of the raw buffer contents" bufferCopy = copy(self.__buffer) diff --git a/Adafruit_LEDBackpack/ex_7segment_clock.py b/Adafruit_LEDBackpack/ex_7segment_clock.py index 634b6178..99f00ab1 100644 --- a/Adafruit_LEDBackpack/ex_7segment_clock.py +++ b/Adafruit_LEDBackpack/ex_7segment_clock.py @@ -23,7 +23,7 @@ # Set minutes segment.writeDigit(3, int(minute / 10)) # Tens segment.writeDigit(4, minute % 10) # Ones - # Toggle color + # Toggle colon segment.setColon(second % 2) # Toggle colon at 1Hz # Wait one second time.sleep(1) diff --git a/Adafruit_LEDBackpack/ex_bargraph.py b/Adafruit_LEDBackpack/ex_bargraph.py new file mode 100644 index 00000000..1fa0a31e --- /dev/null +++ b/Adafruit_LEDBackpack/ex_bargraph.py @@ -0,0 +1,19 @@ +#!/usr/bin/python + +import time +import datetime +from Adafruit_Bargraph import Bargraph + +# =========================================================================== +# Scroll through colors example +# =========================================================================== +bargraph = Bargraph(address=0x70) + +print "Press CTRL+C to exit" + +while(True): + for color in range(1, 4): + for i in range(24): + print i + bargraph.setLed(i, color) + time.sleep(0.05) diff --git a/Adafruit_MCP230xx/Adafruit_MCP230xx.py b/Adafruit_MCP230xx/Adafruit_MCP230xx.py index b7ad9877..dc1b9c27 100755 --- a/Adafruit_MCP230xx/Adafruit_MCP230xx.py +++ b/Adafruit_MCP230xx/Adafruit_MCP230xx.py @@ -40,9 +40,9 @@ class Adafruit_MCP230XX(object): OUTPUT = 0 INPUT = 1 - def __init__(self, address, num_gpios): + def __init__(self, address, num_gpios, busnum=-1): assert num_gpios >= 0 and num_gpios <= 16, "Number of GPIOs must be between 0 and 16" - self.i2c = Adafruit_I2C(address=address) + self.i2c = Adafruit_I2C(address=address, busnum=busnum) self.address = address self.num_gpios = num_gpios @@ -162,7 +162,7 @@ class MCP230XX_GPIO(object): BCM = 0 BOARD = 0 def __init__(self, busnum, address, num_gpios): - self.chip = Adafruit_MCP230XX(busnum, address, num_gpios) + self.chip = Adafruit_MCP230XX(address, num_gpios, busnum) def setmode(self, mode): # do nothing pass diff --git a/Adafruit_MCP3008/mcp3008.py b/Adafruit_MCP3008/mcp3008.py index a1678a7b..73dcd7dd 100644 --- a/Adafruit_MCP3008/mcp3008.py +++ b/Adafruit_MCP3008/mcp3008.py @@ -68,27 +68,35 @@ def readadc(adcnum, clockpin, mosipin, misopin, cspin): adcout /= 2 # first bit is 'null' so drop it return adcout -# change these as desired -SPICLK = 18 -SPIMOSI = 17 -SPIMISO = 21 -SPICS = 22 - -# set up the SPI interface pins -GPIO.setup(SPIMOSI, GPIO.OUT) -GPIO.setup(SPIMISO, GPIO.IN) -GPIO.setup(SPICLK, GPIO.OUT) -GPIO.setup(SPICS, GPIO.OUT) - -# Note that bitbanging SPI is incredibly slow on the Pi as its not -# a RTOS - reading the ADC takes about 30 ms (~30 samples per second) -# which is awful for a microcontroller but better-than-nothing for Linux - -print "| #0 \t #1 \t #2 \t #3 \t #4 \t #5 \t #6 \t #7\t|" -print "-----------------------------------------------------------------" -while True: - print "|", - for adcnum in range(8): - ret = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS) - print ret,"\t", - print "|" +if __name__=='__main__': + + try: + # change these as desired + SPICLK = 18 + SPIMISO = 21 + SPIMOSI = 17 + SPICS = 22 + + # set up the SPI interface pins + GPIO.setup(SPICLK, GPIO.OUT) + GPIO.setup(SPIMISO, GPIO.IN) + GPIO.setup(SPIMOSI, GPIO.OUT) + GPIO.setup(SPICS, GPIO.OUT) + + # Note that bitbanging SPI is incredibly slow on the Pi as its not + # a RTOS - reading the ADC takes about 30 ms (~30 samples per second) + # which is awful for a microcontroller but better-than-nothing for Linux + + print "| #0 \t #1 \t #2 \t #3 \t #4 \t #5 \t #6 \t #7\t|" + print "-----------------------------------------------------------------" + while True: + print "|", + for adcnum in range(8): + ret = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS) + print ret,"\t", + print "|" + + except KeyboardInterrupt: + pass + + GPIO.cleanup() diff --git a/Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py b/Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py index 76ab5137..35c993ca 100644 --- a/Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py +++ b/Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py @@ -9,30 +9,52 @@ # ============================================================================ class PWM : - i2c = None - # Registers/etc. + __MODE1 = 0x00 + __MODE2 = 0x01 __SUBADR1 = 0x02 __SUBADR2 = 0x03 __SUBADR3 = 0x04 - __MODE1 = 0x00 __PRESCALE = 0xFE __LED0_ON_L = 0x06 __LED0_ON_H = 0x07 __LED0_OFF_L = 0x08 __LED0_OFF_H = 0x09 - __ALLLED_ON_L = 0xFA - __ALLLED_ON_H = 0xFB - __ALLLED_OFF_L = 0xFC - __ALLLED_OFF_H = 0xFD + __ALL_LED_ON_L = 0xFA + __ALL_LED_ON_H = 0xFB + __ALL_LED_OFF_L = 0xFC + __ALL_LED_OFF_H = 0xFD + + # Bits + __RESTART = 0x80 + __SLEEP = 0x10 + __ALLCALL = 0x01 + __INVRT = 0x10 + __OUTDRV = 0x04 + + general_call_i2c = Adafruit_I2C(0x00) + + @classmethod + def softwareReset(cls): + "Sends a software reset (SWRST) command to all the servo drivers on the bus" + cls.general_call_i2c.writeRaw8(0x06) # SWRST def __init__(self, address=0x40, debug=False): self.i2c = Adafruit_I2C(address) + self.i2c.debug = debug self.address = address self.debug = debug if (self.debug): - print "Reseting PCA9685" - self.i2c.write8(self.__MODE1, 0x00) + print "Reseting PCA9685 MODE1 (without SLEEP) and MODE2" + self.setAllPWM(0, 0) + self.i2c.write8(self.__MODE2, self.__OUTDRV) + self.i2c.write8(self.__MODE1, self.__ALLCALL) + time.sleep(0.005) # wait for oscillator + + mode1 = self.i2c.readU8(self.__MODE1) + mode1 = mode1 & ~self.__SLEEP # wake up (reset sleep) + self.i2c.write8(self.__MODE1, mode1) + time.sleep(0.005) # wait for oscillator def setPWMFreq(self, freq): "Sets the PWM frequency" @@ -62,6 +84,9 @@ def setPWM(self, channel, on, off): self.i2c.write8(self.__LED0_OFF_L+4*channel, off & 0xFF) self.i2c.write8(self.__LED0_OFF_H+4*channel, off >> 8) - - - + def setAllPWM(self, on, off): + "Sets a all PWM channels" + self.i2c.write8(self.__ALL_LED_ON_L, on & 0xFF) + self.i2c.write8(self.__ALL_LED_ON_H, on >> 8) + self.i2c.write8(self.__ALL_LED_OFF_L, off & 0xFF) + self.i2c.write8(self.__ALL_LED_OFF_H, off >> 8) diff --git a/Adafruit_VCNL4000/Adafruit_VCNL4000.py b/Adafruit_VCNL4000/Adafruit_VCNL4000.py index 4ebc3a20..4d307536 100755 --- a/Adafruit_VCNL4000/Adafruit_VCNL4000.py +++ b/Adafruit_VCNL4000/Adafruit_VCNL4000.py @@ -51,4 +51,11 @@ def read_proximity(self): return self.i2c.readU16(VCNL4000_PROXIMITYDATA) time.sleep(0.001) - + # Read data from ambient sensor + def read_ambient(self): + self.i2c.write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT) + while True: + result = self.i2c.readU8(VCNL4000_COMMAND) + if (result and VCNL4000_AMBIENTREADY): + return self.i2c.readU16(VCNL4000_AMBIENTDATA) + time.sleep(0.001) diff --git a/README.md b/README.md index aba2ae53..8f342a3a 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,11 @@ Adafruit's Raspberry-Pi Python Code Library BSD license, all text above and below must be included in any redistribution To download, we suggest logging into your Pi with Internet accessibility and typing: - git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git +```bash +git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git +``` + ============ Copyright (c) 2012-2013 Limor Fried, Kevin Townsend and Mikey Sklar for Adafruit Industries. All rights reserved.