Add syntax coloring map for SimpleGsm.

Replace LinkedList with SimpleList.

Remove PIR sensor completely.

Refactoring related to rfidFlush.

Now we can break while in enabling alarm.

Remove some print messages.
parent 4fc0fed8
Pipeline #2637066 skipped
......@@ -205,4 +205,4 @@ SimpleGsm::hangCall ()
return this->responseIsReceived(OK_RESPONSE_FORMAT, 3000);
}
char * SimpleGsm::OK_RESPONSE_FORMAT = "\r\nOK\r\n";
char * const SimpleGsm::OK_RESPONSE_FORMAT = "\r\nOK\r\n";
......@@ -60,9 +60,9 @@ class SimpleGsm : public SoftwareSerial
bool responseIsReceived (char * const pattern, const long timeOut);
static char * OK_RESPONSE_FORMAT;
static char * const OK_RESPONSE_FORMAT;
byte _powerPin;
};
#endif /* _SimpleGsm_H */
#endif /* _SimpleGsm_H */
\ No newline at end of file
###################################
# Syntax Coloring Map For SimpleGsm
###################################
######################
# Datatypes (KEYWORD1)
######################
SimpleGsm KEYWORD1
##################################
# Methods and Functions (KEYWORD2)
##################################
begin KEYWORD2
disableEcho KEYWORD2
setSmsTextMode KEYWORD2
sendSms KEYWORD2
missedCall KEYWORD2
startCall KEYWORD2
callIsDialing KEYWORD2
callIsRinging KEYWORD2
waitOnCallDialing KEYWORD2
waitOnCallRinging KEYWORD2
hangCall KEYWORD2
\ No newline at end of file
......@@ -19,7 +19,7 @@
#include <ShiftRegLCD123.h>
#include <SoftwareSerial.h>
#include <SimpleGsm.h>
#include <LinkedList.h>
#include <SimpleList.h>
#include <NewTone.h>
#include <SPI.h>
#include <SD.h>
......@@ -33,16 +33,17 @@ const byte GSM_TRANSMITTER_PIN = 3;
const byte GSM_POWER_PIN = 7;
const byte DOOR_SENSOR_PIN = 4;
const byte PIEZO_BUZZER_PIN = 5;
const byte PIR_SENSOR_PIN = A1;
const byte SIREN_PIN = A0;
const unsigned long DELAY_TIME_OF_PIR_SENSOR_CALIBRATION = 40000; // 40 seconds
const unsigned long DELAY_TIME_OF_CALL_RINGING_DURATION = 10000; // 10 seconds
const unsigned long DELAY_TIME_BEFORE_ENABLING_ALARM = 120000; // 2 minutes
const unsigned long DELAY_TIME_OF_CRITICAL_SECTION = 15000; // 15 seconds
const unsigned long DELAY_TIME_OF_CRITICAL_SECTION = 10000; // 10 seconds
const unsigned long DELAY_TIME_BEFORE_BEEPING_SOUNDS = 7000; // 7 seconds
const unsigned long DELAY_TIME_OF_RINGING_SIREN = 600000; // 10 minutes
const unsigned long DELAY_TIME_OF_LCD_MESSAGE = 2000; // 2 seconds
const byte MAXIMUM_NOTIFICATIONS_PER_USER = 3;
String smsText, encryptionKey;
State &
......@@ -52,7 +53,20 @@ getDisabledState ()
if (object == NULL)
{
object = new State (disabledEnterOperation, disabledUpdateOperation, disabledExitOperation);
object = new State (disabledEnterOperation, disabledUpdateOperation, NULL);
}
return *object;
}
State &
getActivationPreparationState ()
{
static State * object = NULL;
if (object == NULL)
{
object = new State (activationPreparationEnterOperation, activationPreparationUpdateOperation, NULL);
}
return *object;
......@@ -149,27 +163,27 @@ getGsm ()
return *object;
}
LinkedList <String> &
SimpleList <String> &
getRfids ()
{
static LinkedList <String> * object = NULL;
static SimpleList <String> * object = NULL;
if (object == NULL)
{
object = new LinkedList <String> ();
object = new SimpleList <String> ();
}
return *object;
}
LinkedList <String> &
SimpleList <String> &
getMobiles ()
{
static LinkedList <String> * object = NULL;
static SimpleList <String> * object = NULL;
if (object == NULL)
{
object = new LinkedList <String> ();
object = new SimpleList <String> ();
}
return *object;
......@@ -181,8 +195,6 @@ disabledEnterOperation ()
sirenOff();
printStringWithoutDelay(F("Alarm disabled"));
flushRfid();
}
void
......@@ -190,30 +202,44 @@ disabledUpdateOperation ()
{
if (isAuthenticated ())
{
getFsm().transitionTo(getEnabledState());
getFsm().transitionTo(getActivationPreparationState());
}
}
void
disabledExitOperation ()
activationPreparationEnterOperation ()
{
printStringWithoutDelay(F("Enabling alarm"));
printStringWithoutDelay(F("Preparing alarm"));
}
void
activationPreparationUpdateOperation()
{
getFsm().transitionTo(getEnabledState());
const unsigned long timeMark = millis();
delay (DELAY_TIME_BEFORE_ENABLING_ALARM);
while(inTime(timeMark, DELAY_TIME_BEFORE_ENABLING_ALARM))
{
if (isAuthenticated ())
{
getFsm().transitionTo(getDisabledState());
break;
}
}
}
void
enabledEnterOperation ()
{
printStringWithoutDelay(F("Alarm enabled"));
flushRfid();
}
void
enabledUpdateOperation ()
{
if (pirSensorSensesMotion() || doorIsOpen ())
if (doorIsOpen ())
{
getFsm().transitionTo(getPossibleThreatState());
}
......@@ -233,7 +259,7 @@ possibleThreatEnterOperation ()
void
possibleThreatUpdateOperation ()
{
if (authenticatedOnBeeping(DELAY_TIME_OF_CRITICAL_SECTION))
if (authenticatedOnBeeping(DELAY_TIME_OF_CRITICAL_SECTION, DELAY_TIME_BEFORE_BEEPING_SOUNDS))
{
getFsm().transitionTo(getDisabledState());
}
......@@ -250,10 +276,12 @@ realThreatPartAUpdateOperation ()
printStringWithoutDelay(F("Notifying Users"));
notifyUsers();
notifyUsers(DELAY_TIME_OF_CALL_RINGING_DURATION, MAXIMUM_NOTIFICATIONS_PER_USER);
printStringWithoutDelay(F("Waiting police"));
flushRfid();
if (authenticatedOnDelay(DELAY_TIME_OF_RINGING_SIREN))
{
getFsm().transitionTo(getDisabledState());
......@@ -290,14 +318,6 @@ initializeSdCard ()
return SD.begin(SD_SHIELD_CHIP_SELECT_PIN);
}
void
initializePirSensor ()
{
pinMode (PIR_SENSOR_PIN, INPUT);
delay (DELAY_TIME_OF_PIR_SENSOR_CALIBRATION);
}
void
initializeDoorSensor ()
{
......@@ -361,6 +381,21 @@ initializeSiren ()
sirenOff ();
}
bool
inTime (const unsigned long timeMark, const unsigned long timeInterval)
{
const unsigned long currentTime = millis();
const unsigned long elapsedTime = currentTime - timeMark;
if (elapsedTime >= timeInterval)
{
return false;
}
return true;
}
void
sirenOn ()
{
......@@ -376,7 +411,7 @@ sirenOff ()
bool
doorIsOpen ()
{
return digitalRead(DOOR_SENSOR_PIN) == HIGH;
return doorHasValue (HIGH);
}
bool
......@@ -386,9 +421,37 @@ doorIsClosed ()
}
bool
pirSensorSensesMotion ()
doorHasValue (const int expectedValue)
{
return digitalRead(PIR_SENSOR_PIN) == HIGH;
if (digitalRead(DOOR_SENSOR_PIN) == expectedValue)
{
const byte numberOfSamples = 50;
const unsigned long delayTimeBetweenSamples = 10;
const byte thresholdPercentage = 85;
byte actualPercentage = 0;
for (byte i = 0; i < numberOfSamples; i++)
{
if (digitalRead(DOOR_SENSOR_PIN) == expectedValue)
{
actualPercentage++;
}
delay(delayTimeBetweenSamples);
}
actualPercentage *= (100.0 / numberOfSamples);
if (actualPercentage >= thresholdPercentage)
{
return true;
}
}
return false;
}
void
......@@ -427,11 +490,9 @@ flushRfid()
bool
authenticatedOnDelay (const unsigned long milliseconds)
{
const unsigned long previousMillis = millis();
const unsigned long timeMark = millis();
flushRfid();
while ((unsigned long) (millis() - previousMillis) < milliseconds)
while (inTime(timeMark, milliseconds))
{
if (isAuthenticated ())
{
......@@ -443,30 +504,21 @@ authenticatedOnDelay (const unsigned long milliseconds)
}
bool
authenticatedOnBeeping (const unsigned long milliseconds)
authenticatedOnBeeping (const unsigned long delayTimeOfCriticalSection, const unsigned long delayTimeBeforeBeepingSounds)
{
const unsigned long previousMillis = millis();
const unsigned long toneFrequency = 500;
const unsigned long durationTime = 50;
const unsigned long timeMark = millis();
flushRfid();
while ((unsigned long) (millis() - previousMillis) < milliseconds)
while (inTime(timeMark, delayTimeOfCriticalSection))
{
if (isAuthenticated ())
{
return true;
}
NewTone(PIEZO_BUZZER_PIN, toneFrequency);
delay(durationTime);
noNewTone();
delay(durationTime);
if (!inTime(timeMark, delayTimeBeforeBeepingSounds))
{
generateBeepSound();
}
}
return false;
......@@ -480,6 +532,22 @@ isAuthenticated ()
return (rfidTagHandled(rfidCode) && rfidCodeExists(rfidCode));
}
void
generateBeepSound ()
{
const unsigned long toneFrequency = 500;
const unsigned long durationTime = 50;
NewTone(PIEZO_BUZZER_PIN, toneFrequency);
delay(durationTime);
noNewTone();
delay(durationTime);
}
bool
rfidTagHandled (String & rfidCode)
{
......@@ -564,7 +632,7 @@ readRfids()
{
while (file.available())
{
getRfids().add(file.readStringUntil('\n'));
getRfids().push_back(file.readStringUntil('\n'));
}
file.close();
......@@ -606,7 +674,7 @@ readMobiles()
{
while (file.available())
{
getMobiles().add(file.readStringUntil('\n'));
getMobiles().push_back(file.readStringUntil('\n'));
}
file.close();
......@@ -622,13 +690,9 @@ rfidCodeExists (const String & rfidCode)
{
const String cipherText = xorEncryption (encryptionKey, rfidCode);
const byte numberOfRfids = getRfids().size();
for (byte i = 0; i < numberOfRfids; i++)
for (SimpleList<String>::iterator element = getRfids().begin(); element != getRfids().end(); ++element)
{
const String rfid = getRfids().get(i);
if (cipherText == rfid)
if (cipherText == *element)
{
return true;
}
......@@ -638,19 +702,15 @@ rfidCodeExists (const String & rfidCode)
}
void
notifyUsers ()
notifyUsers (const unsigned long delayTimeOfCallRingingDuration, const byte maximumNotificationsPerUser)
{
const byte notificationsPerUser = 3;
const byte numberOfMobiles = getMobiles().size();
for (byte i = 0; i < notificationsPerUser; i++)
for (SimpleList<String>::iterator element = getMobiles().begin(); element != getMobiles().end(); ++element)
{
for (byte j = 0; j < numberOfMobiles; j++)
{
const String mobile = getMobiles().get(j);
const String mobile = *element;
getGsm().missedCall(mobile, DELAY_TIME_OF_CALL_RINGING_DURATION);
for (byte i = 0; i < maximumNotificationsPerUser; i++)
{
getGsm().missedCall(mobile, delayTimeOfCallRingingDuration);
getGsm().sendSms(mobile, smsText);
}
......@@ -710,10 +770,6 @@ setup ()
initializeDoorSensor();
printString(F("Init PIR sensor"));
initializePirSensor();
printString(F("Init GSM"));
if (!initializeGsm())
......@@ -721,8 +777,6 @@ setup ()
systemError (F("GSM failed"));
}
printString(F("GSM OK"));
printString(F("Init SD card"));
if (!initializeSdCard())
......@@ -730,8 +784,6 @@ setup ()
systemError (F("SD card failed"));
}
printString(F("SD card OK"));
printString(F("Read RFIDs"));
if (!readRfids())
......@@ -739,8 +791,6 @@ setup ()
systemError(F("RFIDs failed"));
}
printString(F("RFIDs OK"));
printString(F("Read mobiles"));
if (!readMobiles())
......@@ -748,8 +798,6 @@ setup ()
systemError(F("Mobiles failed"));
}
printString(F("Mobiles OK"));
printString(F("Read SMS text"));
if (!readSmsText())
......@@ -757,8 +805,6 @@ setup ()
systemError(F("SMS text failed"));
}
printString(F("SMS text OK"));
printString(F("Init RFID"));
initializeRfid();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment