-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicationScenario_MetaStates.c
178 lines (154 loc) · 3.56 KB
/
applicationScenario_MetaStates.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <avr/io.h>
#include <util/delay.h>
#include <Arduino.h>
/** Generating code for applicationScenario_MetaStates **/
// Declaring modes and states function headers
void sw_day();
void sw_night();
void s_day_off();
void s_day_on();
void s_night_noff();
void s_night_non();
// Declaring available sensors & actuators
int s5_light=1;
int s5_btn=8;
int s5_buzzer=9;
int s5_led=10;
int s5_ledmode=11;
// Declaring modes
enum mode{day,night}current_mode = day;
// Declaring states
enum state{day_off,day_on,night_noff,night_non}current_state = day_off;
int def_state_day=day_off,def_state_night=night_noff;
long time = 0; long debounce = 200;
void sw_day(){
switch(current_state){
case day_off:
s_day_off();
break;
case day_on:
s_day_on();
break;
default:
break;
}
}
void sw_night(){
switch(current_state){
case night_noff:
s_night_noff();
break;
case night_non:
s_night_non();
break;
default:
break;
}
}
void s_day_off()
{
digitalWrite(s5_led, LOW);
digitalWrite(s5_buzzer, LOW);
digitalWrite(s5_ledmode, HIGH);
boolean guard = millis() - time > debounce;
if(analogRead(s5_light) < 300 && guard){
time = millis();
current_mode = night;
current_state = def_state_night;
return;
}
if(digitalRead(s5_btn) == HIGH && guard){
time = millis();
current_state = day_on;
}
}
void s_day_on()
{
digitalWrite(s5_buzzer, HIGH);
digitalWrite(s5_led, HIGH);
boolean guard = millis() - time > debounce;
if(analogRead(s5_light) < 300 && guard){
time = millis();
current_mode = night;
current_state = def_state_night;
return;
}
if(digitalRead(s5_btn) == LOW && guard){
time = millis();
current_state = day_off;
}
}
void s_night_noff()
{
digitalWrite(s5_ledmode, LOW);
digitalWrite(s5_led, LOW);
digitalWrite(s5_buzzer, LOW);
boolean guard = millis() - time > debounce;
if(analogRead(s5_light) > 300 && guard){
time = millis();
current_mode = day;
current_state = def_state_day;
return;
}
if(digitalRead(s5_btn) == HIGH && guard){
time = millis();
current_state = night_non;
}
}
void s_night_non()
{
digitalWrite(s5_led, HIGH);
digitalWrite(s5_buzzer, HIGH);
boolean guard = millis() - time > debounce;
if(analogRead(s5_light) > 300 && guard){
time = millis();
current_mode = day;
current_state = def_state_day;
return;
}
if(digitalRead(s5_btn) == LOW && guard){
time = millis();
current_state = night_noff;
}
}
void setup()
{
pinMode(s5_buzzer, OUTPUT);
pinMode(s5_led, OUTPUT);
pinMode(s5_ledmode, OUTPUT);
pinMode(s5_light, INPUT);
pinMode(s5_btn, INPUT);
Serial.begin(19200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
delay(5000);
Serial.write('*');
delay(200);
// Kept here for reference
// enum mode{day,night}current_mode = day;
// enum state{day_off,day_on,night_noff,night_non}current_state = day_off;
Serial.print("#hello:\n");
Serial.print("#states:day_off=0,day_on=1,night_noff=2,night_non=3\n");
Serial.print("#modes:day=0,night=1\n");
Serial.print("#params:key=value,key2=value3\n");
Serial.print("#eoi:\n");
Serial.print("#monitor:\n");
delay(200);
}
void loop()
{
// send mode with state
// We can handle a dedicated guard here
Serial.print(String(current_mode) + ',' + String(current_state) + '\n');
switch(current_mode){
case day:
sw_day();
break;
case night:
sw_night();
break;
default:
break;
}
}