diff --git a/firmware/core/core.ino b/firmware/core/core.ino index d9e1362..cffda0c 100644 --- a/firmware/core/core.ino +++ b/firmware/core/core.ino @@ -1,6 +1,7 @@ #include #include #include "protocol.h" +#include "common.h" #include "peripheral.h" const int led = 13; //LED pin config @@ -89,13 +90,21 @@ break; } break; - case PacketTypeRequest: case PacketTypeResponse: - case PacketTypeEvent: break; } } +void sendEventPacket(uint8_t eventType) +{ + struct packet p; + p.characteristicAndVersion = PacketIdentifier; + p.type = PacketTypeEvent; + p.payloadSize = sizeof(eventPayload); + p.payload.event.eventType = eventType; + client.write((byte*)&p, sizeof(struct packet)); +} + void receivePacket(byte charRecved) { static uint8_t packetBuffer[MAX_PACKET_SIZE]; @@ -132,7 +141,6 @@ static uint8_t connect_retries = 0; if(door.UpdateState()){ - Serial.println("Door opened illegally"); alarm.On(); } diff --git a/firmware/core/peripheral.cpp b/firmware/core/peripheral.cpp index 644ba9a..cfa9fe7 100644 --- a/firmware/core/peripheral.cpp +++ b/firmware/core/peripheral.cpp @@ -1,4 +1,6 @@ #include "peripheral.h" +#include "protocol.h" +#include "common.h" void Door::Init() { @@ -38,6 +40,8 @@ case DoorLocked: if (detect()) { state = DoorOpened; + Serial.println("DoorOpened Illegally"); + sendEventPacket(DoorDidOpen); return true; } break; @@ -46,6 +50,7 @@ digitalWrite(lockMagnetPin, HIGH); state = DoorOpened; Serial.println("DoorOpened"); + sendEventPacket(DoorDidOpen); } break; case DoorOpened: @@ -53,6 +58,7 @@ digitalWrite(lockMagnetPin, HIGH); state = DoorLocked; Serial.println("DoorLocked"); + sendEventPacket(DoorDidClose); } break; } @@ -67,6 +73,9 @@ void Alarm::setAlarm(bool _on) { - on = _on; - digitalWrite(alarmPin, _on ? HIGH : LOW); + if(on != _on){ + on = _on; + digitalWrite(alarmPin, _on ? HIGH : LOW); + sendEventPacket(_on ? AlarmDidOn : AlarmDidOff); + } }