Class: Denko::Sensor::AHT2X

Inherits:
AHT1X
  • Object
show all
Defined in:
lib/denko/sensor/aht.rb

Direct Known Subclasses

AHT3X

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 AHT1X

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

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_ADDRESS, I2C::Peripheral::I2C_FREQUENCY, I2C::Peripheral::I2C_REPEATED_START

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::State

#state

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods inherited from AHT1X

#_read, #busy?, #calibrate, #calibrated?, #read_status_register, #reading, #reset, #state, #update_state

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, #read_busy?, #read_nb, #read_raw, #read_using, #update

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #remove_callback, #update

Methods included from Behaviors::State

#update_state

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

#calculate_crc(bytes) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/denko/sensor/aht.rb', line 136

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



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/denko/sensor/aht.rb', line 122

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