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
|
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "shared.h"
extern byte address;
extern int rfd, wfd;
extern void *timer(void *);
extern void *comms(void *);
void packetNotifyReceive(byte *data, byte length)
{
int i;
printf("slave %d received packet:", address);
for(i = 0; i < length; i++)
printf(" %02x", data[i]);
printf("\n");
fflush(stdout);
if (!strncmp(data, "GO", length)) {
reply();
sendDataByte('A');
endMessage();
} else if (!strncmp(data, "B", length)) {
reply();
sendDataByte('C');
endMessage();
} else
releaseReceiveBuffer();
}
int main()
{
pthread_t timer_thread;
pthread_t comms_thread;
pthread_attr_t pthread_custom_attr;
printf("Started slave\n");
fflush(stdout);
address = 1;
rfd = open("1", O_RDONLY);
wfd = open("0", O_WRONLY | O_NONBLOCK);
pthread_attr_init(&pthread_custom_attr);
pthread_create(&timer_thread, &pthread_custom_attr, timer, NULL);
pthread_create(&comms_thread, &pthread_custom_attr, comms, NULL);
pthread_join(timer_thread, NULL);
pthread_join(comms_thread, NULL);
return 0;
}
|