Skip to content

Commit d862556

Browse files
committed
+ 0.1.6
+ remove timeout - issue #63 + small refactor
1 parent 02cc2c5 commit d862556

File tree

3 files changed

+25
-36
lines changed

3 files changed

+25
-36
lines changed

libraries/MCP4725/MCP4725.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// FILE: MCP4725.cpp
33
// AUTHOR: Rob Tillaart
4-
// PURPOSE: Simple MCP4725 DAC library for Arduino
5-
// VERSION: 0.1.05
4+
// PURPOSE: Simple MCP4725 DAC (12 bit I2C) library for Arduino
5+
// VERSION: 0.1.6
66
// HISTORY: See MCP4725.cpp
77
// URL:
88
//
@@ -13,12 +13,21 @@
1313
// 0.1.03 - 2013-12-01 added powerDownMode code
1414
// 0.1.04 - 2013-12-04 improved the generalCall code (still experimental)
1515
// 0.1.05 - 2015-03-06 refactoring, stricter interfaces
16+
// 0.1.6 - 2017-04-19 refactor + remove timeout - https://github.com/RobTillaart/Arduino/issues/63
1617
//
1718
// Released to the public domain
1819
//
1920

2021
#include "MCP4725.h"
2122

23+
#if defined(ARDUINO) && ARDUINO >= 100
24+
#define WIRE_WRITE Wire.write
25+
#define WIRE_READ Wire.read
26+
#else
27+
#define WIRE_WRITE Wire.send
28+
#define WIRE_READ Wire.receive
29+
#endif
30+
2231
MCP4725::MCP4725(const uint8_t deviceAddress)
2332
{
2433
_deviceAddress = deviceAddress;
@@ -159,13 +168,8 @@ int MCP4725::writeFastMode(const uint16_t value)
159168
uint8_t h = ((value / 256) & 0x0F); // set C0 = C1 = 0, no PDmode
160169
h = h | (_powerDownMode << 4);
161170
uint8_t l = value & 0xFF;
162-
#if defined(ARDUINO) && ARDUINO >= 100
163-
Wire.write(h);
164-
Wire.write(l);
165-
#else
166-
Wire.send(h);
167-
Wire.send(l);
168-
#endif
171+
WIRE_WRITE(h);
172+
WIRE_WRITE(l);
169173
return Wire.endTransmission();
170174
}
171175

@@ -188,15 +192,9 @@ int MCP4725::writeRegisterMode(const uint16_t value, const uint8_t reg)
188192
uint8_t l = (value & 0x0F) << 4;
189193
Wire.beginTransmission(_deviceAddress);
190194
reg = reg | (_powerDownMode << 1);
191-
#if defined(ARDUINO) && ARDUINO >= 100
192-
Wire.write(reg);
193-
Wire.write(h);
194-
Wire.write(l);
195-
#else
196-
Wire.send(reg);
197-
Wire.send(h);
198-
Wire.send(l);
199-
#endif
195+
WIRE_WRITE(reg);
196+
WIRE_WRITE(h);
197+
WIRE_WRITE(l);
200198
return Wire.endTransmission();
201199
}
202200

@@ -210,14 +208,10 @@ uint8_t MCP4725::readRegister(uint8_t* buffer, const uint8_t length)
210208

211209
Wire.requestFrom(_deviceAddress, length);
212210
uint8_t cnt = 0;
213-
uint32_t before = millis();
214-
while ((cnt < length) && ((millis() - before) < MCP4725_TIMEOUT))
211+
uint8_t n = Wire.available();
212+
while ( (cnt < n) && (cnt < length))
215213
{
216-
#if defined(ARDUINO) && ARDUINO >= 100
217-
if (Wire.available()) buffer[cnt++] = Wire.read();
218-
#else
219-
if (Wire.available()) buffer[cnt++] = Wire.receive();
220-
#endif
214+
buffer[cnt++] = WIRE_READ();
221215
}
222216
return cnt;
223217
}
@@ -227,11 +221,7 @@ uint8_t MCP4725::readRegister(uint8_t* buffer, const uint8_t length)
227221
int MCP4725::generalCall(const uint8_t gc)
228222
{
229223
Wire.beginTransmission(0);
230-
#if defined(ARDUINO) && ARDUINO >= 100
231-
Wire.write(gc);
232-
#else
233-
Wire.send(gc);
234-
#endif
224+
WIRE_WRITE(gc);
235225
return Wire.endTransmission();
236226
}
237227
#endif

libraries/MCP4725/MCP4725.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//
44
// FILE: MCP4725.h
55
// AUTHOR: Rob Tillaart
6-
// PURPOSE: Simple MCP4725 DAC library for Arduino
7-
// VERSION: 0.1.05
6+
// PURPOSE: Simple MCP4725 DAC (12 bit I2C) library for Arduino
7+
// VERSION: 0.1.6
88
// HISTORY: See MCP4725.cpp
99
// URL:
1010
//
@@ -21,15 +21,14 @@
2121
#include "Wiring.h"
2222
#endif
2323

24-
#define MCP4725_VERSION "0.1.05"
24+
#define MCP4725_VERSION "0.1.6"
2525

2626
// regisiterMode
2727
#define MCP4725_DAC 0x40
2828
#define MCP4725_DACEEPROM 0x60
2929

3030
// constants
3131
#define MCP4725_MAXVALUE 4095
32-
#define MCP4725_TIMEOUT 1000
3332

3433
// errors
3534
#define MCP4725_VALUE_ERROR -999
@@ -52,7 +51,7 @@
5251
class MCP4725
5352
{
5453
public:
55-
MCP4725(const uint8_t deviceAddress);
54+
explicit MCP4725(const uint8_t deviceAddress);
5655

5756
void begin();
5857
// uses writeFastMode

libraries/MCP4725/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MCP4725
2-
version=0.1.05
2+
version=0.1.6
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Library for the MCP4725 DAC.

0 commit comments

Comments
 (0)