printer_receive.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <stdint.h>
  2. #include <furi.h>
  3. #include <gblink/include/gblink.h>
  4. #include "printer_i.h"
  5. #define TAG "printer_receive"
  6. /* XXX: TODO: Double check this */
  7. #define TIMEOUT_US 1000000
  8. static void printer_reset(struct printer_proto *printer)
  9. {
  10. /* Clear out the current packet data */
  11. memset(printer->packet, '\0', sizeof(struct packet));
  12. printer->image->data_sz = 0;
  13. /* This is technically redundant, done for completeness */
  14. printer->packet->state = START_L;
  15. /* Packet timeout start */
  16. printer->packet->time = DWT->CYCCNT;
  17. }
  18. static void byte_callback(void *context, uint8_t val)
  19. {
  20. struct printer_proto *printer = context;
  21. struct packet *packet = printer->packet;
  22. const uint32_t time_ticks = furi_hal_cortex_instructions_per_microsecond() * TIMEOUT_US;
  23. uint8_t data_out = 0x00;
  24. if ((DWT->CYCCNT - printer->packet->time) > time_ticks)
  25. printer_reset(printer);
  26. /* TODO: flash led? */
  27. switch (packet->state) {
  28. case START_L:
  29. if (val == PKT_START_L) {
  30. packet->state = START_H;
  31. /* Packet timeout restart */
  32. packet->time = DWT->CYCCNT;
  33. packet->zero_counter = 0;
  34. }
  35. if (val == 0x00) {
  36. packet->zero_counter++;
  37. if (packet->zero_counter == 16)
  38. printer_reset(printer);
  39. }
  40. break;
  41. case START_H:
  42. if (val == PKT_START_H)
  43. packet->state = COMMAND;
  44. else
  45. packet->state = START_L;
  46. break;
  47. case COMMAND:
  48. packet->cmd = val;
  49. packet->state = COMPRESS;
  50. packet->cksum_calc += val;
  51. /* We only do a real reset after the packet is completed, however
  52. * we need to clear the status flags at this point.
  53. */
  54. if (val == CMD_INIT)
  55. packet->status = 0;
  56. break;
  57. case COMPRESS:
  58. packet->cksum_calc += val;
  59. packet->state = LEN_L;
  60. if (val) {
  61. FURI_LOG_E(TAG, "Compression not supported!");
  62. packet->status |= STATUS_PKT_ERR;
  63. }
  64. break;
  65. case LEN_L:
  66. packet->cksum_calc += val;
  67. packet->state = LEN_H;
  68. packet->len = (val & 0xff);
  69. break;
  70. case LEN_H:
  71. packet->cksum_calc += val;
  72. packet->len |= ((val & 0xff) << 8);
  73. /* Override length for a TRANSFER */
  74. if (packet->cmd == CMD_TRANSFER)
  75. packet->len = TRANSFER_RECV_SZ;
  76. if (packet->len) {
  77. packet->state = COMMAND_DAT;
  78. } else {
  79. packet->state = CKSUM_L;
  80. }
  81. break;
  82. case COMMAND_DAT:
  83. packet->cksum_calc += val;
  84. packet->recv_data[packet->recv_data_sz] = val;
  85. packet->recv_data_sz++;
  86. if (packet->recv_data_sz == packet->len)
  87. packet->state = CKSUM_L;
  88. break;
  89. case CKSUM_L:
  90. packet->state = CKSUM_H;
  91. packet->cksum = (val & 0xff);
  92. break;
  93. case CKSUM_H:
  94. packet->state = ALIVE;
  95. packet->cksum |= ((val & 0xff) << 8);
  96. if (packet->cksum != packet->cksum_calc)
  97. packet->status |= STATUS_CKSUM;
  98. // TRANSFER does not set checksum bytes
  99. if (packet->cmd == CMD_TRANSFER)
  100. packet->status &= ~STATUS_CKSUM;
  101. data_out = PRINTER_ID;
  102. break;
  103. case ALIVE:
  104. packet->state = STATUS;
  105. data_out = packet->status;
  106. break;
  107. case STATUS:
  108. packet->state = START_L;
  109. switch (packet->cmd) {
  110. case CMD_INIT:
  111. printer_reset(printer);
  112. break;
  113. case CMD_DATA:
  114. if (printer->image->data_sz < PRINT_FULL_SZ) {
  115. if ((printer->image->data_sz + packet->len) <= PRINT_FULL_SZ) {
  116. memcpy((printer->image->data)+printer->image->data_sz, packet->recv_data, packet->len);
  117. printer->image->data_sz += packet->len;
  118. } else {
  119. memcpy((printer->image->data)+printer->image->data_sz, packet->recv_data, ((printer->image->data_sz + packet->len)) - PRINT_FULL_SZ);
  120. printer->image->data_sz += (PRINT_FULL_SZ - (printer->image->data_sz + packet->len));
  121. furi_assert(printer->image->data_sz <= PRINT_FULL_SZ);
  122. }
  123. }
  124. if (printer->image->data_sz == PRINT_FULL_SZ)
  125. packet->status |= STATUS_READY;
  126. furi_thread_flags_set(printer->thread, THREAD_FLAGS_DATA);
  127. break;
  128. case CMD_TRANSFER:
  129. /* XXX: TODO: Check to see if we're still printing when getting
  130. * a transfer command. If so, then we have failed to beat the clock.
  131. */
  132. case CMD_PRINT:
  133. packet->status &= ~STATUS_READY;
  134. packet->status |= (STATUS_PRINTING | STATUS_FULL);
  135. furi_thread_flags_set(printer->thread, THREAD_FLAGS_PRINT);
  136. break;
  137. case CMD_STATUS:
  138. /* READY cleared on status request */
  139. packet->status &= ~STATUS_READY;
  140. if ((packet->status & STATUS_PRINTING) &&
  141. (packet->status & STATUS_PRINTED)) {
  142. packet->status &= ~(STATUS_PRINTING | STATUS_PRINTED);
  143. furi_thread_flags_set(printer->thread, THREAD_FLAGS_COMPLETE);
  144. }
  145. }
  146. packet->recv_data_sz = 0;
  147. packet->cksum_calc = 0;
  148. /* XXX: TODO: if the command had something we need to do, do it here. */
  149. /* done! flush our buffers, deal with any status changes like
  150. * not printing -> printing -> not printing, etc.
  151. */
  152. /* Do a callback here?
  153. * if so, I guess we should wait for callback completion before accepting more recv_data?
  154. * but that means the callback is in an interrupt context, which, is probably okay?
  155. */
  156. /* XXX: TODO: NOTE: FIXME:
  157. * all of the notes..
  158. * This module needs to maintain the whole buffer, but it can be safely assumed that the buffer
  159. * will never exceed 20x18 tiles (no clue how many bytes) as that is the max the printer can
  160. * take on in a single print. Printing mulitples needs a print, and then a second print with
  161. * no margin. So the margins are important and need to be passed to the final application,
  162. * SOMEHOW.
  163. *
  164. * More imporatntly, is the completed callback NEEDS to have a return value. This allows
  165. * the end application to take that whole panel, however its laid out, and do whatever
  166. * it wants to do with it. Write it to a file, convert, etc., etc., so that this module
  167. * will forever return that it is printing until the callback returns true.
  168. *
  169. * Once we call the callback and it shows a true, then we can be sure the higher module
  170. * is done with the buffer, and we can tell the host that yes, its done, you can continue
  171. * if you want.
  172. */
  173. /* XXX: On TRANSFER, there is no checking of status, it is only two packets in total.
  174. * I can assume that if we delay a bit in moving the buffer around that should be okay
  175. * but we probably don't want to wait too long.
  176. * Upon testing, transfer seems to doesn't
  177. */
  178. break;
  179. default:
  180. FURI_LOG_E(TAG, "unknown status!");
  181. break;
  182. }
  183. /* transfer next byte */
  184. gblink_transfer(printer->gblink_handle, data_out);
  185. }
  186. void printer_receive_start(void *printer_handle)
  187. {
  188. struct printer_proto *printer = printer_handle;
  189. /* Set up defaults the receive path needs */
  190. gblink_callback_set(printer->gblink_handle, byte_callback, printer);
  191. gblink_clk_source_set(printer->gblink_handle, GBLINK_CLK_EXT);
  192. printer_reset(printer);
  193. gblink_start(printer->gblink_handle);
  194. }
  195. void printer_receive_print_complete(void *printer_handle)
  196. {
  197. struct printer_proto *printer = printer_handle;
  198. printer->packet->status &= ~STATUS_PRINTING;
  199. }