From bebe48d7d990f0de493294ec6418872e2408695d Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Mon, 8 Nov 2021 20:21:36 +0100 Subject: [PATCH] 0.1.4 MCP9808_RT --- libraries/MCP9808_RT/.arduino-ci.yml | 10 +- .../.github/workflows/arduino_test_runner.yml | 10 +- libraries/MCP9808_RT/README.md | 151 ++++++++++-------- .../mcp9808_minimum/mcp9808_minimum.ino | 5 + .../mcp9808_offset/mcp9808_offset.ino | 5 + .../mcp9808_performance.ino | 5 + .../mcp9808_setResolution.ino | 5 + .../examples/mcp9808_test/mcp9808_test.ino | 14 +- .../mcp9808_test_alert/mcp9808_test_alert.ino | 5 + .../mcp9808_test_alert_irq.ino | 6 + .../mcp9808_test_alert_polling.ino | 2 + libraries/MCP9808_RT/keywords.txt | 11 +- libraries/MCP9808_RT/library.json | 2 +- libraries/MCP9808_RT/library.properties | 2 +- libraries/MCP9808_RT/mcp9808.cpp | 55 +++++-- libraries/MCP9808_RT/mcp9808.h | 47 +++--- libraries/MCP9808_RT/test/unit_test_001.cpp | 5 +- 17 files changed, 232 insertions(+), 108 deletions(-) diff --git a/libraries/MCP9808_RT/.arduino-ci.yml b/libraries/MCP9808_RT/.arduino-ci.yml index ff5659b9..cecf5850 100644 --- a/libraries/MCP9808_RT/.arduino-ci.yml +++ b/libraries/MCP9808_RT/.arduino-ci.yml @@ -2,6 +2,10 @@ compile: # Choosing to run compilation tests on 2 different Arduino platforms platforms: - uno - - leonardo - - due - - zero + # - due + # - zero + # - leonardo + - m4 + - esp32 + # - esp8266 + # - mega2560 diff --git a/libraries/MCP9808_RT/.github/workflows/arduino_test_runner.yml b/libraries/MCP9808_RT/.github/workflows/arduino_test_runner.yml index 476456bb..096b975b 100644 --- a/libraries/MCP9808_RT/.github/workflows/arduino_test_runner.yml +++ b/libraries/MCP9808_RT/.github/workflows/arduino_test_runner.yml @@ -4,10 +4,14 @@ name: Arduino CI on: [push, pull_request] jobs: - arduino_ci: + runTest: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: Arduino-CI/action@master - # Arduino-CI/action@v0.1.1 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.6 + - run: | + gem install arduino_ci + arduino_ci.rb diff --git a/libraries/MCP9808_RT/README.md b/libraries/MCP9808_RT/README.md index 5d299a08..6af97975 100644 --- a/libraries/MCP9808_RT/README.md +++ b/libraries/MCP9808_RT/README.md @@ -1,16 +1,20 @@ [![Arduino CI](https://github.com/RobTillaart/MCP9808_RT/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![Arduino-lint](https://github.com/RobTillaart/MCP9808_RT/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MCP9808_RT/actions/workflows/arduino-lint.yml) +[![Arduino-lint](https://github.com/RobTillaart/MCP9808_RT/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MCP9808_RT/actions/workflows/arduino-lint.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MCP9808_RT/blob/master/LICENSE) [![GitHub release](https://img.shields.io/github/release/RobTillaart/MCP9808_RT.svg?maxAge=3600)](https://github.com/RobTillaart/MCP9808_RT/releases) + # MCP9808_RT -Arduino library for I2C MCP9808 temperature sensor +Arduino library for I2C MCP9808 temperature sensor. + ## Description -The MCP9808 is a temperature sensor that measures typically in 1/16th == 0.0625 of a degree celsius. -What makes this sensor interesting is the ALERT pin, which allows triggering of any piece +The MCP9808 is a temperature sensor that measures typically in 1/16th == 0.0625 ° Celsius. +What makes this sensor interesting is the **ALERT** pin, which allows triggering of any piece of electronics if the temperature hits a predefined value or zone. @@ -38,24 +42,27 @@ of electronics if the temperature hits a predefined value or zone. ### Constructor -- **MCP9808(const uint8_t addr)** constructor for e.g. UNO -- **MCP9808(const uint8_t addr, const uint8_t dataPin = 255, const uint8_t clockPin = 255)** constructor for ESP32 and ESP8266 -- **setAddress(const uint8_t address, TwoWire \*wire)** if multiple I2C busses are present one can choose. +- **MCP9808(const uint8_t address)** constructor for e.g. UNO. +- **MCP9808(const uint8_t address, const uint8_t dataPin = 255, const uint8_t clockPin = 255)** constructor for ESP32 and ESP8266. +- **bool setAddress(const uint8_t address, TwoWire \*wire = &Wire)** if multiple I2C buses are present one can choose. +Default I2C bus is Wire. -**Address** -max 8 sensors on one I2C bus + +#### Address + +There ar max 8 sensors on one I2C bus. Normal address = 0011xxx where xxx = A2, A1, A0 -| Address | HEX | A2 | A1 | A0 | -|:----:|:----:|:----:|:----:|:----:| -| 24 | 0x18 | 0 | 0 | 0 | -| 25 | 0x19 | 0 | 0 | 1 | -| 26 | 0x1A | 0 | 1 | 0 | -| 27 | 0x1B | 0 | 1 | 1 | -| 28 | 0x1C | 1 | 0 | 0 | -| 29 | 0x1D | 1 | 0 | 1 | -| 30 | 0x1E | 1 | 1 | 0 | -| 31 | 0x1F | 1 | 1 | 1 | +| Address | HEX | A2 | A1 | A0 | +|:-------:|:------:|:----:|:----:|:----:| +| 24 | 0x18 | 0 | 0 | 0 | +| 25 | 0x19 | 0 | 0 | 1 | +| 26 | 0x1A | 0 | 1 | 0 | +| 27 | 0x1B | 0 | 1 | 1 | +| 28 | 0x1C | 1 | 0 | 0 | +| 29 | 0x1D | 1 | 0 | 1 | +| 30 | 0x1E | 1 | 1 | 0 | +| 31 | 0x1F | 1 | 1 | 1 | On request manufacturer will provide 1001xxx as base address allowing up to 16 temp sensors on one bus. @@ -63,80 +70,87 @@ allowing up to 16 temp sensors on one bus. ### Temperature and status -- **setOffset(float offset)** set an offset to calibrate or to correct for self heating. The value of offset is not validated to keep footprint small. -- **getOffset()** return value of offset (default 0); -- **getTemperature()** read the ambient temperature. -- **getStatus()** returns the status bits of the last call to **getTemperature()** +- **void setOffset(float offset = 0.0)** set an offset to calibrate or to correct for self heating. +The value of offset is not validated to keep footprint small. +- **float getOffset()** return value of offset (default 0). +- **float getTemperature()** read the ambient temperature. +- **uint8_t getStatus()** returns the status bits of the last call to **getTemperature()** -The value returned by **getStatus()** is the last value read by the call to **GetTemperature()**. There are three bits, see table below. A value of 6 means that TA is above the upper and above the critical temp. +The value returned by **getStatus()** is the last value read by the call to **GetTemperature()**. +There are three bits, see table below. +A value of 6 == mask == 110 means that TA is above the upper and above the critical temperature. -| Bit | Mask | Description | Notes | -|:----:|:----:|:----|:----| -| 0 | 0x01 | TA < TLOWER | lower | -| 1 | 0x02 | TA > TUPPER | larger | -| 2 | 0x04 | TA ≥ TCRIT | **larger or equal** | +| Bit | Mask | Description | Notes | +|:----:|:------:|:------------|:----------------| +| 0 | 0x01 | TA < TLOWER | lower | +| 1 | 0x02 | TA > TUPPER | larger | +| 2 | 0x04 | TA ≥ TCRIT | larger or equal | ### Resolution -- **setResolution(uint8_t res)** set the resolution, if res > 3, it is not set. -- **getResolution()** returns the resolution set +- **void setResolution(uint8_t resolution = 3)** set the resolution, if resolution > 3, it is not set. +- **uint8_t getResolution()** returns the resolution set. -| Value | Resolution | Conv time (ms) | Samples/s | Notes | -|:----:|:----|:----:|:----:|:----:| -| 0 | 0.5°C | 30 | 33 || -| 1 | 0.25°C | 65 | 15 || -| 2 | 0.125°C | 130 | 7 || -| 3 | 0.0625°C | 250 | 4 | **default** | +| Value | Resolution | Conv time (ms) | Samples/s | Notes | +|:------:|:-----------|:--------------:|:---------:|:-------:| +| 0 | 0.5°C | 30 | 33 | | +| 1 | 0.25°C | 65 | 15 | | +| 2 | 0.125°C | 130 | 7 | | +| 3 | 0.0625°C | 250 | 4 | default | -### Config +Note: for the same resolution it is about 3x faster than a DS18B20. -- **setConfigRegister(uint16_t cfg)** -- **getConfigRegister()** -| Bit | Mask | Name | Description | Value | -|:----:|:----:|:----|:----|:----| -| 0 | 0x0001 | ALT MOD | alert mode | **0 = comparator output**, 1 = interrupt output | -| 1 | 0x0002 | ALT POL | alert polarity | **0 = active low**, 1 = active high| -| 2 | 0x0004 | ALT SEL | alert select | **0 = upper+lower+crit**, 1 = crit only| -| 3 | 0x0008 | ALT CNT | alert control | **0 = OFF**, 1 = ON | -| 4 | 0x0010 | ALT STAT | alert status | **0 = OFF**, 1 = ON (read!) | -| 5 | 0x0020 | INT CLR | interrupt clear | **0 = none**, 1 = clear interrupt | -| 6 | 0x0040 | WIN LOC | lock Tup + Tlow | **0 = unlocked**, 1 = Lock | -| 7 | 0x0080 | CRT LOC | lock Tcritical | **0 = unlocked**, 1 = Lock | -| 8 | 0x0100 | SHDN | shutdown, | **0 = continuous mode**, 1 = low power | -| 9-10 | 0x0600 | Hysteresis | Thysteresis | **00 = 0°C**, 01 = 1.5°C, 10 = 3°C, 11 = 6°C | -| 11-15 | | always 0 | | +### Configuration + +- **void setConfigRegister(uint16_t configuration)** see table below + read datasheet. +- **uint16_t getConfigRegister()** return set value. + +| Bit | Mask | Name | Description | Value | +|:-----:|:------:|:-----------|:----------------|:-------| +| 0 | 0x0001 | ALT MOD | alert mode | **0 = comparator output**, 1 = interrupt output +| 1 | 0x0002 | ALT POL | alert polarity | **0 = active low**, 1 = active high +| 2 | 0x0004 | ALT SEL | alert select | **0 = upper+lower+crit**, 1 = crit only +| 3 | 0x0008 | ALT CNT | alert control | **0 = OFF**, 1 = ON +| 4 | 0x0010 | ALT STAT | alert status | **0 = OFF**, 1 = ON (read!) +| 5 | 0x0020 | INT CLR | interrupt clear | **0 = none**, 1 = clear interrupt +| 6 | 0x0040 | WIN LOC | lock Tup + Tlow | **0 = unlocked**, 1 = Lock +| 7 | 0x0080 | CRT LOC | lock Tcritical | **0 = unlocked**, 1 = Lock +| 8 | 0x0100 | SHDN | shutdown, | **0 = continuous mode**, 1 = low power +| 9-10 | 0x0600 | Hysteresis | Thysteresis | **00 = 0°C**, 01 = 1.5°C, 10 = 3°C, 11 = 6°C +| 11-15 | | always 0 | | Check datasheet for the details... ### Temperature limits / thresholds -- **setTupper(float temp)** write upper register, accuracy 0.25°C -- **getTupper()** idem -- **setTlower(float temp)** write lower register, accuracy 0.25°C -- **getTlower()** idem -- **setTcritical(float temp)** write critical register, accuracy 0.25°C -- **getTcritical()** idem +- **void setTupper(float temp)** write upper register, accuracy 0.25°C. +- **float getTupper()** idem. +- **void setTlower(float temp)** write lower register, accuracy 0.25°C. +- **float getTlower()** idem. +- **void setTcritical(float temp)** write critical register, accuracy 0.25°C. +- **float getTcritical()** idem. The values written in these registers, are the trigger values for the status read with getStatus. Note that the Hysteresis temperature delta affects the value of these triggers. -The values set are not validated to keep footprint small. +The values set are not validated to keep footprint of the library small. -### Miscelaneous +### Miscellaneous -- **getManufacturerID()** returns 84 (my version) -- **getDeviceID()** returns 0 (my version) -- **getRevision()** returns 84 (my version) -- **getRFU()** returns 29 (my version). Reserved for future use. +- **uint16_t getManufacturerID()** returns 84 (my version). +- **uint8_t getDeviceID()** returns 0 (my version). +- **uint8_t getRevision()** returns 84 (my version). +- **uint16_t getRFU()** returns 29 (my version). Reserved for future use. ### Hidden registers + The MCP9808 has hidden registers mentioned only on p.16 of the datasheet. These are for testing and calibration. The library prevents reading / writing them to keep sensors working. @@ -145,3 +159,12 @@ The library prevents reading / writing them to keep sensors working. ## Operation See examples + + +## Future + +- update documentation +- do unit test +- add more examples for the **ALERT** +- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_minimum/mcp9808_minimum.ino b/libraries/MCP9808_RT/examples/mcp9808_minimum/mcp9808_minimum.ino index b78d4997..70c43aa6 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_minimum/mcp9808_minimum.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_minimum/mcp9808_minimum.ino @@ -5,20 +5,25 @@ // PURPOSE: demo // DATE: 2020-11-12 + #include "mcp9808.h" MCP9808 ts(24); + void setup() { Serial.begin(115200); Serial.println(__FILE__); } + void loop() { delay(2000); Serial.println(ts.getTemperature(), 4); } + // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_offset/mcp9808_offset.ino b/libraries/MCP9808_RT/examples/mcp9808_offset/mcp9808_offset.ino index c766568a..f4242096 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_offset/mcp9808_offset.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_offset/mcp9808_offset.ino @@ -5,16 +5,19 @@ // PURPOSE: demo // DATE: 2020-11-13 + #include "mcp9808.h" MCP9808 ts(24); + void setup() { Serial.begin(115200); Serial.println(__FILE__); } + void loop() { delay(500); @@ -25,4 +28,6 @@ void loop() Serial.println(ts.getTemperature(), 4); } + // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_performance/mcp9808_performance.ino b/libraries/MCP9808_RT/examples/mcp9808_performance/mcp9808_performance.ino index 6756b3db..30233d21 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_performance/mcp9808_performance.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_performance/mcp9808_performance.ino @@ -5,12 +5,14 @@ // PURPOSE: demo // DATE: 2020-11-13 + #include "mcp9808.h" MCP9808 ts(24); uint32_t start, stop; + void setup() { Serial.begin(115200); @@ -36,8 +38,11 @@ void setup() Serial.println(); } + void loop() { } + // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_setResolution/mcp9808_setResolution.ino b/libraries/MCP9808_RT/examples/mcp9808_setResolution/mcp9808_setResolution.ino index 4dbae97c..ab8564b9 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_setResolution/mcp9808_setResolution.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_setResolution/mcp9808_setResolution.ino @@ -5,16 +5,19 @@ // PURPOSE: demo // DATE: 2020-11-13 + #include "mcp9808.h" MCP9808 ts(24); + void setup() { Serial.begin(115200); Serial.println(__FILE__); } + void loop() { for (int res = 0; res < 4; res++) @@ -27,4 +30,6 @@ void loop() } } + // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_test/mcp9808_test.ino b/libraries/MCP9808_RT/examples/mcp9808_test/mcp9808_test.ino index 2302cd4b..b0b0b6a2 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_test/mcp9808_test.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_test/mcp9808_test.ino @@ -7,10 +7,12 @@ // (c) : MIT // + #include "mcp9808.h" MCP9808 ts(24); + void setup() { Serial.begin(115200); @@ -22,6 +24,7 @@ void setup() test_3(); } + void test_0() { Serial.println(); @@ -45,11 +48,12 @@ void test_0() Serial.println(ts.getDeviceID()); Serial.print("Revision: "); Serial.println(ts.getRevision()); - Serial.print("RFU: "); + Serial.print("RFU: "); Serial.println(ts.getRFU()); Serial.println(); } + void test_1() { Serial.println(); @@ -65,6 +69,7 @@ void test_1() } } + void test_2() { Serial.println(); @@ -113,6 +118,7 @@ void test_2() Serial.println(); } + void test_3() { Serial.println(); @@ -131,6 +137,10 @@ void test_3() } -void loop() {} +void loop() +{ +} + // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_test_alert/mcp9808_test_alert.ino b/libraries/MCP9808_RT/examples/mcp9808_test_alert/mcp9808_test_alert.ino index fb729eae..1cc23537 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_test_alert/mcp9808_test_alert.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_test_alert/mcp9808_test_alert.ino @@ -21,10 +21,12 @@ // +----------+ // + #include "mcp9808.h" MCP9808 ts(24); + void setup() { Serial.begin(115200); @@ -33,6 +35,7 @@ void setup() // SET TEMPERATURE WINDOW FOR COMPERATOR MODE °C ts.setTlower(21); ts.setTupper(23); + // SET AUTO RESET (p32 datasheet) // same value as Tupper to have auto reset in comparator mode. // note no hysteresis set @@ -55,6 +58,7 @@ void setup() ts.setConfigRegister(cfg); } + void loop() { Serial.print(ts.getConfigRegister() & 0x0010); @@ -68,3 +72,4 @@ void loop() // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_test_alert_irq/mcp9808_test_alert_irq.ino b/libraries/MCP9808_RT/examples/mcp9808_test_alert_irq/mcp9808_test_alert_irq.ino index d38ee3a5..9b9f2e84 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_test_alert_irq/mcp9808_test_alert_irq.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_test_alert_irq/mcp9808_test_alert_irq.ino @@ -21,6 +21,7 @@ // +----------+ // + #include "mcp9808.h" MCP9808 ts(24); @@ -28,11 +29,13 @@ MCP9808 ts(24); const uint8_t ALERTPIN = 2; // ADJUST IF NEEDED volatile bool flag = false; +// interrupt routine void detectAlert() { flag = true; } + void setup() { Serial.begin(115200); @@ -57,6 +60,7 @@ void setup() pinMode(ALERTPIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(ALERTPIN), detectAlert, RISING); // CHANGE + // SET ALERT PARAMETERS uint16_t cfg = ts.getConfigRegister(); cfg &= ~0x0001; // set comparator mode @@ -67,6 +71,7 @@ void setup() ts.setConfigRegister(cfg); } + void loop() { if (flag) @@ -85,3 +90,4 @@ void loop() // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/examples/mcp9808_test_alert_polling/mcp9808_test_alert_polling.ino b/libraries/MCP9808_RT/examples/mcp9808_test_alert_polling/mcp9808_test_alert_polling.ino index 05f3bc61..ffb1435f 100644 --- a/libraries/MCP9808_RT/examples/mcp9808_test_alert_polling/mcp9808_test_alert_polling.ino +++ b/libraries/MCP9808_RT/examples/mcp9808_test_alert_polling/mcp9808_test_alert_polling.ino @@ -21,6 +21,7 @@ // +----------+ // + #include "mcp9808.h" MCP9808 ts(24); @@ -61,6 +62,7 @@ void setup() ts.setConfigRegister(cfg); } + void loop() { // will keep on alerting until pin = LOW again diff --git a/libraries/MCP9808_RT/keywords.txt b/libraries/MCP9808_RT/keywords.txt index de620f18..ef43c90a 100644 --- a/libraries/MCP9808_RT/keywords.txt +++ b/libraries/MCP9808_RT/keywords.txt @@ -1,29 +1,36 @@ -# Syntax Coloring Map For MCP9808 +# Syntax Colouring Map For MCP9808 -# Datatypes (KEYWORD1) +# Data types (KEYWORD1) MCP9808 KEYWORD1 # Methods and Functions (KEYWORD2) setAddress KEYWORD2 setConfigRegister KEYWORD2 getConfigRegister KEYWORD2 + setTupper KEYWORD2 getTupper KEYWORD2 setTlower KEYWORD2 getTlower KEYWORD2 setTcritical KEYWORD2 getTcritical KEYWORD2 + getTemperature KEYWORD2 getStatus KEYWORD2 + setOffset KEYWORD2 getOffset KEYWORD2 + setResolution KEYWORD2 getResolution KEYWORD2 + getManufacturerID KEYWORD2 getDeviceID KEYWORD2 getRevision KEYWORD2 + # Constants (LITERAL1) +MCP9808_LIB_VERSION LITERAL1 MCP9808_THYSTERESIS LITERAL1 MCP9808_SHUTDOWN LITERAL1 MCP9808_CRIT_LOCK LITERAL1 diff --git a/libraries/MCP9808_RT/library.json b/libraries/MCP9808_RT/library.json index d3e0491c..73f8be0d 100644 --- a/libraries/MCP9808_RT/library.json +++ b/libraries/MCP9808_RT/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/MCP9808_RT.git" }, - "version": "0.1.3", + "version": "0.1.4", "license": "MIT", "frameworks": "arduino", "platforms": "*" diff --git a/libraries/MCP9808_RT/library.properties b/libraries/MCP9808_RT/library.properties index bccd2f9c..2af0c752 100644 --- a/libraries/MCP9808_RT/library.properties +++ b/libraries/MCP9808_RT/library.properties @@ -1,5 +1,5 @@ name=MCP9808_RT -version=0.1.3 +version=0.1.4 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino Library for I2C MCP9808 temperature sensor diff --git a/libraries/MCP9808_RT/mcp9808.cpp b/libraries/MCP9808_RT/mcp9808.cpp index 7828e298..dad58413 100644 --- a/libraries/MCP9808_RT/mcp9808.cpp +++ b/libraries/MCP9808_RT/mcp9808.cpp @@ -1,7 +1,7 @@ // // FILE: mcp9808.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.3 +// VERSION: 0.1.4 // PURPOSE: Arduino Library for I2C mcp9808 temperature sensor // DATE: 2020-05-03 // URL: https://github.com/RobTillaart/MCP9808_RT @@ -12,6 +12,10 @@ // 0.1.2 2020-11-16 removed hasAlert, removed setAlertPin, // added 2 alert examples, refactor low level // 0.1.3 2021-01-01 arduino-ci + unit test +// 0.1.4 2021-11-08 update build-CI, badges +// default offset for offset +// default Wire for I2C bus - setAddress() + #include "mcp9808.h" @@ -25,6 +29,7 @@ #define MCP9808_DID 0x07 #define MCP9808_RES 0x08 + /* 0000 = RFU, Reserved for Future Use (Read-Only register) 0001 = Configuration register (CONFIG) @@ -38,6 +43,7 @@ 1xxx = Reserved(1) */ + #if defined(ESP8266) || defined(ESP32) MCP9808::MCP9808(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin) { @@ -59,6 +65,7 @@ MCP9808::MCP9808(const uint8_t address) } #endif + bool MCP9808::setAddress(const uint8_t address, TwoWire *wire) { if ((address < 24) || (address > 31)) return false; @@ -68,97 +75,116 @@ bool MCP9808::setAddress(const uint8_t address, TwoWire *wire) return true; } + bool MCP9808::isConnected() { Wire.beginTransmission(_address); return (Wire.endTransmission() == 0); } -void MCP9808::setConfigRegister(uint16_t config) + +void MCP9808::setConfigRegister(uint16_t configuration) { - writeReg16(MCP9808_CONFIG, config); + writeReg16(MCP9808_CONFIG, configuration); } + uint16_t MCP9808::getConfigRegister() { return readReg16(MCP9808_CONFIG); } -void MCP9808::setTupper(float temp) + +void MCP9808::setTupper(float temperature) { - writeFloat(MCP9808_TUPPER, temp); + writeFloat(MCP9808_TUPPER, temperature); } + float MCP9808::getTupper() { return readFloat(MCP9808_TUPPER); } -void MCP9808::setTlower(float temp) + +void MCP9808::setTlower(float temperature) { - writeFloat(MCP9808_TLOWER, temp); + writeFloat(MCP9808_TLOWER, temperature); } + float MCP9808::getTlower() { return readFloat(MCP9808_TLOWER); } -void MCP9808::setTcritical(float temp) + +void MCP9808::setTcritical(float temperature) { - writeFloat(MCP9808_TCRIT, temp); + writeFloat(MCP9808_TCRIT, temperature); } + float MCP9808::getTcritical() { return readFloat(MCP9808_TCRIT); } + void MCP9808::setOffset(float offset) { _offset = offset; }; + float MCP9808::getOffset() { return _offset; }; + float MCP9808::getTemperature() { return readFloat(MCP9808_TA) + _offset; } + uint8_t MCP9808::getStatus() { return _status; } + uint16_t MCP9808::getManufacturerID() { return readReg16(MCP9808_MID); } + uint8_t MCP9808::getDeviceID() { return readReg16(MCP9808_DID) >> 8; } + uint8_t MCP9808::getRevision() { return readReg16(MCP9808_DID) & 0xFF; } -void MCP9808::setResolution(uint8_t res) + +void MCP9808::setResolution(uint8_t resolution) { - if (res < 4) writeReg8(MCP9808_RES, res); + if (resolution < 4) writeReg8(MCP9808_RES, resolution); } + uint8_t MCP9808::getResolution() { return readReg8(MCP9808_RES); } + uint16_t MCP9808::getRFU() { return readReg16(MCP9808_RFU); @@ -178,6 +204,7 @@ void MCP9808::writeFloat(uint8_t reg, float f) writeReg16(reg, val); } + float MCP9808::readFloat(uint8_t reg) { uint16_t val = readReg16(reg); @@ -192,6 +219,7 @@ float MCP9808::readFloat(uint8_t reg) return (val & 0x0FFF) * 0.0625; } + void MCP9808::writeReg8(uint8_t reg, uint8_t value) { if (reg > MCP9808_RES) return; // see p.16 @@ -201,6 +229,7 @@ void MCP9808::writeReg8(uint8_t reg, uint8_t value) Wire.endTransmission(); } + uint8_t MCP9808::readReg8(uint8_t reg) { if (reg > MCP9808_RES) return 0; // see p.16 @@ -211,6 +240,7 @@ uint8_t MCP9808::readReg8(uint8_t reg) return Wire.read(); } + void MCP9808::writeReg16(uint8_t reg, uint16_t value) { if (reg > MCP9808_RES) return; // see p.16 @@ -221,6 +251,7 @@ void MCP9808::writeReg16(uint8_t reg, uint16_t value) Wire.endTransmission(); } + uint16_t MCP9808::readReg16(uint8_t reg) { if (reg > MCP9808_RES) return 0; // see p.16 @@ -233,4 +264,6 @@ uint16_t MCP9808::readReg16(uint8_t reg) return val; } + // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/mcp9808.h b/libraries/MCP9808_RT/mcp9808.h index 5cbf0cae..94ea7e18 100644 --- a/libraries/MCP9808_RT/mcp9808.h +++ b/libraries/MCP9808_RT/mcp9808.h @@ -2,12 +2,13 @@ // // FILE: mcp9808.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.3 +// VERSION: 0.1.4 // PURPOSE: Arduino Library for I2C mcp9808 temperature sensor // DATE: 2020-05-03 // URL: https://github.com/RobTillaart/MCP9808_RT // + #include "Arduino.h" #include "Wire.h" @@ -16,18 +17,21 @@ // 24..31 == 0x18..0x1F -// CONFIG REGISTER MASKS +#define MCP9808_LIB_VERSION (F("0.1.4")) + + +// CONFIGURATION REGISTER MASKS // check the datasheet for exact usage -#define MCP9808_THYSTERESIS 0x0600 -#define MCP9808_SHUTDOWN 0x0100 -#define MCP9808_CRIT_LOCK 0x0080 -#define MCP9808_WIN_LOCK 0x0040 -#define MCP9808_INT_CLEAR 0x0020 -#define MCP9808_ALERT_STATUS 0x0010 -#define MCP9808_ALERT_CTRL 0x0008 -#define MCP9808_ALERT_SELECT 0x0004 -#define MCP9808_ALERT_POLAR 0x0002 -#define MCP9808_ALERT_MODE 0x0001 +#define MCP9808_THYSTERESIS 0x0600 +#define MCP9808_SHUTDOWN 0x0100 +#define MCP9808_CRIT_LOCK 0x0080 +#define MCP9808_WIN_LOCK 0x0040 +#define MCP9808_INT_CLEAR 0x0020 +#define MCP9808_ALERT_STATUS 0x0010 +#define MCP9808_ALERT_CTRL 0x0008 +#define MCP9808_ALERT_SELECT 0x0004 +#define MCP9808_ALERT_POLAR 0x0002 +#define MCP9808_ALERT_MODE 0x0001 class MCP9808 @@ -40,27 +44,28 @@ public: MCP9808(const uint8_t address); #endif - bool setAddress(const uint8_t address, TwoWire *wire); + bool setAddress(const uint8_t address, TwoWire *wire = &Wire); bool isConnected(); - void setConfigRegister(uint16_t config); + void setConfigRegister(uint16_t configuration); uint16_t getConfigRegister(); - void setTupper(float temp); + void setTupper(float temperature); float getTupper(); - void setTlower(float temp); + void setTlower(float temperature); float getTlower(); - void setTcritical(float temp); + void setTcritical(float temperature); float getTcritical(); - void setOffset(float offset); + void setOffset(float offset = 0.0); float getOffset(); float getTemperature(); + // getStatus() returns 0..7 // to get latest status, getTemperature() must be called first - uint8_t getStatus(); + uint8_t getStatus(); - void setResolution(uint8_t res); + void setResolution(uint8_t resolution = 3); uint8_t getResolution(); uint16_t getManufacturerID(); @@ -86,4 +91,6 @@ private: uint16_t readReg16(uint8_t reg); }; + // -- END OF FILE -- + diff --git a/libraries/MCP9808_RT/test/unit_test_001.cpp b/libraries/MCP9808_RT/test/unit_test_001.cpp index 41798529..ad0f2a3d 100644 --- a/libraries/MCP9808_RT/test/unit_test_001.cpp +++ b/libraries/MCP9808_RT/test/unit_test_001.cpp @@ -29,6 +29,7 @@ // assertNAN(arg); // isnan(a) // assertNotNAN(arg); // !isnan(a) + #include @@ -49,13 +50,14 @@ unittest_teardown() unittest(test_constructor) { - fprintf(stderr, "VERSION: %s\n", "-"); + fprintf(stderr, "VERSION: %s\n", (char *) MCP9808_LIB_VERSION); MCP9808 ts(24); assertTrue(ts.setAddress(24, &Wire) ); assertTrue(ts.isConnected()); } + unittest(test_offset) { MCP9808 ts(24); @@ -67,6 +69,7 @@ unittest(test_offset) } } + unittest_main() // --------