Class: Denko::OneWire::Peripheral

Inherits:
Object
  • Object
show all
Includes:
Behaviors::BusPeripheralAddressed, Behaviors::Poller, Constants
Defined in:
lib/denko/one_wire/peripheral.rb

Direct Known Subclasses

Sensor::DS18B20

Constant Summary

Constants included from Constants

Constants::ALARM_SEARCH, Constants::CONVERT_T, Constants::COPY_SCRATCH, Constants::MATCH_ROM, Constants::READ_POWER_SUPPLY, Constants::READ_ROM, Constants::READ_SCRATCH, Constants::RECALL_EEPROM, Constants::SEARCH_ROM, Constants::SKIP_ROM, Constants::WRITE_SCRATCH

Instance Attribute Summary collapse

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

Methods included from Behaviors::Threaded

#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop

Methods included from Behaviors::Reader

#_read, #read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

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

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::BusPeripheralAddressed

#before_initialize

Methods included from Behaviors::BusPeripheral

#atomically, #before_initialize

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



8
9
10
# File 'lib/denko/one_wire/peripheral.rb', line 8

def address
  @address
end

Instance Method Details

#address_bytesObject



47
48
49
# File 'lib/denko/one_wire/peripheral.rb', line 47

def address_bytes
  Helper.address_to_bytes(self.address)
end

#copy_scratchObject



28
29
30
31
32
33
34
35
# File 'lib/denko/one_wire/peripheral.rb', line 28

def copy_scratch
  atomically do
    match
    bus.write(COPY_SCRATCH)
    sleep 0.05
    bus.reset if bus.parasite_power
  end
end

#extract_serialObject



55
56
57
58
59
# File 'lib/denko/one_wire/peripheral.rb', line 55

def extract_serial
  # Remove CRC & family code.
  serial = (@address & 0x00FFFFFFFFFFFFFF) >> 8
  serial.to_s(16).rjust(12, "0")
end

#matchObject



37
38
39
40
41
42
43
44
45
# File 'lib/denko/one_wire/peripheral.rb', line 37

def match
  bus.reset
  if bus.found_devices.count < 2
    bus.write(SKIP_ROM)
  else
    bus.write(MATCH_ROM)
    bus.write(address_bytes)
  end
end

#read_scratch(num_bytes, &block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/denko/one_wire/peripheral.rb', line 11

def read_scratch(num_bytes, &block)
  atomically do
    bus.add_callback(:read, &block) if block_given?
    match
    bus.write(READ_SCRATCH)
    bus.read(num_bytes)
  end
end

#serial_numberObject



51
52
53
# File 'lib/denko/one_wire/peripheral.rb', line 51

def serial_number
  @serial_number ||= extract_serial
end

#write_scratch(*bytes) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/denko/one_wire/peripheral.rb', line 20

def write_scratch(*bytes)
  atomically do
    match
    bus.write(WRITE_SCRATCH)
    bus.write(*bytes)
  end
end