pir plugin

This commit is contained in:
2018-12-03 15:34:27 +01:00
parent 65a43fb667
commit d906286eda
5 changed files with 108 additions and 0 deletions

View File

@@ -26,6 +26,18 @@ lib_deps = ${common.lib_deps}
lib_ignore = lib_ignore =
Ai Esp32 Rotary Encoder 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 = +<*> -<examples/> +<examples/pir/> -<inputs/> +<inputs/pir> +<inputs/digital>
lib_deps = ${common.lib_deps}
lib_ignore =
Ai Esp32 Rotary Encoder
[env:audio] [env:audio]
platform = ${common.platform} platform = ${common.platform}
board = ${common.board} board = ${common.board}

20
src/examples/pir/config.h Normal file
View File

@@ -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

22
src/examples/pir/main.cpp Normal file
View File

@@ -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();
}

View File

@@ -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;
}
}
}

View File

@@ -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 <functional>
#include <vector>
#include <FS.h>
#include <TaskSchedulerDeclarations.h>
#include <Plugin.h>
#include <utils/print.h>
#include <utils/misc.h>
#include <inputs/digital/DigitalInputPlugin.h>
#include <GpioConfig.h>
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