Class: Denko::Sensor::AHT10

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

Direct Known Subclasses

AHT20

Constant Summary collapse

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

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

Methods included from Behaviors::Threaded

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

Methods included from Behaviors::Reader

#read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

Methods included from I2C::Peripheral

#i2c_read, #i2c_write

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_readObject



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)

  # Avoid repeated memory allocation for callback data and state.
  @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

Returns:

  • (Boolean)


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

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

#calibrateObject



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

Returns:

  • (Boolean)


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

def calibrated?
  # Should always be true, since INIT_AND_CALIBRATE always sets the calibration bit.
  @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)
  # Handle reading status byte only.
  if bytes.length == 1
    @status_register = bytes[0]
    return nil
  end

  # 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



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

def read_status_register
  read_using -> { i2c_read(READ_STATUS_REGISTER, 1) }
  sleep(COMMAND_DELAY)
end

#resetObject



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