-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_funcs.c
269 lines (217 loc) · 5.88 KB
/
arduino_funcs.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
#include "arduino_funcs.h"
#include "server_helper.h"
int is_open = 0;
/*
This code configures the file descriptor for use as a serial port.
*/
void configure(int fd) {
struct termios pts;
tcgetattr(fd, &pts);
cfsetospeed(&pts, 9600);
cfsetispeed(&pts, 9600);
tcsetattr(fd, TCSANOW, &pts);
}
/**
* function to get temperature from Arduino
* @param p pointer to filename
*/
int get_started(char* filename) {
printf("\t\ttrying to open %s\n", filename);
// try to open the file for reading and writing
// you may need to change the flags depending on your platform
int ard_fd = open(filename, O_RDWR | O_NOCTTY | O_NDELAY);
if (ard_fd <= 0) {
close(ard_fd);
is_open = 0;
printf("could not open\n");
// perror("Could not open file\n");
// exit(1);
return -1;
}
else {
printf("Successfully opened %s for reading and writing\n", filename);
is_open = 1;
}
configure(ard_fd);
printf("ard_fd = %d\n", ard_fd);
return ard_fd;
}
void* handle_arduino(void* p) {
packet* pack = (packet*) p;
pthread_mutex_t* lock = pack->lock;
int ard_fd = -1;
char* filename = "/dev/ttyACM0";
int is_stdby = 0;
pthread_mutex_lock(lock);
int is_quit = pack->quit_flag;
pthread_mutex_unlock(lock);
char* kvp = create_first_pair("Status", "C");
while (!is_quit) {
if (is_open) {
// char* kvp = NULL;
if (!is_stdby) {
pthread_mutex_lock(lock);
if (pack->is_Celsius) {
change_status(&kvp, 'C');
}
else {
change_status(&kvp, 'F');
}
pthread_mutex_unlock(lock);
}
else {
change_status(&kvp, 'Q');
}
if (pack->ctrl_signal != '\0') {
char sig[3];
pthread_mutex_lock(lock);
sig[0] = pack->ctrl_signal;
pthread_mutex_unlock(lock);
sig[1] = '\n';
sig[2] = '\0';
if (sig[0] == 'w') {
is_stdby = 0;
} else if (sig[0] == 'q') {
is_stdby = 1;
}
printf("writing %s to arduino\n", sig);
pthread_mutex_lock(lock);
int bytes_written = 0;
while (bytes_written != strlen(sig)) {
bytes_written += write(ard_fd, sig, strlen(sig));
}
pthread_mutex_unlock(lock);
sleep(3);
// "w\n" afterwards to clear things out
if (sig[0] != 'q') {
strcpy(sig, "w\n");
pthread_mutex_lock(lock);
bytes_written = 0;
while (bytes_written != strlen(sig)) {
bytes_written += write(ard_fd, sig, strlen(sig));
}
pthread_mutex_unlock(lock);
}
pack->ctrl_signal = '\0';
}
char* time = get_current_time();
pthread_mutex_lock(lock);
char* temperature = read_data(filename, ard_fd, NULL);
pthread_mutex_unlock(lock);
if (temperature == NULL) {
change_status(&kvp, 'O');
} else {
char* value = malloc(sizeof(char) * 20);
strcpy(value, temperature);
free(temperature);
add_kvp(&kvp, time, value);
}
}
else if (!is_open) {
change_status(&kvp, 'O');
close(ard_fd);
printf("Arduino is offline\n");
// try to open Arduino
pthread_mutex_lock(lock);
ard_fd = get_started("/dev/ttyACM0");
pthread_mutex_unlock(lock);
if (!is_open) {
sleep(5);
pthread_mutex_lock(lock);
ard_fd = get_started("/dev/ttyACM1");
pthread_mutex_unlock(lock);
if (is_open) {
pack->is_Celsius = 1;
filename = "dev/ttyACM1";
}
} else {
pack->is_Celsius = 1;
filename = "dev/ttyACM0";
}
}
sleep(2);
pthread_mutex_lock(lock);
is_quit = pack->quit_flag;
pthread_mutex_unlock(lock);
if (is_quit) {
break;
}
if (!pack->requesting) {
write_to_file(kvp, "output.json");
}
}
destroy_kvps(&kvp);
close(ard_fd);
pthread_exit(NULL);
}
/**
* function for single read of temperature from Arduino
* @param file_name Arduino filename
* @param fd file descriptor
* @return the temperature as a float, without any surrounding text
*/
char* read_data(char* file_name, int fd, pthread_mutex_t* lock) {
/*
Write the rest of the program below, using the read and write system calls.
*/
printf("file descriptor is: %d\n", fd);
char out[100];
int index = 0;
char buf[100];
int end = 0;
int zero_count = 0;
while (end == 0) {
int bytes_read = read(fd, buf, 100);
printf("\t\t%d bytes read from arduino\n", bytes_read);
if (bytes_read == 0) {
zero_count++;
} else {
zero_count = 0;
}
if (zero_count > 20) {
is_open = 0;
return NULL;
}
if (bytes_read < 0) {
sleep(2);
}
for (int i = 0; i < bytes_read; i++) {
if (buf[i] == '\n') {
out[index + 1] = '\0';
printf("%s\n", out);
end = 1;
break;
}
out[index++] = buf[i];
}
}
char* temp = malloc(sizeof(char) * (strlen(out) + 1));
strcpy(temp, out);
return temp;
}
// int main() {
// int ard_fd = get_started("/dev/ttyACM0");
// if (!is_open) {
// ard_fd = get_started("/dev/ttyACM1");
// if (is_open) {
// // int filename = "dev/ttyACM1";
// }
// } else {
// // filename = "dev/ttyACM0";
// }
// // char c = 'b';
// // sleep(5);
// // printf("writing %c\n", c);
// // write(ard_fd, &c, sizeof(char));
// // char buf[3];
// // while (1) {
// // int bytes_read = read(STDIN_FILENO, buf, (sizeof(buf) - 1));
// // buf[bytes_read] = '\0';
// // if (buf[0] == 'q') {
// // break;
// // }
// // printf("read %d bytes, and wrote %s\n", bytes_read, buf);
// // write(ard_fd, buf, strlen(buf));
// // }
// // close(ard_fd);
// }