Class: Denko::EEPROM::AT24C

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

Constant Summary collapse

I2C_ADDRESS =
0x50
I2C_FREQUENCY =
400_000
READ_WRITE_US =
20_000
WRITE_PAGE_SIZE =
64

Constants included from I2C::Peripheral

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, #read_busy?, #read_nb, #read_raw, #read_using, #update

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #pre_callback_filter, #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

#[](loc) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/denko/eeprom/at24c.rb', line 15

def [](loc)
  if loc.class == Range
    index  = loc.first
    count  = loc.count
    limit  = bus.board.i2c_limit
    result = []

    # Chunked reads based on Board#i2c_limit.
    # Reading appears to cross page borders seamlessly.
    while count > 0
      this_count = (count > limit) ? limit : count
      result += i2c_read_raw(this_count, register: int_to_reg_array(index))
      index = index + this_count
      count = count - this_count
    end

    result
  else
    i2c_read_raw(1, register: int_to_reg_array(loc))
  end
end

#[]=(loc, value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/denko/eeprom/at24c.rb', line 37

def []=(loc, value)
  if value.class == Array
    # Start address uses up 2 bytes.
    limit = bus.board.i2c_limit - 2

    remaining = value.length
    src_start = 0
    dst_start = loc

    while remaining > 0
      # Limit to lowest of: remaining page size, I2C max size, or remaining bytes.
      size = WRITE_PAGE_SIZE - (dst_start % WRITE_PAGE_SIZE)
      size = limit if (limit < size)
      size = remaining if (remaining < size)
      src_end = src_start + size - 1

      i2c_write(int_to_reg_array(dst_start) + value[src_start..src_end])
      micro_delay(READ_WRITE_US)

      remaining -= size
      src_start += size
      dst_start += size
    end
  else
    i2c_write(int_to_reg_array(loc) + [value])
    micro_delay(READ_WRITE_US)
  end
end

#int_to_reg_array(int) ⇒ Object



11
12
13
# File 'lib/denko/eeprom/at24c.rb', line 11

def int_to_reg_array(int)
  [ (int >> 8) & 0xFF, int & 0xFF ]
end