SITL_SFML_LED.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright (C) 2019 Peter Barker. All rights reserved.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <AP_HAL/AP_HAL.h>
  15. #include <AP_Notify/AP_Notify.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #ifdef WITH_SITL_RGBLED
  19. #include "SITL_SFML_LED.h"
  20. SITL_SFML_LED::SITL_SFML_LED():
  21. RGBLed((uint8_t)brightness::LED_OFF,
  22. (uint8_t)brightness::LED_HIGH,
  23. (uint8_t)brightness::LED_MEDIUM,
  24. (uint8_t)brightness::LED_LOW)
  25. {
  26. }
  27. void SITL_SFML_LED::update_thread(void)
  28. {
  29. sf::RenderWindow *w;
  30. {
  31. WITH_SEMAPHORE(AP::notify().sf_window_mutex);
  32. w = new sf::RenderWindow(sf::VideoMode(width, height), "LED");
  33. }
  34. if (!w) {
  35. AP_HAL::panic("Unable to create RGBLed window");
  36. }
  37. while (true) {
  38. {
  39. WITH_SEMAPHORE(AP::notify().sf_window_mutex);
  40. sf::Event event;
  41. while (w->pollEvent(event)) {
  42. if (event.type == sf::Event::Closed) {
  43. w->close();
  44. break;
  45. }
  46. }
  47. if (!w->isOpen()) {
  48. break;
  49. }
  50. const uint32_t colour = red<<16 | green<<8 | blue;
  51. if (colour != last_colour) {
  52. last_colour = colour;
  53. w->clear(sf::Color(red, green, blue, 255));
  54. w->display();
  55. }
  56. }
  57. usleep(10000);
  58. }
  59. }
  60. // trampoline for update thread
  61. void *SITL_SFML_LED::update_thread_start(void *obj)
  62. {
  63. ((SITL_SFML_LED *)obj)->update_thread();
  64. return nullptr;
  65. }
  66. bool SITL_SFML_LED::hw_init()
  67. {
  68. pthread_create(&thread, NULL, update_thread_start, this);
  69. return true;
  70. }
  71. bool SITL_SFML_LED::hw_set_rgb(uint8_t _red, uint8_t _green, uint8_t _blue)
  72. {
  73. red = _red;
  74. green = _green;
  75. blue = _blue;
  76. return true;
  77. }
  78. #endif