Skip to content

Commit

Permalink
Store and Expose last error (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Makuna authored Feb 25, 2019
1 parent b172f9b commit 05256d3
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 64 deletions.
32 changes: 27 additions & 5 deletions examples/DS1307_Memory/DS1307_Memory.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ void setup ()

if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}

if (!Rtc.GetIsRunning())
Expand Down Expand Up @@ -78,9 +89,20 @@ void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}

RtcDateTime now = Rtc.GetDateTime();
Expand Down
49 changes: 35 additions & 14 deletions examples/DS1307_Simple/DS1307_Simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,27 @@ void setup ()

if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing

Serial.println("RTC lost confidence in the DateTime!");

// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue

Rtc.SetDateTime(compiled);
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
// Common Cuases:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing

Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue

Rtc.SetDateTime(compiled);
}
}

if (!Rtc.GetIsRunning())
Expand Down Expand Up @@ -83,9 +93,20 @@ void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}

RtcDateTime now = Rtc.GetDateTime();
Expand Down
28 changes: 25 additions & 3 deletions examples/DS3231_Alarms/DS3231_Alarms.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,19 @@ void setup ()

if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}

if (!Rtc.GetIsRunning())
Expand Down Expand Up @@ -119,7 +130,18 @@ void loop ()
{
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
Serial.println("RTC lost confidence in the DateTime!");
}
}

RtcDateTime now = Rtc.GetDateTime();
Expand Down
32 changes: 27 additions & 5 deletions examples/DS3231_Memory/DS3231_Memory.ino
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,19 @@ void setup ()

if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}

if (!Rtc.GetIsRunning())
Expand Down Expand Up @@ -102,9 +113,20 @@ void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}

RtcDateTime now = Rtc.GetDateTime();
Expand Down
50 changes: 36 additions & 14 deletions examples/DS3231_Simple/DS3231_Simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,28 @@ void setup ()

if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing

Serial.println("RTC lost confidence in the DateTime!");

// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue

Rtc.SetDateTime(compiled);
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
// Common Cuases:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing

Serial.println("RTC lost confidence in the DateTime!");

// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue

Rtc.SetDateTime(compiled);
}
}

if (!Rtc.GetIsRunning())
Expand Down Expand Up @@ -85,9 +96,20 @@ void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
Serial.print("RTC communications error = ");
Serial.println(Rtc.LastError());
}
else
{
// Common Cuases:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}

RtcDateTime now = Rtc.GetDateTime();
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ DayOfWeek KEYWORD1
#######################################

Begin KEYWORD2
LastError KEYWORD2
IsDateTimeValid KEYWORD2
GetIsRunning KEYWORD2
SetIsRunning KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/Makuna/Rtc.git"
},
"version": "2.3.1",
"version": "2.3.2",
"frameworks": "arduino",
"platforms": "*"
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Rtc by Makuna
version=2.3.1
version=2.3.2
author=Michael C. Miller (makuna@live.com)
maintainer=Michael C. Miller (makuna@live.com)
sentence=A library that makes interfacing DS1302, DS1307, DS3231, and DS3234 Real Time Clock modules easy.
Expand Down
19 changes: 15 additions & 4 deletions src/EepromAT24C32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ template<class T_WIRE_METHOD> class EepromAt24c32
public:
EepromAt24c32(T_WIRE_METHOD& wire, uint8_t addressBits = 0b111) :
_address(AT24C32_ADDRESS | (addressBits & 0b00000111)),
_wire(wire)
_wire(wire),
_lastError(0)
{
}

Expand All @@ -17,6 +18,11 @@ template<class T_WIRE_METHOD> class EepromAt24c32
_wire.begin();
}

uint8_t LastError()
{
return _lastError;
}

void SetMemory(uint16_t memoryAddress, uint8_t value)
{
SetMemory(memoryAddress, &value, 1);
Expand Down Expand Up @@ -52,7 +58,7 @@ template<class T_WIRE_METHOD> class EepromAt24c32
countWritten++;
}

_wire.endTransmission();
_lastError = _wire.endTransmission();

return countWritten;
}
Expand All @@ -64,7 +70,12 @@ template<class T_WIRE_METHOD> class EepromAt24c32
{
// set address to read from
beginTransmission(memoryAddress);
_wire.endTransmission();
_lastError = _wire.endTransmission();

if (_lastError != 0)
{
return 0;
}

// read the data
uint8_t countRead = 0;
Expand All @@ -84,12 +95,12 @@ template<class T_WIRE_METHOD> class EepromAt24c32
const uint8_t _address;

T_WIRE_METHOD& _wire;
uint8_t _lastError;

void beginTransmission(uint16_t memoryAddress)
{
_wire.beginTransmission(_address);
_wire.write(memoryAddress >> 8);
_wire.write(memoryAddress & 0xFf);

}
};
Loading

0 comments on commit 05256d3

Please sign in to comment.