-
Notifications
You must be signed in to change notification settings - Fork 142
Description
This is really an amazing library and it helps me a lot to make my first steps with an Arduino UNO and a DS3131 board. However, I have missed the feature to receive the alarm time once set. I would suggest to include a further function to your library doing this.
Here is a hack of myself doing the required task:
/*----------------------------------------------------------------------*
* Reads the current alarm time for alarm from the RTC and returns it *
* in a tmElements_t * structure tm and an ALARM_TYPES_t variable *
* alarmType. I am afraid that tmElements_t is somehow abused... *
*----------------------------------------------------------------------*/
byte DS3232RTC::readAlarm(int alarm, tmElements_t &tm, ALARM_TYPES_t &alarmType)
{
byte s=0, m,h,d, aType;
if (alarm == ALARM_1) {
aType = 0x00;
i2cBeginTransmission(RTC_ADDR);
i2cWrite((uint8_t)ALM1_SECONDS);
if ( byte e = i2cEndTransmission() ) return e;
//request 4 bytes (secs, min, hr, dow)
i2cRequestFrom(RTC_ADDR, 4);
s = i2cRead();
} else {
aType = 0x80;
i2cBeginTransmission(RTC_ADDR);
i2cWrite((uint8_t)ALM2_MINUTES);
if ( byte e = i2cEndTransmission() ) return e;
i2cRequestFrom(RTC_ADDR, 3);
}
m = i2cRead();
h = i2cRead();
d = i2cRead();
// Decode alarm type
aType |= (s & _BV(7)) ? 0x01 : 0;
aType |= (m & _BV(7)) ? 0x02 : 0;
aType |= (h & _BV(7)) ? 0x04 : 0;
aType |= (d & _BV(7)) ? 0x08 : 0;
// set time / day / date values
tm.Second = bcd2dec(s & 0x7f);
tm.Minute = bcd2dec(m & 0x7f);
tm.Hour = bcd2dec(h & 0x7f); // assumes 24hr clock
tm.Wday = (d & _BV(DYDT)) ? 0 : bcd2dec(d & 0x3f);
tm.Day = (d & _BV(DYDT)) ? bcd2dec(d & 0x3f) : 0;
tm.Month = 0;
tm.Year = 0;
alarmType = (ALARM_TYPES_t)aType;
return 0;
} I am quite new for programming Arduino code and thus this code is probably not bullet proof, not well-suited to include, and possibly not well enough tested. I am sorry for that, however, it has worked for myself. Possibly, there is a strong reason not to include something like this to the code.
Best regards and many thanks again for this nice library,
Andreas
P.S.
I've just see that this request was declined before due to keep the code small and simple. I am sorry for requesting it again. In my setting it was really nice to provide the user of the alarm clock the option to edit the alarm time instead of only letting him set the time. But of course, the code simplicity is a big argument - and actually the simplicity of your code was the reason for using it.