int main( int argc, const char ** argv ) { int width = 64; int height = 64; ledscape_config_t * config = &ledscape_matrix_default; if (argc > 1) { config = ledscape_config(argv[1]); if (!config) return EXIT_FAILURE; } if (config->type == LEDSCAPE_MATRIX) { config->matrix_config.width = width; config->matrix_config.height = height; } ledscape_t * const leds = ledscape_init(config, 0); printf("init done\n"); time_t last_time = time(NULL); unsigned last_i = 0; unsigned i = 0; uint32_t * const p = calloc(width*height,4); render_game(p); i++; ledscape_draw(leds, p); usleep(20000); // wait for the previous frame to finish; //const uint32_t response = ledscape_wait(leds); const uint32_t response = 0; time_t now = time(NULL); if (now != last_time) { printf("%d fps. starting %d previous %"PRIx32"\n", i - last_i, i, response); last_i = i; last_time = now; } printf("init done\n"); ledscape_close(leds); return EXIT_SUCCESS; }
int main( int argc, char ** argv ) { const int width = 256; const int height = 128; ledscape_config_t * config = &ledscape_matrix_default; if (argc > 1) { config = ledscape_config(argv[1]); if (!config) return EXIT_FAILURE; } if (config->type == LEDSCAPE_MATRIX) { config->matrix_config.width = width; config->matrix_config.height = height; } ledscape_t * const leds = ledscape_init(config, 0); //printf("init done %d,%d\n", width, height); time_t last_time = time(NULL); unsigned last_i = 0; unsigned i = 0; uint32_t * const p = calloc(width*height,4); int scroll_x = 128; memset(p, 0x10, width*height*4); for (int i = 0 ; i < 8 ; i++) { for (int j = 0 ; j < 8 ; j++) { ledscape_printf( &p[8+j*32 + width*i*16], width, 0xFF0000, // red "%d-%d", i, j ); ledscape_printf( &p[1+j*32 + width*i*16], width, 0x00FF00, // green "^" ); ledscape_printf( &p[1+j*32 + width*(i*16+8)], width, 0x0000FF, // blue "|" ); p[j*32+width*i*16] = 0xFFFF00; } } while (1) { ledscape_draw(leds, p); usleep(20000); // wait for the previous frame to finish; //const uint32_t response = ledscape_wait(leds); const uint32_t response = 0; time_t now = time(NULL); if (now != last_time) { printf("%d fps. starting %d previous %"PRIx32"\n", i - last_i, i, response); last_i = i; last_time = now; } } ledscape_close(leds); return EXIT_SUCCESS; }
int main( int argc, char ** argv ) { /* getopt_long stores the option index here. */ int option_index = 0; int port = 9999; const char * config_file = NULL; const char * startup_message = ""; int timeout = 60; unsigned width = 512; unsigned height = 64; int no_init = 0; while (1) { const int c = getopt_long( argc, argv, "vp:c:t:W:H:m:n", long_options, &option_index ); if (c == -1) break; switch (c) { case 'v': verbose++; break; case 'n': no_init++; break; case 'c': config_file = optarg; break; case 't': timeout = atoi(optarg); break; case 'W': width = atoi(optarg); break; case 'H': height = atoi(optarg); break; case 'm': startup_message = optarg; break; default: usage(); return -1; } } const int sock = udp_socket(port); if (sock < 0) die("socket port %d failed: %s\n", port, strerror(errno)); const size_t image_size = width * height * 3; const size_t buf_size = (width*height*4)/packets_per_frame + 1; // largest possible UDP packet uint8_t *buf = malloc(buf_size); #if 0 if (sizeof(buf) < image_size + 1) die("%u x %u too large for UDP\n", width, height); #endif fprintf(stderr, "%u x %u, UDP port %u\n", width, height, port); ledscape_config_t * config = &ledscape_matrix_default; if (config_file) { config = ledscape_config(config_file); if (!config) return EXIT_FAILURE; } if (config->type == LEDSCAPE_MATRIX) { config->matrix_config.width = width; config->matrix_config.height = height; } ledscape_t * const leds = ledscape_init(config, no_init); if (!leds) return EXIT_FAILURE; const unsigned report_interval = 10; unsigned last_report = 0; unsigned long delta_sum = 0; unsigned frames = 0; uint32_t * const fb = calloc(width*height,4); ledscape_printf(fb, width, 0xFF0000, "%s", startup_message); ledscape_printf(fb+16*width, width, 0x00FF00, "%dx%d UDP port %d", width, height, port); ledscape_draw(leds, fb); while (1) { int rc = wait_socket(sock, timeout*1000); if (rc < 0) { // something failed memset(fb, 0, width*height*4); ledscape_printf(fb, width, 0xFF0000, "read failed?"); ledscape_draw(leds, fb); exit(EXIT_FAILURE); } if (rc == 0) { // go into timeout mode memset(fb, 0, width*height*4); ledscape_printf(fb, width, 0xFF0000, "timeout"); ledscape_draw(leds, fb); continue; } const ssize_t rlen = recv(sock, buf, buf_size, 0); if (rlen < 0) die("recv failed: %s\n", strerror(errno)); warn_once("received %zu bytes\n", rlen); /* if (buf[0] == 2) { // image type printf("image type: %.*s\n", (int) rlen - 1, &buf[1] ); continue; } if (buf[0] != 1) { // What is it? warn_once("Unknown image type '%c' (%02x)\n", buf[0], buf[0] ); continue; } */ const unsigned frame_part = buf[0]; if (frame_part != 0 && frame_part != 1) { printf("bad type %d\n", frame_part); continue; } if ((size_t) rlen != image_size + 1) { warn_once("WARNING: Received packet %zu bytes, expected %zu\n", rlen, image_size + 1 ); } struct timeval start_tv, stop_tv, delta_tv; gettimeofday(&start_tv, NULL); const unsigned frame_num = 0; // copy the 3-byte values into the 4-byte framebuffer // and turn onto the side for (unsigned x = 0 ; x < width ; x++) // 256 { for (unsigned y = 0 ; y < 32 ; y++) // 64 { uint32_t * out = (void*) &fb[(y+32*frame_part)*width + x]; const uint8_t * const in = &buf[1 + 3*(y*width + x)]; uint32_t r = in[0]; uint32_t g = in[1]; uint32_t b = in[2]; *out = (r << 16) | (g << 8) | (b << 0); } } // only draw after the second frame if (frame_part == 1) ledscape_draw(leds, fb); gettimeofday(&stop_tv, NULL); timersub(&stop_tv, &start_tv, &delta_tv); frames++; delta_sum += delta_tv.tv_usec; if (stop_tv.tv_sec - last_report < report_interval) continue; last_report = stop_tv.tv_sec; const unsigned delta_avg = delta_sum / frames; printf("%6u usec avg, max %.2f fps, actual %.2f fps (over %u frames)\n", delta_avg, report_interval * 1.0e6 / delta_avg, frames * 1.0 / report_interval, frames ); frames = delta_sum = 0; } return 0; }