-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandProcessor.h~
111 lines (99 loc) · 2.68 KB
/
CommandProcessor.h~
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
struct Command {
char recv[BUFF_MAX];
unsigned int recv_size = 0;
boolean readMessage = false;
};
void processCommand(struct Command* com, struct ClockState* s) {
char ins = getInstructionFromCommand(com);
switch (ins) {
case SNOOZE_INS:
snooze(com, s);
break;
case SET_TIME_INS:
setTime(com, s);
break;
case TURN_OFF_INS:
turnOff(com, s);
break;
case TURN_ON_INS:
turnOn(com, s);
break;
case ACTIVATE_INS:
activate(com, s);
break;
case DISPLAY_INS:
updateDispay(com, s);
break;
}
}
void snooze(struct Command* com, struct ClockState* s) {
Serial.println("Snoozing");
s->snoozed = true;
s->someStateChanged = true;
}
void setTime(struct Command* com, struct ClockState* s) {
Serial.println("Setting time");
Serial.println(com->recv);
setTimeInClock(com->recv);
s->someStateChanged = true;
}
void turnOff(struct Command* com, struct ClockState* s) {
Serial.println("Turing off");
s->ringing = false;
s->someStateChanged = true;
}
void turnOn(struct Command* com, struct ClockState* s) {
Serial.println("Turing on");
s->ringing = true;
s->someStateChanged = true;
}
void activate(struct Command* com, struct ClockState* s) {
Serial.println("Switch activation");
char activate = com->recv[1];
s->someStateChanged = true;
if (activate == '0') {
Serial.println("Switchin off");
s->hot = false;
} else if (activate == '1') {
Serial.println("Switchin on");
s->hot = true;
}
}
void resetCommand(struct Command* com) {
com->recv_size = 0;
com->recv[0] = 0;
com->readMessage = false;
}
char getInstructionFromCommand(struct Command* com) {
return com->recv[0];
}
void readLine(SoftwareSerial* s, struct Command* com) {
char in;
if (s->available() > 0) {
in = s->read();
if ((in == 10 || in == 13) && (com->recv_size > 0)) {
com->readMessage = true;
} else if (in < 48 || in > 122) {; // ignore ~[0-9A-Za-z]
} else if (com->recv_size > BUFF_MAX - 2) { // drop lines that are too long
com->recv_size = 0;
com->recv[0] = 0;
com->readMessage = false;
} else if (com->recv_size < BUFF_MAX - 2) {
com->recv[com->recv_size] = in;
com->recv[com->recv_size + 1] = 0;
com->recv_size += 1;
}
}
}
void setTimeInClock(char *cmd) {
struct ts t;
//T355720619112011
t.sec = inp2toi(cmd, 1);
t.min = inp2toi(cmd, 3);
t.hour = inp2toi(cmd, 5);
t.wday = cmd[7] - 48;
t.mday = inp2toi(cmd, 8);
t.mon = inp2toi(cmd, 10);
t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
DS3231_set(t);
}