printer_proto.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  82. * Stop a printer instance
  83. *
  84. * Disables interrupts, stops any pending timers, and enters back to an idle
  85. * state. Once called, re-allows changes to be made.
  86. *
  87. * @param printer_handle Printer instance handle
  88. */
  89. void printer_stop(void *printer_handle);
  90. /**
  91. * Get the gblink handle associated with the printer instance.
  92. *
  93. * @warning It is not recommended to use this to change any gblink variables other
  94. * than pin settings! Changing any other settings such as mode, speed, timeout,
  95. * callback, etc., can break printer communication.
  96. *
  97. * @param printer_handle Printer instance handle
  98. *
  99. * @returns Pointer that can be used with gblink calls directly
  100. */
  101. void *printer_gblink_handle_get(void *printer_handle);
  102. #endif // PRINTER_PROTO_H