-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClockProject.c
352 lines (326 loc) · 9.61 KB
/
ClockProject.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Project - Digital clock
Author: Anushanga Pavith
Date: 10/29/2021
*/
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
// for position of digits
#define STARTROW 2
#define COLUMN1 2
#define COLUMN2 15
#define COLUMN3 21
#define COLUMN4 34
#define COLUMN5 40
#define DATEROW 8
#define DATECOL 22
// digit arrays
int zero[5][6] = {
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1}
};
int one[5][6] = {
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1}
};
int two[5][6] = {
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1}
};
int three[5][6] = {
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1}
};
int four[5][6] = {
{1, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1}
};
int five[5][6] = {
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
};
int six[5][6] = {
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
};
int seven[5][6] = {
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 1}
};
int eight[5][6] = {
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
};
int nine[5][6] = {
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
};
int colon[5][6] = {
{0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0},
};
// Global variable for colour. Default value is white.
int ansiClr = 47;
// Functions prototyping
int countArray(char[]);
int absolute(int);
int compare(char []);
void selection(int digit, int row, int col);
void printdigit(int one[5][6], int row, int col);
void clocktime();
int digitColPosition(int index);
/*
background colours (refers to compare function and ansi background colour values)
0 - black 40
1 - red 41
2 - green 42
3 - yellow 43
4 - blue 44
5 - magenta 45
6 - cyan 46
7 - white 47
*/
int main(int argc, char *argv[])
{
char specialArr[100];
/*Error handelling and calling the clock*/
if(argc == 1)
{
// There are only one argument. './program'
// Start the clock
clocktime();
}
else if(argv[1][0] == '-')
{
if(argv[1][1] == 'h')
{
// input -h
printf("usage : clock -h\t\tquick help on cmd\nusage : clock -c <color>\tprint clock with a color\n<color-black|red|green|yellow|blue|magenta|cyan|white> supported colors\n");
return 0;
}
else if(argv[1][1] == '\0')
{
//input -
// Start the clock
clocktime();
}
else if (argv[1][1] != 'c')
{
// input is like -i, -s, -dsd, -wd5 ...
printf("Invalid use of arguments.\nusage : clock -h\t\tquick help on cmd\nusage : clock -c <color>\tprint clock with a color\n");
return 0;
}
else if (argv[1][1] == 'c')
{
if(argc == 2 && argv[1][2] == '\0')
{
// input -c (only -c )
printf("Invalid use of arguments.\nusage : clock -h\t\tquick help on cmd\nusage : clock -c <color>\tprint clock with a color\n");
return 0;
}
else if(argc == 3 && argv[1][2] == '\0')
{
// input -c anything
// The colour will assign to the global variable ansiClr
ansiClr = compare(argv[2]) + 40;
if(ansiClr == 48)
{
// if the input wasn't defined colours compare(arg[2]) will return 8. means the colour not defined
printf("%s :This is not a valid color, Please enter one of these colours: black, red, green, yellow, blue, magenta, cyan, white\n", argv[2]);
return 0;
}
// when the color is acceptable clock will begin
else clocktime();
}
else
{
// input -cred, -cblue, -cbla_bla_bla
for(int k = 2; argv[1][k] != '\0'; k++)
{
// put the things after "-c" to an array
specialArr[k-2] = argv[1][k];
}
ansiClr = compare(specialArr) + 40;
if(ansiClr == 48)
{
printf("%s :This is not a valid color, Please enter one of these colours: black, red, green, yellow, blue, magenta, cyan, white\n", specialArr);
return 0;
}
else clocktime();
}
}
}
clocktime();
return 0;
}
// Function for count elements in an array
int countArray(char toCount[100])
{
int i = 0;
while(toCount[i] != '\0')
{
i++;
}
return i;
}
// Function for calculate absolute value
int absolute(int val)
{
if(val >= 0) return val;
else
{
val = 0 - val;
return val;
}
}
// Function for assign colour according to the input
int compare(char arr[])
{
char colorArr[8][8] = {
{'b', 'l', 'a', 'c', 'k'},
{'r', 'e', 'd'},
{'g', 'r', 'e', 'e', 'n'},
{'y', 'e', 'l', 'l', 'o', 'w'},
{'b', 'l', 'u', 'e'},
{'m', 'a', 'g', 'e', 'n', 't', 'a'},
{'c', 'y', 'a', 'n'},
{'w', 'h', 'i', 't', 'e'}
};
int i = 0;
int retColour = 8;
_Bool isEqual;
for(; i < 8; i++)
{
if(arr[0] == colorArr[i][0] || absolute(arr[0] - colorArr[i][0]) == 32)
{
// If first charactors are same, then compare rest.
isEqual = 1;
for(int j = 0; j < countArray(arr); j++)
{
if(arr[j] != colorArr[i][j] && absolute(arr[j] - colorArr[i][j]) != 32)
{
isEqual = 0;
break;
}
}
// Find are there additional charactors after the name of the colour. if so, it will neglected.
if(isEqual == 1 && arr[countArray(colorArr[i])] == '\0')
{
retColour = i;
}
}
}
return retColour;
}
// Function for select the big digit array and call the print function.
void selection(int digit, int row, int col)
{
if(digit == 0) printdigit(zero, row, col);
else if(digit == 1) printdigit(one, row, col);
else if(digit == 2) printdigit(two, row, col);
else if(digit == 3) printdigit(three, row, col);
else if(digit == 4) printdigit(four, row, col);
else if(digit == 5) printdigit(five, row, col);
else if(digit == 6) printdigit(six, row, col);
else if(digit == 7) printdigit(seven, row, col);
else if(digit == 8) printdigit(eight, row, col);
else if(digit == 9) printdigit(nine, row, col);
else if(digit == 10) printdigit(colon, row, col);
}
// Function for print big digit. Inputs are related array, the position to be print.
void printdigit(int bignum[5][6], int row, int col)
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
if(bignum[i][j] == 1)
{
printf("\e[%d;%df\033[1;%im \033[0m", row+i, col+j, ansiClr);
}
else if (bignum[i][j] == 0)
{
printf("\e[%d;%df ", row+i, col+j);
}
}
printf("\n");
}
}
// Function for get system time and date
void clocktime()
{
system("stty -echo"); // Won't show user inputs while running the clock
while(1)
{
time_t currentTime;
time(¤tTime);
char dateArr[20];
char timeArr[20];
struct tm *myTime = localtime(¤tTime);
struct tm *date = localtime(¤tTime);
strftime(dateArr, 20, "%Y-%m-%d", date);
strftime(timeArr, 20, "%H-%M-%S", myTime);
printf("\033[?25l"); //cursor off
printf("\033[H\033[J"); // Clear the screen
//printf("\033[2J"); // Clear the screen
for(int i = 0; i < countArray(timeArr); i++)
{
// pass values that in time array.
if(timeArr[i] == '-') selection(10, STARTROW, digitColPosition(i));
else selection(timeArr[i]-48, STARTROW, digitColPosition(i));
}
// Print date as string.
printf("\e[%i;%if\033[0;%im%s\033[0m\n", DATEROW, DATECOL, ansiClr-10, dateArr);
usleep(0.5*1000000);
//sleep(1);
printf("\n");
}
}
// Function for assign the position where the digit should print
int digitColPosition(int index)
{
if(index == 0) return (COLUMN1);
else if(index == 1) return (COLUMN1+7);
else if(index == 2) return (COLUMN2);
else if(index == 3) return (COLUMN3);
else if(index == 4) return (COLUMN3+7);
else if(index == 5) return (COLUMN4);
else if(index == 6) return (COLUMN5);
else if(index == 7) return (COLUMN5+7);
else return 0;
}