Class: Denko::Sensor::AHT10
Direct Known Subclasses
AHT20
Constant Summary
collapse
- INIT_AND_CALIBRATE =
[0xE1, 0x08, 0x00]
- READ_STATUS_REGISTER =
[0x71]
- START_MEASUREMENT =
[0xAC, 0x33, 0x00]
- SOFT_RESET =
[0xBA]
- POWER_ON_DELAY =
0.100
- COMMAND_DELAY =
0.010
- MEASURE_DELAY =
0.080
- RESET_DELAY =
0.020
- CRC_INITIAL_VALUE =
0xFF
- CRC_POLYNOMIAL =
0x31
- MSBIT_MASK =
0x80
- CALIBRATED =
0x08
- BUSY =
0x80
- DATA_LENGTH =
Number of bytes in each reading.
6
Instance Attribute Summary
#interrupts_enabled, #thread
#callback_mutex
#i2c_frequency, #i2c_repeated_start
#address
#board
Instance Method Summary
collapse
#poll, #poll_using, #stop
#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop
#read, #read_using, #wait_for_read
#add_callback, #callbacks, #initialize, #remove_callback, #update
#initialize, #state
#i2c_read, #i2c_write
#atomically
#initialize, #micro_delay
Instance Method Details
#_read ⇒ Object
75
76
77
78
79
|
# File 'lib/denko/sensor/aht.rb', line 75
def _read
i2c_write(START_MEASUREMENT)
sleep(MEASURE_DELAY)
i2c_read(nil, self.class::DATA_LENGTH)
end
|
#after_initialize(options = {}) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/denko/sensor/aht.rb', line 36
def after_initialize(options={})
super(options)
@reading = { temperature: nil, humidity: nil }
self.state = { temperature: nil, humidity: nil }
@status_register = 0x00
sleep(self.class::POWER_ON_DELAY)
reset
calibrate
end
|
#before_initialize(options = {}) ⇒ Object
31
32
33
34
|
# File 'lib/denko/sensor/aht.rb', line 31
def before_initialize(options={})
@i2c_address = 0x38
super(options)
end
|
#busy? ⇒ Boolean
59
60
61
62
|
# File 'lib/denko/sensor/aht.rb', line 59
def busy?
@status_register & BUSY
end
|
#calibrate ⇒ Object
64
65
66
67
68
|
# File 'lib/denko/sensor/aht.rb', line 64
def calibrate
i2c_write(self.class::INIT_AND_CALIBRATE)
sleep(COMMAND_DELAY)
read_status_register
end
|
#calibrated? ⇒ Boolean
54
55
56
57
|
# File 'lib/denko/sensor/aht.rb', line 54
def calibrated?
@status_register & CALIBRATED
end
|
#pre_callback_filter(bytes) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/denko/sensor/aht.rb', line 81
def pre_callback_filter(bytes)
if bytes.length == 1
@status_register = bytes[0]
return nil
end
@status_register = bytes[0]
h_raw = ((bytes[1] << 16) | (bytes[2] << 8) | (bytes[3])) >> 4
@reading[:humidity] = (h_raw.to_f / 2**20) * 100
t_raw = ((bytes[3] & 0x0F) << 16) | (bytes[4] << 8) | bytes[5]
@reading[:temperature] = (t_raw.to_f / 2**20) * 200 - 50
@reading
end
|
#read_status_register ⇒ Object
#reset ⇒ Object
49
50
51
52
|
# File 'lib/denko/sensor/aht.rb', line 49
def reset
i2c_write(SOFT_RESET)
sleep(RESET_DELAY)
end
|
#update_state(reading) ⇒ Object
103
104
105
106
107
108
|
# File 'lib/denko/sensor/aht.rb', line 103
def update_state(reading)
@state_mutex.synchronize do
@state[:temperature] = reading[:temperature]
@state[:humidity] = reading[:humidity]
end
end
|