Class: Denko::RTC::DS3231

Inherits:
Object
  • Object
show all
Includes:
I2C::Peripheral
Defined in:
lib/denko/rtc/ds3231.rb

Constant Summary collapse

I2C_ADDRESS =
0x68

Constants included from I2C::Peripheral

I2C::Peripheral::I2C_FREQUENCY, I2C::Peripheral::I2C_REPEATED_START

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Constants included from Behaviors::Reader

Behaviors::Reader::READ_WAIT_TIME

Instance Attribute Summary

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::State

#state

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods included from I2C::Peripheral

#address, #i2c_default, #i2c_read, #i2c_read_raw, #i2c_write

Methods included from Behaviors::Lifecycle

included

Methods included from Behaviors::Reader

#read, #read_busy?, #read_nb, #read_raw, #read_using, #update

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #remove_callback, #update

Methods included from Behaviors::State

#update_state

Methods included from Behaviors::BusPeripheralAddressed

#address

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_readObject

Time data starts at register 0 and is 7 bytes long.



18
19
20
# File 'lib/denko/rtc/ds3231.rb', line 18

def _read
  i2c_read(7, register: 0)
end

#bcd_to_time(bytes) ⇒ Object

Convert 7 byte BCD sequence to Time object.



41
42
43
44
# File 'lib/denko/rtc/ds3231.rb', line 41

def bcd_to_time(bytes)
  t = bytes.map { |b| BCD.encode(b) }
  Time.new t[6] + 1970, t[5], t[4], t[2], t[1], t[0]
end

#pre_callback_filter(bytes) ⇒ Object



22
23
24
# File 'lib/denko/rtc/ds3231.rb', line 22

def pre_callback_filter(bytes)
  bcd_to_time(bytes)
end

#time=(time) ⇒ Object

Write start register 0x00, then bytes to set time.



9
10
11
12
# File 'lib/denko/rtc/ds3231.rb', line 9

def time=(time)
  i2c_write [0] + time_to_bcd(time)
  time
end

#time_to_bcd(time) ⇒ Object

Convert Time object to 7 byte BCD sequence.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/denko/rtc/ds3231.rb', line 27

def time_to_bcd(time)
  wday = time.wday
  wday = 7 if wday == 0

  [ BCD.decode(time.sec),
    BCD.decode(time.min),
    BCD.decode(time.hour),
    BCD.decode(wday),
    BCD.decode(time.day),
    BCD.decode(time.month),
    BCD.decode(time.year - 1970) ]
end