You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.7 KiB

  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <quantum.h>
  4. #include <utf8.h>
  5. #include "qp_internal.h"
  6. #include "qp_comms.h"
  7. #include "qp_draw.h"
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // Internal driver validation
  10. static bool validate_driver_vtable(struct painter_driver_t *driver) {
  11. return (driver->driver_vtable && driver->driver_vtable->init && driver->driver_vtable->power && driver->driver_vtable->clear && driver->driver_vtable->viewport && driver->driver_vtable->pixdata && driver->driver_vtable->palette_convert && driver->driver_vtable->append_pixels) ? true : false;
  12. }
  13. static bool validate_comms_vtable(struct painter_driver_t *driver) {
  14. return (driver->comms_vtable && driver->comms_vtable->comms_init && driver->comms_vtable->comms_start && driver->comms_vtable->comms_stop && driver->comms_vtable->comms_send) ? true : false;
  15. }
  16. static bool validate_driver_integrity(struct painter_driver_t *driver) {
  17. return validate_driver_vtable(driver) && validate_comms_vtable(driver);
  18. }
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. // Quantum Painter External API: qp_init
  21. bool qp_init(painter_device_t device, painter_rotation_t rotation) {
  22. qp_dprintf("qp_init: entry\n");
  23. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  24. driver->validate_ok = false;
  25. if (!validate_driver_integrity(driver)) {
  26. qp_dprintf("Failed to validate driver integrity in qp_init\n");
  27. return false;
  28. }
  29. driver->validate_ok = true;
  30. if (!qp_comms_init(device)) {
  31. driver->validate_ok = false;
  32. qp_dprintf("qp_init: fail (could not init comms)\n");
  33. return false;
  34. }
  35. if (!qp_comms_start(device)) {
  36. qp_dprintf("qp_init: fail (could not start comms)\n");
  37. return false;
  38. }
  39. // Set the rotation before init
  40. driver->rotation = rotation;
  41. // Invoke init
  42. bool ret = driver->driver_vtable->init(device, rotation);
  43. qp_comms_stop(device);
  44. qp_dprintf("qp_init: %s\n", ret ? "ok" : "fail");
  45. return ret;
  46. }
  47. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. // Quantum Painter External API: qp_power
  49. bool qp_power(painter_device_t device, bool power_on) {
  50. qp_dprintf("qp_power: entry\n");
  51. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  52. if (!driver->validate_ok) {
  53. qp_dprintf("qp_power: fail (validation_ok == false)\n");
  54. return false;
  55. }
  56. if (!qp_comms_start(device)) {
  57. qp_dprintf("qp_power: fail (could not start comms)\n");
  58. return false;
  59. }
  60. bool ret = driver->driver_vtable->power(device, power_on);
  61. qp_comms_stop(device);
  62. qp_dprintf("qp_power: %s\n", ret ? "ok" : "fail");
  63. return ret;
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. // Quantum Painter External API: qp_clear
  67. bool qp_clear(painter_device_t device) {
  68. qp_dprintf("qp_clear: entry\n");
  69. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  70. if (!driver->validate_ok) {
  71. qp_dprintf("qp_clear: fail (validation_ok == false)\n");
  72. return false;
  73. }
  74. if (!qp_comms_start(device)) {
  75. qp_dprintf("qp_clear: fail (could not start comms)\n");
  76. return false;
  77. }
  78. bool ret = driver->driver_vtable->clear(device);
  79. qp_comms_stop(device);
  80. qp_dprintf("qp_clear: %s\n", ret ? "ok" : "fail");
  81. return ret;
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  84. // Quantum Painter External API: qp_flush
  85. bool qp_flush(painter_device_t device) {
  86. qp_dprintf("qp_flush: entry\n");
  87. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  88. if (!driver->validate_ok) {
  89. qp_dprintf("qp_flush: fail (validation_ok == false)\n");
  90. return false;
  91. }
  92. if (!qp_comms_start(device)) {
  93. qp_dprintf("qp_flush: fail (could not start comms)\n");
  94. return false;
  95. }
  96. bool ret = driver->driver_vtable->flush(device);
  97. qp_comms_stop(device);
  98. qp_dprintf("qp_flush: %s\n", ret ? "ok" : "fail");
  99. return ret;
  100. }
  101. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. // Quantum Painter External API: qp_get_geometry
  103. void qp_get_geometry(painter_device_t device, uint16_t *width, uint16_t *height, painter_rotation_t *rotation, uint16_t *offset_x, uint16_t *offset_y) {
  104. qp_dprintf("qp_geometry: entry\n");
  105. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  106. switch (driver->rotation) {
  107. default:
  108. case QP_ROTATION_0:
  109. case QP_ROTATION_180:
  110. if (width) {
  111. *width = driver->panel_width;
  112. }
  113. if (height) {
  114. *height = driver->panel_height;
  115. }
  116. break;
  117. case QP_ROTATION_90:
  118. case QP_ROTATION_270:
  119. if (width) {
  120. *width = driver->panel_height;
  121. }
  122. if (height) {
  123. *height = driver->panel_width;
  124. }
  125. break;
  126. }
  127. if (rotation) {
  128. *rotation = driver->rotation;
  129. }
  130. if (offset_x) {
  131. *offset_x = driver->offset_x;
  132. }
  133. if (offset_y) {
  134. *offset_y = driver->offset_y;
  135. }
  136. qp_dprintf("qp_geometry: ok\n");
  137. }
  138. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139. // Quantum Painter External API: qp_set_viewport_offsets
  140. void qp_set_viewport_offsets(painter_device_t device, uint16_t offset_x, uint16_t offset_y) {
  141. qp_dprintf("qp_set_viewport_offsets: entry\n");
  142. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  143. driver->offset_x = offset_x;
  144. driver->offset_y = offset_y;
  145. qp_dprintf("qp_set_viewport_offsets: ok\n");
  146. }
  147. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  148. // Quantum Painter External API: qp_viewport
  149. bool qp_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom) {
  150. qp_dprintf("qp_viewport: entry\n");
  151. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  152. if (!driver->validate_ok) {
  153. qp_dprintf("qp_viewport: fail (validation_ok == false)\n");
  154. return false;
  155. }
  156. if (!qp_comms_start(device)) {
  157. qp_dprintf("qp_viewport: fail (could not start comms)\n");
  158. return false;
  159. }
  160. // Set the viewport
  161. bool ret = driver->driver_vtable->viewport(device, left, top, right, bottom);
  162. qp_dprintf("qp_viewport: %s\n", ret ? "ok" : "fail");
  163. qp_comms_stop(device);
  164. return ret;
  165. }
  166. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  167. // Quantum Painter External API: qp_pixdata
  168. bool qp_pixdata(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
  169. qp_dprintf("qp_pixdata: entry\n");
  170. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  171. if (!driver->validate_ok) {
  172. qp_dprintf("qp_pixdata: fail (validation_ok == false)\n");
  173. return false;
  174. }
  175. if (!qp_comms_start(device)) {
  176. qp_dprintf("qp_pixdata: fail (could not start comms)\n");
  177. return false;
  178. }
  179. bool ret = driver->driver_vtable->pixdata(device, pixel_data, native_pixel_count);
  180. qp_dprintf("qp_pixdata: %s\n", ret ? "ok" : "fail");
  181. qp_comms_stop(device);
  182. return ret;
  183. }