forked from cvra/nastya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_task.c
199 lines (161 loc) · 5.89 KB
/
plot_task.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ucos_ii.h>
#include <lwip/udp.h>
#include <errno.h>
#include <param.h>
#include "tasks.h"
#include "plot_task.h"
#define PLOT_FREQ 1 // [Hz]
param_t plot_freq;
OS_STK plot_stk[PLOT_TASK_STACKSIZE];
struct plot_data{
char description[8];
void* variable;
enum plot_types type;
struct plot_data *next;
};
static struct plot_data *plots = NULL;
struct udp_pcb *pcb;
void plot_task(void *arg)
{
uint32_t plot_period = 0;
int i;
char plot_string[1024];
char* pos;
static struct sockaddr_in* client_addr = NULL;
printf("plot task\n");
// if (client_addr == NULL) {
// //printf("clieent_addr == NULL\n");
// char recv_data[1024];
// socklen_t addr_len;
// addr_len = sizeof(struct sockaddr);
// client_addr = malloc(sizeof(struct sockaddr_in));
// int ret = recvfrom( sock, recv_data, 1024, 0,
// (struct sockaddr *)&client_addr, &addr_len);
// //printf("udp received (%d, errno = %d, sock = %d)\n", ret, errno, sock);
// }
struct plot_data *to_plot;
while (23) {
if (param_has_changed(&plot_freq))
plot_period = (int64_t)OS_TICKS_PER_SEC / param_get(&plot_freq);
pos = plot_string;
to_plot = plots;
while (to_plot != NULL) {
switch(to_plot->type) {
case PLOT_INT8 :
pos += sprintf( pos, "%s %d\n", to_plot->description,
*(int8_t*)to_plot->variable);
break;
case PLOT_INT16 :
pos += sprintf( pos, "%s %d\n", to_plot->description,
*(int16_t*)to_plot->variable);
break;
case PLOT_INT32 :
pos += sprintf( pos, "%s %d\n", to_plot->description,
*(int32_t*)to_plot->variable);
break;
case PLOT_UINT8 :
pos += sprintf( pos, "%s %d\n", to_plot->description,
*(uint8_t*)to_plot->variable);
break;
case PLOT_UINT16 :
pos += sprintf( pos, "%s %d\n", to_plot->description,
*(uint16_t*)to_plot->variable);
break;
case PLOT_UINT32 :
pos += sprintf( pos, "%s %d\n", to_plot->description,
*(uint32_t*)to_plot->variable);
break;
case PLOT_FLOAT :
pos += sprintf( pos, "%s %f\n", to_plot->description,
*(float*)to_plot->variable);
break;
case PLOT_DOUBLE :
pos += sprintf( pos, "%s %lf\n", to_plot->description,
*(double*)to_plot->variable);
break;
}
//pos--; // fuck that null terminator
if (pos - plot_string > 1000) break; // prevent segfaults
to_plot = to_plot->next;
}
struct pbuf *p;
p = pbuf_alloc(PBUF_TRANSPORT, strlen(plot_string), PBUF_REF);
p->payload = plot_string;
udp_send(pcb, p);
//printf("before sendto %s\n", plot_string);
//int ret = sendto( sock, plot_string, strlen(plot_string), 0,
// (struct sockaddr *)&client_addr, sizeof(struct sockaddr));
//printf("sendto ret %d, errno = %d\n", ret, errno);
OSTimeDly(plot_period);
}
}
void plot_add_variable(char description[8], void* variable, enum plot_types type)
{
static struct plot_data *plot;
plot = plots;
if (plot == NULL) {
plots = malloc(sizeof(struct plot_data));
memcpy(plots->description, description, 8);
plots->variable = variable;
plots->type = type;
plots->next = NULL;
return;
}
while (plot->next != NULL) {
plot = plot->next;
}
plot->next = malloc(sizeof(struct plot_data));
memcpy(plot->next->description, description, 8);
plot->next->variable = variable;
plot->next->type = type;
plot->next->next = NULL;
}
void plot_init(void)
{
param_add(&plot_freq, "plot freq", "UDP plot freq");
param_set(&plot_freq, PLOT_FREQ);
// int addr_len, bytes_read;
// struct sockaddr_in server_addr , client_addr;
ip_addr_t ipaddr;
/* initliaze IP addresses to be used */
IP4_ADDR(&ipaddr, 192, 168, 1, 201);
/* start the UDP server */
//------------------------
err_t err;
unsigned port = 4000;
unsigned pc_port = 5000;
/* create new UDP PCB structure */
pcb = udp_new();
err = udp_bind(pcb, IP_ADDR_ANY, port);
err = udp_connect(pcb, &ipaddr, pc_port);
//if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
// printf("plot_init() sock fail, errno = %d\n", errno);
// // error
//}
//server_addr.sin_family = AF_INET;
//server_addr.sin_port = htons(5000);
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
//memset(&(server_addr.sin_zero), 0, 8);
//client_addr.sin_family = AF_INET;
//client_addr.sin_port = htons(5000);
//inet_aton("192.168.3.222", &client_addr.sin_addr);
//memset(&(client_addr.sin_zero), 0, 8);
//if (bind(sock,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
// // error
//}
//char *msg = "hello"
//sendto( sock, msg, strlen(msg), 0,
// (struct sockaddr *)&client_addr, sizeof(struct sockaddr));
//printf("udp sent\n");
OSTaskCreateExt(plot_task,
NULL,
&plot_stk[PLOT_TASK_STACKSIZE-1],
PLOT_TASK_PRIORITY,
PLOT_TASK_PRIORITY,
&plot_stk[0],
PLOT_TASK_STACKSIZE,
NULL, 0);
}