Github Repo | C Header | C source | JS source |
---|---|---|---|
mongoose-os-libs/ds3231 | mgos_ds3231.h | api_ds3231.js |
The library implements 2 structures: struct mgos_ds3231
and a helper struct mgos_ds3231_date_time
.
Both structures are available for mJS too. The mJS support is available if the user includes the mjs library:
- origin: https://github.com/mongoose-os-libs/mjs
This is used to communicate with the DS3231.
The RTC data is read into and written from using the helper struct mgos_ds3231_date_time
struct mgos_ds3231* ds=mgos_ds3231_create(addr);
time_t unixtime=time(NULL);
mgos_ds3231_write_unixtime(ds, unixtime);
mgos_ds3231_free(ds);
let ds=DS3231.create(addr);
let unixtime=Timer.now();
ds.writeUnixtime(unixtime);
ds.free();
Encapsulates the DS3231 date/time information plus the unix timestamp. Several functions are provided to create and free the structure and to get/set different fields.
struct mgos_ds3231_date_time* dt=mgos_ds3231_date_time_create();
mgos_ds3231_date_time_set_date(dt, 2016, 2, 3);
// mgos_ds3231_date_time_set_time will calculate the unixtime.
// It is VERY important to call mgos_ds3231_date_time_set_date first
mgos_ds3231_date_time_set_time(dt, 12, 34, 56);
struct mgos_ds3231* ds=mgos_ds3231_create(addr);
mgos_ds3231_write(ds, dt);
mgos_ds3231_settimeofday(ds);
mgos_ds3231_free(ds);
mgos_ds3231_date_time_free(dt);
let dt=DS3231DateTime.create();
dt.setDate(2016, 2, 3);
dt.setTime(12, 34, 56);
let ds=DS3231.create(addr);
ds.write(dt);
ds.setTimeOfDay();
ds.free();
dt.free();
struct mgos_ds3231_date_time *mgos_ds3231_date_time_create();
Create a
mgos_ds3231_date_time
structure
void mgos_ds3231_date_time_free(struct mgos_ds3231_date_time *dt);
Free the
mgos_ds3231_date_time
structure
void mgos_ds3231_date_time_set_date(struct mgos_ds3231_date_time *dt,
uint16_t year, uint8_t month, uint8_t day);
Set the date part of the
mgos_ds3231_date_time
structure. TheDow
member is computed by the function
void mgos_ds3231_date_time_set_time(struct mgos_ds3231_date_time *dt,
uint8_t hour, uint8_t minute,
uint8_t second);
Set the time part of the
mgos_ds3231_date_time
structure. Theunixtime
member is set by this function. The day/time MUST be UTCmgos_ds3231_date_time_set_date
should be called before.
time_t mgos_ds3231_date_time_get_unixtime(
const struct mgos_ds3231_date_time *dt);
Get the
unixtime
time part of themgos_ds3231_date_time
structure.
void mgos_ds3231_date_time_set_unixtime(struct mgos_ds3231_date_time *dt,
time_t unixtime);
Set the members of
struct mgos_ds3231_date_time
from the providedunixtime
uint16_t mgos_ds3231_date_time_get_year(struct mgos_ds3231_date_time *dt);
struct mgos_ds3231 *mgos_ds3231_create(uint8_t addr);
Create a
struct mgos_ds3231
structure
void mgos_ds3231_free(struct mgos_ds3231 *ds);
Free a
struct mgos_ds3231
structure
const struct mgos_ds3231_date_time *mgos_ds3231_read(struct mgos_ds3231 *ds);
Read the current date and time, returning a structure containing that information.
bool mgos_ds3231_write(struct mgos_ds3231 *ds,
const struct mgos_ds3231_date_time *date);
Set the date and time from the settings in the given structure.
bool mgos_ds3231_write_unixtime(struct mgos_ds3231 *ds, const time_t unixtime);
Set the date and time from unixtime.
int mgos_ds3231_settimeofday(struct mgos_ds3231 *ds);
Sets the system time from the DS3231 data. Assumes DS3231 was previously setup with correct data.
Returns 0 if success.
float mgos_ds3231_get_temperature_c(struct mgos_ds3231 *ds);
Get the temperature accurate to within 0.25 Celsius
float mgos_ds3231_get_temperature_f(struct mgos_ds3231 *ds);
Get the temperature in Fahrenheit
bool mgos_ds3231_disable_alarms(struct mgos_ds3231 *ds);
- Disable any existing alarm settings.
@return Success True/False
uint8_t mgos_ds3231_check_alarms(struct mgos_ds3231 *ds);
Determine if an alarm has triggered, also clears the alarm if so.
Returns 0 for no alarm, 1 for Alarm 1, 2 for Alarm 2, and 3 for both alarms
uint8_t mgos_ds3231_check_stopflag(struct mgos_ds3231 *ds, int clear);
Determine if the oscillator stop flag is set
Returns the flag status
uint8_t mgos_ds3231_set_alarm(struct mgos_ds3231 *ds,
const struct mgos_ds3231_date_time *alarm_date,
uint8_t alarm_mode);
Sets an alarm, the alarm will pull the SQW pin low (you can monitor with an interrupt).
alarm_date
The date/time for the alarm, as appropriate for the alarm mode (example, for ALARM_MATCH_SECOND then alarm_date.Second will be the matching criteria).
alarm_mode
the mode of the alarm, from the following...MGOS_DS3231_ALARM_EVERY_SECOND MGOS_DS3231_ALARM_MATCH_SECOND MGOS_DS3231_ALARM_MATCH_SECOND_MINUTE MGOS_DS3231_ALARM_MATCH_SECOND_MINUTE_HOUR MGOS_DS3231_ALARM_MATCH_SECOND_MINUTE_HOUR_DATE MGOS_DS3231_ALARM_MATCH_SECOND_MINUTE_HOUR_DOW
MGOS_DS3231_ALARM_EVERY_MINUTE MGOS_DS3231_ALARM_MATCH_MINUTE MGOS_DS3231_ALARM_MATCH_MINUTE_HOUR MGOS_DS3231_ALARM_MATCH_MINUTE_HOUR_DATE MGOS_DS3231_ALARM_MATCH_MINUTE_HOUR_DOW
MGOS_DS3231_ALARM_HOURLY MGOS_DS3231_ALARM_DAILY MGOS_DS3231_ALARM_WEEKLY MGOS_DS3231_ALARM_MONTHLY
DS3231DateTime.create()
Creates a DS3231DateTime instance to be used for reading/wrtting data from/to DS3231. Return value: an object with the methods described below.
dsData.free()
Frees a DS3231DateTime instance. No methods can be called on this instance after that. Return value: none.
DS3231.createI2C(address)
Creates a DS3231 instance on the I2C bus with the given address address
.
Return value: an object with the methods described below.
rtc.free()
Frees the DS3231 instance. No methods can be called on this instance after that. Return value: none.
rtc.read()
Reads date/time from the RTC Returns a DS3231DateTime struct.
rtc.write(dt)
Writes a DS3231DateTime structure
Returns true
on success
rtc.writeUnixtime(unixtime)
Sets the date/time from a unixtime
Returns true
on success
rtc.getTemperatureC()
Return the temperature in Celsius
rtc.getTemperatureF()
Return the temperature in Fahrenheit
rtc.disableAlarms()
Disable alarms
Returns true
on success
rtc.checkAlarms()
Disable alarms Returns: 0 if no alarm 1 if Alarm1 was triggered 2 if Alarm2 was triggered 3 if both alarms were triggered
rtc.checkStopFlag()
Check the OSF bit Returns: the oscillator stop flag bit
rtc.setAlarm(alarm)
Set alarm
dt
- a DS3231DateTime structure
mode
- an alarm mode selected from the ALARM_ constants
Returns true
on success
rtc.setTimeOfDay()
Set system time Returns 0 on success
edit this doc