A2Printer.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. A2Printer.pde
  3. Special example code for the A2 Mciro Printer (https://www.sparkfun.com/products/10438)
  4. Universal 8bit Graphics Library, http://code.google.com/p/u8glib/
  5. Copyright (c) 2013, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "U8glib.h"
  29. // use this serial interface
  30. #define PRINTER_SERIAL Serial
  31. // #define PRINTER_SERIAL Serial1
  32. uint8_t u8g_com_uart(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  33. switch(msg) {
  34. case U8G_COM_MSG_WRITE_BYTE:
  35. PRINTER_SERIAL.write(arg_val);
  36. break;
  37. }
  38. return 1;
  39. }
  40. // setup u8g object, please remove comment from one of the following constructor calls
  41. // half resolution
  42. //U8GLIB u8g(&u8g_dev_a2_micro_printer_192x120_ds, (u8g_com_fnptr)u8g_com_uart);
  43. // full resolution, requires to uncomment U8G_16BIT in u8g.h
  44. //U8GLIB u8g(&u8g_dev_a2_micro_printer_384x240, (u8g_com_fnptr)u8g_com_uart);
  45. // half resolution, extra log, requires to uncomment U8G_16BIT in u8g.h
  46. //U8GLIB u8g(&u8g_dev_a2_micro_printer_192x360_ds, (u8g_com_fnptr)u8g_com_uart);
  47. U8GLIB u8g(&u8g_dev_a2_micro_printer_192x720_ds, (u8g_com_fnptr)u8g_com_uart);
  48. void drawLogo(uint8_t d) {
  49. u8g.setFont(u8g_font_gdr25r);
  50. u8g.drawStr(0+d, 30+d, "U");
  51. u8g.setFont(u8g_font_gdr30n);
  52. u8g.drawStr90(23+d,10+d,"8");
  53. u8g.setFont(u8g_font_gdr25r);
  54. u8g.drawStr(53+d,30+d,"g");
  55. u8g.drawHLine(2+d, 35+d, 47);
  56. u8g.drawVLine(45+d, 32+d, 12);
  57. }
  58. void drawURL(void) {
  59. u8g.setFont(u8g_font_4x6);
  60. if ( u8g.getHeight() < 59 ) {
  61. u8g.drawStr(53,9,"code.google.com");
  62. u8g.drawStr(77,18,"/p/u8glib");
  63. }
  64. else {
  65. u8g.drawStr(1,54,"code.google.com/p/u8glib");
  66. }
  67. }
  68. void draw(void) {
  69. // graphic commands to redraw the complete screen should be placed here
  70. drawLogo(0);
  71. drawURL();
  72. u8g.drawFrame(0,0,u8g.getWidth(), u8g.getHeight());
  73. u8g.setFont(u8g_font_helvR24r);
  74. u8g.setPrintPos(0, 100);
  75. u8g.print(u8g.getWidth(), DEC);
  76. u8g.print("x");
  77. u8g.print(u8g.getHeight(), DEC);
  78. }
  79. void setup(void) {
  80. PRINTER_SERIAL.begin(19200);
  81. // flip screen, if required
  82. // u8g.setRot180();
  83. // assign default color value
  84. u8g.setColorIndex(1); // pixel on
  85. }
  86. void loop(void) {
  87. // picture loop: This will print the picture
  88. u8g.firstPage();
  89. do {
  90. draw();
  91. } while( u8g.nextPage() );
  92. // send manual CR to the printer
  93. PRINTER_SERIAL.write('\n');
  94. // reprint the picture after 10 seconds
  95. delay(10000);
  96. }