Skip to content

Commit

Permalink
Added getAlarm()
Browse files Browse the repository at this point in the history
kamocat committed Jun 15, 2020
1 parent fa7aa28 commit 6244b49
Showing 3 changed files with 50 additions and 3 deletions.
48 changes: 46 additions & 2 deletions OPEnS_RTC.cpp
Original file line number Diff line number Diff line change
@@ -68,8 +68,8 @@ static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) {


// Utilities for converting between BCD and binary
static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }

// Effeciently convert two-digit byte to char array.
// Returns pointer to byte *AFTER* the most recent digit, to make repeated calls easy.
@@ -1067,6 +1067,50 @@ bool RTC_DS3231::isArmed(byte alarmNumber) {
return value;
}

/*----------------------------------------------------------------------*
* This method can check either Alarm 1 or Alarm 2, depending on the *
* value of alarmNumber (1 or 2). *
*----------------------------------------------------------------------*/
DateTime RTC_DS3231::getAlarm(byte alarmNumber) {
uint8_t seconds, minutes, hours, daydate, mask;
Wire.beginTransmission(DS3231_ADDRESS);
if( alarmNumber == 1 ){
Wire.write(0x07);
Wire.endTransmission();
Wire.requestFrom((uint8_t)DS3231_ADDRESS, (uint8_t)4);
seconds = Wire._I2C_READ();
minutes = Wire._I2C_READ();
hours = Wire._I2C_READ();
daydate = Wire._I2C_READ();
Wire.endTransmission();
} else {
Wire.write(0x0B);
Wire.endTransmission();
Wire.requestFrom((uint8_t)DS3231_ADDRESS, (uint8_t)3);
seconds = 0;
minutes = Wire._I2C_READ();
hours = Wire._I2C_READ();
daydate = Wire._I2C_READ();
Wire.endTransmission();
}
/* Here's the alarm mask that determines whether
the alarm ignores seconds, minutes, hours, weekday, and date.*/
mask = (seconds>>7) | (0x02 & (minutes>>6)) | (0x03 & (hours>>5)) | (0xC0 & daydate);

// Convert time to binary
seconds = bcd2bin(0x7F & seconds);
minutes = bcd2bin(0x7F & minutes);
hours = bcd2bin(0x7F & hours); // Assume 24-hour format
daydate = bcd2bin(0x3F & daydate); // Assume date, not weekday

DateTime t = now();
// Put into a timestamp
DateTime a = DateTime( t.year(), t.month(), daydate, hours, minutes, seconds);

//TODO: adjust alarm according to mask so it's always future
return( a );
}

/*----------------------------------------------------------------------*
* The temperature registers are updated after every 64-second *
* conversion. If you want force temperature conversion call this *
1 change: 1 addition & 0 deletions OPEnS_RTC.h
Original file line number Diff line number Diff line change
@@ -288,6 +288,7 @@ class RTC_DS3231 {
bool isArmed(byte alarmNumber);
void clearAlarm(byte alarmNumber);
void clearAlarm( void );
DateTime getAlarm(byte alarmNumber);
};

// RTC based on the PCF8523 chip connected via I2C and the Wire library
4 changes: 3 additions & 1 deletion examples/DS3231_BasicRepeating/DS3231_BasicRepeating.ino
Original file line number Diff line number Diff line change
@@ -26,7 +26,9 @@ void setup()
digitalWrite(LED_BUILTIN, HIGH);

RTC_DS.begin();
RTC_DS.adjust(DateTime(__DATE__, __TIME__));
DateTime t = DateTime(__DATE__, __TIME__);
t = t + TimeSpan(5); //account for compilation time
RTC_DS.adjust(t);

Serial.begin(115200);
while (!Serial); // Won't start anything until serial is open, comment this line out if powering from battery

0 comments on commit 6244b49

Please sign in to comment.