From 0380a6395a8d577ae56a988584d82bdb3df9b35a Mon Sep 17 00:00:00 2001 From: vickash Date: Sun, 8 Oct 2023 19:26:38 -0400 Subject: [PATCH] Change AS312 class to GenericPIR and use it for HC-SR501 sensor too --- HARDWARE.md | 12 ++++----- examples/sensor/as312.rb | 16 ------------ examples/sensor/generic_pir.rb | 25 +++++++++++++++++++ lib/denko/sensor.rb | 2 +- lib/denko/sensor/{as312.rb => generic_pir.rb} | 2 +- 5 files changed, 33 insertions(+), 24 deletions(-) delete mode 100644 examples/sensor/as312.rb create mode 100644 examples/sensor/generic_pir.rb rename lib/denko/sensor/{as312.rb => generic_pir.rb} (75%) diff --git a/HARDWARE.md b/HARDWARE.md index a2a67aa..2ac760e 100644 --- a/HARDWARE.md +++ b/HARDWARE.md @@ -204,11 +204,11 @@ Polling and reading follow a call and response pattern. | TCS34725 | :heart: | I2C | `Sensor::TCS34725` | RGB | APDS9960 | :heart: | I2C | `Sensor::APDS9960` | Proximity, RGB, Gesture -### PIR Sensors -| Name | Status | Interface | Component Class | Notes | -| :--------------- | :------: | :-------- | :--------------- |------ | -| HC-SR501 | :yellow_heart: | Digital In | `DigitalIO::Input` | PIR. Needs class: `Sensor::HC-SR501` -| AS312 | :green_heart: | Digital In | `Sensor::AS312` | PIR (based on `DigitalIO::Input`) +### PIR Motion Sensors +| Name | Status | Interface | Component Class | Notes | +| :--------------- | :------: | :-------- | :--------------- |------ | +| HC-SR501 | :green_heart: | Digital In | `Sensor::GenericPIR` | +| AS312 | :green_heart: | Digital In | `Sensor::GenericPIR` | ### Distance Sensors @@ -219,7 +219,7 @@ Polling and reading follow a call and response pattern. | VL53L0X | :heart: | I2C | `Sensor::VL53L0X` | Laser, 30 - 1000mm | GP2Y0E03 | :heart: | I2C | `Sensor::GP2Y0E03` | Infrared, 40 - 500mm -### Motion Sensors +### Inertial Measurement Units | Name | Status | Interface | Component Class | Notes | | :--------------- | :------: | :-------- | :--------------- |------ | diff --git a/examples/sensor/as312.rb b/examples/sensor/as312.rb deleted file mode 100644 index e260e84..0000000 --- a/examples/sensor/as312.rb +++ /dev/null @@ -1,16 +0,0 @@ -# -# Example using AS312 PIR motion sensor. -# -require 'bundler/setup' -require 'denko' - -board = Denko::Board.new(Denko::Connection::Serial.new) -sensor = Denko::Sensor::AS312.new(board: board, pin: 8) - -sensor.on_motion_start { print "Motion detected! \r" } -sensor.on_motion_stop { print "No motion detected...\r" } - -# Trigger an initial read. -sensor.read - -sleep diff --git a/examples/sensor/generic_pir.rb b/examples/sensor/generic_pir.rb new file mode 100644 index 0000000..90fa81a --- /dev/null +++ b/examples/sensor/generic_pir.rb @@ -0,0 +1,25 @@ +# +# Example using a PIR motion sensor. Tested with AS312 and HC-SR501 sensors. +# +# General notes: +# - Both sensors have a few seconds "dead time" after "motion stop" (logical 0), where further +# motion will not trigger "motion start" (logical 1). +# +# HC-SR501 notes: +# - Needs some time to warm up and start working properly. +# - Set the time potentiometer to its lowest value. +# - Make sure retriggering is enabled. It might be default, but there's a jumper to solder too. +# +require 'bundler/setup' +require 'denko' + +board = Denko::Board.new(Denko::Connection::Serial.new) +sensor = Denko::Sensor::GenericPIR.new(board: board, pin: 8) + +sensor.on_motion_start { print "Motion detected! \r" } +sensor.on_motion_stop { print "No motion detected...\r" } + +# Read initial state. +sensor.read + +sleep diff --git a/lib/denko/sensor.rb b/lib/denko/sensor.rb index 362ae53..e844f41 100644 --- a/lib/denko/sensor.rb +++ b/lib/denko/sensor.rb @@ -14,6 +14,6 @@ module Sensor autoload :SHT3X, "#{__dir__}/sensor/sht3x" autoload :QMP6988, "#{__dir__}/sensor/qmp6988" autoload :RCWL9620, "#{__dir__}/sensor/rcwl9620" - autoload :AS312, "#{__dir__}/sensor/as312" + autoload :GenericPIR, "#{__dir__}/sensor/generic_pir" end end diff --git a/lib/denko/sensor/as312.rb b/lib/denko/sensor/generic_pir.rb similarity index 75% rename from lib/denko/sensor/as312.rb rename to lib/denko/sensor/generic_pir.rb index 9aec591..88912d4 100644 --- a/lib/denko/sensor/as312.rb +++ b/lib/denko/sensor/generic_pir.rb @@ -1,6 +1,6 @@ module Denko module Sensor - class AS312 < DigitalIO::Input + class GenericPIR < DigitalIO::Input alias :on_motion_stop :on_low alias :on_motion_start :on_high end