Skip to content

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

Merged
merged 24 commits into from
Oct 13, 2019
Merged

Conversation

cogliano
Copy link
Contributor

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.

@caternuson caternuson requested a review from a team October 10, 2019 16:34
value = data[0]*16.0 + data[1]/16.0 - 4096
else:
# positive temperature
value = data[0]*16.0 + data[1]/16.0
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor

@caternuson caternuson Oct 11, 2019

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 >.

@cogliano cogliano requested a review from ladyada October 10, 2019 20:06
Copy link
Member

@ladyada ladyada left a 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 :)

@cogliano cogliano requested a review from ladyada October 11, 2019 00:11
@cogliano cogliano requested a review from caternuson October 11, 2019 01:05
@cogliano cogliano requested a review from siddacious October 11, 2019 06:17
@ladyada
Copy link
Member

ladyada commented Oct 13, 2019

tested with hardware -> MCP9600 + itsybitsy! i think this is good to merge and we can add more later

@ladyada ladyada merged commit 7c1090a into adafruit:master Oct 13, 2019
@ladyada
Copy link
Member

ladyada commented Oct 13, 2019

great work @cogliano
@kattni please get this into the bundle at your convenience - you can add your extensive code to it now that the sensor is working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants