Skip to content

Commit 997c1b3

Browse files
authoredOct 20, 2016
Merge pull request #5 from nadavmatalon/namespace
Namespace
2 parents 946578c + 35fa346 commit 997c1b3

14 files changed

+405
-360
lines changed
 

‎MCP9802.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
instead of the String class to further reduce memory footprint. To this end, added PString.h and
1616
PString.cpp files to /utility folder. In addition added "I2C STATUS" (CONNECTED / NOT CONNECTED)
1717
field to device information string (9.10.16)
18+
Ver. 1.4.0 - Added namespaces to prevent conflicts with other libraries (15.10.16)
1819
1920
*==============================================================================================================*
2021
LICENSE
@@ -40,16 +41,21 @@
4041
4142
*==============================================================================================================*/
4243

44+
#if 1
45+
__asm volatile ("nop");
46+
#endif
47+
4348
#include "MCP9802.h"
4449

4550
/*==============================================================================================================*
4651
CONSTRUCTOR
4752
*==============================================================================================================*/
4853

49-
MCP9802::MCP9802(int devAddr) {
50-
_devAddr = devAddr;
51-
_tempUnit = CELSIUS;
52-
_singleConfig = DEFAULT_CONFIG;
54+
MCP9802::MCP9802(byte devAddr) {
55+
_devAddr = devAddr;
56+
_tempUnit = CELSIUS;
57+
_comBuffer = COM_SUCCESS;
58+
_singleConfig = DEFAULT_CONFIG;
5359
}
5460

5561
/*==============================================================================================================*
@@ -62,7 +68,7 @@ MCP9802::~MCP9802() {}
6268
PING (0 = SUCCESS / 1-7 = ERROR CODE)
6369
*==============================================================================================================*/
6470

65-
// See explication of error code numbers in the README)
71+
// See explication of error code numbers in the README
6672

6773
byte MCP9802::ping() {
6874
Wire.beginTransmission(_devAddr);

‎MCP9802.h

+113-102
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
instead of the String class to further reduce memory footprint. For this purpose, added PString.h
1616
PString.cpp files to /utility folder. In addition added "I2C STATUS" (CONNECTED / NOT CONNECTED)
1717
field to device information string (9.10.16)
18+
Ver. 1.4.0 - Added namespaces to prevent conflicts with other libraries (15.10.16)
1819
1920
*===============================================================================================================*
2021
INTRODUCTION
@@ -143,111 +144,121 @@
143144
144145
*==============================================================================================================*/
145146

147+
#if 1
148+
__asm volatile ("nop");
149+
#endif
150+
146151
#ifndef MCP9802_h
147152
#define MCP9802_h
148153

149-
#if defined(ARDUINO_ARCH_AVR)
150-
#include <Arduino.h>
151-
#include "WSWire.h"
152-
#include "utility/PString.h"
153-
#else
154+
#if !defined(ARDUINO_ARCH_AVR)
154155
#error “The MCP9802 library only supports AVR processors.”
155156
#endif
156157

157-
const byte DEFAULT_CONFIG = 0; // Configuration Register Default Settings for 'Continuous' conversion mode (0x00)
158-
const int DEFAULT_HYST = 75; // Hysteresis Register Default Settings in degrees Celsius (0x9600)
159-
const int DEFAULT_LIMIT = 80; // Limit Register Default Settings in degrees Celsius (0xA000)
160-
const int CONFIG_BYTE = 1; // Number of Configuration Register Bytes (CONFIG)
161-
const int DATA_BYTES = 2; // Number of Data Register Bytes (TEMP, HYST, LIMIT)
162-
const byte INIT_SINGLE_SHOT = 129; // Initiates a single conversion in 'Single-Shot' mode (0x81)
163-
const byte MIN_CON_TIME = 30; // 30ms - Minimal Conversion Time (based on 9-BIT Resolution)
164-
const byte COM_SUCCESS = 0; // I2C Communication Success (No Error)
165-
const float MINIMUM_TEMP = -55; // Minimum temperature value (in degrees Celsius) for Hysteresis & Limit registers
166-
const float MAXIMUM_TEMP = 125; // Maximum temperature value (in degrees Celsius) for Hysteresis & Limit registers
167-
const float C_TO_F_CONST = 0.5555556; // For faster Convert Degrees Fahrenheit to Celsius
168-
169-
typedef enum:byte {
170-
TEMP = 0,
171-
CONFIG = 1,
172-
HYST = 2,
173-
LIMIT = 3
174-
} reg_ptr_t;
175-
176-
typedef enum:byte {
177-
COMP = 0,
178-
INT = 2
179-
} alert_type_t;
180-
181-
typedef enum:byte {
182-
ACTIVE_LOW = 0,
183-
ACTIVE_HIGH = 4
184-
} alert_mode_t;
185-
186-
typedef enum:byte {
187-
FQ1 = 0,
188-
FQ2 = 8,
189-
FQ4 = 16,
190-
FQ6 = 24
191-
} fault_queue_t;
192-
193-
typedef enum:byte {
194-
RES_9 = 0,
195-
RES_10 = 32,
196-
RES_11 = 64,
197-
RES_12 = 96
198-
} resolution_t;
199-
200-
typedef enum:byte {
201-
CONT = 0,
202-
SINGLE = 128
203-
} con_mode_t;
204-
205-
typedef enum:byte {
206-
CELSIUS = 0,
207-
FAHRENHEIT = 1
208-
} temp_unit_t;
209-
210-
class MCP9802 {
211-
public:
212-
MCP9802(int devAddr);
213-
~MCP9802();
214-
byte ping();
215-
byte getAlertType();
216-
byte getAlertMode();
217-
byte getFaultQueue();
218-
byte getResolution();
219-
byte getConMode();
220-
byte getTempUnit();
221-
byte getComResult();
222-
float getTemp();
223-
float getHyst();
224-
float getLimit();
225-
float singleCon();
226-
void setAlertType(alert_type_t alertType);
227-
void setAlertMode(alert_mode_t alertMode);
228-
void setFaultQueue(fault_queue_t fqVal);
229-
void setResolution(resolution_t resVal);
230-
void setConMode(con_mode_t conMode);
231-
void setTempUnit(temp_unit_t newTempUnit);
232-
void setHyst(float newHyst);
233-
void setLimit(float newLimit);
234-
void reset();
235-
private:
236-
int _devAddr;
237-
byte _tempUnit;
238-
byte _comBuffer;
239-
byte _singleConfig;
240-
void initCall(byte ptr);
241-
void endCall();
242-
byte getConfig();
243-
float getData(reg_ptr_t ptr);
244-
void setConfig(byte newConfig);
245-
void setData(reg_ptr_t ptr, float newData);
246-
float convertCtoF(float valC);
247-
float convertFtoC(float valF);
248-
float roundToHalfDegC(float valC);
249-
friend PString MCP9802ComStr(const MCP9802&);
250-
friend PString MCP9802InfoStr(const MCP9802&);
251-
};
252-
253-
#endif
158+
#include <Arduino.h>
159+
#include "WSWire.h"
160+
#include "utility/MCP9802_PString.h"
161+
162+
namespace Mcp9802 {
163+
164+
const byte DEFAULT_CONFIG = 0; // Configuration Register Default Settings for 'Continuous' conversion mode (0x00)
165+
const int DEFAULT_HYST = 75; // Hysteresis Register Default Settings in degrees Celsius (0x9600)
166+
const int DEFAULT_LIMIT = 80; // Limit Register Default Settings in degrees Celsius (0xA000)
167+
const byte CONFIG_BYTE = 1; // Number of Configuration Register Bytes (CONFIG)
168+
const byte DATA_BYTES = 2; // Number of Data Register Bytes (TEMP, HYST, LIMIT)
169+
const byte INIT_SINGLE_SHOT = 129; // Initiates a single conversion in 'Single-Shot' mode (0x81)
170+
const byte MIN_CON_TIME = 30; // 30ms - Minimal Conversion Time (based on 9-BIT Resolution)
171+
const byte COM_SUCCESS = 0; // I2C Communication Success (No Error)
172+
const float MINIMUM_TEMP = -55; // Minimum temperature value (in degrees Celsius) for Hysteresis & Limit registers
173+
const float MAXIMUM_TEMP = 125; // Maximum temperature value (in degrees Celsius) for Hysteresis & Limit registers
174+
const float C_TO_F_CONST = 0.5555556; // For faster Convert Degrees Fahrenheit to Celsius
175+
176+
typedef enum:byte {
177+
TEMP = 0,
178+
CONFIG = 1,
179+
HYST = 2,
180+
LIMIT = 3
181+
} reg_ptr_t;
182+
183+
typedef enum:byte {
184+
COMP = 0,
185+
INT = 2
186+
} alert_type_t;
187+
188+
typedef enum:byte {
189+
ACTIVE_LOW = 0,
190+
ACTIVE_HIGH = 4
191+
} alert_mode_t;
192+
193+
typedef enum:byte {
194+
FQ1 = 0,
195+
FQ2 = 8,
196+
FQ4 = 16,
197+
FQ6 = 24
198+
} fault_queue_t;
199+
200+
typedef enum:byte {
201+
RES_9 = 0,
202+
RES_10 = 32,
203+
RES_11 = 64,
204+
RES_12 = 96
205+
} resolution_t;
206+
207+
typedef enum:byte {
208+
CONT = 0,
209+
SINGLE = 128
210+
} con_mode_t;
211+
212+
typedef enum:byte {
213+
CELSIUS = 0,
214+
FAHRENHEIT = 1
215+
} temp_unit_t;
216+
217+
class MCP9802 {
218+
public:
219+
MCP9802(byte devAddr);
220+
~MCP9802();
221+
byte ping();
222+
byte getAlertType();
223+
byte getAlertMode();
224+
byte getFaultQueue();
225+
byte getResolution();
226+
byte getConMode();
227+
byte getTempUnit();
228+
byte getComResult();
229+
float getTemp();
230+
float getHyst();
231+
float getLimit();
232+
float singleCon();
233+
void setAlertType(alert_type_t alertType);
234+
void setAlertMode(alert_mode_t alertMode);
235+
void setFaultQueue(fault_queue_t fqVal);
236+
void setResolution(resolution_t resVal);
237+
void setConMode(con_mode_t conMode);
238+
void setTempUnit(temp_unit_t newTempUnit);
239+
void setHyst(float newHyst);
240+
void setLimit(float newLimit);
241+
void reset();
242+
private:
243+
byte _devAddr;
244+
byte _tempUnit;
245+
byte _comBuffer;
246+
byte _singleConfig;
247+
void initCall(byte ptr);
248+
void endCall();
249+
byte getConfig();
250+
float getData(reg_ptr_t ptr);
251+
void setConfig(byte newConfig);
252+
void setData(reg_ptr_t ptr, float newData);
253+
float convertCtoF(float valC);
254+
float convertFtoC(float valF);
255+
float roundToHalfDegC(float valC);
256+
friend MCP9802_PString MCP9802ComStr(const MCP9802&);
257+
friend MCP9802_PString MCP9802InfoStr(const MCP9802&);
258+
};
259+
260+
}
261+
262+
using namespace Mcp9802;
263+
264+
#endif

‎README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[![Platform Badge](https://img.shields.io/badge/platform-Arduino-orange.svg)](https://www.arduino.cc/)
55
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
6-
[![SemVer](https://img.shields.io/badge/SemVer-1.3.0-brightgreen.svg)](http://semver.org/)
6+
[![SemVer](https://img.shields.io/badge/SemVer-1.4.0-brightgreen.svg)](http://semver.org/)
77
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
88
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
99

@@ -25,8 +25,8 @@ This library contains a robust driver for the MCP9802 that exposes its entire fu
2525
- **/utility** -
2626
- **MCP9802InfoStr.h** - Header file containing a functional extention of the library to include generating pritable information String (see Note #9 below).
2727
- **MCP9802ComStr.h** - Header file containing a functional extention of the library to include generating a pritable I2C Communication Result String (see Note #10 below).
28-
- **PString.h** - Header file for PString class (lighter alternative to String class)
29-
- **PString.cpp** - Compilation file for PString class (lighter alternative to String class)
28+
- **MCP9802_PString.h** - Header file for PString class (lighter alternative to String class)
29+
- **MCP9802_PString.cpp** - Compilation file for PString class (lighter alternative to String class)
3030
- **/examples** -
3131
- **/MCP9802_Test/MCP9802_Test.ino** - A basic sketch for testing whether the MCP9802 is hooked-up and operating correctly.
3232
- **MCP9802_Usage/MCP9802_Usage.ino** - A much more extensive sketch offering a complete usage illustration, as well as a rubust testing mechanism.
@@ -279,12 +279,12 @@ If you want to destruct an instantiated MCP9802 object, you can use the followin
279279

280280
(* requires an additional '\#include' of the relevant *.h file as shown in the corresponding example sketches)
281281

282-
__MCP9802ComStr();__
282+
__Mcp9802_ComStr::MCP9802ComStr();__
283283
Parameters:&nbsp;&nbsp;&nbsp;Name of an initialized MCP9802 instance
284284
Description:&nbsp;&nbsp;Returns printable string containing human-friendly information about the device's latest I2C communication result
285285
Returns:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PString
286286

287-
__MCP9802InfoStr();__
287+
__Mcp9802_InfoStr::MCP9802InfoStr();__
288288
Parameters:&nbsp;&nbsp;&nbsp;Name of an initialized MCP9802 instance
289289
Description:&nbsp;&nbsp;Returns printable string containing detailed information about the device's current settings
290290
Returns:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PString
@@ -314,6 +314,7 @@ __Ver. 1.0.0__ - First release (26.9.16)
314314
__Ver. 1.1.0__ - Small change in functionality: attempting to set hysteresis or limit beyond the legitimate range (-55°C - 125°C / -67°F - 257°F) now sets the register to the maximum/minumum allowable value rather than do nothing (4.10.16)
315315
__Ver. 1.2.0__ - Changed license to MIT (5.10.16)
316316
__Ver. 1.3.0__ - Changed auxilliary functions: MCP9802InfoStr() and MCP9802ComStr() to work with the PString class instead of the String class to further reduce memory footprint. For this purpose, added PString.h & PString.cpp files to /utility folder. In addition added "I2C STATUS" (CONNECTED / NOT CONNECTED) field to device information string (9.10.16)
317+
__Ver. 1.4.0__ - Added namespaces to prevent conflicts with other libraries (15.10.16)
317318

318319
## LICENSE
319320

‎examples/MCP9802_I2C_Status/MCP9802_I2C_Status.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@
7575
#include "MCP9802.h"
7676
#include "utility/MCP9802ComStr.h"
7777

78-
const int MCP9802_ADDR = 0x48; // I2C address of the MCP9802 (Change as needed)
78+
const byte DEV_ADDR = 0x48; // I2C address of the MCP9802 (Change as needed)
7979

80-
MCP9802 mcp9802(MCP9802_ADDR);
80+
MCP9802 mcp9802(DEV_ADDR);
8181

8282
void setup() {
8383
Serial.begin(9600);

‎examples/MCP9802_Info/MCP9802_Info.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@
7676
#include "MCP9802.h"
7777
#include "utility/MCP9802InfoStr.h"
7878

79-
const int MCP9802_ADDR = 0x48; // I2C address of the MCP9802 (Change as needed)
79+
const byte DEV_ADDR = 0x48; // I2C address of the MCP9802 (Change as needed)
8080

81-
MCP9802 mcp9802(MCP9802_ADDR);
81+
MCP9802 mcp9802(DEV_ADDR);
8282

8383
void setup() {
8484
Serial.begin(9600);

‎examples/MCP9802_Test/MCP9802_Test.ino

+11-11
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282
#include "MCP9802.h"
8383

84-
const int MCP9802_ADDR = 0x48; // DEC: 72 - I2C address of the MCP9802 (Change as needed)
84+
const byte DEV_ADDR = 0x48; // DEC: 72 - I2C address of the MCP9802 (Change as needed)
8585
const byte PIN_D2 = 2; // Arduino Digital Pin PIN 2 (connected to the MCP9802 ALERT Pin)
8686
float temp, limit, hyst; // Containers for register data
8787

@@ -90,7 +90,7 @@ typedef enum:byte {
9090
ON = 1
9191
} alert_state_t;
9292

93-
MCP9802 MCP9802(MCP9802_ADDR);
93+
MCP9802 mcp9802(DEV_ADDR);
9494

9595
void setup() {
9696
pinMode(PIN_D2, INPUT_PULLUP);
@@ -104,7 +104,7 @@ void setup() {
104104
Serial.print(F("\nSerial Port is "));
105105
Serial.print(Serial ? "Open\n" : "Could not be opened\n");
106106
printDivider();
107-
MCP9802.reset();
107+
mcp9802.reset();
108108
quickDelay();
109109
Serial.print(F("\nINITIALIZING TESTS\n"));
110110
runTests();
@@ -120,7 +120,7 @@ void runTests() {
120120

121121
void testPingDevice() {
122122
Serial.print(F("\nSearching for device...Device "));
123-
MCP9802.ping() ? Serial.print(F("Not Found\n")) : Serial.print(F("Found!\n"));
123+
mcp9802.ping() ? Serial.print(F("Not Found\n")) : Serial.print(F("Found!\n"));
124124
printDivider();
125125
quickDelay();
126126
}
@@ -134,30 +134,30 @@ void testGetConditions() {
134134

135135
void testGetTemp() {
136136
Serial.print(F("\nCURRENT TEMP:\t\t"));
137-
temp = MCP9802.getTemp();
137+
temp = mcp9802.getTemp();
138138
Serial.print(temp, 1);
139139
Serial.print(F("C\n"));
140140
}
141141

142142
void testGetLimit() {
143143
Serial.print(F("\nCURRENT LIMIT:\t\t"));
144-
limit = MCP9802.getLimit();
144+
limit = mcp9802.getLimit();
145145
Serial.print(limit, 1);
146146
Serial.print(F("C\n"));
147147
quickDelay();
148148
}
149149

150150
void testGetHyst() {
151151
Serial.print(F("\nCURRENT HYST:\t\t"));
152-
hyst = MCP9802.getHyst();
152+
hyst = mcp9802.getHyst();
153153
Serial.print(hyst, 1);
154154
Serial.print(F("C\n"));
155155
quickDelay();
156156
}
157157

158158
void testGetAlertMode() {
159159
Serial.print(F("\nALERT MODE:\t\tACTIVE-"));
160-
Serial.print(MCP9802.getAlertMode() ? "HIGH\n" : "LOW\n");
160+
Serial.print(mcp9802.getAlertMode() ? "HIGH\n" : "LOW\n");
161161
quickDelay();
162162
printDivider();
163163
}
@@ -167,8 +167,8 @@ void testAlert() {
167167
testAlertState(OFF);
168168
printDivider();
169169
Serial.print(F("\nSimulating Alert Conditions..."));
170-
MCP9802.setLimit(temp - 10);
171-
MCP9802.setHyst(temp - 20);
170+
mcp9802.setLimit(temp - 10);
171+
mcp9802.setHyst(temp - 20);
172172
Serial.print(F("DONE\n"));
173173
Serial.print(F("\nCurrent Conditions:\n"));
174174
testGetConditions();
@@ -178,7 +178,7 @@ void testAlert() {
178178

179179
void testSetAlertMode() {
180180
Serial.print(F("\nSetting Alert Mode to:\tACTIVE-HIGH..."));
181-
MCP9802.setAlertMode(ACTIVE_HIGH);
181+
mcp9802.setAlertMode(ACTIVE_HIGH);
182182
Serial.print(F("...DONE\n"));
183183
testGetAlertMode();
184184
quickDelay();

‎examples/MCP9802_Usage/MCP9802_Usage.ino

+70-70
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
#include <MCP9802.h>
135135

136136
const byte PIN_D2 = 2; // Arduino PIN D2 (PIN 2) connected to the MCP9802's ALERT Pin
137-
const int MCP9802_ADDR = 0x48; // DEC: 72 - I2C address of the MCP9802 (Change as needed)
137+
const byte DEV_ADDR = 0x48; // DEC: 72 - I2C address of the MCP9802 (Change as needed)
138138

139139
byte config; // Variables to hold configuration register data
140140
int tempC16, tempF16, hystC16, hystF16, limitC16, limitF16; // Variables to hold conversion data
@@ -147,7 +147,7 @@ typedef enum:byte { // Com
147147
ON = 1
148148
} alert_state_t;
149149

150-
MCP9802 MCP9802(MCP9802_ADDR); // Constructs a new MCP9802 object with the relevant I2C address
150+
MCP9802 mcp9802(DEV_ADDR); // Constructs a new MCP9802 object with the relevant I2C address
151151

152152
void setup() {
153153
pinMode(PIN_D2, INPUT_PULLUP); // Setting Arduino Digital Pin 2 to INPUT with pull-up resistors (for Alert testing)
@@ -189,7 +189,7 @@ void runTests() {
189189

190190
void testPingDevice() {
191191
Serial.print(F("\nSearching for device...Device "));
192-
Serial.print(MCP9802.ping() ? "Not Found\n" : "Found!\n");
192+
Serial.print(mcp9802.ping() ? "Not Found\n" : "Found!\n");
193193
quickDelay();
194194
}
195195

@@ -204,18 +204,18 @@ void testGetConfigData() {
204204

205205
void testGetAlertType() {
206206
Serial.print(F("\nALERT TYPE:\t\t"));
207-
Serial.print(MCP9802.getAlertType() ? "INTERRUPT\n" : "COMPARATOR\n");
207+
Serial.print(mcp9802.getAlertType() ? "INTERRUPT\n" : "COMPARATOR\n");
208208
quickDelay();
209209
}
210210

211211
void testGetAlertMode() {
212212
Serial.print(F("\nALERT MODE:\t\t"));
213-
Serial.print(MCP9802.getAlertMode() ? "ACTIVE-HIGH\n" : "ACTIVE-LOW\n");
213+
Serial.print(mcp9802.getAlertMode() ? "ACTIVE-HIGH\n" : "ACTIVE-LOW\n");
214214
quickDelay();
215215
}
216216

217217
void testGetFaultQueue() {
218-
byte fqVal = MCP9802.getFaultQueue();
218+
byte fqVal = mcp9802.getFaultQueue();
219219
Serial.print("\nFAULT QUEUE:\t\t");
220220
Serial.print(fqVal);
221221
Serial.print(fqVal == 1 ? " FAULT\n" : " FAULTS\n");
@@ -224,20 +224,20 @@ void testGetFaultQueue() {
224224

225225
void testGetResolution() {
226226
Serial.print(F("\nRESOLUTION:\t\t"));
227-
Serial.print(MCP9802.getResolution());
227+
Serial.print(mcp9802.getResolution());
228228
Serial.print(F("-BIT\n"));
229229
quickDelay();
230230
}
231231

232232
void testGetConMode() {
233233
Serial.print(F("\nCONVERSION MODE:\t"));
234-
Serial.print(MCP9802.getConMode() ? "SINGLE-SHOT\n" : "CONTINUOUS\n");
234+
Serial.print(mcp9802.getConMode() ? "SINGLE-SHOT\n" : "CONTINUOUS\n");
235235
quickDelay();
236236
}
237237

238238
void testGetTempUnit() {
239239
Serial.print(F("\nTEMPERATURE UNIT:\t"));
240-
Serial.print(MCP9802.getTempUnit() ? "FAHRENHEIT\n" : "CELSIUS\n");
240+
Serial.print(mcp9802.getTempUnit() ? "FAHRENHEIT\n" : "CELSIUS\n");
241241
}
242242

243243
void testGetRegData() {
@@ -257,18 +257,18 @@ void testGetTempReadings() {
257257
}
258258

259259
void testGetTempC() {
260-
tempC = MCP9802.getTemp();
260+
tempC = mcp9802.getTemp();
261261
Serial.print(F("\nTemp (C): \t"));
262262
Serial.println(tempC, 4);
263263
quickDelay();
264264
}
265265

266266
void testGetTempF() {
267-
MCP9802.setTempUnit(FAHRENHEIT);
268-
tempF = MCP9802.getTemp();
267+
mcp9802.setTempUnit(FAHRENHEIT);
268+
tempF = mcp9802.getTemp();
269269
Serial.print(F("\nTemp (F): \t"));
270270
Serial.println(tempF, 4);
271-
MCP9802.setTempUnit(CELSIUS);
271+
mcp9802.setTempUnit(CELSIUS);
272272
quickDelay();
273273
}
274274

@@ -278,18 +278,18 @@ void testGetHystSetting() {
278278
}
279279

280280
void testGetHystC() {
281-
hystC = MCP9802.getHyst();
281+
hystC = mcp9802.getHyst();
282282
Serial.print(F("\nHyst (C): \t"));
283283
Serial.println(hystC, 4);
284284
quickDelay();
285285
}
286286

287287
void testGetHystF() {
288-
MCP9802.setTempUnit(FAHRENHEIT);
289-
hystF = MCP9802.getHyst();
288+
mcp9802.setTempUnit(FAHRENHEIT);
289+
hystF = mcp9802.getHyst();
290290
Serial.print(F("\nHyst (F): \t"));
291291
Serial.println(hystF, 4);
292-
MCP9802.setTempUnit(CELSIUS);
292+
mcp9802.setTempUnit(CELSIUS);
293293
quickDelay();
294294
}
295295

@@ -299,18 +299,18 @@ void testGetLimitSetting() {
299299
}
300300

301301
void testGetLimitC() {
302-
limitC = MCP9802.getLimit();
302+
limitC = mcp9802.getLimit();
303303
Serial.print(F("\nLimit (C): \t"));
304304
Serial.println(limitC, 4);
305305
quickDelay();
306306
}
307307

308308
void testGetLimitF() {
309-
MCP9802.setTempUnit(FAHRENHEIT);
310-
limitF = MCP9802.getLimit();
309+
mcp9802.setTempUnit(FAHRENHEIT);
310+
limitF = mcp9802.getLimit();
311311
Serial.print(F("\nLimit (F): \t"));
312312
Serial.println(limitF, 4);
313-
MCP9802.setTempUnit(CELSIUS);
313+
mcp9802.setTempUnit(CELSIUS);
314314
quickDelay();
315315
}
316316

@@ -328,7 +328,7 @@ void testSetAlertType() {
328328
for (byte i=0; i<2; i++) {
329329
Serial.print(F("\nSetting Alert Type to:\t"));
330330
i ? Serial.print("COMPARATOR") : Serial.print("INTERRUPT");
331-
MCP9802.setAlertType(alertTypeParams[i]);
331+
mcp9802.setAlertType(alertTypeParams[i]);
332332
Serial.print(F("...DONE\n"));
333333
testGetAlertType();
334334
quickDelay();
@@ -340,7 +340,7 @@ void testSetAlertMode() {
340340
for (byte i=0; i<2; i++) {
341341
Serial.print(F("\nSetting Alert Mode to:\t"));
342342
i ? Serial.print("ACTIVE-LOW") : Serial.print("ACTIVE-HIGH");
343-
MCP9802.setAlertMode(alertModeParams[i]);
343+
mcp9802.setAlertMode(alertModeParams[i]);
344344
Serial.print(F("...DONE\n"));
345345
testGetAlertMode();
346346
quickDelay();
@@ -353,7 +353,7 @@ void testSetFaultQueue() {
353353
Serial.print(F("\nSetting Fault Queue to:\t"));
354354
fqParams[i] == 0 ? Serial.print(F("1")) : Serial.print(fqParams[i] >> 2);
355355
fqParams[i] == 0 ? Serial.print(F(" FAULT")) : Serial.print(F(" FAULTS"));
356-
MCP9802.setFaultQueue(fqParams[i]);
356+
mcp9802.setFaultQueue(fqParams[i]);
357357
Serial.print(F("...DONE\n"));
358358
testGetFaultQueue();
359359
quickDelay();
@@ -366,7 +366,7 @@ void testSetResolution() {
366366
Serial.print(F("\nSetting Resolution to:\t"));
367367
resParams[i] == 0 ? Serial.print(F("9")) : Serial.print((resParams[i] >> 5) + 9);
368368
Serial.print(F("-BIT"));
369-
MCP9802.setResolution(resParams[i]);
369+
mcp9802.setResolution(resParams[i]);
370370
Serial.print(F("...DONE\n"));
371371
testGetResolution();
372372
quickDelay();
@@ -378,7 +378,7 @@ void testSetConMode() {
378378
for (byte i=0; i<2; i++) {
379379
Serial.print(F("\nSetting Conversion Mode to: "));
380380
i ? Serial.print("CONTINUOUS") : Serial.print("SINGLE-SHOT");
381-
MCP9802.setConMode(conModeParams[i]);
381+
mcp9802.setConMode(conModeParams[i]);
382382
Serial.print(F("...DONE\n"));
383383
testGetConMode();
384384
quickDelay();
@@ -390,7 +390,7 @@ void testSetTempUnit() {
390390
for (byte i=0; i<2; i++) {
391391
Serial.print(F("\nSetting Temperature Unit to: "));
392392
i ? Serial.print("CELSIUS") : Serial.print("FAHRENHEIT");
393-
MCP9802.setTempUnit(tempUnitParams[i]);
393+
mcp9802.setTempUnit(tempUnitParams[i]);
394394
Serial.print(F("...DONE\n"));
395395
testGetTempUnit();
396396
quickDelay();
@@ -408,27 +408,27 @@ void testSetHyst() {
408408
float hystVal[5] = { 57, -32, 43.5, 21.1, -11.6 };
409409
for (byte i=0; i<2 ; i++) {
410410
Serial.print(F("\nSETTING HYSTERESIS ("));
411-
Serial.print(MCP9802.getTempUnit() ? "FAHRENHEIT)\n" : "CELSIUS)\n");
411+
Serial.print(mcp9802.getTempUnit() ? "FAHRENHEIT)\n" : "CELSIUS)\n");
412412
for (byte j=0; j<5; j++) {
413413
valStr = "\nCurrent Hysteresis:\t";
414-
valStr += String(MCP9802.getHyst(), 4);
415-
valStr += (MCP9802.getTempUnit() ? "F" : "C");
414+
valStr += String(mcp9802.getHyst(), 4);
415+
valStr += (mcp9802.getTempUnit() ? "F" : "C");
416416
Serial.print(valStr);
417417
valStr = "\n\nSetting Hysteresis to:\t";
418418
valStr += String(hystVal[j], 4);
419-
MCP9802.setHyst(hystVal[j]);
420-
valStr += (MCP9802.getTempUnit() ? "F" : "C");
419+
mcp9802.setHyst(hystVal[j]);
420+
valStr += (mcp9802.getTempUnit() ? "F" : "C");
421421
valStr += ("...DONE\n");
422422
Serial.print(valStr);
423423
}
424424
valStr = "\nCurrent Hysteresis:\t";
425-
valStr += String(MCP9802.getHyst(), 4);
426-
valStr += (MCP9802.getTempUnit() ? "F" : "C");
425+
valStr += String(mcp9802.getHyst(), 4);
426+
valStr += (mcp9802.getTempUnit() ? "F" : "C");
427427
Serial.print(valStr);
428428
Serial.print(F("\n"));
429-
MCP9802.setTempUnit(MCP9802.getTempUnit() ? CELSIUS : FAHRENHEIT);
429+
mcp9802.setTempUnit(mcp9802.getTempUnit() ? CELSIUS : FAHRENHEIT);
430430
}
431-
MCP9802.setHyst(DEFAULT_HYST);
431+
mcp9802.setHyst(DEFAULT_HYST);
432432
quickDelay();
433433
}
434434

@@ -437,67 +437,67 @@ void testSetLimit() {
437437
float limitVal[5] = { 64, -16, 35.5, 17.8, -24.1 };
438438
for (byte i=0; i<2 ; i++) {
439439
Serial.print(F("\nSETTING LIMIT ("));
440-
Serial.print(MCP9802.getTempUnit() ? "FAHRENHEIT)\n" : "CELSIUS)\n");
440+
Serial.print(mcp9802.getTempUnit() ? "FAHRENHEIT)\n" : "CELSIUS)\n");
441441
for (byte j=0; j<5; j++) {
442442
valStr = "\nCurrent Limit:\t\t";
443-
valStr += String(MCP9802.getLimit(), 4);
444-
valStr += (MCP9802.getTempUnit() ? "F" : "C");
443+
valStr += String(mcp9802.getLimit(), 4);
444+
valStr += (mcp9802.getTempUnit() ? "F" : "C");
445445
Serial.print(valStr);
446446
valStr = "\n\nSetting Limit to:\t";
447447
valStr += String(limitVal[j], 4);
448-
MCP9802.setLimit(limitVal[j]);
449-
valStr += (MCP9802.getTempUnit() ? "F" : "C");
448+
mcp9802.setLimit(limitVal[j]);
449+
valStr += (mcp9802.getTempUnit() ? "F" : "C");
450450
valStr += ("...DONE\n");
451451
Serial.print(valStr);
452452
}
453453
valStr = "\nCurrent Limit:\t\t";
454-
valStr += String(MCP9802.getLimit(), 4);
455-
valStr += (MCP9802.getTempUnit() ? "F" : "C");
454+
valStr += String(mcp9802.getLimit(), 4);
455+
valStr += (mcp9802.getTempUnit() ? "F" : "C");
456456
Serial.print(valStr);
457457
Serial.print(F("\n"));
458-
MCP9802.setTempUnit(MCP9802.getTempUnit() ? CELSIUS : FAHRENHEIT);
458+
mcp9802.setTempUnit(mcp9802.getTempUnit() ? CELSIUS : FAHRENHEIT);
459459
}
460-
MCP9802.setLimit(DEFAULT_LIMIT);
460+
mcp9802.setLimit(DEFAULT_LIMIT);
461461
quickDelay();
462462
}
463463

464464
void testSingleConversion() {
465465
Serial.print(F("\nChanging Conversion Mode to SINGLE-SHOT..."));
466-
MCP9802.setConMode(SINGLE);
466+
mcp9802.setConMode(SINGLE);
467467
Serial.print(F("DONE\n"));
468468
testGetSingleConC();
469-
MCP9802.setTempUnit(FAHRENHEIT);
469+
mcp9802.setTempUnit(FAHRENHEIT);
470470
testGetSingleConF();
471-
MCP9802.setTempUnit(CELSIUS);
471+
mcp9802.setTempUnit(CELSIUS);
472472
quickDelay();
473473
}
474474

475475
void testGetSingleConC() {
476-
tempC = MCP9802.getTemp();
476+
tempC = mcp9802.getTemp();
477477
Serial.print(F("\nTemp (C): \t"));
478478
Serial.println(tempC, 4);
479479
quickDelay();
480480
}
481481

482482
void testGetSingleConF() {
483-
tempF = MCP9802.getTemp();
483+
tempF = mcp9802.getTemp();
484484
Serial.print(F("\nTemp (F): \t"));
485485
Serial.println(tempF, 4);
486486
quickDelay();
487487
}
488488

489489
void testAlertFunctionality() {
490-
MCP9802.reset();
490+
mcp9802.reset();
491491
for (byte i=0; i< 2; i++) {
492-
MCP9802.setAlertMode(alertModeParams[i]);
492+
mcp9802.setAlertMode(alertModeParams[i]);
493493
Serial.print(F("\nTESTING ACTIVE-"));
494494
Serial.print(i ? "HIGH" : "LOW");
495495
Serial.print(F(" SETTINGS\n\nInitial Conditions:\n"));
496496
testGetTempC();
497497
quickDelay();
498-
MCP9802.setLimit(tempC + 20);
498+
mcp9802.setLimit(tempC + 20);
499499
quickDelay();
500-
MCP9802.setHyst(tempC + 10);
500+
mcp9802.setHyst(tempC + 10);
501501
quickDelay();
502502
testGetLimitC();
503503
testGetHystC();
@@ -512,18 +512,18 @@ void testAlertFunctionality() {
512512
testAlertState(alertModeParams[i], ON);
513513
Serial.print(F("\nVerifying Alert Presistance after Entering Shut-Down Mode\n"));
514514
Serial.print(F("\nSwitching Device to Shut-Down Mode..."));
515-
MCP9802.setConMode(SINGLE);
515+
mcp9802.setConMode(SINGLE);
516516
Serial.print(F("DONE\n"));
517517
testAlertState(alertModeParams[i], ON);
518518
Serial.print(F("\nSimulating Return to Normal Conditions..."));
519-
MCP9802.setConMode(CONT);
520-
MCP9802.setLimit(tempC + 20);
521-
MCP9802.setHyst(tempC + 10);
519+
mcp9802.setConMode(CONT);
520+
mcp9802.setLimit(tempC + 20);
521+
mcp9802.setHyst(tempC + 10);
522522
Serial.print(F("DONE\n"));
523523
testAlertState(alertModeParams[i], OFF);
524524
if (!i) printDivider();
525525
}
526-
MCP9802.reset();
526+
mcp9802.reset();
527527
}
528528

529529
void testAlertState(alert_mode_t alertMode, alert_state_t alertState) {
@@ -537,27 +537,27 @@ void testAlertState(alert_mode_t alertMode, alert_state_t alertState) {
537537

538538
void testSimulateTestConditions() {
539539
Serial.print(F("\nSimulating Alert Conditions..."));
540-
MCP9802.setLimit(tempC - 10);
541-
MCP9802.setHyst(tempC - 20);
540+
mcp9802.setLimit(tempC - 10);
541+
mcp9802.setHyst(tempC - 20);
542542
Serial.print(F("DONE\n"));
543543
}
544544

545545
void testReset() {
546546
Serial.print(F("\nCurrent Settings:\n"));
547547
testGetConfigData();
548548
Serial.print(F("\nCreating New Settings..."));
549-
MCP9802.setAlertType(INT);
550-
MCP9802.setAlertMode(ACTIVE_HIGH);
551-
MCP9802.setFaultQueue(FQ4);
552-
MCP9802.setResolution(RES_11);
553-
MCP9802.setConMode(SINGLE);
554-
MCP9802.setTempUnit(FAHRENHEIT);
555-
MCP9802.setLimit(52.5);
556-
MCP9802.setHyst(49.5);
549+
mcp9802.setAlertType(INT);
550+
mcp9802.setAlertMode(ACTIVE_HIGH);
551+
mcp9802.setFaultQueue(FQ4);
552+
mcp9802.setResolution(RES_11);
553+
mcp9802.setConMode(SINGLE);
554+
mcp9802.setTempUnit(FAHRENHEIT);
555+
mcp9802.setLimit(52.5);
556+
mcp9802.setHyst(49.5);
557557
Serial.print(F("DONE\n\nCurrent Settings:\n"));
558558
testGetConfigData();
559559
Serial.print(F("\nResetting Device to Default Settings..."));
560-
MCP9802.reset();
560+
mcp9802.reset();
561561
Serial.print(F("DONE\n\nCurrent Settings:\n"));
562562
testGetConfigData();
563563
}

‎library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "MCP9802",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"frameworks": "AVR",
55
"license": "MIT",
66
"keywords": "MCP9802, SENSOR, TEMPERATURE, I2C, 12-BIT, HYSTERESIS",

‎library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MCP9802
2-
version=1.3.0
2+
version=1.4.0
33
author=Nadav Matalon <nadav.matalon@gmail.com>
44
maintainer=Nadav Matalon <nadav.matalon@gmail.com>
55
sentence=MCP9802 Driver (12-BIT Temperature Sensor with I2C Interface)

‎utility/MCP9802ComStr.h

+40-29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
instead of the String class to further reduce memory footprint. For this purpose, added PString.h
1616
PString.cpp files to /utility folder. In addition added "I2C STATUS" (CONNECTED / NOT CONNECTED)
1717
field to device information string (9.10.16)
18+
Ver. 1.4.0 - Added namespaces to prevent conflicts with other libraries (15.10.16)
1819
1920
*===============================================================================================================*
2021
LICENSE
@@ -40,45 +41,55 @@
4041
4142
*==============================================================================================================*/
4243

44+
#if 1
45+
__asm volatile ("nop");
46+
#endif
47+
4348
#ifndef MCP9802ComStr_h
4449
#define MCP9802ComStr_h
4550

4651
#include <avr/pgmspace.h>
4752

48-
const byte COM_BUFFER_SIZE = 60;
49-
const int NUM_OF_COM_CODES = 8;
50-
51-
const char comMsg0[] PROGMEM = "Success";
52-
const char comMsg1[] PROGMEM = "Error Code #1: I2C Buffer overflow";
53-
const char comMsg2[] PROGMEM = "Error Code #2: Address sent, NACK received";
54-
const char comMsg3[] PROGMEM = "Error Code #3: Data send, NACK received";
55-
const char comMsg4[] PROGMEM = "Error Code #4: Other error (bus error, etc.)";
56-
const char comMsg5[] PROGMEM = "Error Code #5: Timed-out while trying to become Bus Master";
57-
const char comMsg6[] PROGMEM = "Error Code #6: Timed-out while waiting for data to be sent";
58-
const char comMsgDefault[] PROGMEM = "Error Code #%d: Unlisted error";
59-
60-
const char * const comCodes[NUM_OF_COM_CODES] PROGMEM = {
61-
comMsg0,
62-
comMsg1,
63-
comMsg2,
64-
comMsg3,
65-
comMsg4,
66-
comMsg5,
67-
comMsg6,
68-
comMsgDefault
69-
};
53+
namespace Mcp9802 {
54+
55+
const byte COM_BUFFER_SIZE = 60;
56+
const int NUM_OF_COM_CODES = 8;
57+
58+
const char comMsg0[] PROGMEM = "Success";
59+
const char comMsg1[] PROGMEM = "Error Code #1: I2C Buffer overflow";
60+
const char comMsg2[] PROGMEM = "Error Code #2: Address sent, NACK received";
61+
const char comMsg3[] PROGMEM = "Error Code #3: Data send, NACK received";
62+
const char comMsg4[] PROGMEM = "Error Code #4: Other error (bus error, etc.)";
63+
const char comMsg5[] PROGMEM = "Error Code #5: Timed-out while trying to become Bus Master";
64+
const char comMsg6[] PROGMEM = "Error Code #6: Timed-out while waiting for data to be sent";
65+
const char comMsgDefault[] PROGMEM = "Error Code #%d: Unlisted error";
66+
67+
const char * const comCodes[NUM_OF_COM_CODES] PROGMEM = {
68+
comMsg0,
69+
comMsg1,
70+
comMsg2,
71+
comMsg3,
72+
comMsg4,
73+
comMsg5,
74+
comMsg6,
75+
comMsgDefault
76+
};
7077

7178
/*==============================================================================================================*
7279
GET I2C COMMUNICATIONS RESULT MESSAGE (PRINTABLE FORMAT)
7380
*==============================================================================================================*/
7481

75-
PString MCP9802ComStr(const MCP9802& devParams) {
76-
char devComBuffer[COM_BUFFER_SIZE];
77-
PString comStr(devComBuffer, COM_BUFFER_SIZE);
78-
char comCodeResult = devParams._comBuffer;
79-
char * ptr = (char *) pgm_read_word(&comCodes[comCodeResult]);
80-
snprintf_P(devComBuffer, COM_BUFFER_SIZE, ptr, comCodeResult);
81-
return comStr;
82+
MCP9802_PString MCP9802ComStr(const MCP9802& devParams) {
83+
char devComBuffer[COM_BUFFER_SIZE];
84+
MCP9802_PString comStr(devComBuffer, COM_BUFFER_SIZE);
85+
char comCodeResult = devParams._comBuffer;
86+
char * ptr = (char *) pgm_read_word(&comCodes[comCodeResult]);
87+
snprintf_P(devComBuffer, COM_BUFFER_SIZE, ptr, comCodeResult);
88+
return comStr;
89+
}
90+
8291
}
8392

93+
using namespace Mcp9802;
94+
8495
#endif

‎utility/MCP9802InfoStr.h

+88-77
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
instead of the String class to further reduce memory footprint. To this end, added PString.h and
1616
PString.cpp files to /utility folder. In addition added "I2C STATUS" (CONNECTED / NOT CONNECTED)
1717
field to device information string (9.10.16)
18-
18+
Ver. 1.4.0 - Added namespaces to prevent conflicts with other libraries (15.10.16)
19+
1920
*===============================================================================================================*
2021
LICENSE
2122
*===============================================================================================================*
@@ -40,98 +41,108 @@
4041
4142
*==============================================================================================================*/
4243

44+
#if 1
45+
__asm volatile ("nop");
46+
#endif
47+
4348
#ifndef MCP9802InfoStr_h
4449
#define MCP9802InfoStr_h
4550

4651
#include <avr/pgmspace.h>
4752
#include "utility/MCP9802ComStr.h"
4853

49-
const int INFO_BUFFER_SIZE = 60;
50-
const byte NUM_OF_INFO_STR = 15;
54+
namespace Mcp9802 {
5155

52-
const char infoStr0[] PROGMEM = "\nMCP9802 DEVICE INFORMATION";
53-
const char infoStr1[] PROGMEM = "\n--------------------------";
54-
const char infoStr2[] PROGMEM = "\nI2C ADDRESS:\t %d (%#X)";
55-
const char infoStr3[] PROGMEM = "\nI2C COM STATUS:\t %sCONNECTED";
56-
const char infoStr4[] PROGMEM = "\nCONFIG BYTE:\t B%d%d%d%d%d%d%d%d";
57-
const char infoStr5[] PROGMEM = "\nDEVICE MODE:\t %s";
58-
const char infoStr6[] PROGMEM = "\nALERT TYPE:\t %s";
59-
const char infoStr7[] PROGMEM = "\nALERT MODE:\t ACTIVE-%s";
60-
const char infoStr8[] PROGMEM = "\nFAULT-QUEUE:\t %d FAULT%s";
61-
const char infoStr9[] PROGMEM = "\nRESOLUTION:\t %d-BIT";
62-
const char infoStr10[] PROGMEM = "\nCONVERSION MODE: %s";
63-
const char infoStr11[] PROGMEM = "\nDEGREES UNIT:\t %s";
64-
const char infoStr12[] PROGMEM = "\nHYSTERESIS:\t %d.%d%s";
65-
const char infoStr13[] PROGMEM = "\nLIMIT:\t\t %d.%d%s\n";
66-
const char errStr[] PROGMEM = "\nI2C ERROR:\t ";
56+
const int INFO_BUFFER_SIZE = 60;
57+
const byte NUM_OF_INFO_STR = 15;
6758

68-
const char * const infoStrs[NUM_OF_INFO_STR] PROGMEM = {
69-
infoStr0,
70-
infoStr1,
71-
infoStr2,
72-
infoStr3,
73-
infoStr4,
74-
infoStr5,
75-
infoStr6,
76-
infoStr7,
77-
infoStr8,
78-
infoStr9,
79-
infoStr10,
80-
infoStr11,
81-
infoStr12,
82-
infoStr13,
83-
errStr
84-
};
59+
const char infoStr0[] PROGMEM = "\nMCP9802 DEVICE INFORMATION";
60+
const char infoStr1[] PROGMEM = "\n--------------------------";
61+
const char infoStr2[] PROGMEM = "\nI2C ADDRESS:\t %d (%#X)";
62+
const char infoStr3[] PROGMEM = "\nI2C COM STATUS:\t %sCONNECTED";
63+
const char infoStr4[] PROGMEM = "\nCONFIG BYTE:\t B%d%d%d%d%d%d%d%d";
64+
const char infoStr5[] PROGMEM = "\nDEVICE MODE:\t %s";
65+
const char infoStr6[] PROGMEM = "\nALERT TYPE:\t %s";
66+
const char infoStr7[] PROGMEM = "\nALERT MODE:\t ACTIVE-%s";
67+
const char infoStr8[] PROGMEM = "\nFAULT-QUEUE:\t %d FAULT%s";
68+
const char infoStr9[] PROGMEM = "\nRESOLUTION:\t %d-BIT";
69+
const char infoStr10[] PROGMEM = "\nCONVERSION MODE: %s";
70+
const char infoStr11[] PROGMEM = "\nDEGREES UNIT:\t %s";
71+
const char infoStr12[] PROGMEM = "\nHYSTERESIS:\t %d.%d%s";
72+
const char infoStr13[] PROGMEM = "\nLIMIT:\t\t %d.%d%s\n";
73+
const char errStr[] PROGMEM = "\nI2C ERROR:\t ";
74+
75+
const char * const infoStrs[NUM_OF_INFO_STR] PROGMEM = {
76+
infoStr0,
77+
infoStr1,
78+
infoStr2,
79+
infoStr3,
80+
infoStr4,
81+
infoStr5,
82+
infoStr6,
83+
infoStr7,
84+
infoStr8,
85+
infoStr9,
86+
infoStr10,
87+
infoStr11,
88+
infoStr12,
89+
infoStr13,
90+
errStr
91+
};
8592

8693
/*==============================================================================================================*
8794
GENERATE DEVICE INFORMATION STRING (PRINTABLE FORMAT)
8895
*==============================================================================================================*/
8996

90-
PString MCP9802InfoStr(const MCP9802& devParams) {
91-
char * ptr;
92-
char strBuffer[338];
93-
int devAddr = devParams._devAddr;
94-
MCP9802 mcp9802(devAddr);
95-
byte comErrCode = mcp9802.ping();
96-
PString resultStr(strBuffer, sizeof(strBuffer));
97-
char devInfoBuffer[INFO_BUFFER_SIZE];
98-
byte tempUnit = devParams._tempUnit;
99-
mcp9802.setTempUnit(tempUnit ? FAHRENHEIT : CELSIUS);
100-
byte config = devParams._singleConfig ? devParams._singleConfig : mcp9802.getConfig();
101-
byte fqVal = (config & 0x18) >> 2;
102-
float hyst = mcp9802.getHyst();
103-
float limit = mcp9802.getLimit();
104-
for (byte i=0; i<4; i++) {
105-
ptr = (char *) pgm_read_word(&infoStrs[i]);
106-
if (i < 2) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr);
107-
if (i == 2) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, devAddr, devAddr);
108-
if (i == 3) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (comErrCode ? "NOT " : ""));
109-
resultStr += devInfoBuffer;
110-
}
111-
if (!comErrCode) {
112-
for (byte i=4; i<(NUM_OF_INFO_STR - 1); i++) {
97+
MCP9802_PString MCP9802InfoStr(const MCP9802& devParams) {
98+
char * ptr;
99+
char strBuffer[338];
100+
int devAddr = devParams._devAddr;
101+
MCP9802 mcp9802(devAddr);
102+
byte comErrCode = mcp9802.ping();
103+
MCP9802_PString resultStr(strBuffer, sizeof(strBuffer));
104+
char devInfoBuffer[INFO_BUFFER_SIZE];
105+
byte tempUnit = devParams._tempUnit;
106+
mcp9802.setTempUnit(tempUnit ? FAHRENHEIT : CELSIUS);
107+
byte config = devParams._singleConfig ? devParams._singleConfig : mcp9802.getConfig();
108+
byte fqVal = (config & 0x18) >> 2;
109+
float hyst = mcp9802.getHyst();
110+
float limit = mcp9802.getLimit();
111+
for (byte i=0; i<4; i++) {
113112
ptr = (char *) pgm_read_word(&infoStrs[i]);
114-
if (i == 4) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (config >> 7)&1, (config >> 6)&1, (config >> 5)&1,
115-
(config >> 4)&1, (config >> 3)&1, (config >> 2)&1, (config >> 1)&1, config&1);
116-
if (i == 5) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (config ? "STANDBY" : "ON"));
117-
if (i == 6) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, ((config >> 1)&1 ? "INTERRUPT": "COMPARATOR"));
118-
if (i == 7) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, ((config >> 2)&1 ? "HIGH": "LOW"));
119-
if (i == 8) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (fqVal ? fqVal : 1), (fqVal ? "S" : ""));
120-
if (i == 9) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (((config & 0x60) >> 5) + 9));
121-
if (i == 10) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (config ? "SINGLE-SHOT" : "CONTINUOUS"));
122-
if (i == 11) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (tempUnit ? "FAHRENHEIT" : "CELSIUS"));
123-
if (i == 12) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (int)hyst, abs((int)(hyst * 10) % 10), (tempUnit ? "F" : "C"));
124-
if (i == 13) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (int)limit, abs((int)(limit * 10) % 10), (tempUnit ? "F" : "C"));
113+
if (i < 2) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr);
114+
if (i == 2) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, devAddr, devAddr);
115+
if (i == 3) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (comErrCode ? "NOT " : ""));
125116
resultStr += devInfoBuffer;
126117
}
127-
} else {
128-
snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, (char *) pgm_read_word(&infoStrs[14]));
129-
resultStr += devInfoBuffer;
130-
snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, (char *) pgm_read_word(&comCodes[comErrCode]));
131-
resultStr += devInfoBuffer;
132-
resultStr += "\n";
118+
if (!comErrCode) {
119+
for (byte i=4; i<(NUM_OF_INFO_STR - 1); i++) {
120+
ptr = (char *) pgm_read_word(&infoStrs[i]);
121+
if (i == 4) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (config >> 7)&1, (config >> 6)&1, (config >> 5)&1,
122+
(config >> 4)&1, (config >> 3)&1, (config >> 2)&1, (config >> 1)&1, config&1);
123+
if (i == 5) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (config ? "STANDBY" : "ON"));
124+
if (i == 6) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, ((config >> 1)&1 ? "INTERRUPT": "COMPARATOR"));
125+
if (i == 7) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, ((config >> 2)&1 ? "HIGH": "LOW"));
126+
if (i == 8) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (fqVal ? fqVal : 1), (fqVal ? "S" : ""));
127+
if (i == 9) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (((config & 0x60) >> 5) + 9));
128+
if (i == 10) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (config ? "SINGLE-SHOT" : "CONTINUOUS"));
129+
if (i == 11) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (tempUnit ? "FAHRENHEIT" : "CELSIUS"));
130+
if (i == 12) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (int)hyst, abs((int)(hyst * 10) % 10), (tempUnit ? "F" : "C"));
131+
if (i == 13) snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, ptr, (int)limit, abs((int)(limit * 10) % 10), (tempUnit ? "F" : "C"));
132+
resultStr += devInfoBuffer;
133+
}
134+
} else {
135+
snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, (char *) pgm_read_word(&infoStrs[14]));
136+
resultStr += devInfoBuffer;
137+
snprintf_P(devInfoBuffer, INFO_BUFFER_SIZE, (char *) pgm_read_word(&comCodes[comErrCode]));
138+
resultStr += devInfoBuffer;
139+
resultStr += "\n";
140+
}
141+
return resultStr;
133142
}
134-
return resultStr;
143+
135144
}
136145

137-
#endif
146+
using namespace Mcp9802;
147+
148+
#endif

‎utility/MCP9802_PString.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
MCP9802_PString.cpp - Lightweight printable string class
3+
Original Code by: Mikal Hart (http://arduiniana.org/libraries/PString/)
4+
*/
5+
6+
#if 1
7+
__asm volatile ("nop");
8+
#endif
9+
10+
#include "MCP9802_PString.h"
11+
12+
void MCP9802_PString::begin() {
13+
_cur = _buf;
14+
if (_size > 0)
15+
_buf[0] = '\0';
16+
}
17+
18+
#if defined(ARDUINO) && ARDUINO >= 100
19+
size_t MCP9802_PString::write(uint8_t b)
20+
#else
21+
void MCP9802_PString::write(uint8_t b)
22+
#endif
23+
{
24+
if (_cur + 1 < _buf + _size) {
25+
*_cur++ = (char)b;
26+
*_cur = '\0';
27+
#if defined(ARDUINO) && ARDUINO >= 100
28+
return 1;
29+
#endif
30+
}
31+
#if defined(ARDUINO) && ARDUINO >= 100
32+
return 0;
33+
#endif
34+
}
35+
36+
int MCP9802_PString::format(char *str, ...) {
37+
va_list argptr;
38+
va_start(argptr, str);
39+
int ret = vsnprintf(_cur, _size - (_cur - _buf), str, argptr);
40+
if (_size)
41+
while (*_cur)
42+
++_cur;
43+
return ret;
44+
}

‎utility/PString.h ‎utility/MCP9802_PString.h

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/*
2-
PString.h - Lightweight printable string class
3-
Code by: Mikal Hart (http://arduiniana.org/libraries/PString/)
2+
MCP9802_PString.h - Lightweight printable string class
3+
Original Code by: Mikal Hart (http://arduiniana.org/libraries/PString/)
44
*/
55

6-
#ifndef PString_h
7-
#define PString_h
6+
#if 1
7+
__asm volatile ("nop");
8+
#endif
9+
10+
#ifndef MCP9802_PString_h
11+
#define MCP9802_PString_h
812

913
#include "Print.h"
1014
#include <stdarg.h>
1115
#include <stddef.h>
1216
#include <stdio.h>
1317
#include <string.h>
1418

15-
#define PSTRING_LIBRARY_VERSION 3
16-
17-
class PString : public Print {
19+
class MCP9802_PString : public Print {
1820
private:
1921
char *_buf, *_cur;
2022
size_t _size;
@@ -26,17 +28,17 @@ class PString : public Print {
2628
#endif
2729

2830
// Basic constructor requires a preallocated buffer
29-
PString(char *buf, size_t size) : _buf(buf), _size(size) {
31+
MCP9802_PString(char *buf, size_t size) : _buf(buf), _size(size) {
3032
begin();
3133
}
3234

33-
// templated constructors allow inline renderings of this type: PString(buf, size, myfloat[, modifier]);
34-
template<class T> PString(char *buf, size_t size, T arg) : _buf(buf), _size(size) {
35+
// templated constructors allow inline renderings of this type: MCP9802_PString(buf, size, myfloat[, modifier]);
36+
template<class T> MCP9802_PString(char *buf, size_t size, T arg) : _buf(buf), _size(size) {
3537
begin();
3638
print(arg);
3739
}
3840

39-
template<class T> PString(char *buf, size_t size, T arg, int modifier) : _buf(buf), _size(size) {
41+
template<class T> MCP9802_PString(char *buf, size_t size, T arg, int modifier) : _buf(buf), _size(size) {
4042
begin();
4143
print(arg, modifier);
4244
}
@@ -65,14 +67,14 @@ class PString : public Print {
6567
void begin();
6668

6769
// This function allows assignment to an arbitrary scalar value like str = myfloat;
68-
template<class T> inline PString &operator =(T arg) {
70+
template<class T> inline MCP9802_PString &operator =(T arg) {
6971
begin();
7072
print(arg);
7173
return *this;
7274
}
7375

7476
// Concatenation str += myfloat;
75-
template<class T> inline PString &operator +=(T arg) {
77+
template<class T> inline MCP9802_PString &operator +=(T arg) {
7678
print(arg);
7779
return *this;
7880
}
@@ -81,4 +83,4 @@ class PString : public Print {
8183
int format(char *str, ...);
8284
};
8385

84-
#endif
86+
#endif

‎utility/PString.cpp

-41
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.