ShrimpingIt / micropython-mcp230xx

Micropython I2C-based manipulation of the MCP series GPIO expander, derived from Adafruit_MCP230xx
GNU Affero General Public License v3.0
30 stars 16 forks source link

TypeError on writing to pin after reading another #2

Closed nelsonroliveira closed 3 years ago

nelsonroliveira commented 6 years ago

Upon reading a pin it becomes impossible to write to another one

Minimal implementation:

import mcp

mcp1 = mcp.MCP23017(address=0x20, gpioScl=22, gpioSda=21)
mcp1.setup(0, mcp.OUT)
mcp1.setup(5, mcp.IN)
mcp1.pullup(5, True)

mcp1.output(0, 0)
print(mcp1.input(5))
mcp1.output(0, 0)

Returns

False
Traceback (most recent call last):
  File "boot.py", line 9, in <module>
  File "mcp.py", line 92, in output
  File "mcp.py", line 105, in output_pins
TypeError: 'bytes' object does not support item assignment

Fix appears to be simple, just changing line 71 to return a bytearray to keep self.gpio in the original type set on the __init__()

def readList(self, register, length):
        """Introduced to match the readList implementation of the Adafruit I2C _device member"""
        return bytearray(self.i2c.readfrom_mem(self.address, register, length))
cefn commented 6 years ago

Olá and thanks for the feedback Nelson!

Would you like to try raising your first PR to fix this? There's some info here, which should guide you through it if you feel comfortable. You can't get it wrong as I'll get a chance to review the change before it is merged into the repository. If you don't feel comfortable I'll look at making the change myself.

I've never used it for both input and output in any real project. I was mainly using it for coin-detection using TCRT5000 Photo-interrupters as inputs https://forum.micropython.org/viewtopic.php?t=4347 and the outputs came for free with the library. I've been thinking of bringing this library back to life to build demo projects with ESP01 boards manipulating arrays of GPIOs, but it's just as likely I'll opt for using Arduino Pro Mini clones as IO boards through Firmata, especially since I want to replicate the @ShrimpingIt Simon project and it needs PWM for the beeps.

Intrigued to know what you are using the library for if you have the time to comment.

Please also note there is a new way to get the latest Adafruit-maintained MCP230xx support from Adafruit's @tdicola.

The Adafruit-Blinka repository has a set of modules which you should be able to drop into a Micropython board (tested on ESP8266) to provide a compatibility layer between Micropython and Circuitpython.

Once that is installed, it should be possible to use the mainstream CircuitPython library maintained by @tdicola directly at... https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx ...to get you the latest and greatest support from Adafruit.

In fact, you should be able to use any I2C or UART circuitpython library with Adafruit_Blinka, although YMMV since it's fairly new and minimally tested code. SPI support isn't tested yet.

Alternatively if you are running a suitable platform, then you should be able to flash the latest stable version of Circuitpython directly to your board (eliminating the need for the Micropython->Circuitpython compatibility layer).

nelsonroliveira commented 6 years ago

PR is live.

Currently studying the ESP32, so as a pet project, I am currently trying to build a TCP ModBus IO expansion module. It's my first wired ethernet project and the ESP32 is so far making it easy on that end while allowing the use of wifi for easy user configuration (user presses button and a wifi ap is temporarily enabled to allow for a simple web page config).

Test bed is a Olimex ESP32-EVB so it does not have an MCP23017, discovered the bug while testing the breadboard connections, if I already had the final boards it would have gone undetected as it will be using separate ICs for inputs and outputs.

So far it's been great, all it's missing now is a modbus slave library and some time with kicad

Unfortunately CircuitPython seems to not be support the ESP32.