Add class support for door sensor and led
This commit is contained in:
parent
24e993b47d
commit
8a419375aa
109
esp32/LedController.h
Normal file
109
esp32/LedController.h
Normal file
@ -0,0 +1,109 @@
|
||||
#include "alarm.h"
|
||||
|
||||
class LedController {
|
||||
private:
|
||||
int bluePin = 32;
|
||||
int redPin = 33;
|
||||
int greenPin = 25;
|
||||
AlarmStatus* status;
|
||||
|
||||
void turnOn(int led){
|
||||
digitalWrite(led, LED_ON_VAL);
|
||||
}
|
||||
|
||||
void turnOff(int led){
|
||||
digitalWrite(led, LED_OFF_VAL);
|
||||
}
|
||||
public:
|
||||
static const int PIN_LED_BLUE = 32;
|
||||
static const int PIN_LED_RED = 33;
|
||||
static const int PIN_LED_GREEN = 25;
|
||||
|
||||
static const int LED_OFF_VAL = 0;
|
||||
static const int LED_ON_VAL = 255;
|
||||
|
||||
LedController(AlarmStatus* statusObj){
|
||||
Serial.println("const.");
|
||||
status = statusObj;
|
||||
}
|
||||
void SetPins(int blueLedPin, int greenLedPin, int redLedPin){
|
||||
bluePin = blueLedPin;
|
||||
redPin = redLedPin;
|
||||
greenPin = greenLedPin;
|
||||
}
|
||||
void Init(){
|
||||
pinMode(bluePin, OUTPUT);
|
||||
pinMode(redPin, OUTPUT);
|
||||
pinMode(greenPin, OUTPUT);
|
||||
}
|
||||
|
||||
void Off(){
|
||||
turnOff(bluePin);
|
||||
turnOff(redPin);
|
||||
turnOff(greenPin);
|
||||
}
|
||||
|
||||
void Red(){
|
||||
turnOn(redPin);
|
||||
turnOff(greenPin);
|
||||
turnOff(bluePin);
|
||||
}
|
||||
|
||||
void Green(){
|
||||
turnOn(greenPin);
|
||||
turnOff(redPin);
|
||||
turnOff(bluePin);
|
||||
}
|
||||
|
||||
void Yellow(){
|
||||
turnOn(redPin);
|
||||
turnOn(greenPin);
|
||||
turnOff(bluePin);
|
||||
}
|
||||
|
||||
void Blue(){
|
||||
turnOn(bluePin);
|
||||
turnOff(redPin);
|
||||
turnOff(greenPin);
|
||||
}
|
||||
|
||||
void Error(){
|
||||
Red();
|
||||
delay(100);
|
||||
|
||||
Off();
|
||||
delay(100);
|
||||
|
||||
Red();
|
||||
delay(100);
|
||||
|
||||
Off();
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void Update(){
|
||||
if(!status->ledOn)
|
||||
{
|
||||
Off();
|
||||
return;
|
||||
}
|
||||
if(status->isFired)
|
||||
{
|
||||
Red();
|
||||
return;
|
||||
}
|
||||
if(status->doorStatus==DOOR_OPEN)
|
||||
{
|
||||
Yellow();
|
||||
return;
|
||||
}
|
||||
if(status->isArmed)
|
||||
{
|
||||
Blue();
|
||||
return;
|
||||
}
|
||||
Green();
|
||||
}
|
||||
};
|
||||
|
||||
|
2
esp32/Siren.h
Normal file
2
esp32/Siren.h
Normal file
@ -0,0 +1,2 @@
|
||||
#include "alarm.h"
|
||||
|
15
esp32/alarm.h
Normal file
15
esp32/alarm.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef _ALARM_H_
|
||||
#define _ALARM_H_
|
||||
|
||||
const int DOOR_OPEN = HIGH;
|
||||
const int DOOR_CLOSED = LOW;
|
||||
struct AlarmStatus{
|
||||
int doorStatus;
|
||||
bool doorChanged;
|
||||
bool isArmed;
|
||||
bool isFired;
|
||||
bool ledOn;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
48
esp32/doorSensor.h
Normal file
48
esp32/doorSensor.h
Normal file
@ -0,0 +1,48 @@
|
||||
#include "alarm.h"
|
||||
|
||||
AlarmStatus* status;
|
||||
|
||||
class DoorSensor {
|
||||
private:
|
||||
int DOOR_SENSOR_PIN = 23;
|
||||
AlarmStatus* status;
|
||||
int lastDoorState=1;
|
||||
|
||||
public:
|
||||
|
||||
static const int DEFAULT_DOOR_SENSOR_PIN=23;
|
||||
|
||||
DoorSensor(int doorSensorPin, AlarmStatus* statusObj){
|
||||
Serial.println("const.");
|
||||
DOOR_SENSOR_PIN = doorSensorPin;
|
||||
status = statusObj;
|
||||
}
|
||||
void Init(){
|
||||
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
|
||||
status->doorStatus = digitalRead(DOOR_SENSOR_PIN);
|
||||
}
|
||||
|
||||
bool IsDoorOpen(){
|
||||
return status->doorStatus == DOOR_OPEN;
|
||||
}
|
||||
bool IsDoorClosed(){
|
||||
return status->doorStatus == DOOR_CLOSED;
|
||||
}
|
||||
|
||||
void HandleDoor(){
|
||||
|
||||
//status->doorStatus = digitalRead(DOOR_SENSOR_PIN);
|
||||
lastDoorState = status->doorStatus; // save the last state
|
||||
status->doorStatus = digitalRead(DOOR_SENSOR_PIN); // read new state
|
||||
// state change: LOW -> HIGH
|
||||
if (lastDoorState == DOOR_CLOSED && status->doorStatus == DOOR_OPEN) {
|
||||
//handleOpen();
|
||||
status->doorChanged=true;
|
||||
}
|
||||
else
|
||||
if (lastDoorState == DOOR_OPEN && status->doorStatus == DOOR_CLOSED) { // state change: HIGH -> LOW
|
||||
// handleClose();
|
||||
status->doorChanged=true;
|
||||
}
|
||||
}
|
||||
};
|
@ -1,17 +1,67 @@
|
||||
#include <WiFi.h>
|
||||
#include <Arduino.h>
|
||||
#include "alarm.h"
|
||||
#include "doorSensor.h"
|
||||
#include "LedController.h"
|
||||
// #include <WiFi.h>
|
||||
|
||||
AlarmStatus s = {DOOR_OPEN, false, false, false, true};
|
||||
DoorSensor doorSensor(DoorSensor::DEFAULT_DOOR_SENSOR_PIN, &s);
|
||||
LedController led(&s);
|
||||
|
||||
void printStatus(){
|
||||
String msg = String("Door: ") + String(doorSensor.IsDoorOpen() ? "Open" : "Closed");
|
||||
msg = msg + " | Is Fired: " + String(s.isFired ? "Fired": "Not Fired");
|
||||
msg = msg + " | Led: " + String(s.ledOn ? "On": "Off");
|
||||
Serial.println(msg);
|
||||
}
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
delay(100);
|
||||
printStatus();
|
||||
doorSensor.Init();
|
||||
led.Init();
|
||||
printStatus();
|
||||
|
||||
|
||||
s.isArmed = doorSensor.IsDoorClosed();
|
||||
Serial.println("Alarm is " + String(s.isArmed ? "Armed" : "Disarmed"));
|
||||
// WiFi.mode(WIFI_STA);
|
||||
// WiFi.disconnect();
|
||||
// delay(100);
|
||||
|
||||
Serial.println("Stp Done");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("loop");
|
||||
delay(2000);
|
||||
doorSensor.HandleDoor();
|
||||
|
||||
if(s.doorChanged){
|
||||
DoorEvent();
|
||||
}
|
||||
s.doorChanged=false;
|
||||
|
||||
|
||||
led.Update();
|
||||
|
||||
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
||||
void DoorEvent(){
|
||||
if(doorSensor.IsDoorOpen()){
|
||||
Serial.println("The door-opening");
|
||||
if(!s.isArmed)
|
||||
{
|
||||
Serial.println("Alarm is dissarmed, not fireing");
|
||||
return;
|
||||
}
|
||||
Serial.println("Alarm is armed, firing");
|
||||
s.isFired=true;
|
||||
}else{
|
||||
delay(500);
|
||||
Serial.println("The door-closing");
|
||||
s.isArmed=true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user