-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz2.c
293 lines (193 loc) · 4.27 KB
/
z2.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <pthread.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
enum STATE {BUTTON, B0, B1, B2, B3};
typedef enum STATE STATE;
pthread_t t1;
pthread_t t2;
int gotsignal = 0;
void sighandler(int signo){
if(signo == SIGIO) gotsignal = 1;
}
void clr(){
system("clear");
}
unsigned long read_timer(){
FILE *fp;
char *str;
size_t len = 8;
int i = 0;
unsigned long long tenano = 0;
unsigned long ms = 0;
fp = fopen ("/dev/timer", "r");
str = (char *)malloc(len+1);
getline(&str, &len, fp);
fclose(fp);
for(i=0; i<8; ++i){
tenano+=(unsigned long long)str[i] << (8*i);
// printf("tenano %d: %llu", i, tenano);
}
free(str);
ms = tenano/100000;
return ms;
}
void write_time(unsigned long time){
unsigned int ms = 0;
unsigned int millis = 0;
unsigned int sek = 0;
unsigned int minutes = 0;
unsigned int hours = 0;
ms = time;
hours = ms / 3600000;
ms -= hours*3600000;
minutes = ms /60000;
ms -= minutes*60000;
sek = ms / 1000;
ms = ms - sek*1000;
millis = ms;
printf("%u:%u:%u:%u \n", hours, minutes, sek, millis);
}
void write_timer(unsigned long time, char state){
FILE *fp;
fp = fopen ("/dev/timer", "w");
fprintf(fp, "%c, %lu", state, time);
// printf("write_timer ms: %c, %d \n", state, time);
fclose(fp);
}
void read_buttons(bool * button){
FILE *fp;
char *str;
char tval1,tval2,tval3,tval4;
size_t num_of_bytes = 6;
//Citanje vrednosti tastera
fp = fopen ("/dev/button", "r");
str = (char *)malloc(num_of_bytes+1);
getline(&str, &num_of_bytes, fp);
fclose(fp);
button[0] = str[2] - 48;
button[1] = str[3] - 48;
button[2] = str[4] - 48;
button[3] = str[5] - 48;
free(str);
// printf("Vrednosti button: %d %d %d %d \n",button[0],button[1],button[2],button[3]);
sleep(1);
}
void *read_keyboard(void *argv);
void * app(void *argv);
int main ()
{
unsigned long time = 0;
struct sigaction action;
clr();
write_timer(0, 'p');
time = read_timer();
write_time(time);
//asinhroni signal:
int fd = open("/dev/timer", O_RDWR|O_NONBLOCK);
memset(&action, 0, sizeof(action));
action.sa_handler = sighandler;
sigaction(SIGIO, &action, NULL);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);
//thread:
pthread_create(&t1, NULL, read_keyboard, NULL);
pthread_create(&t2, NULL, app, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
gotsignal = 0;
close(fd);
return 0;
}
void *read_keyboard(void *argv){
int i = 0;
char key;
unsigned long time = 0;
while(1) {
system("stty raw");
if(getchar()){
system(" stty cooked");
clr();
time = read_timer();
write_time(time);
}
usleep(1000);
}
}
void * app(void *argv){
//pom za tastere:
bool button[4];
//pom za BUTTON:
bool last_state = 0;
bool current_state = 1;
//pom za B0:
int start_pause = 1;
unsigned long time = 0;
STATE state = BUTTON;
while(1)
{
switch(state){
case BUTTON:
usleep(1000);
if(gotsignal) state = B3;
read_buttons(button);
if(current_state != last_state){
if(button[0]) state = B0;
if(button[1]) state = B1;
if(button[2]) state = B2;
if(button[3]) state = B3;
current_state = last_state;
}
if(!(button[0] || button[1] || button[2] || button[3])) current_state = !last_state;
break;
case B0:
time = read_timer();
// write_time(time);
if(start_pause){
write_timer(time, 's');
start_pause = !start_pause;
}else{
write_timer(time, 'p');
start_pause = !start_pause;
}
state = BUTTON;
break;
case B1:
time = read_timer();
// write_time(time);
//printf("B1\n");
if(time >= 10000) time-=10000;
// write_time(time);
if(!start_pause){
write_timer(time, 's');
}else{
write_timer(time, 'p');
}
state = BUTTON;
break;
case B2:
time = read_timer();
// write_time(time);
//printf("B2\n");
time+=10000;
// write_time(time);
if(!start_pause){
write_timer(time, 's');
}else{
write_timer(time, 'p');
}
state = BUTTON;
break;
case B3:
clr();
sleep(1);
pthread_cancel(t1);
pthread_exit(NULL);
break;
}
}
}