BlinkenArea - GitList
Repositories
Blog
Wiki
libflexipix
Code
Commits
Branches
Tags
Search
Tree:
e2a21a2
Branches
Tags
master
v1.0.0
v1.0.1
v1.0.2
v1.0.3
v1.0.4
v1.0.5
v1.0.6
v1.0.7
v1.0.8
libflexipix
examples
src
event.c
v1.0.6
Stefan Schuermans
commited
e2a21a2
at 2011-09-11 17:17:58
event.c
Blame
History
Raw
#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <flexipix/flexipix.h> #ifdef WINDOWS #include <winsock.h> #endif /* just an example for some data */ typedef struct userdata_s { int i; float f; char c; } userdata_t; /* callback function called during display creation, the p_ctx parameter is the userdata supplied in the call to flp_display_create() */ void msg(void *p_ctx, flp_msg_type_t type, const char *fmt, ...) { userdata_t *p_userdata = (userdata_t *)p_ctx; va_list va; printf("msg (%d %f %c): ", p_userdata->i, p_userdata->f, p_userdata->c); /* make use of the userdata */ /* type tells if the message is an error, a warning or an information */ switch (type) { case flp_msg_type_err: printf("ERROR: "); break; case flp_msg_type_warn: printf("WARNING: "); break; case flp_msg_type_info: printf("INFORMATION: "); break; } /* fmt is a printf-style format string, the arguments are passed exactly as for printf */ va_start(va, fmt); vprintf(fmt, va); va_end(va); } int main(int argc, char *argv[]) { const char *config; userdata_t userdata = { 42, 42.0, '!' }; /* just an example */ flp_displayer_t *p_displayer; unsigned int width, height, i, j, c, x, y; flp_u8_t *p_image; char line[64]; if (argc < 2) { printf("usage: %s <config.flp>\n", argv[0]); return -1; } config = argv[1]; #ifdef WINDOWS { WSADATA data; WSAStartup(MAKEWORD(1,1), &data); } #endif /* create a displayer that manages a display in the background and takes care that data is sent periodically to the distributors to prevent that the pixels turn off after a timeout, take the configuration from a file, deliver messages to the callback function msg(), the last parameter is passed as first parameter to msg() and can be used to supply some user defined data */ p_displayer = flp_displayer_create(config, msg, (void *)&userdata); if (!p_displayer) { printf("could not create displayer\n"); return -1; } flp_displayer_get_size(p_displayer, &width, &height); printf("width: %5u\nheight: %5u\n", width, height); /* create a buffer for the image */ p_image = (flp_u8_t *)malloc(height * width * 3); if (!p_image) { flp_displayer_free(p_displayer); printf("out of memory\n"); return -1; } /* make displayer start sending data to distributors */ flp_displayer_activate(p_displayer); /* The following loop will spent most waiting for user input. While it is waiting, the displayer will periodically sent data to the distributors. */ printf("enter something, \".\" for end\n"); while (1) { /* wait for user input */ printf("> "); scanf("%50s", line); if (line[0] == 0 || (line[0] == '.' && line[1] == 0)) break; /* create some image from entered text (this is just creating some image out of the entered data, it does not do any useful processing) */ i = 0; j = 0; for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { for (c = 0; c < 3; ++c) { p_image[i++] = (line[j++] & 0xF) * 0x11; if (line[j] == 0) j = 0; } } } /* set new image */ flp_displayer_data(p_displayer, p_image, 3, /* consecutive pixels are 3 bytes apart */ width*3, /* consecutive lines are width*3 bytes apart */ 0, 0, width, height); /* force sending data to distris immediately */ flp_displayer_send(p_displayer); } /* while (1) */ free(p_image); flp_displayer_free(p_displayer); #ifdef WINDOWS WSACleanup(); #endif return 0; }