printer_proto.h 4.0 KB

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