-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command support using built-in LED
- Loading branch information
Showing
4 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef ARDUINO_H | ||
#define ARDUINO_H | ||
#include <Arduino.h> | ||
#endif | ||
|
||
class Command | ||
{ | ||
public: | ||
virtual String getCode(); | ||
virtual int execute(const char* payload); | ||
}; | ||
|
||
typedef Command* CommandPtr; | ||
|
||
class CommandHandler | ||
{ | ||
private: | ||
CommandPtr *_commands; | ||
int _size; | ||
int _maxCommands; | ||
public: | ||
CommandHandler(int maxCommands); | ||
~CommandHandler(); | ||
void registerCommand(Command *command); | ||
int handleCommand(const char* templateCode, const char* payload); | ||
}; | ||
|
||
class BuiltinLedCommand : public Command | ||
{ | ||
private: | ||
int _state; | ||
public: | ||
BuiltinLedCommand(); | ||
String getCode(); | ||
int execute(const char* payload); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#ifndef COMMANDS_H | ||
#define COMMANDS_H | ||
#include "Commands.h" | ||
#endif | ||
|
||
#include <string.h> | ||
|
||
#define LED_BUILTIN 10 | ||
const int MAX_COMMANDS = 10; | ||
|
||
CommandHandler::CommandHandler(int maxCommands) | ||
{ | ||
this->_maxCommands = maxCommands; | ||
this->_commands = new CommandPtr[maxCommands]; | ||
this->_size = 0; | ||
}; | ||
|
||
CommandHandler::~CommandHandler() | ||
{ | ||
if (this->_commands != NULL) | ||
free(this->_commands); | ||
}; | ||
|
||
void CommandHandler::registerCommand(Command *command) | ||
{ | ||
if (this->_size >= this->_maxCommands) | ||
{ | ||
throw "Max commands reached"; | ||
} | ||
|
||
this->_commands[this->_size++] = command; | ||
}; | ||
|
||
int CommandHandler::handleCommand(const char *commandCode, const char *payload) | ||
{ | ||
Serial.printf("command: %s, payload: %s \n", commandCode, payload); | ||
if (this->_commands == NULL) | ||
return -1; | ||
|
||
for (int i = 0; i < this->_size; i++) | ||
{ | ||
CommandPtr command = this->_commands[i]; | ||
if (command->getCode() == commandCode) | ||
{ | ||
command->execute(payload); | ||
return 0; | ||
} | ||
} | ||
|
||
return -1; | ||
}; | ||
|
||
BuiltinLedCommand::BuiltinLedCommand() | ||
{ | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
this->_state = HIGH; | ||
digitalWrite(LED_BUILTIN, this->_state); | ||
}; | ||
|
||
String BuiltinLedCommand::getCode() | ||
{ | ||
return "LED"; | ||
}; | ||
|
||
int BuiltinLedCommand::execute(const char *payload) | ||
{ | ||
Serial.printf("BuiltinLedCommand::payload: %s \n", payload); | ||
if (strcmp("ON", payload) == 0) | ||
{ | ||
this->_state = LOW; | ||
} | ||
else | ||
{ | ||
this->_state = HIGH; | ||
} | ||
|
||
digitalWrite(LED_BUILTIN, this->_state); | ||
return 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters