7d31e7efef2eee6fa4f5f78ab47154e95db5349f
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

1) /*
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

2)  * EtherPix library
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

3)  *
Stefan Schuermans update copyright year

Stefan Schuermans authored 7 years ago

4)  * Copyright 2010-2017 Stefan Schuermans <stefan schuermans info>
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

5)  *
6)  * This program is free software: you can redistribute it and/or modify
7)  * it under the terms of the GNU General Public License as published by
8)  * the Free Software Foundation, version 3 of the License.
9)  *
10)  *
11)  * This program is distributed in the hope that it will be useful,
12)  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13)  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14)  * GNU General Public License for more details.
15)  *
16)  * You should have received a copy of the GNU Lesser General Public License
17)  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18)  */
19) 
20) #include <errno.h>
21) #include <stdlib.h>
22) #include <string.h>
23) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

24) #include <etherpix/display.h>
25) #include <etherpix/msg.h>
26) #include <etherpix/types.h>
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

27) #include <intern/config.h>
28) #include <intern/constants.h>
29) #include <intern/net.h>
30) #include <intern/types.h>
31) 
32) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

33)  * \brief create a new EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

34)  *
35)  * \param[in] sz_config_file name of config file to read
36)  * \param[in] p_msg_func message callback function or NULL
37)  * \param[in] p_msg_ctx user context for message callback
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

38)  * \return pointer to new EtherPix display on success
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

39)  *         or NULL on error
40)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

41) etp_display_t *etp_display_create(const char *sz_config_file,
42)                                   etp_msg_func_p_t p_msg_func,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

43)                                   void *p_msg_ctx)
44) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

45)   etp_display_t *p_display;
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

46) 
47)   /* basic display setup */
48) 
49)   /* create display structure */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

50)   p_display = (etp_display_t *)calloc(1, sizeof (etp_display_t));
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

51)   if (!p_display) {
52)     if (p_msg_func)
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

53)       p_msg_func(p_msg_ctx, etp_msg_type_err,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

54)                  "out of memory\n");
55)     return NULL;
56)   }
57) 
58)   /* set default bind address */
59)   p_display->bind_addr.sin_family = AF_INET;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

60)   p_display->bind_addr.sin_port = htons(ETP_BIND_PORT);
61)   p_display->bind_addr.sin_addr.s_addr = htonl(ETP_BIND_IP);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

62) 
63)   /* no socket yet */
64)   p_display->sock = -1;
65) 
66)   /* config file */
67) 
68)   /* read config file */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

69)   if(etp_config_proc_file(p_display, sz_config_file,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

70)                           p_msg_func, p_msg_ctx)) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

71)     etp_display_free(p_display); /* cleanup everything that config file
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

72)                                     processing might heave created already */
Stefan Schuermans fixed segfault if using wit...

Stefan Schuermans authored 12 years ago

73)     if (p_msg_func)
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

74)       p_msg_func(p_msg_ctx, etp_msg_type_err, "reading config file failed\n");
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

75)     return NULL;
76)   }
77) 
78)   /* set up UDP socket */
79) 
80)   /* create socket */
Stefan Schuermans make output socket non-bloc...

Stefan Schuermans authored 7 years ago

81)   p_display->sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

82)   if (!etp_sock_is_valid(p_display->sock)) {
83)     etp_display_free(p_display);
Stefan Schuermans v1.0.2

Stefan Schuermans authored 13 years ago

84)     if (p_msg_func) {
85)       char errmsg[256];
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

86)       etp_sock_get_last_error(errmsg, sizeof(errmsg));
87)       p_msg_func(p_msg_ctx, etp_msg_type_err,
Stefan Schuermans v1.0.2

Stefan Schuermans authored 13 years ago

88)                  "could not create UDP socket: %s\n", errmsg);
89)     }
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

90)     return NULL;
91)   }
92) 
93)   /* bind socket */
94)   if (bind(p_display->sock, (struct sockaddr *)&p_display->bind_addr,
95)            sizeof (p_display->bind_addr))) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

96)     etp_display_free(p_display);
Stefan Schuermans v1.0.2

Stefan Schuermans authored 13 years ago

97)     if (p_msg_func) {
98)       char errmsg[256];
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

99)       etp_sock_get_last_error(errmsg, sizeof(errmsg));
100)       p_msg_func(p_msg_ctx, etp_msg_type_err,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

101)                  "could not bind UDP socket to \"%s:%u\": %s\n",
102)                  inet_ntoa(p_display->bind_addr.sin_addr),
Stefan Schuermans v1.0.2

Stefan Schuermans authored 13 years ago

103)                  (unsigned int)ntohs(p_display->bind_addr.sin_port), errmsg);
104)     }
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

105)     return NULL;
106)   }
107) 
108)   /* clear display */
109) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

110)   etp_display_data_clear(p_display);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

111) 
112)   return p_display;
113) }
114) 
115) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

116)  * \brief free a EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

117)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

118)  * \param[in] p_display pointer to EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

119)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

120) void etp_display_free(etp_display_t *p_display)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

121) {
122)   int i;
123) 
124)   if (p_display) {
125) 
126)     /* free distributors */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

127)     for (i = 0; i < ETP_DISTRI_MAX_CNT; i++) {
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

128)       if (p_display->distri_ptrs[i]) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

129)         etp_distri_t *p_distri = p_display->distri_ptrs[i];
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

130)         free(p_distri->p_pixels);
131)         free(p_distri->p_msg_buf);
132)         free(p_distri);
133)       }
134)     }
135) 
136)     /* close network connection */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

137)     if (etp_sock_is_valid(p_display->sock))
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

138)       closesocket(p_display->sock);
139) 
140)     /* free display structutre */
141)     free(p_display);
142) 
143)   } /* if (p_display) */
144) }
145) 
146) /**
147)  * \brief get size of display
148)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

149)  * \param[in] p_display pointer to EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

150)  * \param[out] p_width width of display
151)  * \param[out] p_height height of display
152)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

153) void etp_display_get_size(etp_display_t *p_display,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

154)                           unsigned int *p_width, unsigned int *p_height)
155) {
156)   *p_width = (unsigned int)p_display->size.x;
157)   *p_height = (unsigned int)p_display->size.y;
158) }
159) 
160) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

161)  * \brief clear image data to output on EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

162)  *
163)  * clears the stored image data,
164)  * stored image data can later be sent to the distributors using
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

165)  * etp_display_send()
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

166)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

167)  * \param[in] p_display pointer to EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

168)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

169) void etp_display_data_clear(etp_display_t *p_display)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

170) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

171)   etp_u8_t black[3] = { 0, 0, 0 };
172)   etp_display_data_fmt(p_display, black, 0, 0,
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

173)                        0, 0, p_display->size.x, p_display->size.y,
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

174)                        etp_pixfmt_rgb24);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

175) }
176) 
177) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

178)  * \brief set image data to output on EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

179)  *
180)  * updates (part of) the stored image data,
181)  * stored image data can later be sent to the distributors using
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

182)  * etp_display_send()
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

183)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

184)  * \param[in] p_display pointer to EtherPix display
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

185)  * \param[in] p_data pointer to rectangular section of image data
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

186)  * \param[in] stride_x stride between two pixels in X direction
187)  * \param[in] stride_y stride between two pixels in Y direction
188)  * \param[in] x X coordinate of left side of rectangular area
189)  * \param[in] y Y coordinate of top side of rectangular area
190)  * \param[in] width with of rectangular area
191)  * \param[in] height height of rectangular area
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

192)  * \param[in] pixfmt format of pixel data
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

193)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

194) void etp_display_data_fmt(etp_display_t *p_display, etp_u8_t *p_data,
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

195)                           int stride_x, int stride_y,
196)                           unsigned int x, unsigned int y,
197)                           unsigned int width, unsigned int height,
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

198)                           etp_pixfmt_t pixfmt)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

199) {
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

200)   unsigned int distri, out, pix, i;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

201)   etp_distri_t *p_distri;
202)   etp_u8_t *dest, *src;
203)   etp_pixel_t *p_pixel;
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

204)   int rx, ry;
205) 
206)   /* set data for all distributors */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

207)   for (distri = 0; distri < ETP_DISTRI_MAX_CNT; distri++) {
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

208)     p_distri = p_display->distri_ptrs[distri];
209)     if (p_distri) {
210) 
211)       /* set pointer to start of RGB data for pixels in message buffer
212)          (header is already set up and stays the same) */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

213)       dest = p_distri->p_msg_buf + ETP_MCUF_HDR_LEN;
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

214) 
215)       /* get RGB data of pixels for every output */
216)       for (out = 0, i = 0; out < p_distri->output_cnt; out++) {
217)         for (pix = 0; pix < p_distri->pixel_cnt; pix++, i++) {
218)           p_pixel = &p_distri->p_pixels[i];
219) 
220)           /* get pixel coordinates relative to rectangular area of image */
221)           rx = p_pixel->x - (int)x;
222)           ry = p_pixel->y - (int)y;
223)           /* check if pixel is within rectangular area of image */
224)           if (rx >= 0 && (unsigned int)rx < width
225)               && ry >= 0 && (unsigned int)ry < height) {
226)             /* get pixel data and map it */
227)             src = (p_data + rx * stride_x + ry * stride_y);
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

228)             switch (pixfmt) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

229)               case etp_pixfmt_rgb24:
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

230)                 dest[0] = p_distri->mapping[0].table[src[0]];
231)                 dest[1] = p_distri->mapping[1].table[src[1]];
232)                 dest[2] = p_distri->mapping[2].table[src[2]];
233)                 break;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

234)               case etp_pixfmt_bgr24:
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

235)                 dest[0] = p_distri->mapping[0].table[src[2]];
236)                 dest[1] = p_distri->mapping[1].table[src[1]];
237)                 dest[2] = p_distri->mapping[2].table[src[0]];
238)                 break;
239)               default:
240)                 dest[0] = 0;
241)                 dest[1] = 0;
242)                 dest[2] = 0;
243)             }
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

244)           }
245) 
246)           dest += 3;
247)         } /* for (pix ...) */
248)       } /* for (out ...) */
249) 
250)     } /* if (p_distri) */
251)   } /* for (distri ...) */
252) }
253) 
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

254) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

255)  * \brief set image data to output on EtherPix display
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

256)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

257)  * see etp_display_data_fmt for documentation
258)  * pixfmt is fixed to etp_pixfmt_rgb24
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

259)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

260) void etp_display_data(etp_display_t *p_display, etp_u8_t *p_data,
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

261)                       int stride_x, int stride_y,
262)                       unsigned int x, unsigned int y,
263)                       unsigned int width, unsigned int height)
264) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

265)   etp_display_data_fmt(p_display, p_data, stride_x, stride_y,
266)                        x, y, width, height, etp_pixfmt_rgb24);
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

267) }
268) 
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

269) /**
270)  * \brief send image data to distributors
271)  *
272)  * sends the currently stored image data to all configured distributors
273)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

274)  * \param[in] p_display pointer to EtherPix display
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

275)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

276) void etp_display_send(etp_display_t *p_display)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

277) {
Stefan Schuermans support multiple addresses...

Stefan Schuermans authored 7 years ago

278)   unsigned int distri, addr_i;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

279)   etp_distri_t *p_distri;
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

280) 
281)   /* send data to all distributors */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

282)   for (distri = 0; distri < ETP_DISTRI_MAX_CNT; distri++) {
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

283)     p_distri = p_display->distri_ptrs[distri];
284)     if (p_distri) {
285) 
Stefan Schuermans support multiple addresses...

Stefan Schuermans authored 7 years ago

286)       /* send message as UDP packet to each address */
287)       for (addr_i = 0; addr_i < p_distri->addr_cnt; ++addr_i) {
288)         sendto(p_display->sock,
289)                (char *)p_distri->p_msg_buf, p_distri->msg_len, 0,
290)                (struct sockaddr *)&p_distri->addrs[addr_i],
291)                sizeof (p_distri->addrs[addr_i]));
292)       } /* for (addr_i ...) */