qp_internal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2023 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "qp_internal.h"
  4. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. // Quantum Painter Core API: device registration
  6. enum {
  7. // Work out how many devices we're actually going to be instantiating
  8. // NOTE: We intentionally do not include surfaces here, despite them conforming to the same API.
  9. QP_NUM_DEVICES = (ILI9163_NUM_DEVICES) // ILI9163
  10. + (ILI9341_NUM_DEVICES) // ILI9341
  11. + (ILI9486_NUM_DEVICES) // ILI9486
  12. + (ILI9488_NUM_DEVICES) // ILI9488
  13. + (ST7789_NUM_DEVICES) // ST7789
  14. + (ST7735_NUM_DEVICES) // ST7735
  15. + (GC9A01_NUM_DEVICES) // GC9A01
  16. + (SSD1351_NUM_DEVICES) // SSD1351
  17. + (SH1106_NUM_DEVICES) // SH1106
  18. };
  19. static painter_device_t qp_devices[QP_NUM_DEVICES] = {NULL};
  20. bool qp_internal_register_device(painter_device_t driver) {
  21. for (uint8_t i = 0; i < QP_NUM_DEVICES; i++) {
  22. if (qp_devices[i] == NULL) {
  23. qp_devices[i] = driver;
  24. return true;
  25. }
  26. }
  27. // We should never get here -- someone has screwed up their device counts during config
  28. qp_dprintf("qp_internal_register_device: no more space for devices!\n");
  29. return false;
  30. }
  31. #if (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  32. static void qp_internal_display_timeout_task(void) {
  33. // Handle power on/off state
  34. static bool display_on = true;
  35. bool should_change_display_state = false;
  36. bool target_display_state = false;
  37. if (last_input_activity_elapsed() < (QUANTUM_PAINTER_DISPLAY_TIMEOUT)) {
  38. should_change_display_state = display_on == false;
  39. target_display_state = true;
  40. } else {
  41. should_change_display_state = display_on == true;
  42. target_display_state = false;
  43. }
  44. if (should_change_display_state) {
  45. for (uint8_t i = 0; i < QP_NUM_DEVICES; i++) {
  46. if (qp_devices[i] != NULL) {
  47. qp_power(qp_devices[i], target_display_state);
  48. }
  49. }
  50. display_on = target_display_state;
  51. }
  52. }
  53. #endif // (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. // Quantum Painter Core API: qp_internal_task
  56. _Static_assert((QUANTUM_PAINTER_TASK_THROTTLE) > 0 && (QUANTUM_PAINTER_TASK_THROTTLE) < 1000, "QUANTUM_PAINTER_TASK_THROTTLE must be between 1 and 999");
  57. void qp_internal_task(void) {
  58. // Perform throttling of the internal processing of Quantum Painter
  59. static uint32_t last_tick = 0;
  60. uint32_t now = timer_read32();
  61. if (TIMER_DIFF_32(now, last_tick) < (QUANTUM_PAINTER_TASK_THROTTLE)) {
  62. return;
  63. }
  64. last_tick = now;
  65. #if (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  66. qp_internal_display_timeout_task();
  67. #endif // (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  68. // Handle animations
  69. void qp_internal_animation_tick(void);
  70. qp_internal_animation_tick();
  71. #ifdef QUANTUM_PAINTER_LVGL_INTEGRATION_ENABLE
  72. // Run LVGL ticks
  73. void qp_lvgl_internal_tick(void);
  74. qp_lvgl_internal_tick();
  75. #endif
  76. // Flush (render) dirty regions to corresponding displays
  77. #if !defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  78. bool old_debug_state = debug_enable;
  79. debug_enable = false;
  80. #endif // defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  81. for (uint8_t i = 0; i < QP_NUM_DEVICES; i++) {
  82. if (qp_devices[i] != NULL) {
  83. qp_flush(qp_devices[i]);
  84. }
  85. }
  86. #if !defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  87. debug_enable = old_debug_state;
  88. #endif // defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  89. }