qp_internal.c 3.8 KB

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