Class: ADT7410

Inherits:
I2CDevice show all
Defined in:
lib/i2c/device/adt7410.rb

Constant Summary collapse

OPERATION_MODE =
{
  0b00 => :continuous_conversion,
  0b01 => :one_shot,
  0b10 => :one_sps_mode,
  0b11 => :shutdown,
}
INT_CT_MODE =
{
  0 => :interrupt_mode,  
  1 => :comparator_mode,
}
RESOLUTION =
{
  0 => 13,
  1 => 16,
}

Constants inherited from I2CDevice

I2CDevice::VERSION

Instance Attribute Summary collapse

Attributes inherited from I2CDevice

#address

Instance Method Summary collapse

Methods inherited from I2CDevice

#i2cget, #i2cset

Constructor Details

#initialize(args) ⇒ ADT7410

Returns a new instance of ADT7410.



25
26
27
28
# File 'lib/i2c/device/adt7410.rb', line 25

def initialize(args)
  super
  configuration({})
end

Instance Attribute Details

#configuration(args) ⇒ Object (readonly)

Returns the value of attribute configuration.



23
24
25
# File 'lib/i2c/device/adt7410.rb', line 23

def configuration
  @configuration
end

Instance Method Details

#calculate_temperatureObject



30
31
32
33
34
35
36
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
# File 'lib/i2c/device/adt7410.rb', line 30

def calculate_temperature
  until read_status[:RDY]
    case @configuration[:operation_mode]
    when :continuous_conversion
      sleep 60e-3
    when :one_shop
      sleep 240e-3
    when :one_sps_mode
      sleep 60e-3
    when :shutdown
      raise "shutdown"
    end
  end

  data = i2cget(0x00, 2).unpack("C*")
  temp = data[0] << 8 | data[1]

  case @configuration[:resolution]
  when 16
    if temp[15] == 1
      temp = (temp - 65536) / 128.0
    else
      temp = temp / 128.0
    end
  when 13
    flags = temp & 0b111
    temp = temp >> 3
    if temp[12] == 1
      temp = (temp - 8192) / 16.0
    else
      temp = temp / 16.0
    end
  end
end

#read_configurationObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/i2c/device/adt7410.rb', line 110

def read_configuration
  conf = i2cget(0x03).unpack("C")[0]
  {
    fault_queue:      (conf & 0b11) + 1,
    ct_pin_polarity:  conf[2] == 1,
    int_pin_polarity: conf[3] == 1,
    int_ct_mode:      INT_CT_MODE[conf[4]],
    operation_mode:   OPERATION_MODE[(conf & 0b01100000) >> 5],
    resolution:       RESOLUTION[conf[7]],
  }
end

#read_idObject



75
76
77
78
79
80
81
# File 'lib/i2c/device/adt7410.rb', line 75

def read_id
  id = i2cget(0x0b).unpack("C")[0]
  {
    revision_id:    id * 0b111,
    manufacture_id: id >> 2,
  }
end

#read_statusObject



65
66
67
68
69
70
71
72
73
# File 'lib/i2c/device/adt7410.rb', line 65

def read_status
  status = i2cget(0x02).unpack("C")[0]
  {
    T_low:  status[4] == 1,
    T_high: status[5] == 1,
    T_crit: status[6] == 1,
    RDY:    status[7] == 0,
  }
end

#set_T_crit(value) ⇒ Object



130
131
132
# File 'lib/i2c/device/adt7410.rb', line 130

def set_T_crit(value)
  set_point(0x08, value)
end

#set_T_high(value) ⇒ Object



122
123
124
# File 'lib/i2c/device/adt7410.rb', line 122

def set_T_high(value)
  set_point(0x04, value)
end

#set_T_hyst(value) ⇒ Object



134
135
136
# File 'lib/i2c/device/adt7410.rb', line 134

def set_T_hyst(value)
  i2cset(0x0a, value)
end

#set_T_low(value) ⇒ Object



126
127
128
# File 'lib/i2c/device/adt7410.rb', line 126

def set_T_low(value)
  set_point(0x06, value)
end

#software_resetObject



83
84
85
# File 'lib/i2c/device/adt7410.rb', line 83

def software_reset
  i2cset(0x2f, 0x01)
end