Class: Denko::EEPROM::BuiltIn

Inherits:
Object
  • Object
show all
Includes:
Behaviors::Component, Behaviors::Reader
Defined in:
lib/denko/eeprom/built_in.rb

Instance Attribute Summary

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Reader

#_read, #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::Component

#initialize, #micro_delay

Instance Method Details

#[](index) ⇒ Object



40
41
42
# File 'lib/denko/eeprom/built_in.rb', line 40

def [](index)
  @state_mutex.synchronize { @state.send :[], index }
end

#[]=(index, value) ⇒ Object



44
45
46
# File 'lib/denko/eeprom/built_in.rb', line 44

def []=(index, value)
  @state_mutex.synchronize { @state.send :[]=, index, value }
end

#after_initialize(options = {}) ⇒ Object



13
14
15
16
17
# File 'lib/denko/eeprom/built_in.rb', line 13

def after_initialize(options={})
  super(options)
  self.state = Array.new(board.eeprom_length, nil)
  load
end

#each(&block) ⇒ Object



48
49
50
# File 'lib/denko/eeprom/built_in.rb', line 48

def each(&block)
  @state_mutex.synchronize { @state.send :each, &block }
end

#each_with_index(&block) ⇒ Object



52
53
54
# File 'lib/denko/eeprom/built_in.rb', line 52

def each_with_index(&block)
  @state_mutex.synchronize { @state.send :each_with_index, &block }
end

#lengthObject Also known as: count

Specific Array-like methods for convenience.



37
# File 'lib/denko/eeprom/built_in.rb', line 37

def length; board.eeprom_length; end

#loadObject



19
20
21
22
23
# File 'lib/denko/eeprom/built_in.rb', line 19

def load
  state.each_slice(128).with_index do |slice, index|
    read_using -> { board.eeprom_read(index * 128, slice.length) }
  end
end

#pinObject



9
10
11
# File 'lib/denko/eeprom/built_in.rb', line 9

def pin
  254
end

#pre_callback_filter(message) ⇒ Object



56
57
58
59
60
# File 'lib/denko/eeprom/built_in.rb', line 56

def pre_callback_filter(message)
  address = message.split("-", 2)[0].to_i
  bytes = message.split("-", 2)[1].split(",").map(&:to_i)
  {address: address, data: bytes}
end

#saveObject



25
26
27
28
29
30
31
32
# File 'lib/denko/eeprom/built_in.rb', line 25

def save
  @state_mutex.synchronize do
    @state.each_slice(128).with_index do |slice, index|
      board.eeprom_write(index * 128, slice)
    end
  end
  load
end

#update_state(hash) ⇒ Object



62
63
64
65
66
# File 'lib/denko/eeprom/built_in.rb', line 62

def update_state(hash)
  @state_mutex.synchronize do
    @state[hash[:address], hash[:data].length] = hash[:data]
  end
end