protodemo.c
1 #include <cmath> 2 #include <cstdint> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <ctime> 7 #include <vector> 8 9 #include "piomatter/piomatter.h" 10 11 #define _ (0) 12 #define r (255 << 16) 13 #define g (255 << 8) 14 #define b (255) 15 #define y (r | g) 16 #define c (g | b) 17 #define m (r | b) 18 #define w (r | g | b) 19 20 constexpr int width = 64, height = 64; 21 22 uint32_t pixels[height][width] = { 23 {_, w, _, _, r, r, _, _, _, g, _, _, b, b, b, _, 24 c, c, _, _, y, _, y, _, m, m, m, _, w, w, w, _}, // 0 25 {w, _, w, _, r, _, r, _, g, _, g, _, b, _, _, _, 26 c, _, c, _, y, _, y, _, _, m, _, _, _, w, _, _}, // 1 27 {w, w, w, _, r, _, r, _, g, g, g, _, b, b, _, _, 28 c, c, _, _, y, _, y, _, _, m, _, _, _, w, _, _}, // 2 29 {w, _, w, _, r, _, r, _, g, _, g, _, b, _, _, _, 30 c, _, c, _, y, _, y, _, _, m, _, _, _, w, _, _}, // 3 31 {w, _, w, _, r, r, _, _, g, _, g, _, b, _, _, _, 32 c, _, c, _, _, y, _, _, m, m, m, _, _, w, _, _}, // 4 33 {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 34 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, // 5 35 {_, c, _, _, y, y, _, _, _, m, _, _, r, r, r, _, 36 g, g, _, _, b, _, b, _, w, w, w, _, c, c, c, _}, // 6 37 {c, _, c, _, y, _, y, _, m, _, m, _, r, _, _, _, 38 g, _, g, _, b, _, b, _, _, w, _, _, _, c, _, _}, // 7 39 {c, c, c, _, y, _, y, _, m, m, m, _, r, r, _, _, 40 g, g, _, _, b, _, b, _, _, w, _, _, _, c, _, _}, // 8 41 {c, _, c, _, y, _, y, _, m, _, m, _, r, _, _, _, 42 g, _, g, _, b, _, b, _, _, w, _, _, _, c, _, _}, // 9 43 {c, _, c, _, y, y, _, _, m, _, m, _, r, _, _, _, 44 g, _, g, _, _, b, _, _, w, w, w, _, _, c, _, _}, // 10 45 {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 46 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, // 11 47 {r, y, g, c, b, m, r, y, g, c, b, m, r, y, g, c, 48 b, m, r, y, g, c, b, m, r, y, g, c, b, m, r, g}, // 12 49 {y, g, c, b, m, r, y, g, c, b, m, r, y, g, c, b, 50 m, r, y, g, c, b, m, r, y, g, c, b, m, r, g, y}, // 13 51 {g, c, b, m, r, y, g, c, b, m, r, y, g, c, b, m, 52 r, y, g, c, b, m, r, y, g, c, b, m, r, g, c, b}, // 14 53 {c, b, m, r, y, g, c, b, m, r, y, g, c, b, m, r, 54 y, g, c, b, m, r, y, g, c, b, m, r, g, c, b, m}, // 15 55 }; 56 #undef r 57 #undef g 58 #undef b 59 #undef c 60 #undef y 61 #undef w 62 #undef _ 63 64 #define rgb(r, g, b) ((r << 16) | (g << 8) | b) 65 66 uint32_t colorwheel(int i) { 67 i = i & 0xff; 68 if (i < 85) { 69 return rgb(255 - i * 3, 0, i * 3); 70 } 71 if (i < 170) { 72 i -= 85; 73 return rgb(0, i * 3, 255 - i * 3); 74 } 75 i -= 170; 76 return rgb(i * 3, 255 - i * 3, 0); 77 } 78 79 void test_pattern(int offs) { 80 for (int i = 0; i < width; i++) { 81 pixels[height - 5][i] = rgb(1 + i * 4, 1 + i * 4, 1 + i * 4); 82 pixels[height - 4][i] = colorwheel(2 * i + offs / 3); 83 pixels[height - 3][i] = colorwheel(2 * i + 64 + offs / 5); 84 pixels[height - 2][i] = colorwheel(2 * i + 128 + offs / 2); 85 pixels[height - 1][i] = colorwheel(2 * i + 192 + offs / 7); 86 } 87 for (int i = 0; i < height; i++) { 88 pixels[i][i] = rgb(0xff, 0xff, 0xff); 89 } 90 } 91 92 static uint64_t monotonicns64() { 93 94 struct timespec tp; 95 clock_gettime(CLOCK_MONOTONIC, &tp); 96 return tp.tv_sec * UINT64_C(1000000000) + tp.tv_nsec; 97 } 98 99 int main(int argc, char **argv) { 100 int n = argc > 1 ? atoi(argv[1]) : 0; 101 102 piomatter::matrix_geometry geometry(128, 4, 10, 64, 64, true, 103 piomatter::orientation_normal); 104 piomatter::piomatter p(std::span(&pixels[0][0], 64 * 64), geometry); 105 106 uint64_t start = monotonicns64(); 107 for (int i = 0; i < n; i++) { 108 test_pattern(i); 109 p.show(); 110 } 111 uint64_t end = monotonicns64(); 112 113 uint64_t duration = end - start; 114 double fps = n * 1e9 / duration; 115 printf("%.1f FPS [%d frames in %fs]\n", fps, n, duration / 1e9); 116 }