-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
90 lines (72 loc) · 1.93 KB
/
main.c
File metadata and controls
90 lines (72 loc) · 1.93 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "socket.h"
void SocketOnEventReceived(struct Event ev){
switch(ev.id){
case DEVICE_ID_BATTERY: printf("\t\tReceived Battery=%fv\n", ConvertToBatteryValue(ev.data)); break;
case DEVICE_ID_COMPASS: printf("\t\tReceived Compass=%f°\n", ConvertToCompassValue(ev.data)); break;
case DEVICE_ID_GPS: printf("\t\tReceived GPS=%.8f° ; %.8f°\n", ConvertToGpsValue(ev.data).lat, ConvertToGpsValue(ev.data).lon); break;
case DEVICE_ID_ROLL: printf("\t\tReceived Roll=%f°\n", ConvertToRollValue(ev.data)); break;
case DEVICE_ID_WINDDIR: printf("\t\tReceived Wind=%f°\n", ConvertToWindDirValue(ev.data)); break;
case DEVICE_ID_TURNSPEED: printf("\t\tReceived TurnSpeed=%f°\n", ConvertToTurnSpeedValue(ev.data)); break;
default: printf("\t\tReceived \e[1;33mUnhandled device id=%d\e[m\n", ev.id);
}
}
int running=1;
void term(int signum)
{
printf("Received SIGINT, exiting...\n");
running = 0;
}
int main(int argc, char** argv)
{
//Setup interrupt signal handling
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = term;
sigaction(SIGINT, &action, NULL);
char* addr;
if(argc==2){
addr = argv[1];
}
else{
printf("Usage: ./tcpapi server_address\n");
return -1;
}
srand(time(NULL));
int error = SocketInit(addr);
if(error==0){
//Start client handling thread
SocketStart();
//Main loop
while(running){
sleep(1);
switch(rand()%4){
case 0:
{
float fValue = rand()%900 / 10.0 - 45.0;
SocketSendHelm(fValue);
printf("Sending Helm=%f\n", fValue);
break;
}
case 1:
{
int nValue = rand()%256;
SocketSendSail(nValue);
printf("Sending Sail=%d\n", nValue);
break;
}
}
}
//Close sockets
SocketClose();
}
else{
printf("Unable to init socket ! Error code %d",error);
return -1;
}
return 0;
}