Skip to content

Commit

Permalink
Initial commit with temperature support.
Browse files Browse the repository at this point in the history
  • Loading branch information
tannewt committed Jan 19, 2017
0 parents commit 9db19a4
Show file tree
Hide file tree
Showing 9 changed files with 596 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
_build
*.pyc
53 changes: 53 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Travis CI configuration for automated .mpy file generation.
# Author: Tony DiCola
# License: Public Domain
# This configuration will work with Travis CI (travis-ci.org) to automacially
# build .mpy files for MicroPython when a new tagged release is created. This
# file is relatively generic and can be shared across multiple repositories by
# following these steps:
# 1. Copy this file into a .travis.yml file in the root of the repository.
# 2. Change the deploy > file section below to list each of the .mpy files
# that should be generated. The config will automatically look for
# .py files with the same name as the source for generating the .mpy files.
# Note that the .mpy extension should be lower case!
# 3. Commit the .travis.yml file and push it to GitHub.
# 4. Go to travis-ci.org and find the repository (it needs to be setup to access
# your github account, and your github account needs access to write to the
# repo). Flip the 'ON' switch on for Travis and the repo, see the Travis
# docs for more details: https://docs.travis-ci.com/user/getting-started/
# 5. Get a GitHub 'personal access token' which has at least 'public_repo' or
# 'repo' scope: https://help.github.com/articles/creating-an-access-token-for-command-line-use/
# Keep this token safe and secure! Anyone with the token will be able to
# access and write to your GitHub repositories. Travis will use the token
# to attach the .mpy files to the release.
# 6. In the Travis CI settings for the repository that was enabled find the
# environment variable editing page: https://docs.travis-ci.com/user/environment-variables/#Defining-Variables-in-Repository-Settings
# Add an environment variable named GITHUB_TOKEN and set it to the value
# of the GitHub personal access token above. Keep 'Display value in build
# log' flipped off.
# 7. That's it! Tag a release and Travis should go to work to add .mpy files
# to the release. It takes about a 2-3 minutes for a worker to spin up,
# build mpy-cross, and add the binaries to the release.
language: generic

sudo: true

deploy:
provider: releases
api_key: $GITHUB_TOKEN
file:
- "adafruit_mcp9808.mpy"
skip_cleanup: true
on:
tags: true

before_install:
- sudo apt-get -yqq update
- sudo apt-get install -y build-essential git python python-pip
- git clone https://github.com/adafruit/circuitpython.git
- make -C circuitpython/mpy-cross
- export PATH=$PATH:$PWD/circuitpython/mpy-cross/
- sudo pip install shyaml

before_deploy:
- shyaml get-values deploy.file < .travis.yml | sed 's/.mpy/.py/' | xargs -L1 mpy-cross
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Scott Shawcroft, written for Adafruit Industries

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 52 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

Introduction
============

The MCP9808 is an awesome, high accuracy temperature sensor that communicates
over I2C. Its available on `Adafruit as a breakout <https://www.adafruit.com/products/1782>`_.

Dependencies
=============

This driver depends on the `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
library. Please ensure its also available on the CircuitPython filesystem.
This is easily achieved by downloading
`a library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.

Usage Notes
===========

Getting the temperature in celsius is easy! First, import all of the pins from
the board, nativeio for native I2C communication and the thermometer library
itself.

.. code-block:: python
from board import *
import nativeio
import adafruit_mcp9808
Next, initializd the I2C bus in a with statement so it always gets shut down ok.
Then, construct the thermometer class:

.. code-block:: python
# Do one reading
with nativeio.I2C(SCL, SDA) as i2c:
t = adafruit_mcp9808.MCP9808(i2c)
Finally, read the temperature property.

.. code-block:: python
# and print it out
print(t.temperature)
API Reference
=============

.. toctree::
:maxdepth: 2

api
105 changes: 105 additions & 0 deletions adafruit_mcp9808.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# The MIT License (MIT)
#
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_mcp9808` - MCP9808 I2C Temperature Sensor
====================================================
CircuitPython library to support MCP9808 high accuracy temperature sensor.
* Author(s): Scott Shawcroft
Implementation Notes
--------------------
**Hardware:**
* Adafruit `MCP9808 High Accuracy I2C Temperature Sensor Breakout <https://www.adafruit.com/products/1782>`_ (Product ID: 1782)
**Software and Dependencies:**
* Adafruit CircuitPython firmware (0.8.0+) for the ESP8622 and M0-based boards: https://github.com/adafruit/circuitpython/releases
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
**Notes:**
#. Datasheet: http://www.adafruit.com/datasheets/MCP9808.pdf
"""

from adafruit_bus_device.i2c_device import I2CDevice

# Resolution settings
HALF_C = 0x0
QUARTER_C = 0x1
EIGHTH_C = 0x2
SIXTEENTH_C = 0x3

class MCP9808:
"""Interface to the MCP9808 temperature sensor."""

# alert_lower_temperature_bound
# alert_upper_temperature_bound
# critical_temperature
# temperature
# temperature_resolution

def __init__(self, i2c, device_address=0b0011000):
self.i2c_device = I2CDevice(i2c, device_address)

# Verify the manufacturer and device ids to ensure we are talking to
# what we expect.
self.buf = bytearray(3)
self.buf[0] = 0x06
with self.i2c_device as i2c:
i2c.writeto(self.buf, end=1, stop=False)
i2c.readfrom_into(self.buf, start=1)

ok = self.buf[2] == 0x54 and self.buf[1] == 0

# Check device id.
self.buf[0] = 0x07
with self.i2c_device as i2c:
i2c.writeto(self.buf, end=1, stop=False)
i2c.readfrom_into(self.buf, start=1)

if not ok or self.buf[1] != 0x04:
raise ValueError("Unable to find MCP9808 at i2c address " + str(hex(device_address)))

@property
def temperature(self):
"""Temperature in celsius. Read-only."""
self.buf[0] = 0x05
with self.i2c_device as i2c:
i2c.writeto(self.buf, end=1, stop=False)
i2c.readfrom_into(self.buf, start=1)

# Clear flags from the value
self.buf[1] = self.buf[1] & 0x1f
if self.buf[1] & 0x10 == 0x10:
self.buf[1] = self.buf[1] & 0x0f
return 256 - (self.buf[1] * 16 + self.buf[2] / 16.0)
else:
return self.buf[1] * 16 + self.buf[2] / 16.0

@temperature.setter
def temperature(self, value):
raise AttributeError()
3 changes: 3 additions & 0 deletions api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

.. automodule:: adafruit_mcp9808
:members:
Loading

0 comments on commit 9db19a4

Please sign in to comment.