qp_internal.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. + (GC9107_NUM_DEVICES) // GC9107
  17. + (SSD1351_NUM_DEVICES) // SSD1351
  18. + (SH1106_NUM_DEVICES) // SH1106
  19. + (LD7032_NUM_DEVICES) // LD7032
  20. };
  21. static painter_device_t qp_devices[QP_NUM_DEVICES] = {NULL};
  22. bool qp_internal_register_device(painter_device_t driver) {
  23. for (uint8_t i = 0; i < QP_NUM_DEVICES; i++) {
  24. if (qp_devices[i] == NULL) {
  25. qp_devices[i] = driver;
  26. return true;
  27. }
  28. }
  29. // We should never get here -- someone has screwed up their device counts during config
  30. qp_dprintf("qp_internal_register_device: no more space for devices!\n");
  31. return false;
  32. }
  33. #if (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  34. static void qp_internal_display_timeout_task(void) {
  35. // Handle power on/off state
  36. static bool display_on = true;
  37. bool should_change_display_state = false;
  38. bool target_display_state = false;
  39. if (last_input_activity_elapsed() < (QUANTUM_PAINTER_DISPLAY_TIMEOUT)) {
  40. should_change_display_state = display_on == false;
  41. target_display_state = true;
  42. } else {
  43. should_change_display_state = display_on == true;
  44. target_display_state = false;
  45. }
  46. if (should_change_display_state) {
  47. for (uint8_t i = 0; i < QP_NUM_DEVICES; i++) {
  48. if (qp_devices[i] != NULL) {
  49. qp_power(qp_devices[i], target_display_state);
  50. }
  51. }
  52. display_on = target_display_state;
  53. }
  54. }
  55. #endif // (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. // Quantum Painter Core API: qp_internal_task
  58. _Static_assert((QUANTUM_PAINTER_TASK_THROTTLE) > 0 && (QUANTUM_PAINTER_TASK_THROTTLE) < 1000, "QUANTUM_PAINTER_TASK_THROTTLE must be between 1 and 999");
  59. void qp_internal_task(void) {
  60. // Perform throttling of the internal processing of Quantum Painter
  61. static uint32_t last_tick = 0;
  62. uint32_t now = timer_read32();
  63. if (TIMER_DIFF_32(now, last_tick) < (QUANTUM_PAINTER_TASK_THROTTLE)) {
  64. return;
  65. }
  66. last_tick = now;
  67. #if (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  68. qp_internal_display_timeout_task();
  69. #endif // (QUANTUM_PAINTER_DISPLAY_TIMEOUT) > 0
  70. // Handle animations
  71. void qp_internal_animation_tick(void);
  72. qp_internal_animation_tick();
  73. #ifdef QUANTUM_PAINTER_LVGL_INTEGRATION_ENABLE
  74. // Run LVGL ticks
  75. void qp_lvgl_internal_tick(void);
  76. qp_lvgl_internal_tick();
  77. #endif
  78. // Flush (render) dirty regions to corresponding displays
  79. #if !defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  80. bool old_debug_state = debug_enable;
  81. debug_enable = false;
  82. #endif // defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  83. for (uint8_t i = 0; i < QP_NUM_DEVICES; i++) {
  84. if (qp_devices[i] != NULL) {
  85. qp_flush(qp_devices[i]);
  86. }
  87. }
  88. #if !defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  89. debug_enable = old_debug_state;
  90. #endif // defined(QUANTUM_PAINTER_DEBUG_ENABLE_FLUSH_TASK_OUTPUT)
  91. }