-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCajuStreamShell.cpp
More file actions
69 lines (55 loc) · 1.57 KB
/
Copy pathCajuStreamShell.cpp
File metadata and controls
69 lines (55 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <string.h>
#include "CajuStreamShell.h"
/*
class CajuStreamShell {
public:
CajuStreamShell(Stream* stream);
void addCommand(const char* command, void(*func)());
void tick();
#ifndef TEST
private:
#endif
Stream* stream;
unsigned char numCommands;
unsigned char limitCommand[NUM_COMMANDS];
char commands[NUM_COMMANDS][COMMAND_STRING_SIZE];
unsigned char countCommand[NUM_COMMANDS];
void(*funcCommands[NUM_COMMANDS])();
};
*/
CajuStreamShell::CajuStreamShell(Stream* stream) {
this->stream = stream;
this->numCommands = 0;
}
void CajuStreamShell::addCommand(const char* command, void(*func)()) {
//int i;
//this->stream->println("teste");
strncpy(this->commands[this->numCommands], command, strlen(command)+2);
this->commands[this->numCommands][strlen(command)] = '\n';
//for(i=0; command[i] != '\n'; i++){
// this->commands[this->numCommands][i] = command[i];
//}
this->limitCommand[this->numCommands] = strlen(command)+1;
this->countCommand[this->numCommands] = 0;
(funcCommands[this->numCommands]) = func;
this->numCommands++;
}
void CajuStreamShell::tick() {
int i;
if(this->stream->available()) {
char c = this->stream->read();
//this->stream->print(c);
for(i=0; i<this->numCommands; i++) {
if(c == this->commands[i][this->countCommand[i]]) {
this->countCommand[i] = this->countCommand[i]+1;
//if(this->commands[i][this->countCommand[i]] == '\n') {
if(this->countCommand[i] == this->limitCommand[i]) {
(*this->funcCommands[i])();
this->countCommand[i] = 0;
}
} else {
this->countCommand[i] = 0;
}
}
}
}