-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stop_Watch.c
145 lines (117 loc) · 3.53 KB
/
Stop_Watch.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
// C code to create stop watch
// Header file for necessary system library
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
// starting from zero
#define MIN 0
// 1 hr= 60 min ; 1 min= 60 sec
#define MAX 60
#define MILLI 200000
int i, j, k, n, s;
char c;
pthread_t t1;
// Function to perform operations
// according keyboeard hit.
int keyboardhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if (ch != EOF) {
ungetc(ch, stdin);
return 1;
}
return 0;
}
// display stopwatch on screen
void print()
{
// clear screen escape sequence
printf("\033[2J\033[1;1H");
// Display Hour Min Sec in screen
printf("TIME\t\t\t\tHr: %d Min: %d Sec: %d", n, i, j);
}
// function to pause stopwatch
void* wait(void* arg)
{
while (1) {
// wait for keyboard signal if keyboard
// hit it return non integer number
if (keyboardhit()) {
// take input from user and do
// operation accordingly
c = getchar();
if (c == 'S' || c == 's') {
break;
}
else if (c == 'r' || c == 'R') {
s = 1;
break;
}
else if (c == 'e' || c == 'E')
exit(0);
}
}
}
// driver code
int main()
{
// label to reset the valuereset:
n = MIN;
i = MIN;
j = MIN;
k = MIN, s = MIN;
print();
while (1) {
/* s for start
e for exit
p for pause
r for reset
*/
if (keyboardhit()) {
c = getchar();
if (c != 's')
continue;
for (n = MIN; n < MAX; n++) {
for (i = MIN; i < MAX; i++) {
for (j = MIN; j < MAX; j++) {
for (k = MIN; k < MILLI; k++) {
start:
print();
if (keyboardhit()) {
c = getchar();
if (c == 'r' || c == 'R')
goto reset;
else if (c == 'e' || c == 'E')
exit(0);
else if (c == 's' || c == 'S')
goto start;
else if (c == 'P' || c == 'p') {
pthread_create(&t1, NULL, &wait, NULL);
// waiting for join a thread
pthread_join(t1, NULL);
if (s == 1)
goto reset;
}
}
}
}
}
}
}
}
return 0;
}