BlinkenArea - GitList
Repositories
Blog
Wiki
libetherpix
Code
Commits
Branches
Tags
Search
Tree:
9b23234
Branches
Tags
master
libetherpix
src
displayer.c
update Windows support
Stefan Schuermans
commited
9b23234
at 2017-09-24 08:46:27
displayer.c
Blame
History
Raw
/* * EtherPix library * * Copyright 2010-2017 Stefan Schuermans <stefan schuermans info> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <sys/time.h> #include <stdlib.h> #include <intern/net.h> // first because of winsock2.h ordering #include <etherpix/display.h> #include <etherpix/displayer.h> #include <etherpix/msg.h> #include <etherpix/types.h> #include <intern/displayer.h> #include <intern/thread.h> #include <intern/types.h> /** * \brief create a new EtherPix displayer * * A displayer manages a display and ensures that a the current * image data is sent often enough to the distributors so * that the pixels do detect a timeout and turn off automatically. * The displayer uses a thread to do this. * Initially, the new displayer is inactive and has to be activated * using etp_displayer_activate(). * * \param[in] sz_config_file name of config file to read * \param[in] p_msg_func message callback function or NULL * \param[in] p_msg_ctx user context for message callback * \return pointer to new EtherPix displayer on success * or NULL on error */ etp_displayer_t *etp_displayer_create(const char *sz_config_file, etp_msg_func_p_t p_msg_func, void *p_msg_ctx) { etp_displayer_t *p_displayer; /* set up basic structure and display */ /* create displayer structure */ p_displayer = (etp_displayer_t *)calloc(1, sizeof (etp_displayer_t)); if (!p_displayer) { if (p_msg_func) p_msg_func(p_msg_ctx, etp_msg_type_err, "out of memory\n"); return NULL; } /* set up flags */ p_displayer->active = 0; /* not sending data to distributors */ p_displayer->send = 1; /* send first data as soon as active */ p_displayer->end = 0; /* do not end yet */ /* set up managed display */ p_displayer->p_display = etp_display_create(sz_config_file, p_msg_func, p_msg_ctx); if (!p_displayer->p_display) { free(p_displayer); if (p_msg_func) p_msg_func(p_msg_ctx, etp_msg_type_err, "could not create display\n"); return NULL; } /* set up output thread */ /* initialize mutex */ if (!etp_thread_mutex_init(&p_displayer->mtx)) { etp_display_free(p_displayer->p_display); free(p_displayer); if (p_msg_func) p_msg_func(p_msg_ctx, etp_msg_type_err, "could not initialize mutex\n"); return NULL; } /* initialize condition */ if (!etp_thread_cond_init(&p_displayer->cond)) { etp_thread_mutex_destroy(&p_displayer->mtx); etp_display_free(p_displayer->p_display); free(p_displayer); if (p_msg_func) p_msg_func(p_msg_ctx, etp_msg_type_err, "could not initialize condition\n"); return NULL; } /* create thread */ if (!etp_thread_create(&p_displayer->tid, etp_displayer_thread, (void *)p_displayer)) { etp_thread_cond_destroy(&p_displayer->cond); etp_thread_mutex_destroy(&p_displayer->mtx); etp_display_free(p_displayer->p_display); free(p_displayer); if (p_msg_func) p_msg_func(p_msg_ctx, etp_msg_type_err, "could not create thread\n"); return NULL; } return p_displayer; } /** * \brief free a EtherPix displayer * * \param[in] p_displayer pointer to EtherPix displayer */ void etp_displayer_free(etp_displayer_t *p_displayer) { /* tear down thread */ p_displayer->end = 1; etp_thread_cond_signal(&p_displayer->cond); etp_thread_join(p_displayer->tid); etp_thread_cond_destroy(&p_displayer->cond); etp_thread_mutex_destroy(&p_displayer->mtx); /* free managed display */ etp_display_free(p_displayer->p_display); /* free basic structure */ free(p_displayer); } /** * \brief get size of display managed by displayer * * \param[in] p_displayer pointer to EtherPix displayer * \param[out] p_width width of display * \param[out] p_height height of display */ void etp_displayer_get_size(etp_displayer_t *p_displayer, unsigned int *p_width, unsigned int *p_height) { etp_thread_mutex_lock(&p_displayer->mtx); etp_display_get_size(p_displayer->p_display, p_width, p_height); etp_thread_mutex_unlock(&p_displayer->mtx); } /** * \brief activate the displayer * * set the displayer to active, i.e. make it send image data * to the distributors automatically, * this function might trigger sending of data if the last * sending time was too long ago * * \param[in] p_displayer pointer to EtherPix displayer */ void etp_displayer_activate(etp_displayer_t *p_displayer) { p_displayer->active = 1; etp_thread_cond_signal(&p_displayer->cond); } /** * \brief deactivate the displayer * * set the displayer to deactive, i.e. prevent it from sening image * data to the distributors automatically, * * \param[in] p_displayer pointer to EtherPix displayer */ void etp_displayer_deactivate(etp_displayer_t *p_displayer) { p_displayer->active = 0; /* no need to signal condition: thread does not need to do something yet */ } /** * \brief clear image data to output on EtherPix displayer * * clears the stored image data * * \param[in] p_displayer pointer to EtherPix displayer */ void etp_displayer_data_clear(etp_displayer_t *p_displayer) { etp_thread_mutex_lock(&p_displayer->mtx); etp_display_data_clear(p_displayer->p_display); etp_thread_mutex_unlock(&p_displayer->mtx); } /** * \brief set image data to output on EtherPix display * * updates (part of) the stored image data * * \param[in] p_displayer pointer to EtherPix displayer * \param[in] p_data pointer to rectangular section of image data * \param[in] stride_x stride between two pixels in X direction * \param[in] stride_y stride between two pixels in Y direction * \param[in] x X coordinate of left side of rectangular area * \param[in] y Y coordinate of top side of rectangular area * \param[in] width with of rectangular area * \param[in] height height of rectangular area * \param[in] pixfmt format of pixel data */ void etp_displayer_data_fmt(etp_displayer_t *p_displayer, etp_u8_t *p_data, int stride_x, int stride_y, unsigned int x, unsigned int y, unsigned int width, unsigned int height, etp_pixfmt_t pixfmt) { etp_thread_mutex_lock(&p_displayer->mtx); etp_display_data_fmt(p_displayer->p_display, p_data, stride_x, stride_y, x, y, width, height, pixfmt); etp_thread_mutex_unlock(&p_displayer->mtx); } /** * \brief set image data to output on EtherPix display * * see etp_displayer_data_fmt for documentation * pixfmt is fixed to etp_pixfmt_rgb24 */ void etp_displayer_data(etp_displayer_t *p_displayer, etp_u8_t *p_data, int stride_x, int stride_y, unsigned int x, unsigned int y, unsigned int width, unsigned int height) { etp_displayer_data_fmt(p_displayer, p_data, stride_x, stride_y, x, y, width, height, etp_pixfmt_rgb24); } /** * \brief trigger immediate sending of new image data to distributors * * this only works if the displayer is active * * \param[in] p_displayer pointer to EtherPix displayer */ void etp_displayer_send(etp_displayer_t *p_displayer) { p_displayer->send = 1; etp_thread_cond_signal(&p_displayer->cond); } /** * \brief output thread of displayer object * * \param[in] vp_displayer pointer to displayer object * \return unused, always NULL */ ETP_THREAD_FUNC(etp_displayer_thread) { etp_displayer_t *p_displayer = (etp_displayer_t *)vp_displayer; struct timeval last, now, delta; int timed_send; int interval_ms = ETP_MCUF_MAX_FRAME_INTERVAL_MS; etp_thread_mutex_lock(&p_displayer->mtx); gettimeofday(&last, NULL); /* initialize last to some valid time */ /* while thread shall not end */ while (!p_displayer->end) { /* active */ if (p_displayer->active) { /* get current time */ gettimeofday(&now, NULL); /* get time since sending last frame */ delta.tv_sec = now.tv_sec - last.tv_sec; delta.tv_usec = now.tv_usec - last.tv_usec; if (delta.tv_usec < 0) { delta.tv_usec += 1000000; ++delta.tv_sec; } /* check if late enough for timed send */ timed_send = delta.tv_sec > interval_ms / 1000 || (delta.tv_sec == interval_ms / 1000 && delta.tv_usec >= (interval_ms % 1000) * 1000); /* send if send requested or late enough */ if (p_displayer->send || timed_send) { etp_display_send(p_displayer->p_display); /* send */ last = now; /* remember last send time */ p_displayer->send = 0; /* clear send request */ } /* sleep until next frame has to be sent */ etp_thread_cond_timedwait(&p_displayer->cond, &p_displayer->mtx, &last, interval_ms); } /* if (p_displayer->active) */ /* inactive */ else { /* sleep */ etp_thread_cond_wait(&p_displayer->cond, &p_displayer->mtx); } /* if (p_displayer->active) ... else */ } /* while(!p_displayer->end) */ etp_thread_mutex_unlock(&p_displayer->mtx); return ETP_THREAD_RETURN_OK; }