Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
I/O Support
Browse files Browse the repository at this point in the history
Added support for Input (Contact) and Output (Relay) testing

Includes fix for compiler problem on Windows for quotes quote characters
  • Loading branch information
Steven Dillingham authored and Steven Dillingham committed Jun 9, 2014
1 parent 2feeadd commit ca5ea95
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 14 deletions.
28 changes: 27 additions & 1 deletion PACSDoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void PACSDoor::addReader(char* id, uint8_t pin0, uint8_t pin1) {
* Adds a new peripheral to the door.
*/
void PACSDoor::addPeripheral(char* id, PACSPeripheralType_t type, uint8_t pin, uint8_t activeLevel) {

peripherals.push_back(PACSPeripheral(id, type, pin, activeLevel));
}

Expand Down Expand Up @@ -164,6 +164,32 @@ bool PACSDoor::pushREX(char* rexId) {
return false;
}

/*
*
*/
bool PACSDoor::activateInput(char* inputId) {

PACSPeripheral* p = findPeripheralById(inputId);
if ((p != NULL) && (p->type == CONTACT)) {
setPinActive(p->pin, p->activeLevel);
return true;
}
return false;
}

/*
*
*/
bool PACSDoor::deactivateInput(char* inputId) {

PACSPeripheral* p = findPeripheralById(inputId);
if ((p != NULL) && (p->type == CONTACT)) {
setPinInactive(p->pin, p->activeLevel);
return true;
}
return false;
}

/*
* Check if there has been any change in pin-states and if so, call the registered callback.
*/
Expand Down
4 changes: 3 additions & 1 deletion PACSDoor.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class PACSDoor {
bool openDoor(char*);
bool closeDoor(char*);
bool pushREX(char*);

bool activateInput(char*);
bool deactivateInput(char*);

// Callback called when pin state changes.
typedef void StateChangeCallback(PACSDoor&, PACSPeripheral&);
void registerStateChangeCallback(StateChangeCallback*);
Expand Down
43 changes: 42 additions & 1 deletion PACSDoorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,47 @@ bool PACSDoorManager::pushREX(char* doorId, char* rexId) {
return false;
}


/*
*
*/
bool PACSDoorManager::activateInput(char* doorId, char* inputId) {

PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->activateInput(inputId)) {
cout << "[" << doorId << "|" << inputId << "]" << F(": Input activated.") << endl;
return true;
}
else {
cout << "Peripheral not found: " << inputId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}

/*
*
*/
bool PACSDoorManager::deactivateInput(char* doorId, char* inputId) {

PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->deactivateInput(inputId)) {
cout << "[" << doorId << "|" << inputId << "]" << F(": Input deactivated.") << endl;
return true;
}
else {
cout << "Peripheral not found: " << inputId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}

/*
* Calls the updateLevels function for all the doors in the doors vector.
* This will read the current levels of all the peripherals connected to the Arduino,
Expand Down Expand Up @@ -224,4 +265,4 @@ void PACSDoorManager::registerStateChangeCallback(StateChangeCallback *callback)
for (unsigned i=0; i < doors.size(); i++) {
doors[i].registerStateChangeCallback(callback);
}
}
}
6 changes: 4 additions & 2 deletions PACSDoorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class PACSDoorManager {
bool enterPIN(char*, char*, char*);
bool openDoor(char*, char*);
bool closeDoor(char*, char*);
bool pushREX(char*, char*);
bool pushREX(char*, char*);
bool activateInput(char*, char*);
bool deactivateInput(char*, char*);

void updateLevels();
int isPeripheralActive(char*, char*);
Expand All @@ -55,4 +57,4 @@ class PACSDoorManager {
PACSPeripheral* findPeripheralById(char*, char*);
};

#endif
#endif
8 changes: 6 additions & 2 deletions PACSPeripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void PACSPeripheral::initialize() {
switch (type) {
case DOORMONITOR:
case REX:
case CONTACT:
case RELAY:
pinMode(pin, OUTPUT);
digitalWrite(pin, initialLevel);
break;
Expand Down Expand Up @@ -77,7 +79,9 @@ void PACSPeripheral::updateLevels() {
case REX:
case GREENLED:
case BEEPER:
case LOCK:
case LOCK:
case CONTACT:
case RELAY:
currentLevel = digitalRead(pin);
break;

Expand All @@ -100,4 +104,4 @@ void PACSPeripheral::updateLevels() {
*/
bool PACSPeripheral::isActive() {
return (currentLevel == activeLevel) ? true : false;
}
}
4 changes: 2 additions & 2 deletions PACSPeripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

#define PERIPHERAL_ID_MAX_LENGTH 16 // The max number of characters for the ID.

typedef enum {GREENLED, BEEPER, DOORMONITOR, REX, LOCK} PACSPeripheralType_t;
typedef enum {GREENLED, BEEPER, DOORMONITOR, REX, LOCK, CONTACT, RELAY} PACSPeripheralType_t;

class PACSPeripheral {
public:
Expand All @@ -43,4 +43,4 @@ class PACSPeripheral {
bool levelChanged; // Has the pin level changes since last update?
};

#endif
#endif
72 changes: 68 additions & 4 deletions dctt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ typedef enum Command
OPENDOOR,
CLOSEDOOR,
PUSHREX,
ACTIVATEINPUT,
DEACTIVATEINPUT,
GETPERIPHERALSTATE,
GETNETWORKCONFIG,
GETDOORCONFIG,
Expand Down Expand Up @@ -253,7 +255,7 @@ void waitForData(Stream& stream, int timeout) {
bool getNextToken(Stream& stream, char* tokenBuffer, uint8_t length) {

stream.find("\"");
uint8_t bytesRead = stream.readBytesUntil('\"', tokenBuffer, length);
uint8_t bytesRead = stream.readBytesUntil('"', tokenBuffer, length);
tokenBuffer[bytesRead] = '\0';

if (bytesRead > 0)
Expand All @@ -268,7 +270,7 @@ bool getNextToken(Stream& stream, char* tokenBuffer, uint8_t length) {
// the global one.
namespace Cfg {
enum Pos {
NONE, DOOR, READER, DOOR_MONITOR, REX, LOCK, //Container
NONE, DOOR, READER, DOOR_MONITOR, REX, LOCK, CONTACT, RELAY, //Container
WIEGAND, GREEN_LED, BEEPER, //Subcontainer
ID, PIN, PIN_ZERO, PIN_ONE, ACTIVE //Property
};
Expand Down Expand Up @@ -472,6 +474,16 @@ int parseDoor(Stream& stream, char* startToken, char* stopToken) {
cfgParent = Cfg::DOOR;
openBraces = 0;
}
else if (strcmp(token, "Input") == 0) {
cfgPos = Cfg::CONTACT;
cfgParent = Cfg::DOOR;
openBraces = 0;
}
else if (strcmp(token, "Relay") == 0) {
cfgPos = Cfg::RELAY;
cfgParent = Cfg::DOOR;
openBraces = 0;
}
//
// "SUB-CONTAINERS"
//
Expand Down Expand Up @@ -504,6 +516,8 @@ int parseDoor(Stream& stream, char* startToken, char* stopToken) {
case Cfg::DOOR_MONITOR:
case Cfg::REX:
case Cfg::LOCK:
case Cfg::CONTACT:
case Cfg::RELAY:
strcpy(tempPeripheral.id, token);
}
}
Expand All @@ -515,6 +529,8 @@ int parseDoor(Stream& stream, char* startToken, char* stopToken) {
case Cfg::DOOR_MONITOR:
case Cfg::REX:
case Cfg::LOCK:
case Cfg::CONTACT:
case Cfg::RELAY:
tempPeripheral.pin = getPinNumber(token);
if (!isValidPin) {
return -1;
Expand Down Expand Up @@ -547,6 +563,8 @@ int parseDoor(Stream& stream, char* startToken, char* stopToken) {
case Cfg::DOOR_MONITOR:
case Cfg::REX:
case Cfg::LOCK:
case Cfg::CONTACT:
case Cfg::RELAY:
case Cfg::GREEN_LED:
case Cfg::BEEPER:
if (strcmp(token, "HIGH") == 0) {
Expand All @@ -559,7 +577,7 @@ int parseDoor(Stream& stream, char* startToken, char* stopToken) {
}

// Token is parsed. Now we need to traverse the container.
while ((stream.available()) && (stream.peek() != '\"')) {
while ((stream.available()) && (stream.peek() != '"')) {

switch (stream.read()) {
case ']':
Expand Down Expand Up @@ -620,6 +638,18 @@ int parseDoor(Stream& stream, char* startToken, char* stopToken) {
tempPeripheral.pin,
tempPeripheral.activeLevel);
break;
case Cfg::CONTACT:
tempDoor->addPeripheral(tempPeripheral.id,
CONTACT,
tempPeripheral.pin,
tempPeripheral.activeLevel);
break;
case Cfg::RELAY:
tempDoor->addPeripheral(tempPeripheral.id,
RELAY,
tempPeripheral.pin,
tempPeripheral.activeLevel);
break;
}

}
Expand Down Expand Up @@ -1036,6 +1066,8 @@ void apiCMD(WebServer &server, WebServer::ConnectionType type, char *url_tail, b
else if (strcmp(value, "opendoor") == 0) cmd = OPENDOOR;
else if (strcmp(value, "closedoor") == 0) cmd = CLOSEDOOR;
else if (strcmp(value, "pushrex") == 0) cmd = PUSHREX;
else if (strcmp(value, "activateinput") == 0) cmd = ACTIVATEINPUT;
else if (strcmp(value, "deactivateinput") == 0) cmd = DEACTIVATEINPUT;
else if (strcmp(value, "getperipheralstate") == 0) cmd = GETPERIPHERALSTATE;
else if (strcmp(value, "getnetworkconfig") == 0) cmd = GETNETWORKCONFIG;
else if (strcmp(value, "getdoorconfig") == 0) cmd = GETDOORCONFIG;
Expand Down Expand Up @@ -1124,7 +1156,23 @@ void apiCMD(WebServer &server, WebServer::ConnectionType type, char *url_tail, b
return;
}
break;


// Activate Input command
case ACTIVATEINPUT:
if (!doorManager.activateInput(doorId, id)) {
apiResponse(false, id_not_found);
return;
}
break;

// Deactivate Input command
case DEACTIVATEINPUT:
if (!doorManager.deactivateInput(doorId, id)) {
apiResponse(false, id_not_found);
return;
}
break;

// Get peripheral state command
case GETPERIPHERALSTATE:
{
Expand Down Expand Up @@ -1217,6 +1265,8 @@ void onStateChange(PACSDoor &door, PACSPeripheral &p) {
case DOORMONITOR:
case REX:
case LOCK:
case CONTACT:
case RELAY:

cout << "[" << door.id << "|" << p.id << "]: ";

Expand Down Expand Up @@ -1426,6 +1476,20 @@ void onData(WebSocket &socket, char* dataString, unsigned short frameLength) {
doorManager.pushREX(doorId->valuestring, id->valuestring);
}

//
// ActivateInput command
//
else if (strcmp(cmd->name, "ActivateInput") == 0) {
doorManager.activateInput(doorId->valuestring, id->valuestring);
}

//
// DeactivateInput command
//
else if (strcmp(cmd->name, "DeactivateInput") == 0) {
doorManager.deactivateInput(doorId->valuestring, id->valuestring);
}

//
// Not a recognized command.
//
Expand Down
Loading

0 comments on commit ca5ea95

Please sign in to comment.