-
Notifications
You must be signed in to change notification settings - Fork 679
Improvements and Bug Fixes to CharLCDPlate.py and MCP230xx.py #27
Conversation
… tidied this up. Unfortunately this is hard for diff to handle, so the changes appear as big blocks of code being removed and other blocks being added. CharLCDPlate.py Added more routines - ToggleCursor(), blink(), ToggleBlink(), read4bits(), readBF() In particular implemented Reading BF and Address and ReadingRAM Implemented waitBFlow() which can replace a time delay Tidied up comments for many of the routines, as they did not accurately reflect what they do. MCP230xx.py Fixed some bugs config(self, pin, mode) did not maintain a 16bit record of the I/O state of of the pins. input(self, pin, check=True) fixed to return either 0 or 1 rather then 0 or various non-zero values added parameter which defaults to previous rountine, but allows asserstion to be skipped - to enable experimenting reading 'output' pins Removed orphaned code after return in output(self, pin, value) All the low level rountines readU8, readS8 etc. where indented so they were sub function of input and no used there, and inaccessible in the class Moved indentation and modified may to use the i2c routines more efficiently.
Hi bbkiwi, Hope it helps, Andrew |
Hi Andrew, Thanks for the tip. I'm quite new to python, but have lots of experience with other languages. This all started with a Xmas present of a raspberry pi and a LCD display. Keeping me busy. I think the indentation muddle comes about especially on code where various people are editing it. On the WebIDE supplied by Adafruit there is no way to 'see' tabs or spaces. I'll check out pep8. |
so indent = 4 spaces
Hi, I have a problem using MCP230XX library. I configure a pin to INPUT and when I read this value I receive error message that the pin is not in input mode. The error message is print by line 118 in MCP230xx library. This problem is present with GPB0-7 pin. It's ok with GPA0-7 pin. I changed this line to assert self.direction & (1 << pin) != 1, "Pin %s not set to input" % pin and it works now. I don't known the effect of this change |
Hi, As I mention above, I found a number of bugs in MCP230xx.py. In your case the config rountine is the cause of your problem. As supplied by adafruit, config is changing the pin properly to input or output on the chip, but it is not keeping track in the variable direction. direction should keep 16bits corresponding to both GPA and GPB (1 for input and 0 for output), but it changes the low order 8 bits for both GPA and GPB, thus overwriting the GPA information and storing the GPB information in the wrong bits. In short - the bits of direction do not accurately reflect the I/O state of the pins. Thus when you try and read from GPB the input routine does not see that the pins have been set as input. I only play with electronics so I'm no expert, but from my reading of the specs on the chips, if you attempt to read an output pin you just get the wrong answer, so preventing reading of output pins can cause no damage. I'd recommend leaving input as it stands and correctiing the code for config which my push corrects. Here is a version of config that keeps track of the I/O state correctly: def config(self, pin, mode):
if self.num_gpios <= 8:
self.direction = self._readandchangepin(MCP23017_IODIRA, pin, mode)
if self.num_gpios <= 16:
if (pin < 8):
# replace low bits
self.direction = (self.direction >> 8)<<8 | self._readandchangepin(MCP23017_IODIRA, pin, mode)
else:
# replace hi bits
self.direction = (self.direction & 0xff) | self._readandchangepin(MCP23017_IODIRB, pin-8, mode) << 8
#print "config ", pin, mode, self.direction, bin(self.direction)[2:].zfill(16)
return self.direction |
Thanks BBkiwi, I use your MCP230XX library now and all works fine |
Glad to help. I'm hoping my pull request will be looked at and my changes |
nice work. really embarrassed that i haven't spotted that bit in config, but please...
|
Hi mtbf0, Have fixed that, don't know why I coded it in such a silly way. Are you the original author? |
m3m0ry, we want to keep the tone positive here - please edit and re-post your comment. Thanks! |
ladyada, I think adafruit is doing a great job. I've enjoyed learning about the LCD Plate and LED strips - even though I've blown up one Pi :-) , and it's nice to be able to be part of the community to develop improvements. Cheers from New Zealand! |
Thanks! We have plans to speed up this library considerably - still working on getting it perfect before committing :) |
I'm sorry i didn't want to be rude. I did not choose my words well, what i meant was that adafruit does merge the other forkes, but a little bit slow. Which i completely understand, because it is important to test and read what the others have written. I didn't want to be critical or rude to adafruit. So sorry for that. I wrote that i also have a repo, which i "forked" from bbkiwi, but because you apparently cannot fork a fork on github (or with git???) i created my "own" repo. I will merge it with bbkiwi's fork. My goal is to write some useful functions (like the status of the CPU, or memory, or whatever in my opinion could be useful for anybody), related to the Char_LCDPlate. |
Thanks for understanding! We like to make sure we always have completely tested and working code in our repositories to avoid problems with the many customers who are following our tutorials. |
Can't automatically merge this request due to the extent of the change, so I'll be making the changes 'manually' and will mention your contribution in the commit comments. Thanks! |
Hi PaintYourDragon,
|
Note - this pull request superceeds those I made about 11 days ago.
Many of the routines in Adafruit-Raspberry-Pi-Python-Code have been
indented using a mixture of spacing and tab. This tends to confuse WebIDE. I have tried to tidy this up.
Some functions had comments that did not accurately reflect what
they do. I've fixed these.
CharLCDPlate.py
Added more routines - ToggleCursor(), blink(), ToggleBlink(),
read4bits(), readBF(), waitBFlow()
these later 3 being able to read information from the plate.
waitBFlow() waits till the 'busy flag' is low which means the LCD is ready for more intstructions so can replace a time delay
MCP230xx.py
Fixed some bugs
config(self, pin, mode) did not maintain a 16bit record of the
I/O state of of the pins.
input(self, pin, check=True)
fixed to return either 0 or 1 rather then 0 or various non-zero values
added parameter which defaults to previous rountine, but allows assertion to be skipped - to enable experimental reading of 'output' pins
Removed orphaned code after return in output(self, pin, value)
All the low level rountines readU8, readS8 etc. where indented so they were sub functions of input but not used there, and inaccessible in the class. Moved indentation and modified many to use the i2c routines more efficiently.
Unfortunately because I have replaced spaces with tabs the diff view shows my changes appearing as big blocks of code being removed and other blocks being added, when in fact only a small number of lines have
been changed.
Note I have now removed all tabs so all indenting is done via the standard 4 spaces.
commit 9d03735 compares a tab free version of my suggested changes on Adafruit_CharLCDPlate.py to a tab free version of the original code.
commit e0ab25d does the same for my suggested changes on Adafruit_MCP230xx.py