printer_proto.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. // Copyright (c) 2024 KBEmbedded
  3. #ifndef PRINTER_PROTO_H
  4. #define PRINTER_PROTO_H
  5. #include <gblink/include/gblink.h>
  6. #pragma once
  7. enum cb_reason {
  8. reason_data,
  9. reason_print,
  10. reason_complete,
  11. };
  12. /* Dual purpose struct used for both receiving image data from game boy, and
  13. * sending it to printer.
  14. */
  15. struct gb_image {
  16. /* NOTE: Do not change the order of these 4 bytes!
  17. * TODO: Maybe make this a struct, or a union, or something to help
  18. * enforce their ordering to allow for a memcpy to and from printer.
  19. */
  20. /* TODO: Need to understand this more */
  21. uint8_t num_sheets;
  22. uint8_t margins;
  23. /* TODO: Does this actually matter? */
  24. uint8_t palette;
  25. /* TODO: Need to play with this more */
  26. uint8_t exposure;
  27. /* Always expected to be 160 px wide */
  28. size_t data_sz;
  29. uint8_t data[];
  30. };
  31. /**
  32. * Allocate a printer instance
  33. *
  34. * This will manage a gblink instance under the hood, top level applications
  35. * needing to send/receive Game Boy Printer packets only need to work with
  36. * the printer specific functions.
  37. *
  38. * @returns Pointer to a printer instance.
  39. */
  40. void *printer_alloc(void);
  41. /**
  42. * Free a printer instance
  43. *
  44. * @param printer_handle Printer instance handle
  45. */
  46. void printer_free(void *printer_handle);
  47. /**
  48. * Set context for registered callback
  49. *
  50. * @param printer_handle Printer instance handle
  51. * @param context Pointer to context
  52. */
  53. void printer_callback_context_set(void *printer_handle, void *context);
  54. /**
  55. * Register a callback
  56. *
  57. * The callback can be called multiple times and for different reasons
  58. *
  59. * The callback is called with the arguments of the user specified context,
  60. * a pointer to a struct gb_image, and a reason for the callback.
  61. *
  62. * @note The struct gb_image pointer is valid until the the print is marked
  63. * as completed.
  64. *
  65. * @param printer_handle Printer instance handle
  66. * @param callback Pointer to callback
  67. */
  68. void printer_callback_set(void *printer_handle, void (*callback)(void *context, struct gb_image *image, enum cb_reason reason));
  69. /* Can only be run after alloc, before start */
  70. /**
  71. * Set one of the pre-configured pinouts
  72. *
  73. * @param printer_handle Printer instance handle
  74. * @param pinout Which pinout to use
  75. *
  76. * @note The printer instance must not be actively sending or receiving
  77. *
  78. * @returns 0 on success, 1 if gblink instance is not gblink_stop()'ed.
  79. */
  80. int printer_pin_set_default(void *printer_handle, gblink_pinouts pinout);
  81. /**
  82. * Set a gpio pin to a specific pin mode
  83. *
  84. * @param printer_handle Printer instance handle
  85. * @param pin Pin mode to assign to the gpio pin
  86. * @param gpio Which gpio pin to assign the pin mode
  87. *
  88. * @note The printer instance must not be actively sending or receiving
  89. *
  90. * @returns 0 on success, 1 if gblink instance is not gblink_stop()'ed.
  91. */
  92. int printer_pin_set(void *printer_handle, gblink_bus_pins pin, const GpioPin *gpio);
  93. /**
  94. * Get the gpio pin associated with the requested pin mode
  95. *
  96. * @param printer_handle Printer instance handle
  97. * @param pin Pin mode to inquire about
  98. *
  99. * @returns GpioPin pointer
  100. */
  101. const GpioPin *printer_pin_get(void *printer_handle, gblink_bus_pins pin);
  102. /**
  103. * Stop a printer instance
  104. *
  105. * Disables interrupts, stops any pending timers, and enters back to an idle
  106. * state. Once called, re-allows changes to be made.
  107. *
  108. * @param printer_handle Printer instance handle
  109. */
  110. void printer_stop(void *printer_handle);
  111. /**
  112. * Allocate a gb_image structure for use outside of the printer instance
  113. *
  114. * Allocates a buffer of the maximum size that the Game Boy Printer can work as
  115. * a single image, 160x144 px, 20x18 tiles. Provided as a convenience function so
  116. * higher level applications don't need to know how big of a buffer to create.
  117. *
  118. * Useful for, e.g. copying a received image to and the marking that image as
  119. * "printed".
  120. *
  121. * Must be freed manually
  122. *
  123. * @returns Pointer to a struct gb_image
  124. */
  125. struct gb_image *printer_image_buffer_alloc(void);
  126. /**
  127. * Free a previously allocated gb_image structure
  128. *
  129. * @param image Pointer to gb_image struct
  130. */
  131. void printer_image_buffer_free(struct gb_image *image);
  132. #endif // PRINTER_PROTO_H