-
Notifications
You must be signed in to change notification settings - Fork 10
MCP9600 driver for CircuitPython #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
adafruit_mcp9600.py
Outdated
value = data[0]*16.0 + data[1]/16.0 - 4096 | ||
else: | ||
# positive temperature | ||
value = data[0]*16.0 + data[1]/16.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here for using struct.unpack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I simplified the return of _read_register() to return the entire array. Do you still think unpack is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest switching to using unpack. No need to re-invent the wheel, and it's a bit more succinct. Personally I trust it more than I trust my own ability to get the math right every time.
Your call, but I think it has its merits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the code below uses the same algorithm as the Arduino MCP9600 driver, using CP and unpack. Is this any better? The results are the same.
[(https://github.com/adafruit/Adafruit_MCP9600/blob/master/Adafruit_MCP9600.cpp)]
data = self._read_register(_REGISTER_DELTA_TEMP, 2)
d = unpack(">xH",data)
if d[0] & 0x80:
# negative temperature
value = d[0]*0.0625 - 4096
else:
# positive temperature
value = d[0]*0.0625
return value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, does CP care about big-endian and little-endian integers or does it abstract it away? I had to use big-endian for the unpack on the PyPortal, don't know if other CP boards will be different. The original algorithm does not care.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So thinking about this the return data will always be big-endian since the data is always big-endian when returned from read_register(). So the original question still stands, which algorithm is better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using struct.unpack
is better. It's not available in Arduino, so that driver does it manually.
Your _read_register
puts the actual read results in buf[1]
and buf[2]
. So you should be able to do something like:
data = self._read_register(_REGISTER_HOT_JUNCTION, 2)
temperature = 0.0625 * struct.unpack(">h", data[1:])[0]
Note how you can indicate endian-ness to struct.unpack
with <
or >
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sooo close! i will test this :)
tested with hardware -> MCP9600 + itsybitsy! i think this is good to merge and we can add more later |
FYI, here is my code for the MCP9600. You can test when the Adafruit boards are in stock, I have it working with another MCP9600 board. It currently does not support alerts, it only reads temperatures.