bool RTC_DS3231::setAlarm(uint8_t alarm) { clearAlarm(alarm); uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); ctrl |= 0x04; // turn on INTCN ctrl |= alarm; // set bit alarm write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); return true; }
bool RTC_DS3231::clearAlarm(uint8_t alarm) { uint8_t regStatus = read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG); if(alarm==0) regStatus &= ~0x03; else regStatus &= ~alarm; write_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG, regStatus); return true; }
bool RTC_DS3231::switchClock(bool state) { uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); if(state) ctrl |= 0x04; // turn on INTCN else ctrl &= ~0x04; // turn off INTCN write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); return true; }
void RTC_DS3231::writeSqwPinMode(Ds3231SqwPinMode mode) { uint8_t ctrl; ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); ctrl &= ~0x04; // turn off INTCON ctrl &= ~0x18; // set freq bits to 0 if (mode == DS3231_OFF) { ctrl |= 0x04; // turn on INTCN } else { ctrl |= mode; } write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); }
void RTC_DS3231::adjust(const DateTime& dt) { Wire.beginTransmission(DS3231_ADDRESS); Wire._I2C_WRITE((byte)0); // start at location 0 Wire._I2C_WRITE(bin2bcd(dt.second())); Wire._I2C_WRITE(bin2bcd(dt.minute())); Wire._I2C_WRITE(bin2bcd(dt.hour())); Wire._I2C_WRITE(bin2bcd(0)); Wire._I2C_WRITE(bin2bcd(dt.day())); Wire._I2C_WRITE(bin2bcd(dt.month())); Wire._I2C_WRITE(bin2bcd(dt.year() - 2000)); Wire.endTransmission(); uint8_t statreg = read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG); statreg &= ~0x80; // flip OSF bit write_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG, statreg); }
// Returns the number of characters it read from the Bluetooth // Data is available in the I2CSlaveBuffer int16_t receive_bl_message() { int16_t index = 0; uint64_t start = unixtime; while(unixtime - start <= BL_TIMEOUT) { if(is_bl_message_available()) { bl_receive[index] = (char) read_i2c_register(BL_RAADR, BL_WAADR, BL_RHR); index++; if(bl_receive[index - 1] == '\0') { // Got the entire message // printf("BL msg rcv size: [%d]\n", index); break; } // If we have gotten 64 characters but not null than we want to send an ack and read the rest of them again if(index % 64 == 0) { // Send an ack set_bl_opcode(B_ACK); start = unixtime; send_bl_message(); } } } if(unixtime - start >= 3) index = -1; return index; }
bool RTC_DS3231::isAlarmRinging(uint8_t alarm) { return (read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG) & alarm) ? true : false; }
bool RTC_DS3231::isAlarmSet(uint8_t alarm) { return (read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL) & alarm) ? true : false; }
uint8_t RTC_DS3231::readRegister(uint8_t reg) { return read_i2c_register(DS3231_ADDRESS, reg); }
uint8_t RTC_DS3231::readRegStatus() { return read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG); }
bool RTC_DS3231::isClockOn() { return (read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL) & 0x04) ? true : false; }
uint8_t RTC_DS3231::readRegControl() { return read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); }
uint8_t is_bl_message_available() { return read_i2c_register(BL_RAADR, BL_WAADR, BL_LSR) & 0x01; }
uint16_t get_current_voltage() { return (read_i2c_register(FG_RAADR, FG_WAADR, 0x02) << 8) + (read_i2c_register(FG_RAADR, FG_WAADR, 0x03) << 4); }
uint8_t get_power_remaining() { return read_i2c_register(FG_RAADR, FG_WAADR, 0x04); }
uint8_t is_running_on_battery() { return read_i2c_register(FG_RAADR, FG_WAADR, 0x04) == 0 ? 0 : 1; }