You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've just done a quick clone/convert for a backend for the MCP23017 GPI expander chip, use it as you will. It's not been tested or checked in any way yet as I'm not near my pi - just came across your code and liked it, so will be using it for my project, plus my little addition!
Notes: Needs some code to read and set individual pins as input/output instead of all at once, and there is unfortunately no PWM available AFAIK for the chip.
classMCP23017Backend:
# For the 16 pin MCP23017 (can also be used for the 8 pin version, but need to check for lack of pins# useful quick reference for the pin definitions that you can use in the pinmapa= [(0x12, 0), (0x12, 1), (0x12, 2), (0x12, 3), (0x12, 4), (0x12, 5), (0x12, 6), (0x12, 7)]
b= [(0x13, 0), (0x13, 1), (0x13, 2), (0x13, 3), (0x13, 4), (0x13, 5), (0x13, 6), (0x13, 7)]
def__init__(self, display, pinmap, bus_id, address): # bus_id is the i2c interface, address is 0x20/21/22 etc. (hex address of i2c address )self.display=displaytry:
importsmbusfromtimeimportsleepself.bus=smbus.SMBus(bus_id)
except:
raiseIOError("Could not export the MCP23017. Make sure that you have the smbus library installed, run as root and are on a Raspberry Pi.")
self.address=address.
self.reverse_pinmap=dict([(value, key) forkey, valueinpinmap.iteritems()])
self.bus.write_byte_data(self.address, 0x00, 0) # 1 is for inputself.bus.write_byte_data(self.address, 0x01, 0) # 1 is for inputdefhigh(self, output):
value=self.bus.read_byte_data(self.address, register)
value|= (1<<pin)
self.bus.write_byte_data(self.address, register, value)
deflow(self, output):
value=self.bus.read_byte_data(self.address, register)
value&=~(1<<pin)
self.bus.write_byte_data(self.address, register, value)
defpulse(self, output):
self.high(output)
time.sleep(0.001)
self.low(output)
defall_low(self):
foroutputinself.reverse_pinmap.keys():
self.low(output)
defwrite_nibble(self, nibble, data=True):
self.write_bit(self.PIN_RS, data)
self.write_bit(self.PIN_D4, nibble[3])
self.write_bit(self.PIN_D5, nibble[2])
self.write_bit(self.PIN_D6, nibble[1])
self.write_bit(self.PIN_D7, nibble[0])
defwrite_bit(self, pin, bit):
if (bit):
self.high(pin)
else:
self.low(pin)
defwrite_byte(self, byte, data=True):
self.write_bit(self.PIN_RS, data)
foriinrange(8):
self.write_bit(getattr(self, "PIN_D%i"%i), byte[i])
defset_brightness(self, level):
assertlevel>=0assertlevel<=1023self.display.brightness=levelself.write_bit(self.PIN_LED, level>0)
The text was updated successfully, but these errors were encountered:
Hi,
I've just done a quick clone/convert for a backend for the MCP23017 GPI expander chip, use it as you will. It's not been tested or checked in any way yet as I'm not near my pi - just came across your code and liked it, so will be using it for my project, plus my little addition!
Notes: Needs some code to read and set individual pins as input/output instead of all at once, and there is unfortunately no PWM available AFAIK for the chip.
The text was updated successfully, but these errors were encountered: