-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
266 lines (225 loc) · 7.1 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <termio.h>
#include <assert.h>
#include <signal.h>
#define TEST 0
#define MAX_LEN 100
#define DEC 10
#define HEX 16
typedef struct RGB{
unsigned char r;
unsigned char g;
unsigned char b;
}RGB;
unsigned long int TimeCompare(int hour, int minuate);
void SetColor(RGB* rgb, int type, char* r, char* g, char* b);
void ShowHelp();
void alarmFinish();
void alarmStart();
int main(int argc, char* argv[]){
int fd, sfd, let;
//data type
typedef struct ALARM_SCHEDULE{
int startMinate;
int startHour;
int finishMinate;
int finishHour;
}schedule;
schedule rec;
// Connecting Arduino by UART communication
if(!(TEST)){
printf("Initializing...\n");
sfd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY);
assert(sfd != -1);
struct termios newtio;
memset(&newtio, 0, sizeof(struct termios));
newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ~(ICANON | ECHO | ECHOE | ISIG);
tcflush(sfd, TCIFLUSH);
tcsetattr(sfd, TCSANOW, &newtio);
printf("Done.\n");
}
// 2Way-handshaking
char receive[MAX_LEN];
int readSize = 0;
let = write(sfd,"OK",2);
assert(let == 2);
while(1){
readSize = read(sfd,receive,MAX_LEN);
if(readSize == 2){
printf("%s. Ready.\n",receive);
break;
}
}
printf("Enter the Command.\nIf you check the menual, type 'help'.\n");
while(1){
if((fd=open("schedule", O_RDWR|O_CREAT, 0660)) == -1){
fprintf(stderr, "err : file open");
return -1;
}
char** av;
int ac = 0, i = 0;
char command[MAX_LEN];
printf("> ");
fgets(command,MAX_LEN,stdin);
command[strlen(command)-1]='\0';
char *ptr;
ptr = strtok(command, " ");
while(ptr != NULL){
av[i] = ptr;
i++; ac++;
ptr = strtok(NULL, " ");
}
// Command excute
if(!(strcmp(av[0], "help"))){
printf("\n");
ShowHelp();
printf("\n");
}
else if(!(strcmp(av[0], "set"))){
if(ac!=3){
fprintf(stderr, "1. set : %s [hour] [minuate]\n", av[0]);
continue;
}else if(atoi(av[1])<0 || atoi(av[1])>23){
fprintf(stderr, "err : hour\n");
continue;
}else if(atoi(av[2])<0 || atoi(av[2])>59){
fprintf(stderr, "err : munuate\n");
continue;
}
rec.startHour=atoi(av[1]);
rec.startMinate=atoi(av[2]);
//printf("%s %d %d\n",av[1], rec.hour, rec.minuate);
alarm(TimeCompare(rec.startHour, rec.startMinate));
lseek(fd, 0L, SEEK_END);
if(write(fd, &rec, sizeof(rec)) == sizeof(rec)){
printf("Setting Success\n");
}
else{
printf("Setting Failed. Try again.\n");
}
}
//confirm : exeption
else if(!(strcmp(av[0], "confirm"))){
if(ac!=1){
fprintf(stderr, "2. confirm : %s\n", av[0]);
continue;
}
//confirm
lseek(fd, 0L, SEEK_SET);
while(1){
printf("Checking...\n");
if(read(fd, &rec, sizeof(rec))>0) printf("hour: %d minuate: %d\n", rec.startHour, rec.startMinate);
else break;
}
}
//remove : exeption
else if(!(strcmp(av[0], "remove"))){
if(ac!=1){
fprintf(stderr, "3. remove : %s", av[0]);
return 1;
}
// remove file
if((remove("./schedule"))==0)printf("remove success\n");
else printf("remove fail\n");
}
//================================================
else if(!(strcmp(av[0], "on"))){
char commandString[MAX_LEN] = {'\0',};
RGB color;
// input anymore
if(ac == 1){
printf("You are not type Color. Turn on white Color\n");
SetColor(&color, HEX, "#C8C8C8", NULL, NULL);
}
// input dec
else if(ac == 4){
SetColor(&color, DEC, av[1], av[2], av[3]);
}
// input hex
else if(ac == 2){
SetColor(&color, HEX, av[1], NULL, NULL);
}
sprintf(commandString,"L %d %d %d", color.r, color.g, color.b);
let = write(sfd,commandString,strlen(commandString));
assert(let == strlen(commandString));
printf("Turn on the Light - R:%d G:%d B:%d\n", color.r, color.g, color.b);
}
else if(!(strcmp(av[0], "off"))){
printf("Turn off the Light\n");
let = write(sfd,"L #000000",9);
assert(let == 9);
}
else if(!(strcmp(av[0], "clear"))){
printf("%c[2J", 27 );
}
else if(!(strcmp(av[0], "exit"))){
close(fd);
printf("Good Bye\n");
break;
}
else{
fprintf(stderr,"\nCommand '%s' is not found. If you check the menual, type 'help'\n\n",av[0]);
}
close(fd);
}
close(sfd);
return 0;
}
void alarmStart(){
printf("start\n");
signal(SIGALRM, alarmFinish);
alarm(5);
}
void alarmFinish(){
printf("finish\n");
signal(SIGALRM, alarmStart);
alarm(5);
}
void SetColor(RGB* rgb, int type, char* r, char* g, char* b){
if(type == DEC){
rgb->r = strtol(r,NULL,10);
rgb->g = strtol(g,NULL,10);
rgb->b = strtol(b,NULL,10);
}
else if(type == HEX){
char str[10];
strcpy(str,r);
if(str[0] == '#') str[0] = '0';
int data = strtol(str,NULL,16);
rgb->r = (data&0xFF0000)>>16;
rgb->g = (data&0x00FF00)>>8;
rgb->b = (data&0x0000FF);
}
}
unsigned long int TimeCompare(int hour, int minuate){
int chaHour=0, chaMinuate=0;
unsigned long int sum=0;
struct tm *t;
time_t timer;
timer=time(NULL);
t=localtime(&timer);
chaHour=abs(hour-(t->tm_hour));
chaMinuate=abs(minuate-(t->tm_min));
sum+=chaHour*3600;
sum+=chaMinuate*60;
return sum;
}
void ShowHelp(){
printf("This is Automation Control Lamp. You can control the Lamp to the color you wnat.\n");
fprintf(stderr, "- How to use this program? : help\n");
fprintf(stderr, "- Turn on the Light : on [Color(dec or hex)(option)]\n");
fprintf(stderr, "- Turn off the Light : off\n");
fprintf(stderr, "- Setting Lamp Alarm : set [hour] [minuate]\n");
fprintf(stderr, "- Confirm Lamp Alarm : confirm\n");
fprintf(stderr, "- Remove Lamp Alarm : remove\n");
fprintf(stderr, "- Clear this consol : clear\n");
fprintf(stderr, "- Stop this Program : exit\n");
}