u8x8_d_a2printer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. u8x8_d_a2printer.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. Use DC2 bitmap command of the A2 Micro panel termal printer
  27. double stroke
  28. */
  29. #include "u8x8.h"
  30. #define LINE_MIN_DELAY_MS 15
  31. /* higher values improve quality */
  32. /* however if the value is too high (>=5) then form feed does not work any more */
  33. #define LINE_EXTRA_8PIXEL_DELAY_MS 3
  34. /* this must be a power of two and between 1 and 8 */
  35. /* best quality only with 1 */
  36. #define NO_OF_LINES_TO_SEND_WITHOUT_DELAY 1
  37. /* calculates the delay, based on the number of black pixel */
  38. /* actually only "none-zero" bytes are calculated which is, of course not so accurate, but should be good enough */
  39. uint16_t get_delay_in_milliseconds(uint8_t cnt, uint8_t *data)
  40. {
  41. uint8_t i;
  42. uint16_t time = LINE_MIN_DELAY_MS;
  43. for ( i = 0; i < cnt; i++ )
  44. if ( data[i] != 0 )
  45. time += LINE_EXTRA_8PIXEL_DELAY_MS;
  46. return time;
  47. }
  48. uint8_t u8x8_d_a2printer_common(u8x8_t *u8x8, uint8_t msg, U8X8_UNUSED uint8_t arg_int, void *arg_ptr)
  49. {
  50. uint8_t c, i, j;
  51. uint8_t *ptr;
  52. uint16_t delay_in_milliseconds;
  53. switch(msg)
  54. {
  55. /* U8X8_MSG_DISPLAY_SETUP_MEMORY is handled by the calling function */
  56. /*
  57. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  58. break;
  59. */
  60. case U8X8_MSG_DISPLAY_INIT:
  61. u8x8_d_helper_display_init(u8x8);
  62. // no setup required
  63. // u8x8_cad_SendSequence(u8x8, u8x8_d_a2printer_init_seq);
  64. break;
  65. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  66. // no powersave
  67. break;
  68. case U8X8_MSG_DISPLAY_DRAW_TILE:
  69. u8x8_cad_StartTransfer(u8x8);
  70. u8x8_cad_SendCmd(u8x8, 27); /* ESC */
  71. u8x8_cad_SendCmd(u8x8, 55 ); /* parameter command */
  72. /* increasing the "max printing dots" requires a good power supply, but LINE_EXTRA_8PIXEL_DELAY_MS could be reduced then */
  73. u8x8_cad_SendCmd(u8x8, 0); /* Max printing dots,Unit(8dots),Default:7(64 dots) 8*(x+1) ... lower values improve, probably my current supply is not sufficient */
  74. u8x8_cad_SendCmd(u8x8, 200); /* 3-255 Heating time,Unit(10us),Default:80(800us) */
  75. u8x8_cad_SendCmd(u8x8, 2); /* 0-255 Heating interval,Unit(10us),Default:2(20us) ... does not have much influence */
  76. //c = ((u8x8_tile_t *)arg_ptr)->cnt; /* number of tiles */
  77. c = u8x8->display_info->tile_width;
  78. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr; /* data ptr to the tiles */
  79. u8x8_cad_SendCmd(u8x8, 18); /* DC2 */
  80. u8x8_cad_SendCmd(u8x8, 42 ); /* * */
  81. u8x8_cad_SendCmd(u8x8, 8 ); /* height */
  82. u8x8_cad_SendCmd(u8x8, c ); /* c, u8x8->display_info->tile_width */
  83. for( j = 0; j < 8 / NO_OF_LINES_TO_SEND_WITHOUT_DELAY; j ++ )
  84. {
  85. delay_in_milliseconds = 0;
  86. for( i = 0; i < NO_OF_LINES_TO_SEND_WITHOUT_DELAY; i++ )
  87. {
  88. u8x8_cad_SendData(u8x8, c, ptr); /* c, note: SendData can not handle more than 255 bytes, send one line of data */
  89. delay_in_milliseconds += get_delay_in_milliseconds(c, ptr);
  90. ptr += c;
  91. }
  92. while( delay_in_milliseconds > 200 )
  93. {
  94. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_MILLI, 200, NULL);
  95. delay_in_milliseconds -= 200;
  96. }
  97. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_MILLI, delay_in_milliseconds, NULL);
  98. }
  99. /* set parameters back to their default values */
  100. u8x8_cad_SendCmd(u8x8, 27); /* ESC */
  101. u8x8_cad_SendCmd(u8x8, 55 ); /* parameter command */
  102. u8x8_cad_SendCmd(u8x8, 7); /* Max printing dots,Unit(8dots),Default:7(64 dots) 8*(x+1)*/
  103. u8x8_cad_SendCmd(u8x8, 80); /* 3-255 Heating time,Unit(10us),Default:80(800us) */
  104. u8x8_cad_SendCmd(u8x8, 2); /* 0-255 Heating interval,Unit(10us),Default:2(20us)*/
  105. u8x8_cad_EndTransfer(u8x8);
  106. break;
  107. default:
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. static const u8x8_display_info_t u8x8_a2printer_384x240_display_info =
  113. {
  114. /* most of the settings are not required, because this is a serial RS232 printer */
  115. /* chip_enable_level = */ 1,
  116. /* chip_disable_level = */ 0,
  117. /* post_chip_enable_wait_ns = */ 5,
  118. /* pre_chip_disable_wait_ns = */ 5,
  119. /* reset_pulse_width_ms = */ 1,
  120. /* post_reset_wait_ms = */ 6,
  121. /* sda_setup_time_ns = */ 20,
  122. /* sck_pulse_width_ns = */ 140,
  123. /* sck_clock_hz = */ 1000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  124. /* spi_mode = */ 0, /* old: sck_takeover_edge, new: active high (bit 1), rising edge (bit 0) */
  125. /* i2c_bus_clock_100kHz = */ 4,
  126. /* data_setup_time_ns = */ 30,
  127. /* write_pulse_width_ns = */ 40,
  128. /* tile_width = */ 48,
  129. /* tile_hight = */ 30,
  130. /* default_x_offset = */ 0,
  131. /* flipmode_x_offset = */ 0,
  132. /* pixel_width = */ 384,
  133. /* pixel_height = */ 240
  134. };
  135. uint8_t u8x8_d_a2printer_384x240(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  136. {
  137. switch(msg)
  138. {
  139. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  140. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_a2printer_384x240_display_info);
  141. break;
  142. default:
  143. return u8x8_d_a2printer_common(u8x8, msg, arg_int, arg_ptr);
  144. }
  145. return 1;
  146. }