-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.c
More file actions
454 lines (400 loc) · 10.8 KB
/
Copy pathClock.c
File metadata and controls
454 lines (400 loc) · 10.8 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdlib.h>
#include <stdio.h>
#include <pcf8574.h>
#include <lcd.h>
#include <time.h>
#include <signal.h>
#include <string.h>
// LED wPi pins
#define GREEN 24
#define RED 25
void init_LED(){
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
}
// Buzzer wPi pin
#define Buzzer 29
void init_Buzzer(){
pinMode(Buzzer, OUTPUT);
}
// Keypadd wPi pins
#define ROWS 4
#define COLS 4
char pressedKey = '\0';
int rowPins[ROWS] = {1, 4, 5, 6};
int colPins[COLS] = {12, 3, 2, 0};
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// setup keypad pins
void init_keypad(){
for (int c = 0; c < COLS; c++){
pinMode(colPins[c], OUTPUT);
digitalWrite(colPins[c], HIGH);
}
for (int r = 0; r < ROWS; r++){
pinMode(rowPins[r], INPUT);
pullUpDnControl(rowPins[r], PUD_UP);
}
}
// find row
int findRow(){
for (int r = 0; r < ROWS; r++){
if (digitalRead(rowPins[r]) == LOW){
return r;
}
}
return -1;
}
// find col and row and then output using 2d array
char get_key(){
int rowIndex;
for (int c = 0; c < COLS; c++){
digitalWrite(colPins[c], LOW);
rowIndex = findRow();
if (rowIndex > -1){
if (!pressedKey)
pressedKey = keys[rowIndex][c];
return pressedKey;
}
digitalWrite(colPins[c], HIGH);
}
pressedKey = '\0'; // null if nothing is pressed
return pressedKey;
}
// LCD screen Setup
int pcf8574_address = 0x27;
#define BASE 64 // BASE any number above 64
// Define the output pins of the PCF8574, which are directly connected to the LCD1602 pin.
#define RS BASE+0
#define RW BASE+1
#define EN BASE+2
#define LED BASE+3
#define D4 BASE+4
#define D5 BASE+5
#define D6 BASE+6
#define D7 BASE+7
int lcdhd;// used to handle LCD
int detectI2C(int addr){
int _fd = wiringPiI2CSetup (addr);
if (_fd < 0){
printf("Error address : 0x%x \n",addr);
return 0;
}
else{
if(wiringPiI2CWrite(_fd,0) < 0){
printf("Not found device in address 0x%x \n",addr);
return 0;
}
else{
return 1;
}
}
}
void init_LCD(){
if(detectI2C(0x27)){
pcf8574_address = 0x27;
}else{
printf("Incorrect I2C address\n");
}
pcf8574Setup(BASE,pcf8574_address);//initialize PCF8574
for(int i = 0; i < 8; i++){
pinMode(BASE+i,OUTPUT); //set PCF8574 port to output mode
}
digitalWrite(LED,HIGH); //turn on LCD backlight
digitalWrite(RW,LOW); //allow writing to LCD
lcdhd = lcdInit(2,16,4,RS,EN,D4,D5,D6,D7,0,0,0,0);// initialize LCD
if(lcdhd == -1){
printf("lcdInit failed");
}
}
void cleanLCD(){
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd," ");
lcdPosition(lcdhd,0,1);
lcdPrintf(lcdhd," ");
}
// set alarm function
int set_hour = -1;
int set_minute = -1;
int set_second = -1;
int set_alarm(){
cleanLCD();
int cont = 1;
int count = 2;
char hour[2] = "\0";
char min[2] = "\0";
char sec[2] = "\0";
char x = '\0';
while (cont){
// set alarm hour
while (cont){
delay(150);
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"Set Hour:");
lcdPosition(lcdhd,10,0);
lcdPrintf(lcdhd,"%s", hour);
lcdPosition(lcdhd,0,1);
lcdPrintf(lcdhd,"Back 'D'");
// get input
x = get_key();
if (x == 'A' || x == 'B' || x == 'C' || x == '*' || x == '#'){
// dont accept input
}
// break out of loop and cancel everything
else if (get_key() == 'D'){
cleanLCD();
set_hour = -1;
cont = 0;
break;
}
// made 2 inputs and sets min finally
else if (count == 0){
cleanLCD();
set_hour = atoi(hour);
set_hour = set_hour % 60;
count = 2;
break;
}
// append input to a string
else if (x){
count--;
int lenh = strlen(hour);
hour[lenh] = x;
x = '\0';
}
else {
// do nothing
}
}
// set alarm minute
while (cont){
delay(150);
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"Set Min:");
lcdPosition(lcdhd,10,0);
lcdPrintf(lcdhd,"%s", min);
lcdPosition(lcdhd,0,1);
lcdPrintf(lcdhd,"Back 'D'");
// get input
x = get_key();
if (x == 'A' || x == 'B' || x == 'C' || x == '*' || x == '#'){
// dont accept input
}
// break out of loop and cancel everything
else if (get_key() == 'D'){
cleanLCD();
set_hour = -1;
cont = 0;
break;
}
// made 2 inputs and sets min finally
else if (count == 0){
cleanLCD();
set_minute = atoi(min);
set_minute = set_minute % 60;
count = 2;
break;
}
// append input to a string
else if (x){
count--;
int lenm = strlen(min);
min[lenm] = x;
x = '\0';
}
else {
// do nothing
}
}
// set alarm second
while (cont){
delay(150);
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"Set Sec:");
lcdPosition(lcdhd,10,0);
lcdPrintf(lcdhd,"%s", sec);
lcdPosition(lcdhd,0,1);
lcdPrintf(lcdhd,"Back 'D'");
// get input
x = get_key();
if (x == 'A' || x == 'B' || x == 'C' || x == '*' || x == '#'){
// dont accept input
}
// break out of loop and cancel everything
else if (get_key() == 'D'){
cleanLCD();
set_hour = -1;
set_minute = -1;
cont = 0;
break;
}
// made 2 inputs and sets seconds finally
else if (count == 0){
cleanLCD();
set_second = atoi(sec);
set_second = set_second % 60;
count = 2;
return 1;
}
// append input to a string
else if (x){
count--;
int lens = strlen(sec);
sec[lens] = x;
x = '\0';
}
else {
// do nothing
}
}
}
set_hour = -1;
set_minute = -1;
set_second = -1;
return 0;
}
// check alarm function and play alarm if this is true
void check_alarm(){
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
// if set alarm time == curr time turn on the alarm
if (set_hour == timeinfo->tm_hour && set_minute == timeinfo->tm_min && set_second == timeinfo->tm_sec){
cleanLCD();
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"Press '#'");
while(get_key() != '#'){
digitalWrite(Buzzer, HIGH);
digitalWrite(GREEN, HIGH);
delay(500);
digitalWrite(GREEN, LOW);
digitalWrite(Buzzer, LOW);
digitalWrite(RED, HIGH);
delay(500);
digitalWrite(RED, LOW);
}
// alarm is off
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(Buzzer, HIGH);
cleanLCD();
}
}
int play_CPU = 1;
void printCPUTemperature(){// sub function used to print CPU temperature
FILE *fp;
char str_temp[15];
float CPU_temp;
// CPU temperature data is stored in this directory.
while (play_CPU){
fp=fopen("/sys/class/thermal/thermal_zone0/temp","r");
cleanLCD();
fgets(str_temp,15,fp); // read file temp
CPU_temp = atof(str_temp)/1000.0; // convert to Celsius degrees
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"CPU:%.2fC",CPU_temp);// Display CPU temperature on LCD
lcdPosition(lcdhd,0,1);
lcdPrintf(lcdhd, "Back 'D'");
if (get_key() == 'D'){
play_CPU = 0;
}
check_alarm();
delay(100);
fclose(fp);
}
play_CPU = 1;
cleanLCD();
}
int play_TIME = 1;
void printDataTime(){//used to print system time
time_t rawtime;
struct tm *timeinfo;
while (play_TIME){
time(&rawtime);// get system time
timeinfo = localtime(&rawtime);//convert to local time
cleanLCD();
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"Time:%02d:%02d:%02d",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec); //Display system time on LCD
lcdPosition(lcdhd,0,1);
lcdPrintf(lcdhd, "Back 'D'");
if (get_key() == 'D'){
play_TIME = 0;
}
check_alarm();
delay(100);
}
play_TIME = 1;
cleanLCD();
}
// end loop
int play = 1;
void cleanup(int signo){
cleanLCD();
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(Buzzer, HIGH);
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"POWER OFF");
play = 0;
delay(1000);
cleanLCD();
}
// main program
int main(void){
signal(SIGINT, cleanup); // program terminated using ctrl+c calls cleanup class
signal(SIGTERM, cleanup); // kill command
signal(SIGHUP, cleanup); // closing the window
// setup wiringPi
wiringPiSetup();
// setup LEDs
init_LED();
// setup Buzzer
init_Buzzer();
// setup Keypad
init_keypad();
// setup LCD screen
init_LCD();
// clear LCD
cleanLCD();
// turn off alarm
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(Buzzer, HIGH);
while (play){
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"A:CPU TEMP");
lcdPosition(lcdhd,0,1);
lcdPrintf(lcdhd,"B:Time");
lcdPosition(lcdhd,8,1);
lcdPrintf(lcdhd,"C:Alarm");
if (get_key() == '*'){
cleanLCD();
lcdPosition(lcdhd,0,0);
lcdPrintf(lcdhd,"POWER OFF");
delay(1000);
cleanLCD();
play = 0;
}
if (get_key() == 'A'){
printCPUTemperature();
}
if (get_key() == 'B'){
printDataTime();
}
if (get_key() == 'C'){
set_alarm();
}
check_alarm();
delay(100);
}
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(Buzzer, HIGH);
}