Class: Denko::Sensor::AHT20

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

Constant Summary collapse

INIT_AND_CALIBRATE =

Changed constants compared to AHT10. Always access with self.class::CONSTANT_NAME in shared methods coming from the superclass.

[0xBE, 0x08, 0x00]
POWER_ON_DELAY =
0.100
DATA_LENGTH =
7
CRC_INITIAL_VALUE =

CRC Constants (unique to AHT20)

0xFF
CRC_POLYNOMIAL =
0x31
MSBIT_MASK =
0x80

Constants inherited from AHT10

Denko::Sensor::AHT10::BUSY, Denko::Sensor::AHT10::CALIBRATED, Denko::Sensor::AHT10::COMMAND_DELAY, Denko::Sensor::AHT10::MEASURE_DELAY, Denko::Sensor::AHT10::READ_STATUS_REGISTER, Denko::Sensor::AHT10::RESET_DELAY, Denko::Sensor::AHT10::SOFT_RESET, Denko::Sensor::AHT10::START_MEASUREMENT

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, #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

#before_initialize, #i2c_read, #i2c_write

Methods included from Behaviors::BusPeripheralAddressed

#before_initialize

Methods included from Behaviors::BusPeripheral

#atomically, #before_initialize

Methods included from Behaviors::Component

#initialize, #micro_delay

Methods inherited from AHT10

#_read, #after_initialize, #before_initialize, #busy?, #calibrate, #calibrated?, #read_status_register, #reset, #update_state

Instance Method Details

#calculate_crc(bytes) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/denko/sensor/aht.rb', line 145

def calculate_crc(bytes)
  crc = CRC_INITIAL_VALUE

  # Ignore last byte. That's the CRC value to compare with.
  bytes.take(bytes.length - 1).each do |byte|
    crc = crc ^ byte
    8.times do
      if (crc & MSBIT_MASK) > 0
        crc = (crc << 1) ^ CRC_POLYNOMIAL
      else
        crc = crc << 1
      end
    end
  end
  
  # Limit CRC size to 8 bits.
  crc = crc & 0xFF
end

#pre_callback_filter(bytes) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/denko/sensor/aht.rb', line 131

def pre_callback_filter(bytes)
  # Handle reading status byte only.
  return super(bytes) if bytes.length == 1

  # Normal readings are 7 bytes given as:
  #   [STATUS, H19-H12, H11-H4, H3-H0+T19-T16, T15-T8, T7-T0, CRC]
  #
  # Ignore everything if CRC fails.
  return nil if calculate_crc(bytes) != bytes.last

  # Same calculation as AHT10 once CRC passes.
  super(bytes)
end