printer_proto.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #define STATUS_LOWBATT (1 << 7)
  8. #define STATUS_ERR (1 << 6)
  9. #define STATUS_JAM (1 << 5)
  10. #define STATUS_PKT_ERR (1 << 4)
  11. #define STATUS_READY (1 << 3)
  12. #define STATUS_FULL (1 << 2)
  13. #define STATUS_PRINTING (1 << 1)
  14. #define STATUS_CKSUM_ERR (1 << 0)
  15. /* emulate printer's internal print receive buffer */
  16. #define TILE_SIZE 16 // 8x8 tile, 2bpp color
  17. #define WIDTH 20 // 20 tiles wide
  18. #define HEIGHT 18 // 18 tiles tall
  19. #define PRINT_FULL_SZ 5760 // (LINE_BUF_SZ * HEIGHT / 2)
  20. enum cb_reason {
  21. reason_line_xfer,
  22. reason_print,
  23. reason_complete,
  24. };
  25. /* Dual purpose struct used for both receiving image data from game boy, and
  26. * sending it to printer.
  27. */
  28. struct gb_image {
  29. /* NOTE: Do not change the order of these 4 bytes!
  30. * TODO: Maybe make this a struct, or a union, or something to help
  31. * enforce their ordering to allow for a memcpy to and from printer.
  32. */
  33. /* TODO: Need to understand this more */
  34. uint8_t num_sheets;
  35. uint8_t margins;
  36. /* TODO: Does this actually matter? */
  37. uint8_t palette;
  38. /* TODO: Need to play with this more */
  39. uint8_t exposure;
  40. size_t data_sz;
  41. uint8_t data[PRINT_FULL_SZ];
  42. };
  43. /**
  44. * Allocate a printer instance
  45. *
  46. * This will manage a gblink instance under the hood, top level applications
  47. * needing to send/receive Game Boy Printer packets only need to work with
  48. * the printer specific functions.
  49. *
  50. * @returns Pointer to a printer instance.
  51. */
  52. void *printer_alloc(void);
  53. /**
  54. * Free a printer instance
  55. *
  56. * @param printer_handle Printer instance handle
  57. */
  58. void printer_free(void *printer_handle);
  59. /**
  60. * Set context for registered callback
  61. *
  62. * @param printer_handle Printer instance handle
  63. * @param context Pointer to context
  64. */
  65. void printer_callback_context_set(void *printer_handle, void *context);
  66. /**
  67. * Register a callback
  68. *
  69. * The callback can be called multiple times and for different reasons
  70. *
  71. * The callback is called with the arguments of the user specified context,
  72. * a pointer to a struct gb_image, and a reason for the callback.
  73. *
  74. * @note The struct gb_image pointer is valid until the the print is marked
  75. * as completed.
  76. *
  77. * @param printer_handle Printer instance handle
  78. * @param callback Pointer to callback
  79. */
  80. void printer_callback_set(void *printer_handle, void (*callback)(void *context, struct gb_image *image, enum cb_reason reason));
  81. /* Can only be run after alloc, before start */
  82. /**
  83. * Set one of the pre-configured pinouts
  84. *
  85. * @param printer_handle Printer instance handle
  86. * @param pinout Which pinout to use
  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_default(void *printer_handle, gblink_pinouts pinout);
  93. /**
  94. * Set a gpio pin to a specific pin mode
  95. *
  96. * @param printer_handle Printer instance handle
  97. * @param pin Pin mode to assign to the gpio pin
  98. * @param gpio Which gpio pin to assign the pin mode
  99. *
  100. * @note The printer instance must not be actively sending or receiving
  101. *
  102. * @returns 0 on success, 1 if gblink instance is not gblink_stop()'ed.
  103. */
  104. int printer_pin_set(void *printer_handle, gblink_bus_pins pin, const GpioPin *gpio);
  105. /**
  106. * Get the gpio pin associated with the requested pin mode
  107. *
  108. * @param printer_handle Printer instance handle
  109. * @param pin Pin mode to inquire about
  110. *
  111. * @returns GpioPin pointer
  112. */
  113. const GpioPin *printer_pin_get(void *printer_handle, gblink_bus_pins pin);
  114. /**
  115. * Stop a printer instance
  116. *
  117. * Disables interrupts, stops any pending timers, and enters back to an idle
  118. * state. Once called, re-allows changes to be made.
  119. *
  120. * @param printer_handle Printer instance handle
  121. */
  122. void printer_stop(void *printer_handle);
  123. #endif // PRINTER_PROTO_H