Skip to content

Commit 9425ef6

Browse files
committed
Added begin() and end() functions to handle the TwoWire library.
1 parent bc7b13e commit 9425ef6

File tree

5 files changed

+128
-105
lines changed

5 files changed

+128
-105
lines changed

examples/BasicUsage/BasicUsage.ino

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,33 @@
88
// Licensed under the MIT license, see the LICENSE.txt file.
99
//
1010
//---------------------------------------------------------------------------------------------
11+
// 2017-04-19 Added begin() and end() functions for Wire handling.
12+
// 2017-04-19 Fixed a code merge problem
1113
//
1214
////////////////////////////////////////////////////////////////////////////////////////////////
1315
//
1416
// Includes
1517
//
16-
#include <Wire.h>
1718
#include <M2M_LM75A.h>
1819

1920
////////////////////////////////////////////////////////////////////////////////////////////////
2021
//
2122
// Global variables
2223
//
23-
LM75A lm75a;
24+
M2M_LM75A lm75a;
2425

2526
////////////////////////////////////////////////////////////////////////////////////////////////
2627
//
2728
// Setup
2829
//
2930
void setup()
3031
{
31-
Wire.begin();
32+
lm75a.begin();
33+
while (!Serial); // Leonardo: wait for serial monitor
3234
Serial.begin(115200);
33-
while (!Serial)
34-
; // Leonardo: wait for serial monitor
35-
Serial.println("M2M_LM75A Arduino library basic usage");
35+
Serial.println(F("M2M_LM75A - Basic usage"));
36+
Serial.println(F("==========================================="));
37+
Serial.println("");
3638
}
3739

3840
////////////////////////////////////////////////////////////////////////////////////////////////
@@ -42,30 +44,36 @@ void setup()
4244
void loop()
4345
{
4446
// Temperature
45-
Serial.print("Temperature in Celsius: ");
47+
Serial.print(F("Temperature in Celsius: "));
4648
Serial.print(lm75a.getTemperature());
47-
Serial.println(" *C");
48-
Serial.print("Temperature in Farenheit: ");
49-
Serial.println(lm75a.getTemperatureInFarenheit());
50-
Serial.print("Hysteris temperature: ");
51-
Serial.print("Hysteris temperature: ");
52-
Serial.print("Hysteris temperature: ");
53-
Serial.print(" *F");
49+
Serial.println(F(" *C"));
50+
51+
Serial.print(F("Temperature in Farenheit: "));
52+
Serial.print(lm75a.getTemperatureInFarenheit());
53+
Serial.println(F(" *F"));
54+
55+
Serial.print(F("Hysteris temperature: "));
5456
Serial.print(lm75a.getHysterisisTemperature());
55-
Serial.println(" *C");
56-
Serial.print("OS trip temperature: ");
57+
Serial.println(F(" *C"));
58+
59+
Serial.print(F("OS trip temperature: "));
5760
Serial.print(lm75a.getOSTripTemperature());
58-
Serial.println(" *C");
61+
Serial.println(F(" *C"));
5962

6063
// Shutdown/Wake up
61-
Serial.println("Shutting down");
64+
Serial.println(F("Shutting down"));
6265
lm75a.shutdown();
63-
Serial.print("Is shutdown: ");
66+
67+
Serial.print(F("Is shutdown: "));
6468
Serial.println(lm75a.isShutdown());
6569
delay(5000);
66-
Serial.println("Waking up");
70+
71+
Serial.println(F("Waking up"));
6772
lm75a.wakeup();
68-
Serial.print("Is shutdown: ");
73+
Serial.print(F("Is shutdown: "));
6974
Serial.println(lm75a.isShutdown());
70-
delay(5000);
71-
}
75+
delay(1000);
76+
Serial.println(F(""));
77+
Serial.println(F("==========================================="));
78+
Serial.println(F(""));
79+
}

examples/LibraryTest/LibraryTest.ino

Lines changed: 76 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//
1414
// Includes
1515
//
16-
#include <Wire.h>
1716
#include <M2M_LM75A.h>
1817

1918
////////////////////////////////////////////////////////////////////////////////////////////////
@@ -39,10 +38,9 @@ DeviceMode newDeviceMode = DeviceMode::DEVICE_MODE_INTERRUPT;
3938
//
4039
void setup()
4140
{
42-
Wire.begin();
41+
lm75a.begin();
42+
while (!Serial); // Leonardo: wait for serial monitor
4343
Serial.begin(115200);
44-
while (!Serial)
45-
; // Leonardo: wait for serial monitor
4644
Serial.println("Running M2M_LM75A Arduino library full test");
4745
Serial.println("===========================================");
4846
Serial.println("");
@@ -137,11 +135,11 @@ void checkTemperatureResult(const char* caption, const float value)
137135
Serial.print(": ");
138136
if (fail)
139137
{
140-
Serial.print("LM75A_INVALID_TEMPERATURE");
138+
Serial.print("LM75A_INVALID_TEMPERATURE");
141139
}
142140
else
143141
{
144-
Serial.print(value);
142+
Serial.print(value);
145143
}
146144
Serial.print(" - ");
147145
Serial.println(fail ? "FAIL" : "OK");
@@ -156,14 +154,14 @@ void checkTemperatureValue(const char* caption, const float value, const float e
156154
Serial.print(" - ");
157155
if (fail)
158156
{
159-
testResult = false;
160-
Serial.print("FAIL (");
161-
Serial.print(expected);
162-
Serial.println(")");
157+
testResult = false;
158+
Serial.print("FAIL (");
159+
Serial.print(expected);
160+
Serial.println(")");
163161
}
164162
else
165163
{
166-
Serial.println("OK");
164+
Serial.println("OK");
167165
}
168166
}
169167

@@ -176,14 +174,14 @@ void check8BitValue(const char* caption, const uint8_t value, const uint8_t expe
176174
Serial.print(" - ");
177175
if (fail)
178176
{
179-
testResult = false;
180-
Serial.print("FAIL (");
181-
Serial.print(expected);
182-
Serial.println(")");
177+
testResult = false;
178+
Serial.print("FAIL (");
179+
Serial.print(expected);
180+
Serial.println(")");
183181
}
184182
else
185183
{
186-
Serial.println("OK");
184+
Serial.println("OK");
187185
}
188186
}
189187

@@ -196,14 +194,14 @@ void checkFaultQueueValue(const char* caption, const FaultQueueValue value, cons
196194
Serial.print(" - ");
197195
if (fail)
198196
{
199-
testResult = false;
200-
Serial.print("FAIL (");
201-
Serial.print(getFaultQueueValueString(expected));
202-
Serial.println(")");
197+
testResult = false;
198+
Serial.print("FAIL (");
199+
Serial.print(getFaultQueueValueString(expected));
200+
Serial.println(")");
203201
}
204202
else
205203
{
206-
Serial.println("OK");
204+
Serial.println("OK");
207205
}
208206
}
209207

@@ -216,14 +214,14 @@ void checkOsPolarityValue(const char* caption, const OsPolarity value, const OsP
216214
Serial.print(" - ");
217215
if (fail)
218216
{
219-
testResult = false;
220-
Serial.print("FAIL (");
221-
Serial.print(getOsPolarityString(expected));
222-
Serial.println(")");
217+
testResult = false;
218+
Serial.print("FAIL (");
219+
Serial.print(getOsPolarityString(expected));
220+
Serial.println(")");
223221
}
224222
else
225223
{
226-
Serial.println("OK");
224+
Serial.println("OK");
227225
}
228226
}
229227

@@ -236,77 +234,77 @@ void checkDeviceModeValue(const char* caption, const DeviceMode value, const Dev
236234
Serial.print(" - ");
237235
if (fail)
238236
{
239-
testResult = false;
240-
Serial.print("FAIL (");
241-
Serial.print(getDeviceModeString(expected));
242-
Serial.println(")");
237+
testResult = false;
238+
Serial.print("FAIL (");
239+
Serial.print(getDeviceModeString(expected));
240+
Serial.println(")");
243241
}
244242
else
245243
{
246-
Serial.println("OK");
244+
Serial.println("OK");
247245
}
248246
}
249247

250248
void checkTrueValue(const char* caption, const bool value, const bool expected)
251249
{
252-
bool fail = value != expected;
253-
Serial.print(caption);
254-
Serial.print(": ");
255-
Serial.print(value ? "True" : "False");
256-
Serial.print(" - ");
257-
if (fail)
258-
{
259-
testResult = false;
260-
Serial.print("FAIL (");
261-
Serial.print(expected);
262-
Serial.println(")");
263-
}
264-
else
265-
{
266-
Serial.println("OK");
267-
}
250+
bool fail = value != expected;
251+
Serial.print(caption);
252+
Serial.print(": ");
253+
Serial.print(value ? "True" : "False");
254+
Serial.print(" - ");
255+
if (fail)
256+
{
257+
testResult = false;
258+
Serial.print("FAIL (");
259+
Serial.print(expected);
260+
Serial.println(")");
261+
}
262+
else
263+
{
264+
Serial.println("OK");
265+
}
268266
}
269267

270268
const char* getFaultQueueValueString(const FaultQueueValue value)
271269
{
272-
switch (value)
273-
{
274-
case FaultQueueValue::NUMBER_OF_FAULTS_1:
275-
return "NUMBER_OF_FAULTS_1";
276-
case FaultQueueValue::NUMBER_OF_FAULTS_2:
277-
return "NUMBER_OF_FAULTS_2";
278-
case FaultQueueValue::NUMBER_OF_FAULTS_4:
279-
return "NUMBER_OF_FAULTS_4";
280-
case FaultQueueValue::NUMBER_OF_FAULTS_6:
281-
return "NUMBER_OF_FAULTS_4";
282-
default:
283-
return "** ILLEGAL VALUE **";
284-
}
270+
switch (value)
271+
{
272+
case FaultQueueValue::NUMBER_OF_FAULTS_1:
273+
return "NUMBER_OF_FAULTS_1";
274+
case FaultQueueValue::NUMBER_OF_FAULTS_2:
275+
return "NUMBER_OF_FAULTS_2";
276+
case FaultQueueValue::NUMBER_OF_FAULTS_4:
277+
return "NUMBER_OF_FAULTS_4";
278+
case FaultQueueValue::NUMBER_OF_FAULTS_6:
279+
return "NUMBER_OF_FAULTS_4";
280+
default:
281+
return "** ILLEGAL VALUE **";
282+
}
285283
}
286284

287285
const char* getOsPolarityString(const OsPolarity value)
288286
{
289-
switch (value)
290-
{
291-
case OsPolarity::OS_POLARITY_ACTIVEHIGH:
292-
return "OS_POLARITY_ACTIVEHIGH";
293-
case OsPolarity::OS_POLARITY_ACTIVELOW:
294-
return "OS_POLARITY_ACTIVELOW";
295-
default:
296-
return "** ILLEGAL VALUE **";
297-
}
287+
switch (value)
288+
{
289+
case OsPolarity::OS_POLARITY_ACTIVEHIGH:
290+
return "OS_POLARITY_ACTIVEHIGH";
291+
case OsPolarity::OS_POLARITY_ACTIVELOW:
292+
return "OS_POLARITY_ACTIVELOW";
293+
default:
294+
return "** ILLEGAL VALUE **";
295+
}
298296
}
299297

300298
const char* getDeviceModeString(DeviceMode value)
301299
{
302-
switch (value)
303-
{
304-
case DeviceMode::DEVICE_MODE_COMPARATOR:
305-
return "DEVICE_MODE_COMPARATOR";
306-
case DeviceMode::DEVICE_MODE_INTERRUPT:
307-
return "DEVICE_MODE_INTERRUPT";
308-
default:
309-
return "** ILLEGAL VALUE **";
310-
}
300+
switch (value)
301+
{
302+
case DeviceMode::DEVICE_MODE_COMPARATOR:
303+
return "DEVICE_MODE_COMPARATOR";
304+
case DeviceMode::DEVICE_MODE_INTERRUPT:
305+
return "DEVICE_MODE_INTERRUPT";
306+
default:
307+
return "** ILLEGAL VALUE **";
308+
}
311309
}
312310

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
name=M2M Solution LM75A Library
2-
version=1.0.0
1+
name=LM75A Arduino library
2+
version=1.0.1
33
author=M2M Solutions AB
44
maintainer=M2M Solutions AB <info@m2msolutions.se>
5-
sentence=LM75A Arduino library.
5+
sentence=M2M Solutions LM75A Arduino library.
66
paragraph=A library for the LM75A digital temperature sensor and thermal watchdog.
77
category=Sensors
88
url=https://github.com/m2m-solutions/M2M_LM75A

src/M2M_LM75A.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,20 @@ M2M_LM75A::M2M_LM75A(uint8_t i2cAddress)
2727

2828
////////////////////////////////////////////////////////////////////////////////////////////////
2929
//
30-
//// Power management
30+
// Startup/Teardown
31+
//
32+
void M2M_LM75A::begin()
33+
{
34+
Wire.begin();
35+
}
36+
void M2M_LM75A::end()
37+
{
38+
Wire.end();
39+
}
40+
41+
////////////////////////////////////////////////////////////////////////////////////////////////
42+
//
43+
// Power management
3144
//
3245
void M2M_LM75A::shutdown()
3346
{

src/M2M_LM75A.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class M2M_LM75A
6666
M2M_LM75A();
6767
M2M_LM75A(uint8_t address);
6868

69+
// Startup/Teardown
70+
void begin();
71+
void end();
72+
6973
// Power management
7074
void shutdown();
7175
void wakeup();

0 commit comments

Comments
 (0)