AP_OSD_SITL.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. */
  16. /*
  17. OSD backend for SITL. Uses SFML media library. See
  18. https://www.sfml-dev.org/index.php
  19. To use install SFML libraries, and run sim_vehicle.py with --osd
  20. option. Then set OSD_TYPE to 2
  21. */
  22. #ifdef WITH_SITL_OSD
  23. #include "AP_OSD_SITL.h"
  24. #include <AP_HAL/Util.h>
  25. #include <AP_HAL/Semaphores.h>
  26. #include <AP_HAL/Scheduler.h>
  27. #include <AP_ROMFS/AP_ROMFS.h>
  28. #include <utility>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include "pthread.h"
  34. #include <AP_Notify/AP_Notify.h>
  35. extern const AP_HAL::HAL &hal;
  36. /*
  37. load *.bin font file, in MAX7456 format
  38. */
  39. void AP_OSD_SITL::load_font(void)
  40. {
  41. uint32_t font_size;
  42. char fontname[] = "font0.bin";
  43. last_font = get_font_num();
  44. fontname[4] = last_font + '0';
  45. uint8_t *font_data = AP_ROMFS::find_decompress(fontname, font_size);
  46. if (font_data == nullptr && last_font != 0) {
  47. last_font = 0;
  48. fontname[4] = last_font + '0';
  49. font_data = AP_ROMFS::find_decompress(fontname, font_size);
  50. }
  51. if (font_data == nullptr || font_size != 54 * 256) {
  52. AP_HAL::panic("Bad font file");
  53. }
  54. for (uint16_t i=0; i<256; i++) {
  55. const uint8_t *c = &font_data[i*54];
  56. // each pixel is 4 bytes, RGBA
  57. sf::Uint8 *pixels = new sf::Uint8[char_width * char_height * 4];
  58. if (!font[i].create(char_width, char_height)) {
  59. AP_HAL::panic("Failed to create texture");
  60. }
  61. for (uint16_t y=0; y<char_height; y++) {
  62. for (uint16_t x=0; x<char_width; x++) {
  63. // 2 bits per pixel
  64. uint16_t bitoffset = (y*char_width+x)*2;
  65. uint8_t byteoffset = bitoffset / 8;
  66. uint8_t bitshift = 6-(bitoffset % 8);
  67. uint8_t v = (c[byteoffset] >> bitshift) & 3;
  68. sf::Uint8 *p = &pixels[(y*char_width+x)*4];
  69. switch (v) {
  70. case 0:
  71. p[0] = 0;
  72. p[1] = 0;
  73. p[2] = 0;
  74. p[3] = 255;
  75. break;
  76. case 1:
  77. case 3:
  78. p[0] = 0;
  79. p[1] = 0;
  80. p[2] = 0;
  81. p[3] = 0;
  82. break;
  83. case 2:
  84. p[0] = 255;
  85. p[1] = 255;
  86. p[2] = 255;
  87. p[3] = 255;
  88. break;
  89. }
  90. }
  91. }
  92. font[i].update(pixels);
  93. }
  94. free(font_data);
  95. }
  96. void AP_OSD_SITL::write(uint8_t x, uint8_t y, const char* text)
  97. {
  98. if (y >= video_lines || text == nullptr) {
  99. return;
  100. }
  101. WITH_SEMAPHORE(mutex);
  102. while ((x < video_cols) && (*text != 0)) {
  103. buffer[y][x] = *text;
  104. ++text;
  105. ++x;
  106. }
  107. }
  108. void AP_OSD_SITL::clear(void)
  109. {
  110. AP_OSD_Backend::clear();
  111. WITH_SEMAPHORE(mutex);
  112. memset(buffer, 0, sizeof(buffer));
  113. }
  114. void AP_OSD_SITL::flush(void)
  115. {
  116. counter++;
  117. }
  118. // main loop of graphics thread
  119. void AP_OSD_SITL::update_thread(void)
  120. {
  121. load_font();
  122. {
  123. WITH_SEMAPHORE(AP::notify().sf_window_mutex);
  124. w = new sf::RenderWindow(sf::VideoMode(video_cols*(char_width+char_spacing)*char_scale,
  125. video_lines*(char_height+char_spacing)*char_scale),
  126. "OSD");
  127. }
  128. if (!w) {
  129. AP_HAL::panic("Unable to create OSD window");
  130. }
  131. while (true) {
  132. {
  133. WITH_SEMAPHORE(AP::notify().sf_window_mutex);
  134. sf::Event event;
  135. while (w->pollEvent(event)) {
  136. if (event.type == sf::Event::Closed) {
  137. w->close();
  138. }
  139. }
  140. if (!w->isOpen()) {
  141. break;
  142. }
  143. if (counter != last_counter) {
  144. last_counter = counter;
  145. uint8_t buffer2[video_lines][video_cols];
  146. {
  147. WITH_SEMAPHORE(mutex);
  148. memcpy(buffer2, buffer, sizeof(buffer2));
  149. }
  150. w->clear();
  151. for (uint8_t y=0; y<video_lines; y++) {
  152. for (uint8_t x=0; x<video_cols; x++) {
  153. uint16_t px = x * (char_width+char_spacing) * char_scale;
  154. uint16_t py = y * (char_height+char_spacing) * char_scale;
  155. sf::Sprite s;
  156. uint8_t c = buffer2[y][x];
  157. s.setTexture(font[c]);
  158. s.setPosition(sf::Vector2f(px, py));
  159. s.scale(sf::Vector2f(char_scale,char_scale));
  160. w->draw(s);
  161. }
  162. }
  163. w->display();
  164. if (last_font != get_font_num()) {
  165. load_font();
  166. }
  167. }
  168. }
  169. usleep(10000);
  170. }
  171. }
  172. // trampoline for update thread
  173. void *AP_OSD_SITL::update_thread_start(void *obj)
  174. {
  175. ((AP_OSD_SITL *)obj)->update_thread();
  176. return nullptr;
  177. }
  178. // initialise backend
  179. bool AP_OSD_SITL::init(void)
  180. {
  181. pthread_create(&thread, NULL, update_thread_start, this);
  182. return true;
  183. }
  184. AP_OSD_Backend *AP_OSD_SITL::probe(AP_OSD &osd)
  185. {
  186. AP_OSD_SITL *backend = new AP_OSD_SITL(osd);
  187. if (!backend) {
  188. return nullptr;
  189. }
  190. if (!backend->init()) {
  191. delete backend;
  192. return nullptr;
  193. }
  194. return backend;
  195. }
  196. AP_OSD_SITL::AP_OSD_SITL(AP_OSD &osd):
  197. AP_OSD_Backend(osd)
  198. {
  199. }
  200. #endif // WITH_SITL_OSD