Class: Denko::Sensor::HDC1080
Constant Summary
collapse
- I2C_ADDRESS =
0x40
- CONFIG_ADDRESS =
0x02
- CONFIG_DEFAULT =
0x10
- RESET_MASK =
0b10000000
- HEATER_MASK =
0b00100000
- BATTERY_MASK =
0b00001000
- TEMPERATURE_ADDRESS =
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 },
}
Behaviors::Lifecycle::CALLBACK_METHODS
Behaviors::Reader::READ_WAIT_TIME
I2C::Peripheral::I2C_FREQUENCY, I2C::Peripheral::I2C_REPEATED_START
Instance Attribute Summary collapse
#interrupts_enabled, #thread
#i2c_frequency, #i2c_repeated_start
#board, #params
Instance Method Summary
collapse
#humidity
#temperature, #temperature_f, #temperature_k
included
#poll, #poll_using, #stop
#enable_interrupts, included, #mruby_thread_check, #stop, #stop_thread, #threaded, #threaded_loop
#read, #read_busy?, #read_nb, #read_raw, #read_using, #update
#add_callback, #callbacks, #remove_callback, #update
#address, #i2c_default, #i2c_read, #i2c_read_raw, #i2c_write
#address
#atomically
#initialize, #micro_delay
Instance Attribute Details
#humidity_resolution ⇒ Object
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_resolution ⇒ Object
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
#_read ⇒ Object
131
132
133
134
135
136
|
# File 'lib/denko/sensor/hdc1080.rb', line 131
def _read
_start_conversion
sleep 0.200
_read_values
end
|
#_read_values ⇒ Object
143
144
145
146
|
# File 'lib/denko/sensor/hdc1080.rb', line 143
def _read_values
i2c_read(4)
end
|
#_start_conversion ⇒ Object
138
139
140
141
|
# File 'lib/denko/sensor/hdc1080.rb', line 138
def _start_conversion
i2c_write [TEMPERATURE_ADDRESS]
end
|
#battery_low? ⇒ 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_id ⇒ Object
111
112
113
|
# File 'lib/denko/sensor/hdc1080.rb', line 111
def device_id
device_info[:device_id]
end
|
#device_info ⇒ Object
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_off ⇒ Object
61
62
63
64
|
# File 'lib/denko/sensor/hdc1080.rb', line 61
def heater_off
@config &= ~HEATER_MASK
write_config
end
|
#heater_off? ⇒ Boolean
52
53
54
|
# File 'lib/denko/sensor/hdc1080.rb', line 52
def heater_off?
!heater_on?
end
|
#heater_on ⇒ Object
56
57
58
59
|
# File 'lib/denko/sensor/hdc1080.rb', line 56
def heater_on
@config |= HEATER_MASK
write_config
end
|
#heater_on? ⇒ Boolean
48
49
50
|
# File 'lib/denko/sensor/hdc1080.rb', line 48
def heater_on?
(@config & HEATER_MASK) > 0
end
|
#manufacturer_id ⇒ Object
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
|
#reading ⇒ Object
27
28
29
|
# File 'lib/denko/sensor/hdc1080.rb', line 27
def reading
@reading ||= { temperature: nil, humidity: nil }
end
|
#reset ⇒ Object
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_number ⇒ Object
103
104
105
|
# File 'lib/denko/sensor/hdc1080.rb', line 103
def serial_number
device_info[:serial_number]
end
|
#state ⇒ Object
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
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)
i2c_write [CONFIG_ADDRESS, config, 0]
end
|