examples/sensor/bme280.rb in denko-0.13.3 vs examples/sensor/bme280.rb in denko-0.13.4
- old
+ new
@@ -3,32 +3,15 @@
#
require 'bundler/setup'
require 'denko'
board = Denko::Board.new(Denko::Connection::Serial.new)
-
-#
-# Default pins for the I2C0 (first) interface on most chips:
-#
-# ATmega 328p: SDA = 'A4' SCL = 'A5' - Arduino Uno, Nano
-# ATmega 32u4: SDA = 2 SCL = 3 - Arduino Leonardo, Pro Micro
-# ATmega1280 / 2560: SDA = 20 SCL = 21 - Arduino Mega
-# SAM3X8E: SDA = 20 SCL = 21 - Arduino Due
-# SAMD21G18: SDA = 20 SCL = 21 - Arduino Zero, M0, M0 Pro
-# ESP8266: SDA = 4 SCL = 5
-# ESP32: SDA = 21 SCL = 22
-# RP2040: SDA = 4 SCL = 5 - Raspberry Pi Pico (W)
-#
-# Only give the SDA pin of the I2C bus. SCL (clock) pin must be
-# connected for it to work, but we don't need to control it.
-#
bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
-sensor = Denko::Sensor::BME280.new(bus: bus, address: 0x76)
-
+sensor = Denko::Sensor::BME280.new(bus: bus) # address: 0x76 default
# Use A BMP280 with no humidity instead.
-# sensor = Denko::Sensor::BMP280.new(bus: bus, address: 0x76)
+# sensor = Denko::Sensor::BMP280.new(bus: bus) # address: 0x76
# Default reading mode is oneshot ("forced" in datasheet).
# sensor.oneshot_mode
# Enable oversampling independently on each sensor.
@@ -42,34 +25,14 @@
# sensor.iir_coefficient = 4
# Print raw config register bits.
# print sensor.config_register_bits
-def display_reading(reading)
- # Time
- print "#{Time.now.strftime '%Y-%m-%d %H:%M:%S'} - "
-
- # Temperature
- formatted_temp = reading[:temperature].round(2).to_s.ljust(5, '0')
- print "Temperature: #{formatted_temp} \xC2\xB0C"
-
- # Pressure
- if reading[:pressure]
- formatted_pressure = (reading[:pressure] / 101325).round(5).to_s.ljust(7, '0')
- print " | Pressure #{formatted_pressure} atm"
- end
-
- # Humidity
- if reading[:humidity]
- formatted_humidity = reading[:humidity].round(2).to_s.ljust(5, '0')
- print " | Humidity #{formatted_humidity} %"
- end
-
- puts
-end
+# Get the shared #print_tph_reading method to print readings neatly.
+require_relative 'neat_tph_readings'
-# Poll the sensor and print readings.
+# Poll it and print readings.
sensor.poll(5) do |reading|
- display_reading(reading)
+ print_tph_reading(reading)
end
sleep