a3a92abf1ca043aaaaaa79381afa316abaa00ace
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 removed version information...

Stefan Schuermans authored 13 years ago

4)  * Copyright 2010-2011 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 <sys/time.h>
21) #include <stdlib.h>
22) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

23) #include <etherpix/display.h>
24) #include <etherpix/displayer.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/displayer.h>
Stefan Schuermans v1.0.6

Stefan Schuermans authored 13 years ago

28) #include <intern/thread.h>
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

29) #include <intern/types.h>
30) 
31) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

32)  * \brief create a new EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

33)  *
34)  * A displayer manages a display and ensures that a the current
35)  * image data is sent often enough to the distributors so
36)  * that the pixels do detect a timeout and turn off automatically.
37)  * The displayer uses a thread to do this.
38)  * Initially, the new displayer is inactive and has to be activated
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

39)  * using etp_displayer_activate().
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

40)  *
41)  * \param[in] sz_config_file name of config file to read
42)  * \param[in] p_msg_func message callback function or NULL
43)  * \param[in] p_msg_ctx user context for message callback
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

44)  * \return pointer to new EtherPix displayer on success
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

45)  *         or NULL on error
46)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

47) etp_displayer_t *etp_displayer_create(const char *sz_config_file,
48)                                       etp_msg_func_p_t p_msg_func,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

49)                                       void *p_msg_ctx)
50) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

51)   etp_displayer_t *p_displayer;
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

52) 
53)   /* set up basic structure and display */
54) 
55)   /* create displayer structure */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

56)   p_displayer = (etp_displayer_t *)calloc(1, sizeof (etp_displayer_t));
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

57)   if (!p_displayer) {
58)     if (p_msg_func)
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

60)                  "out of memory\n");
61)     return NULL;
62)   }
63) 
64)   /* set up flags */
65)   p_displayer->active = 0; /* not sending data to distributors */
66)   p_displayer->send   = 1; /* send first data as soon as active */
67)   p_displayer->end    = 0; /* do not end yet */
68) 
69)   /* set up managed display */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

70)   p_displayer->p_display = etp_display_create(sz_config_file,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

71)                                               p_msg_func, p_msg_ctx);
72)   if (!p_displayer->p_display) {
73)     free(p_displayer);
74)     if (p_msg_func)
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

76)                  "could not create display\n");
77)     return NULL;
78)   }
79) 
80)   /* set up output thread */
81) 
82)   /* initialize mutex */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

83)   if (!etp_thread_mutex_init(&p_displayer->mtx)) {
84)     etp_display_free(p_displayer->p_display);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

85)     free(p_displayer);
86)     if (p_msg_func)
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

88)                  "could not initialize mutex\n");
89)     return NULL;
90)   }
91) 
92)   /* initialize condition */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

93)   if (!etp_thread_cond_init(&p_displayer->cond)) {
94)     etp_thread_mutex_destroy(&p_displayer->mtx);
95)     etp_display_free(p_displayer->p_display);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

96)     free(p_displayer);
97)     if (p_msg_func)
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

99)                  "could not initialize condition\n");
100)     return NULL;
101)   }
102) 
103)   /* create thread */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

104)   if (!etp_thread_create(&p_displayer->tid, etp_displayer_thread,
Stefan Schuermans v1.0.6

Stefan Schuermans authored 13 years ago

105)                         (void *)p_displayer)) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

106)     etp_thread_cond_destroy(&p_displayer->cond);
107)     etp_thread_mutex_destroy(&p_displayer->mtx);
108)     etp_display_free(p_displayer->p_display);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

109)     free(p_displayer);
110)     if (p_msg_func)
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

112)                  "could not create thread\n");
113)     return NULL;
114)   }
115) 
116)   return p_displayer;
117) }
118) 
119) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

120)  * \brief free a EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

122)  * \param[in] p_displayer pointer to EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

124) void etp_displayer_free(etp_displayer_t *p_displayer)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

125) {
126)   /* tear down thread */
127)   p_displayer->end = 1;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

128)   etp_thread_cond_signal(&p_displayer->cond);
129)   etp_thread_join(p_displayer->tid);
130)   etp_thread_cond_destroy(&p_displayer->cond);
131)   etp_thread_mutex_destroy(&p_displayer->mtx);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

132) 
133)   /* free managed display */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

134)   etp_display_free(p_displayer->p_display);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

135) 
136)   /* free basic structure */
137)   free(p_displayer);
138) }
139) 
140) /**
141)  * \brief get size of display managed by displayer
142)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

143)  * \param[in] p_displayer pointer to EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

144)  * \param[out] p_width width of display
145)  * \param[out] p_height height of display
146)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

147) void etp_displayer_get_size(etp_displayer_t *p_displayer,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

148)                             unsigned int *p_width, unsigned int *p_height)
149) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

150)   etp_thread_mutex_lock(&p_displayer->mtx);
151)   etp_display_get_size(p_displayer->p_display, p_width, p_height);
152)   etp_thread_mutex_unlock(&p_displayer->mtx);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

153) }
154) 
155) /**
156)  * \brief activate the displayer
157)  *
158)  * set the displayer to active, i.e. make it send image data
159)  * to the distributors automatically,
160)  * this function might trigger sending of data if the last
161)  * sending time was too long ago
162)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

163)  * \param[in] p_displayer pointer to EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

165) void etp_displayer_activate(etp_displayer_t *p_displayer)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

166) {
167)   p_displayer->active = 1;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

168)   etp_thread_cond_signal(&p_displayer->cond);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

169) }
170) 
171) /**
172)  * \brief deactivate the displayer
173)  *
174)  * set the displayer to deactive, i.e. prevent it from sening image
175)  * data to the distributors automatically,
176)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

177)  * \param[in] p_displayer pointer to EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

179) void etp_displayer_deactivate(etp_displayer_t *p_displayer)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

180) {
181)   p_displayer->active = 0;
182)   /* no need to signal condition: thread does not need to do something yet */
183) }
184) 
185) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

187)  *
188)  * clears the stored image data
189)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

190)  * \param[in] p_displayer pointer to EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

192) void etp_displayer_data_clear(etp_displayer_t *p_displayer)
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)   etp_thread_mutex_lock(&p_displayer->mtx);
195)   etp_display_data_clear(p_displayer->p_display);
196)   etp_thread_mutex_unlock(&p_displayer->mtx);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

197) }
198) 
199) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

201)  *
202)  * updates (part of) the stored image data
203)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

204)  * \param[in] p_displayer pointer to EtherPix displayer
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 13 years ago

206)  * \param[in] stride_x stride between two pixels in X direction
207)  * \param[in] stride_y stride between two pixels in Y direction
208)  * \param[in] x X coordinate of left side of rectangular area
209)  * \param[in] y Y coordinate of top side of rectangular area
210)  * \param[in] width with of rectangular area
211)  * \param[in] height height of rectangular area
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

212)  * \param[in] pixfmt format of pixel data
213)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

214) void etp_displayer_data_fmt(etp_displayer_t *p_displayer, etp_u8_t *p_data,
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

215)                             int stride_x, int stride_y,
216)                             unsigned int x, unsigned int y,
217)                             unsigned int width, unsigned int height,
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

218)                             etp_pixfmt_t pixfmt)
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

220)   etp_thread_mutex_lock(&p_displayer->mtx);
221)   etp_display_data_fmt(p_displayer->p_display, p_data, stride_x, stride_y,
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

222)                        x, y, width, height, pixfmt);
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

223)   etp_thread_mutex_unlock(&p_displayer->mtx);
Stefan Schuermans support BGR input in additi...

Stefan Schuermans authored 7 years ago

224) }
225) 
226) /**
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

229)  * see etp_displayer_data_fmt for documentation
230)  * pixfmt is fixed to etp_pixfmt_rgb24
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

232) void etp_displayer_data(etp_displayer_t *p_displayer, etp_u8_t *p_data,
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

233)                         int stride_x, int stride_y,
234)                         unsigned int x, unsigned int y,
235)                         unsigned int width, unsigned int height)
236) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

237)   etp_displayer_data_fmt(p_displayer, p_data, stride_x, stride_y,
238)                          x, y, width, height, etp_pixfmt_rgb24);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

239) }
240) 
241) /**
242)  * \brief trigger immediate sending of new image data to distributors
243)  *
244)  * this only works if the displayer is active
245)  *
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

246)  * \param[in] p_displayer pointer to EtherPix displayer
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

248) void etp_displayer_send(etp_displayer_t *p_displayer)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

249) {
250)   p_displayer->send = 1;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

251)   etp_thread_cond_signal(&p_displayer->cond);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

252) }
253) 
254) /**
255)  * \brief output thread of displayer object
256)  *
257)  * \param[in] vp_displayer pointer to displayer object
258)  * \return unused, always NULL
259)  */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

260) ETP_THREAD_FUNC(etp_displayer_thread)
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

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

Stefan Schuermans authored 7 years ago

262)   etp_displayer_t *p_displayer = (etp_displayer_t *)vp_displayer;
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

263)   struct timeval last, now, delta;
264)   int timed_send;
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

265)   int interval_ms = ETP_MCUF_MAX_FRAME_INTERVAL_MS;
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

266) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

267)   etp_thread_mutex_lock(&p_displayer->mtx);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

268) 
269)   gettimeofday(&last, NULL); /* initialize last to some valid time */
270) 
271)   /* while thread shall not end */
272)   while (!p_displayer->end) {
273) 
274)     /* active */
275)     if (p_displayer->active) {
276) 
277)       /* get current time */
278)       gettimeofday(&now, NULL);
279)       /* get time since sending last frame */
280)       delta.tv_sec = now.tv_sec - last.tv_sec;
281)       delta.tv_usec = now.tv_usec - last.tv_usec;
282)       if (delta.tv_usec < 0) {
283)         delta.tv_usec += 1000000;
284)         ++delta.tv_sec;
285)       }
286)       /* check if late enough for timed send */
Stefan Schuermans v1.0.6

Stefan Schuermans authored 13 years ago

287)       timed_send = delta.tv_sec > interval_ms / 1000
288)                  || (delta.tv_sec == interval_ms / 1000
289)                      && delta.tv_usec >= (interval_ms % 1000) * 1000);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

290) 
291)       /* send if send requested or late enough */
292)       if (p_displayer->send || timed_send) {
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

293)         etp_display_send(p_displayer->p_display); /* send */
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

294)         last = now;                               /* remember last send time */
295)         p_displayer->send = 0;                    /* clear send request */
296)       }
297) 
298)       /* sleep until next frame has to be sent */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

299)       etp_thread_cond_timedwait(&p_displayer->cond, &p_displayer->mtx,
Stefan Schuermans v1.0.6

Stefan Schuermans authored 13 years ago

300)                                 &last, interval_ms);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

301) 
302)     } /* if (p_displayer->active) */
303) 
304)     /* inactive */
305)     else {
306) 
307)       /* sleep */
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

308)       etp_thread_cond_wait(&p_displayer->cond, &p_displayer->mtx);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

309) 
310)     } /* if (p_displayer->active) ... else */
311) 
312)   } /* while(!p_displayer->end) */
313) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

314)   etp_thread_mutex_unlock(&p_displayer->mtx);
Stefan Schuermans v1.0.0

Stefan Schuermans authored 13 years ago

315) 
Stefan Schuermans rename "FlexiPix" to "Ether...

Stefan Schuermans authored 7 years ago

316)   ETP_THREAD_RETURN(NULL);