Class: Denko::Sensor::HDC1080

Inherits:
Object
  • Object
show all
Includes:
Behaviors::Lifecycle, Behaviors::Poller, I2C::Peripheral, HumidityHelper, TemperatureHelper
Defined in:
lib/denko/sensor/hdc1080.rb

Constant Summary collapse

I2C_ADDRESS =
0x40
CONFIG_ADDRESS =

Config Register

0x02
CONFIG_DEFAULT =
0x10
RESET_MASK =
0b10000000
HEATER_MASK =
0b00100000
BATTERY_MASK =
0b00001000
TEMPERATURE_ADDRESS =

Reading registers

0x00
HUMIDITY_ADDRESS =
0x01
TEMPERATURE_RESOLUTION_MASK =

Conversion times from datasheet, but not really used.

0b00000100
TEMPERATURE_RESOLUTIONS =
{
  14 => { bits: 0b0, conversion_time: 0.007 },
  11 => { bits: 0b1, conversion_time: 0.004 },
}
HUMIDITY_RESOLUTION_MASK =
0b00000011
HUMIDITY_RESOLUTIONS =
{
  14 => { bits: 0b00, conversion_time: 0.007 },
  11 => { bits: 0b01, conversion_time: 0.004 },
  8  => { bits: 0b10, conversion_time: 0.003 },
}

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Constants included from Behaviors::Reader

Behaviors::Reader::READ_WAIT_TIME

Constants included from I2C::Peripheral

I2C::Peripheral::I2C_FREQUENCY, I2C::Peripheral::I2C_REPEATED_START

Instance Attribute Summary collapse

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods included from HumidityHelper

#humidity

Methods included from TemperatureHelper

#temperature, #temperature_f, #temperature_k

Methods included from Behaviors::Lifecycle

included

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

Methods included from Behaviors::Threaded

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

Methods included from Behaviors::Reader

#read, #read_busy?, #read_nb, #read_raw, #read_using, #update

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #remove_callback, #update

Methods included from I2C::Peripheral

#address, #i2c_default, #i2c_read, #i2c_read_raw, #i2c_write

Methods included from Behaviors::BusPeripheralAddressed

#address

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#humidity_resolutionObject

Returns the value of attribute humidity_resolution.



85
86
87
# File 'lib/denko/sensor/hdc1080.rb', line 85

def humidity_resolution
  @humidity_resolution
end

#temperature_resolutionObject

Returns the value of attribute temperature_resolution.



85
86
87
# File 'lib/denko/sensor/hdc1080.rb', line 85

def temperature_resolution
  @temperature_resolution
end

Instance Method Details

#_readObject



131
132
133
134
135
136
# File 'lib/denko/sensor/hdc1080.rb', line 131

def _read
  _start_conversion
  # This is way more than given conversion times, but unreliable without.
  sleep 0.200
  _read_values
end

#_read_valuesObject



143
144
145
146
# File 'lib/denko/sensor/hdc1080.rb', line 143

def _read_values
  # Read 2 bytes each for temperature and humidity.
  i2c_read(4)
end

#_start_conversionObject



138
139
140
141
# File 'lib/denko/sensor/hdc1080.rb', line 138

def _start_conversion
  # Writing to temperature register triggers both reads.
  i2c_write [TEMPERATURE_ADDRESS]
end

#battery_low?Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/denko/sensor/hdc1080.rb', line 66

def battery_low?
  config = i2c_read_raw(2, register: CONFIG_ADDRESS)
  (config[0] & BATTERY_MASK) > 0
end

#device_idObject



111
112
113
# File 'lib/denko/sensor/hdc1080.rb', line 111

def device_id
  device_info[:device_id]
end

#device_infoObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/denko/sensor/hdc1080.rb', line 115

def device_info
  return @device_info if @device_info

  man_id_bytes = i2c_read_raw(2, register: 0xFE)
  dev_id_bytes = i2c_read_raw(2, register: 0xFF)
  serial_l = i2c_read_raw(2, register: 0xFD)
  serial_m = i2c_read_raw(2, register: 0xFC)
  serial_h = i2c_read_raw(2, register: 0xFB)

  @device_info = {
    manufacturer_id: man_id_bytes[0] << 8 | man_id_bytes[1],
    device_id:       dev_id_bytes[0] << 8 | dev_id_bytes[1],
    serial_number:   serial_h[0] << 32 | serial_h[1] << 24 | serial_m[0] << 16 | serial_m[1] << 8 | serial_l[0],
  }
end

#heater_offObject



61
62
63
64
# File 'lib/denko/sensor/hdc1080.rb', line 61

def heater_off
  @config &= ~HEATER_MASK
  write_config
end

#heater_off?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/denko/sensor/hdc1080.rb', line 52

def heater_off?
  !heater_on?
end

#heater_onObject



56
57
58
59
# File 'lib/denko/sensor/hdc1080.rb', line 56

def heater_on
  @config |= HEATER_MASK
  write_config
end

#heater_on?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/denko/sensor/hdc1080.rb', line 48

def heater_on?
  (@config & HEATER_MASK) > 0
end

#manufacturer_idObject



107
108
109
# File 'lib/denko/sensor/hdc1080.rb', line 107

def manufacturer_id
  device_info[:manufacturer_id]
end

#pre_callback_filter(bytes) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/denko/sensor/hdc1080.rb', line 148

def pre_callback_filter(bytes)
  raw_t = bytes[0] << 8 | bytes[1]
  raw_h = bytes[2] << 8 | bytes[3]

  reading[:temperature] = ((raw_t.to_f / 2 ** 16) * 165) - 40
  reading[:humidity]    =  (raw_h.to_f / 2 ** 16) * 100

  return nil unless (reading[:temperature] && reading[:humidity])

  reading
end

#readingObject



27
28
29
# File 'lib/denko/sensor/hdc1080.rb', line 27

def reading
  @reading ||= { temperature: nil, humidity: nil }
end

#resetObject



35
36
37
38
39
40
41
# File 'lib/denko/sensor/hdc1080.rb', line 35

def reset
  @config = CONFIG_DEFAULT
  @temperature_resolution = 14
  @humidity_resolution    = 14
  write_config(@config | RESET_MASK)
  sleep 0.010
end

#serial_numberObject



103
104
105
# File 'lib/denko/sensor/hdc1080.rb', line 103

def serial_number
  device_info[:serial_number]
end

#stateObject



23
24
25
# File 'lib/denko/sensor/hdc1080.rb', line 23

def state
  @state ||= { temperature: nil, humidity: nil }
end

#update_state(reading) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/denko/sensor/hdc1080.rb', line 160

def update_state(reading)
  @state_mutex.lock
  @state[:temperature] = reading[:temperature]
  @state[:humidity]    = reading[:humidity]
  @state_mutex.unlock

  # Reset so pre_callback_filter can check for both values.
  reading[:temperature] = nil
  reading[:humidity]    = nil

  @state
end

#write_config(config = @config) ⇒ Object



43
44
45
46
# File 'lib/denko/sensor/hdc1080.rb', line 43

def write_config(config=@config)
  # Actually 2 bytes, but 2nd byte is reserved and 0, so ignore it.
  i2c_write [CONFIG_ADDRESS, config, 0]
end