Class: Denko::RTC::DS3231

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

Instance Attribute Summary

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from I2C::Peripheral

#i2c_read, #i2c_write

Methods included from Behaviors::Reader

#read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

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.



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

def _read
  i2c_read(0, 7)
end

#bcd_to_time(bytes) ⇒ Object

Convert 7 byte BCD sequence to Time object.



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

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

#before_initialize(options = {}) ⇒ Object



7
8
9
10
# File 'lib/denko/rtc/ds3231.rb', line 7

def before_initialize(options={})
  @i2c_address = 0x68
  super(options)
end

#pre_callback_filter(bytes) ⇒ Object



26
27
28
# File 'lib/denko/rtc/ds3231.rb', line 26

def pre_callback_filter(bytes)
  bcd_to_time(bytes)
end

#time=(time) ⇒ Object

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



13
14
15
16
# File 'lib/denko/rtc/ds3231.rb', line 13

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.



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

def time_to_bcd(time)
  [ BCD.decode(time.sec),
    BCD.decode(time.min),
    BCD.decode(time.hour),
    BCD.decode(time.strftime('%u').to_i),
    BCD.decode(time.day),
    BCD.decode(time.month),
    BCD.decode(time.year - 1970) ]
end