From d906286eda8d8b7d29b1b72ca791e97d95178890 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 3 Dec 2018 15:34:27 +0100 Subject: [PATCH] pir plugin --- platformio.ini | 12 ++++++++++++ src/examples/pir/config.h | 20 ++++++++++++++++++++ src/examples/pir/main.cpp | 22 ++++++++++++++++++++++ src/inputs/pir/PirInputPlugin.cpp | 26 ++++++++++++++++++++++++++ src/inputs/pir/PirInputPlugin.h | 28 ++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 src/examples/pir/config.h create mode 100644 src/examples/pir/main.cpp create mode 100644 src/inputs/pir/PirInputPlugin.cpp create mode 100644 src/inputs/pir/PirInputPlugin.h diff --git a/platformio.ini b/platformio.ini index 678a6b5..230f6d7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,6 +26,18 @@ lib_deps = ${common.lib_deps} lib_ignore = Ai Esp32 Rotary Encoder +[env:pir] +platform = ${common.platform} +board = ${common.board} +framework = ${common.framework} +upload_speed = ${common.upload_speed} +monitor_baud = ${common.monitor_baud} +build_flags = -DSPROCKET_PRINT=1 +src_filter = +<*> - + - + + +lib_deps = ${common.lib_deps} +lib_ignore = + Ai Esp32 Rotary Encoder + [env:audio] platform = ${common.platform} board = ${common.board} diff --git a/src/examples/pir/config.h b/src/examples/pir/config.h new file mode 100644 index 0000000..c236de4 --- /dev/null +++ b/src/examples/pir/config.h @@ -0,0 +1,20 @@ +#ifndef __DEVICE_CONFIG__ +#define __DEVICE_CONFIG__ + +// Scheduler +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STD_FUNCTION +#define _TASK_PRIORITY + +// Chip +#define SPROCKET_TYPE "SPROCKET" +#define SERIAL_BAUD_RATE 115200 +#define STARTUP_DELAY 1000 + +// PIR +#define PIR_THRESHOLD 16 +#define PIR_POLL_INTERVAL 50 +#define PIR_TOPIC "pir" +#define PIR_PIN D3 + +#endif \ No newline at end of file diff --git a/src/examples/pir/main.cpp b/src/examples/pir/main.cpp new file mode 100644 index 0000000..9ccc609 --- /dev/null +++ b/src/examples/pir/main.cpp @@ -0,0 +1,22 @@ +#include "config.h" +#include "Sprocket.h" +#include "inputs/pir/PirInputPlugin.h" + +Sprocket *sprocket; + +void setup() +{ + sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE}); + sprocket->addPlugin( + new PirInputPlugin({PIR_PIN, PIR_THRESHOLD, PIR_POLL_INTERVAL, PIR_TOPIC})); + sprocket->subscribe(PIR_TOPIC, bind([](String label, String val){ + PRINT_MSG(Serial, label.c_str(), val.c_str()); + }, PIR_TOPIC, _1)); + sprocket->activate(); +} + +void loop() +{ + sprocket->loop(); + yield(); +} \ No newline at end of file diff --git a/src/inputs/pir/PirInputPlugin.cpp b/src/inputs/pir/PirInputPlugin.cpp new file mode 100644 index 0000000..9ee82e8 --- /dev/null +++ b/src/inputs/pir/PirInputPlugin.cpp @@ -0,0 +1,26 @@ +#include "PirInputPlugin.h" + +void PirInputPlugin::checkInput() +{ + val = digitalRead(config.pin); // read input value + if (val == HIGH) + { // check if the input is HIGH + if (pirState == LOW) + { + // we have just turned on + publish(config.topic, "1"); + // We only want to print on the output change, not state + pirState = HIGH; + } + } + else + { + if (pirState == HIGH) + { + // we have just turned of + publish(config.topic, "0"); + // We only want to print on the output change, not state + pirState = LOW; + } + } +} diff --git a/src/inputs/pir/PirInputPlugin.h b/src/inputs/pir/PirInputPlugin.h new file mode 100644 index 0000000..e657595 --- /dev/null +++ b/src/inputs/pir/PirInputPlugin.h @@ -0,0 +1,28 @@ +#ifndef __PIR_INPUT__PLUGIN_H__ +#define __PIR_INPUT__PLUGIN_H__ + +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STD_FUNCTION + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::placeholders; + +class PirInputPlugin : public DigitalInputPlugin { + public: + PirInputPlugin(GpioConfig cfg) : DigitalInputPlugin(cfg){} + int pirState = LOW; // we start, assuming no motion detected + int val = 0; + void checkInput(); +}; + +#endif