qp_internal.c 3.9 KB

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