led-matrix-c.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
  2. // Copyright (C) 2013 Henner Zeller <h.zeller@acm.org>
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation version 2.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>
  15. //
  16. // C-bridge for led matrix.
  17. #include "led-matrix-c.h"
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "led-matrix.h"
  21. #include "graphics.h"
  22. // Make sure C++ is in sync with C
  23. static_assert(sizeof(rgb_matrix::RGBMatrix::Options) == sizeof(RGBLedMatrixOptions), "C and C++ out of sync");
  24. static_assert(sizeof(rgb_matrix::RuntimeOptions) == sizeof(RGBLedRuntimeOptions), "C and C++ out of sync");
  25. // Our opaque dummy structs to communicate with the c-world
  26. struct RGBLedMatrix {};
  27. struct LedCanvas {};
  28. struct LedFont {};
  29. static rgb_matrix::RGBMatrix *to_matrix(struct RGBLedMatrix *matrix) {
  30. return reinterpret_cast<rgb_matrix::RGBMatrix*>(matrix);
  31. }
  32. static struct RGBLedMatrix *from_matrix(rgb_matrix::RGBMatrix *matrix) {
  33. return reinterpret_cast<struct RGBLedMatrix*>(matrix);
  34. }
  35. static rgb_matrix::FrameCanvas *to_canvas(struct LedCanvas *canvas) {
  36. return reinterpret_cast<rgb_matrix::FrameCanvas*>(canvas);
  37. }
  38. static struct LedCanvas *from_canvas(rgb_matrix::FrameCanvas *canvas) {
  39. return reinterpret_cast<struct LedCanvas*>(canvas);
  40. }
  41. static rgb_matrix::Font *to_font(struct LedFont *font) {
  42. return reinterpret_cast<rgb_matrix::Font*>(font);
  43. }
  44. static struct LedFont *from_font(rgb_matrix::Font *font) {
  45. return reinterpret_cast<struct LedFont*>(font);
  46. }
  47. static struct RGBLedMatrix *led_matrix_create_from_options_optional_edit(
  48. struct RGBLedMatrixOptions *opts, struct RGBLedRuntimeOptions *rt_opts,
  49. int *argc, char ***argv, bool remove_consumed_flags) {
  50. rgb_matrix::RuntimeOptions default_rt;
  51. rgb_matrix::RGBMatrix::Options default_opts;
  52. if (opts) {
  53. // Copy between C struct and C++ struct. The C++ struct already has a
  54. // default constructor that sets some values. These we override with the
  55. // C-struct values if available.
  56. // We assume everything non-zero has an explicit value.
  57. #define OPT_COPY_IF_SET(o) if (opts->o) default_opts.o = opts->o
  58. OPT_COPY_IF_SET(hardware_mapping);
  59. OPT_COPY_IF_SET(rows);
  60. OPT_COPY_IF_SET(cols);
  61. OPT_COPY_IF_SET(chain_length);
  62. OPT_COPY_IF_SET(parallel);
  63. OPT_COPY_IF_SET(pwm_bits);
  64. OPT_COPY_IF_SET(pwm_lsb_nanoseconds);
  65. OPT_COPY_IF_SET(pwm_dither_bits);
  66. OPT_COPY_IF_SET(brightness);
  67. OPT_COPY_IF_SET(scan_mode);
  68. OPT_COPY_IF_SET(row_address_type);
  69. OPT_COPY_IF_SET(multiplexing);
  70. OPT_COPY_IF_SET(disable_hardware_pulsing);
  71. OPT_COPY_IF_SET(show_refresh_rate);
  72. OPT_COPY_IF_SET(inverse_colors);
  73. OPT_COPY_IF_SET(led_rgb_sequence);
  74. OPT_COPY_IF_SET(pixel_mapper_config);
  75. OPT_COPY_IF_SET(panel_type);
  76. OPT_COPY_IF_SET(limit_refresh_rate_hz);
  77. #undef OPT_COPY_IF_SET
  78. }
  79. if (rt_opts) {
  80. // Same story as RGBMatrix::Options
  81. #define RT_OPT_COPY_IF_SET(o) if (rt_opts->o) default_rt.o = rt_opts->o
  82. RT_OPT_COPY_IF_SET(gpio_slowdown);
  83. RT_OPT_COPY_IF_SET(daemon);
  84. RT_OPT_COPY_IF_SET(drop_privileges);
  85. RT_OPT_COPY_IF_SET(do_gpio_init);
  86. #undef RT_OPT_COPY_IF_SET
  87. }
  88. rgb_matrix::RGBMatrix::Options matrix_options = default_opts;
  89. rgb_matrix::RuntimeOptions runtime_opt = default_rt;
  90. if (argc != NULL && argv != NULL) {
  91. if (!ParseOptionsFromFlags(argc, argv, &matrix_options, &runtime_opt,
  92. remove_consumed_flags)) {
  93. rgb_matrix::PrintMatrixFlags(stderr, default_opts, default_rt);
  94. return NULL;
  95. }
  96. }
  97. if (opts) {
  98. #define ACTUAL_VALUE_BACK_TO_OPT(o) opts->o = matrix_options.o
  99. ACTUAL_VALUE_BACK_TO_OPT(hardware_mapping);
  100. ACTUAL_VALUE_BACK_TO_OPT(rows);
  101. ACTUAL_VALUE_BACK_TO_OPT(cols);
  102. ACTUAL_VALUE_BACK_TO_OPT(chain_length);
  103. ACTUAL_VALUE_BACK_TO_OPT(parallel);
  104. ACTUAL_VALUE_BACK_TO_OPT(pwm_bits);
  105. ACTUAL_VALUE_BACK_TO_OPT(pwm_lsb_nanoseconds);
  106. ACTUAL_VALUE_BACK_TO_OPT(pwm_dither_bits);
  107. ACTUAL_VALUE_BACK_TO_OPT(brightness);
  108. ACTUAL_VALUE_BACK_TO_OPT(scan_mode);
  109. ACTUAL_VALUE_BACK_TO_OPT(row_address_type);
  110. ACTUAL_VALUE_BACK_TO_OPT(multiplexing);
  111. ACTUAL_VALUE_BACK_TO_OPT(disable_hardware_pulsing);
  112. ACTUAL_VALUE_BACK_TO_OPT(show_refresh_rate);
  113. ACTUAL_VALUE_BACK_TO_OPT(inverse_colors);
  114. ACTUAL_VALUE_BACK_TO_OPT(led_rgb_sequence);
  115. ACTUAL_VALUE_BACK_TO_OPT(pixel_mapper_config);
  116. ACTUAL_VALUE_BACK_TO_OPT(panel_type);
  117. ACTUAL_VALUE_BACK_TO_OPT(limit_refresh_rate_hz);
  118. #undef ACTUAL_VALUE_BACK_TO_OPT
  119. }
  120. if (rt_opts) {
  121. #define ACTUAL_VALUE_BACK_TO_RT_OPT(o) rt_opts->o = runtime_opt.o
  122. ACTUAL_VALUE_BACK_TO_RT_OPT(gpio_slowdown);
  123. ACTUAL_VALUE_BACK_TO_RT_OPT(daemon);
  124. ACTUAL_VALUE_BACK_TO_RT_OPT(drop_privileges);
  125. ACTUAL_VALUE_BACK_TO_RT_OPT(do_gpio_init);
  126. #undef ACTUAL_VALUE_BACK_TO_RT_OPT
  127. }
  128. rgb_matrix::RGBMatrix *matrix
  129. = rgb_matrix::RGBMatrix::CreateFromOptions(matrix_options, runtime_opt);
  130. return from_matrix(matrix);
  131. }
  132. struct RGBLedMatrix *led_matrix_create_from_options(
  133. struct RGBLedMatrixOptions *opts, int *argc, char ***argv) {
  134. return led_matrix_create_from_options_optional_edit(opts, NULL, argc, argv,
  135. true);
  136. }
  137. struct RGBLedMatrix *led_matrix_create_from_options_const_argv(
  138. struct RGBLedMatrixOptions *opts, int argc, char **argv) {
  139. return led_matrix_create_from_options_optional_edit(opts, NULL, &argc, &argv,
  140. false);
  141. }
  142. struct RGBLedMatrix *led_matrix_create_from_options_and_rt_options(
  143. struct RGBLedMatrixOptions *opts, struct RGBLedRuntimeOptions * rt_opts) {
  144. return led_matrix_create_from_options_optional_edit(opts, rt_opts, NULL, NULL,
  145. false);
  146. }
  147. struct RGBLedMatrix *led_matrix_create(int rows, int chained, int parallel) {
  148. struct RGBLedMatrixOptions opts;
  149. memset(&opts, 0, sizeof(opts));
  150. opts.rows = rows;
  151. opts.chain_length = chained;
  152. opts.parallel = parallel;
  153. return led_matrix_create_from_options(&opts, NULL, NULL);
  154. }
  155. void led_matrix_print_flags(FILE *out) {
  156. rgb_matrix::RGBMatrix::Options defaults;
  157. rgb_matrix::RuntimeOptions rt_opt;
  158. rt_opt.daemon = -1;
  159. rt_opt.drop_privileges = -1;
  160. rgb_matrix::PrintMatrixFlags(out, defaults, rt_opt);
  161. }
  162. void led_matrix_delete(struct RGBLedMatrix *matrix) {
  163. delete to_matrix(matrix);
  164. }
  165. struct LedCanvas *led_matrix_get_canvas(struct RGBLedMatrix *matrix) {
  166. return from_canvas(to_matrix(matrix)->SwapOnVSync(NULL));
  167. }
  168. struct LedCanvas *led_matrix_create_offscreen_canvas(struct RGBLedMatrix *m) {
  169. return from_canvas(to_matrix(m)->CreateFrameCanvas());
  170. }
  171. struct LedCanvas *led_matrix_swap_on_vsync(struct RGBLedMatrix *matrix,
  172. struct LedCanvas *canvas) {
  173. return from_canvas(to_matrix(matrix)->SwapOnVSync(to_canvas(canvas)));
  174. }
  175. void led_matrix_set_brightness(struct RGBLedMatrix *matrix,
  176. uint8_t brightness) {
  177. to_matrix(matrix)->SetBrightness(brightness);
  178. }
  179. uint8_t led_matrix_get_brightness(struct RGBLedMatrix *matrix) {
  180. return to_matrix(matrix)->brightness();
  181. }
  182. void led_canvas_get_size(const struct LedCanvas *canvas,
  183. int *width, int *height) {
  184. rgb_matrix::FrameCanvas *c = to_canvas((struct LedCanvas*)canvas);
  185. if (c == NULL ) return;
  186. if (width != NULL) *width = c->width();
  187. if (height != NULL) *height = c->height();
  188. }
  189. void led_canvas_set_pixel(struct LedCanvas *canvas, int x, int y,
  190. uint8_t r, uint8_t g, uint8_t b) {
  191. to_canvas(canvas)->SetPixel(x, y, r, g, b);
  192. }
  193. void led_canvas_clear(struct LedCanvas *canvas) {
  194. to_canvas(canvas)->Clear();
  195. }
  196. void led_canvas_fill(struct LedCanvas *canvas, uint8_t r, uint8_t g, uint8_t b) {
  197. to_canvas(canvas)->Fill(r, g, b);
  198. }
  199. struct LedFont *load_font(const char *bdf_font_file) {
  200. rgb_matrix::Font* font = new rgb_matrix::Font();
  201. font->LoadFont(bdf_font_file);
  202. return from_font(font);
  203. }
  204. int baseline_font(struct LedFont * font) {
  205. return to_font(font)->baseline();
  206. }
  207. int height_font(struct LedFont * font) {
  208. return to_font(font)->height();
  209. }
  210. struct LedFont *create_outline_font(struct LedFont * font) {
  211. rgb_matrix::Font* outlineFont = to_font(font)->CreateOutlineFont();
  212. return from_font(outlineFont);
  213. }
  214. void delete_font(struct LedFont *font) {
  215. delete to_font(font);
  216. }
  217. // -- Some utility functions.
  218. void set_image(struct LedCanvas *c, int canvas_offset_x, int canvas_offset_y,
  219. const uint8_t *image_buffer, size_t buffer_size_bytes,
  220. int image_width, int image_height,
  221. char is_bgr) {
  222. SetImage(to_canvas(c), canvas_offset_x, canvas_offset_y,
  223. image_buffer, buffer_size_bytes,
  224. image_width, image_height,
  225. is_bgr);
  226. }
  227. // Draw text, a standard NUL terminated C-string encoded in UTF-8,
  228. // with given "font" at "x","y" with "color".
  229. // "color" always needs to be set (hence it is a reference),
  230. // "background_color" is a pointer to optionally be NULL for transparency.
  231. // "kerning_offset" allows for additional spacing between characters (can be
  232. // negative)
  233. // Returns how many pixels we advanced on the screen.
  234. int draw_text(struct LedCanvas *c, struct LedFont *font, int x, int y,
  235. uint8_t r, uint8_t g, uint8_t b, const char *utf8_text, int kerning_offset) {
  236. const rgb_matrix::Color col = rgb_matrix::Color(r, g, b);
  237. return DrawText(to_canvas(c), *to_font(font), x, y, col, NULL, utf8_text, kerning_offset);
  238. }
  239. // Draw text, a standard NUL terminated C-string encoded in UTF-8,
  240. // with given "font" at "x","y" with "color".
  241. // Draw text as above, but vertically (top down).
  242. // The text is a standard NUL terminated C-string encoded in UTF-8.
  243. // "font, "x", "y", "color" and "background_color" are same as DrawText().
  244. // "kerning_offset" allows for additional spacing between characters (can be
  245. // negative).
  246. // Returns font height to advance up on the screen.
  247. int vertical_draw_text(struct LedCanvas *c, struct LedFont *font, int x, int y,
  248. uint8_t r, uint8_t g, uint8_t b,
  249. const char *utf8_text, int kerning_offset = 0) {
  250. const rgb_matrix::Color col = rgb_matrix::Color(r, g, b);
  251. return VerticalDrawText(to_canvas(c), *to_font(font), x, y, col, NULL, utf8_text, kerning_offset);
  252. }
  253. // Draw a circle centered at "x", "y", with a radius of "radius" and with "color"
  254. void draw_circle(struct LedCanvas *c, int xx, int y, int radius, uint8_t r, uint8_t g, uint8_t b) {
  255. const rgb_matrix::Color col = rgb_matrix::Color( r,g,b );
  256. DrawCircle(to_canvas(c), xx, y, radius, col);
  257. }
  258. // Draw a line from "x0", "y0" to "x1", "y1" and with "color"
  259. void draw_line(struct LedCanvas *c, int x0, int y0, int x1, int y1, uint8_t r, uint8_t g, uint8_t b) {
  260. const rgb_matrix::Color col = rgb_matrix::Color(r, g, b);
  261. DrawLine(to_canvas(c), x0, y0, x1, y1, col);
  262. }