Class: Denko::Sensor::AHT1X

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

Direct Known Subclasses

AHT2X

Constant Summary collapse

I2C_ADDRESS =
0x38
INIT_AND_CALIBRATE =

Commands

[0xE1, 0x08, 0x00]
READ_STATUS_REGISTER =
[0x71]
START_MEASUREMENT =
[0xAC, 0x33, 0x00]
SOFT_RESET =
[0xBA]
POWER_ON_DELAY =

Delay Times (in seconds)

0.100
COMMAND_DELAY =
0.010
MEASURE_DELAY =
0.080
RESET_DELAY =
0.020
CRC_INITIAL_VALUE =

CRC Constants

0xFF
CRC_POLYNOMIAL =
0x31
MSBIT_MASK =
0x80
CALIBRATED =

Status Register Masks

0x08
BUSY =
0x80
DATA_LENGTH =

Number of bytes in each reading.

6

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

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 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 Method Details

#_readObject



77
78
79
80
81
# File 'lib/denko/sensor/aht.rb', line 77

def _read
  i2c_write(START_MEASUREMENT)
  sleep(MEASURE_DELAY)
  i2c_read(self.class::DATA_LENGTH)
end

#busy?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/denko/sensor/aht.rb', line 60

def busy?
  # Should always be false once correct wait times are used.
  @status_register & BUSY
end

#calibrateObject



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

def calibrate
  i2c_write(self.class::INIT_AND_CALIBRATE)
  sleep(COMMAND_DELAY)
  read_status_register
end

#calibrated?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/denko/sensor/aht.rb', line 55

def calibrated?
  # Should always be true, since INIT_AND_CALIBRATE always sets the calibration bit.
  @status_register & CALIBRATED
end

#pre_callback_filter(bytes) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/denko/sensor/aht.rb', line 83

def pre_callback_filter(bytes)
  # Normal readings are 6 bytes given as:
  #   [STATUS, H19-H12, H11-H4, H3-H0+T19-T16, T15-T8, T7-T0]
  @status_register = bytes[0]

  # Humidity uses the upper 4 bits of the shared byte as its lowest 4 bits.
  h_raw = ((bytes[1] << 16) | (bytes[2] << 8) | (bytes[3])) >> 4
  reading[:humidity] = (h_raw.to_f / 2**20) * 100

  # Temperature uses the lower 4 bits of the shared byte as its highest 4 bits.
  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_registerObject



71
72
73
74
75
# File 'lib/denko/sensor/aht.rb', line 71

def read_status_register
  bytes = i2c_read_raw(1, register: READ_STATUS_REGISTER)
  @status_register = bytes[0] if bytes
  sleep(COMMAND_DELAY)
end

#readingObject



46
47
48
# File 'lib/denko/sensor/aht.rb', line 46

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

#resetObject



50
51
52
53
# File 'lib/denko/sensor/aht.rb', line 50

def reset
  i2c_write(SOFT_RESET)
  sleep(RESET_DELAY)
end

#stateObject



42
43
44
# File 'lib/denko/sensor/aht.rb', line 42

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

#update_state(reading) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/denko/sensor/aht.rb', line 99

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