-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Thermal_Printer.cpp
1777 lines (1676 loc) · 58.3 KB
/
Thermal_Printer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Thermal_Printer Arduino library
//
// Copyright (c) 2020 BitBank Software, Inc.
// Written by Larry Bank (bitbank@pobox.com)
// Project started 1/6/2020
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <Arduino.h>
// uncomment this line to see debug info on the serial monitor
//#define DEBUG_OUTPUT
// Two sets of code - one for ESP32
#ifdef HAL_ESP32_HAL_H_
#ifdef NIMBLE_SUPPORT
#include <NimBLEDevice.h>
#else
#include <BLEDevice.h>
#endif
#endif
#if defined ( ARDUINO_ARDUINO_NANO33BLE ) || defined( ARDUINO_UNOR4_WIFI )
#include <ArduinoBLE.h>
#endif
#ifdef ARDUINO_NRF52_ADAFRUIT
#include <bluefruit.h>
#endif
#include "Thermal_Printer.h"
static char szPrinterName[32];
volatile uint8_t ucPrinterType; // one of PRINTER_MTP2, PRINTER_CAT, etc
static int bb_width, bb_height; // back buffer width and height in pixels
static int tp_wrap, bb_pitch;
static int16_t iCursorX = 0, iCursorY = 0;
static uint8_t bWithResponse = 0; // default to not wait for a response
static uint8_t *pBackBuffer = NULL;
static uint8_t bConnected = 0;
static void tpWriteData(uint8_t *pData, int iLen);
extern "C" {
extern unsigned char ucFont[], ucBigFont[];
};
static uint8_t tpFindPrinterName(char *szName);
static void tpPreGraphics(int iWidth, int iHeight);
static void tpPostGraphics(void);
static void tpSendScanline(uint8_t *pSrc, int iLen);
struct PRINTERID
{
const char *szBLEName;
uint8_t ucBLEType;
} ;
// Names and types of supported printers
const PRINTERID szPrinterIDs[] PROGMEM = {
{(char *)"MP210", PRINTER_MTP2},
{(char *)"PT-210", PRINTER_MTP2},
{(char *)"MTP-2", PRINTER_MTP2},
{(char *)"MPT-II", PRINTER_MTP2},
{(char *)"MPT-3", PRINTER_MTP3},
{(char *)"MPT-3F", PRINTER_MTP3},
{(char *)"GT01", PRINTER_CAT},
{(char *)"GT02", PRINTER_CAT},
{(char *)"GB01", PRINTER_CAT},
{(char *)"GB02", PRINTER_CAT},
{(char *)"GB03", PRINTER_CAT},
{(char *)"YHK-A133", PRINTER_CAT},
{(char *)"PeriPage+", PRINTER_PERIPAGEPLUS},
{(char *)"PeriPage_", PRINTER_PERIPAGE},
{(char *)"T02", PRINTER_FOMEMO},
{(char *)"MX06", PRINTER_CAT},
{(char *)"MX10", PRINTER_CAT},
{NULL, 0} // terminator
};
const int iPrinterWidth[] = {384, 576, 384, 576, 384, 384};
const uint8_t PeriPrefix[] = {0x10,0xff,0xfe,0x01};
const char *szServiceNames[] = {(char *)"18f0", (char *)"18f0", (char *)"ae30", (char *)"ff00",(char *)"ff00", (char *)"ff00"}; // 16-bit UUID of the printer services we want
const char *szCharNames[] = {(char *)"2af1", (char *)"2af1", (char *)"ae01",(char *)"ff02", (char *)"ff02", (char *)"ff02"}; // 16-bit UUID of printer data characteristics we want
// Command sequences for the 'cat' printer
// for more details see https://github.com/fulda1/Thermal_Printer/wiki/Cat-printer-protocol
const uint8_t paperRetract = 0xA0; // 0xA0 Retract Paper - Data: Number of steps to go backward
const uint8_t paperFeed = 0xA1; // 0xA1 Feed Paper - Data: Number of steps to go forward
//const uint8_t DataLine = 0xA2; # Data: Line to draw. 0 bit -> don't draw pixel, 1 bit -> draw pixel
const uint8_t getDevState = 0xA3; // 0xA3 Get Device State - data 0; reply is by notification
const uint8_t setQuality = 0xA4; // 0xA4 Set quality 0x31-0x36 GB01 printer always 0x33, other 0x32?
// 0xA5 ???
const uint8_t controlLattice = 0xA4; // 0xA6 control Lattice Eleven bytes, all constants. One set used before printing, one after.
const uint8_t latticeStart[] = {0x51, 0x78, 0xA6, 0, 0x0B, 0, 0xAA, 0x55, 0x17, 0x38, 0x44, 0x5F, 0x5F, 0x5F, 0x44, 0x38, 0x2C, 0xA1, 0xFF};
const uint8_t latticeEnd[] = {0x51, 0x78, 0xA6, 0, 0x0B, 0, 0xAA, 0x55, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x11, 0xFF};
// A7 ??
const uint8_t getDevInfo = 0xA8; // 0xA8 Get Device Info - data 0; reply with notify something look like version.
const uint8_t setEnergy = 0xAF; // 0xAF Set Energy - Data: 1 - 0xFFFF
const uint8_t setDrawingMode = 0xBE; // 0xBE DrawingMode - Data: 1 for Text, 0 for Images
// GetDevInfo = 0xA8 # Data: 0
// XOff = (0x51, 0x78, 0xAE, 0x01, 0x01, 0x00, 0x10, 0x70, 0xFF)
// XOn = (0x51, 0x78, 0xAE, 0x01, 0x01, 0x00, 0x00, 0x00, 0xFF)
//OtherFeedPaper = 0xBD # Data: one byte, set to a device-specific "Speed" value before printing
//# and to 0x19 before feeding blank paper
int i;
// variables for printing text on cat printer
uint8_t CatStrLen = 0;
char CatStr[48]; // max 48 characters * 8 pixels
//CRC8 pre calculated values
const uint8_t cChecksumTable[] PROGMEM = {
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3};
/* Table of byte flip values to mirror-image incoming CCITT data */
const unsigned char ucMirror[256] PROGMEM =
{0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF};
#ifdef ARDUINO_NRF52_ADAFRUIT
// Bluetooth support for Adafruit nrf52 boards
//#define myServiceUUID 0xFEA0
//#define myDataUUID 0xFEA1
static ble_gap_evt_adv_report_t the_report;
static uint16_t the_conn_handle;
static int bNRFFound;
//BLEClientCharacteristic myDataChar(myDataUUID);
//BLEClientService myService(myServiceUUID);
BLEClientService myService; //(0x18f0);
BLEClientCharacteristic myDataChar; //(0x2af1);
/**
* Callback invoked when an connection is established
* @param conn_handle
*/
static void connect_callback(uint16_t conn_handle)
{
#ifdef DEBUG_OUTPUT
Serial.println("Connected!");
Serial.print("Discovering Service ... ");
#endif
the_conn_handle = conn_handle;
// If Service UUID is not found, disconnect and return
if ( !myService.discover(conn_handle) )
{
#ifdef DEBUG_OUTPUT
Serial.println("Didn't find our service, disconnecting...");
#endif
// disconect since we couldn't find our service
Bluefruit.disconnect(conn_handle);
return;
}
// Once FEA0 service is found, we continue to discover its characteristics
if ( !myDataChar.discover() )
{
// Data char is mandatory, if it is not found (valid), then disconnect
#ifdef DEBUG_OUTPUT
Serial.println("Data characteristic is mandatory but not found");
#endif
Bluefruit.disconnect(conn_handle);
return;
}
bConnected = 1; // success!
} /* connect_callback() */
/**
* Callback invoked when a connection is dropped
* @param conn_handle
* @param reason
*/
static void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
bConnected = 0;
// Serial.println("Disconnected");
} /* disconnect_callback() */
void ParseDeviceName(uint8_t *s, char *szOut)
{
int bDone = 0, i = 0;
int iLen;
szOut[0] = 0; // assume we won't find a device name
while (i < 64 && !bDone) {
iLen = s[i++];
if (s[i] == 0x09) { // device name
i++; // skip type
memcpy(szOut, &s[i], iLen-1);
szOut[iLen-1] = 0; // zero terminate
bDone = 1;
} else {
i += iLen; // skip this data field
}
}
} /* ParseDeviceName() */
static void scan_callback(ble_gap_evt_adv_report_t* report)
{
char szTemp[32];
ParseDeviceName(report->data.p_data, szTemp);
// Serial.printf("found something %s\n", report->data.p_data);
// if (Bluefruit.Scanner.checkReportForUuid(report, myServiceUUID))
// char *name = (char *)report->data.p_data;
// int i;
// for (i=0; i<report->data.len; i++)
// if (name[i] == szPrinterName[0]) break; // "parse" for the name in the adv data
// if (name && memcmp(&name[i], szPrinterName, strlen(szPrinterName)) == 0)
#ifdef DEBUG_OUTPUT
Serial.println("Found something!");
Serial.print("device name = ");
Serial.println(szTemp);
#endif
ucPrinterType = tpFindPrinterName(szTemp);
if (ucPrinterType > PRINTER_COUNT) { // nothing supported found
Bluefruit.Scanner.resume();
} else { // we can stop scanning
bNRFFound = 1;
ParseDeviceName(report->data.p_data, szPrinterName); // allow query by user
Bluefruit.Scanner.stop();
memcpy(&the_report, report, sizeof(ble_gap_evt_adv_report_t));
#ifdef DEBUG_OUTPUT
Serial.println("Found a supported printer!");
#endif
}
// else // keep looking
// {
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner resume() to continue scanning
// Bluefruit.Scanner.resume();
// }
} /* scan_callback() */
static void notify_callback(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
{
} /* notify_callback() */
#endif // Adafruit nrf52
#ifdef HAL_ESP32_HAL_H_
static void ESP_notify_callback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
Serial.print("data: ");
for (int i=0; i<length; i++)
{
Serial.print(pData[i], HEX);
Serial.print(" ");
}
Serial.println(" ");
}
#endif // ESP callback
#ifdef HAL_ESP32_HAL_H_
static BLEUUID SERVICE_UUID0("49535343-FE7D-4AE5-8FA9-9FAFD205E455");
static BLEUUID CHAR_UUID_DATA0 ("49535343-8841-43F4-A8D4-ECBE34729BB3");
//static BLEUUID SERVICE_UUID1("0000AE30-0000-1000-8000-00805F9B34FB"); //Service
//static BLEUUID CHAR_UUID_DATA1("0000AE01-0000-1000-8000-00805F9B34FB"); // data characteristic
static BLEUUID SERVICE_UUID1(BLEUUID ((uint16_t)0xae30));
static BLEUUID CHAR_UUID_DATA1(BLEUUID((uint16_t)0xae01));
static BLEUUID CHAR_UUID_NOTIFY1(BLEUUID((uint16_t)0xae02));
static BLEUUID SERVICE_UUID2(BLEUUID ((uint16_t)0xff00));
static BLEUUID CHAR_UUID_DATA2(BLEUUID((uint16_t)0xff02));
static String Scanned_BLE_Address;
static BLEScanResults foundDevices;
static BLEAddress *Server_BLE_Address;
static BLERemoteCharacteristic* pRemoteCharacteristicData;
static BLERemoteCharacteristic* pRemoteCharacteristicNotify;
static BLEScan *pBLEScan;
static BLEClient* pClient;
static char Scanned_BLE_Name[32];
#endif
#ifdef _ARDUINO_BLE_H_
static BLEDevice peripheral;
static BLEService prtService;
static BLECharacteristic pRemoteCharacteristicData;
#endif
void tpSetWriteMode(uint8_t bWriteMode)
{
bWithResponse = bWriteMode;
} /* tpSetWriteMode() */
#ifdef HAL_ESP32_HAL_H_
#ifdef NIMBLE_SUPPORT
typedef BLEAdvertisedDevice* GeneralBLEAdvertisedDevice;
#else
typedef BLEAdvertisedDevice GeneralBLEAdvertisedDevice;
#endif
// Called for each device found during a BLE scan by the client
class tpAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(GeneralBLEAdvertisedDevice genAdvertisedDevice)
{
#ifdef NIMBLE_SUPPORT
auto advertisedDevice = genAdvertisedDevice;
#else
auto advertisedDevice = &genAdvertisedDevice;
#endif
int iLen = strlen(szPrinterName);
#ifdef DEBUG_OUTPUT
Serial.printf("Scan Result: %s \n", advertisedDevice->toString().c_str());
#endif
if (iLen > 0 && memcmp(advertisedDevice->getName().c_str(), szPrinterName, iLen) == 0)
{ // this is what we want
Server_BLE_Address = new BLEAddress(advertisedDevice->getAddress());
Scanned_BLE_Address = Server_BLE_Address->toString().c_str();
strcpy(Scanned_BLE_Name, advertisedDevice->getName().c_str());
#ifdef DEBUG_OUTPUT
Serial.println("A match!");
Serial.println((char *)Scanned_BLE_Address.c_str());
Serial.println(Scanned_BLE_Name);
#endif
} else if (iLen == 0) { // check for supported printers
char szName[32];
uint8_t ucType;
strcpy(szName, advertisedDevice->getName().c_str());
ucType = tpFindPrinterName(szName);
if (ucType < PRINTER_COUNT) { // found a valid one!
Server_BLE_Address = new BLEAddress(advertisedDevice->getAddress());
Scanned_BLE_Address = Server_BLE_Address->toString().c_str();
ucPrinterType = ucType;
strcpy(Scanned_BLE_Name, advertisedDevice->getName().c_str());
strcpy(szPrinterName, Scanned_BLE_Name); // allow user to query this
#ifdef DEBUG_OUTPUT
Serial.print("A match! - ");
Serial.print((char *)Scanned_BLE_Address.c_str());
Serial.print(" - ");
Serial.println(Scanned_BLE_Name);
#endif
}
} // if auto-detecting printers
}
}; // class tpAdvertisedDeviceCallbacks
#endif
// Provide a back buffer for your printer graphics
// This allows you to manage the RAM used on
// embedded platforms like Arduinos
// The memory is laid out horizontally (384/576 pixels across = 48/72 bytes)
// So a 384x384 buffer would need to be 48x384 = 18432 bytes
//
void tpSetBackBuffer(uint8_t *pBuffer, int iWidth, int iHeight)
{
pBackBuffer = pBuffer;
bb_width = iWidth;
bb_height = iHeight;
bb_pitch = (iWidth + 7) >> 3;
} /* tpSetBackBuffer() */
//
// Fill the frame buffer with a byte pattern
// e.g. all off (0x00) or all on (0xff)
//
void tpFill(unsigned char ucData)
{
if (pBackBuffer != NULL)
memset(pBackBuffer, ucData, bb_pitch * bb_height);
} /* tpFill() */
//
// Turn text wrap on or off for the oldWriteString() function
//
void tpSetTextWrap(int bWrap)
{
tp_wrap = bWrap;
} /* tpSetTextWrap() */
//
// Invert font data
//
static void InvertBytes(uint8_t *pData, uint8_t bLen)
{
uint8_t i;
for (i=0; i<bLen; i++)
{
*pData = ~(*pData);
pData++;
}
} /* InvertBytes() */
//
// Return the measurements of a rectangle surrounding the given text string
// rendered in the given font
//
void tpGetStringBox(GFXfont *pFont, char *szMsg, int *width, int *top, int *bottom)
{
int cx = 0;
int c, i = 0;
GFXglyph *pGlyph;
int miny, maxy;
if (width == NULL || top == NULL || bottom == NULL || pFont == NULL || szMsg == NULL) return; // bad pointers
miny = 100; maxy = 0;
while (szMsg[i]) {
c = szMsg[i++];
if (c < pFont->first || c > pFont->last) // undefined character
continue; // skip it
c -= pFont->first; // first char of font defined
pGlyph = &pFont->glyph[c];
cx += pGlyph->xAdvance;
if (pGlyph->yOffset < miny) miny = pGlyph->yOffset;
if (pGlyph->height+pGlyph->yOffset > maxy) maxy = pGlyph->height+pGlyph->yOffset;
}
*width = cx;
*top = miny;
*bottom = maxy;
} /* tpGetStringBox() */
//
// Draw a string of characters in a custom font into the gfx buffer
//
int tpDrawCustomText(GFXfont *pFont, int x, int y, char *szMsg)
{
int i, end_y, dx, dy, tx, ty, c, iBitOff;
uint8_t *s, *d, bits, ucMask, ucClr, uc;
GFXglyph glyph, *pGlyph;
if (pBackBuffer == NULL || pFont == NULL || x < 0 || y > bb_height)
return -1;
pGlyph = &glyph;
i = 0;
while (szMsg[i] && x < bb_width)
{
c = szMsg[i++];
if (c < pFont->first || c > pFont->last) // undefined character
continue; // skip it
c -= pFont->first; // first char of font defined
memcpy_P(&glyph, &pFont->glyph[c], sizeof(glyph));
dx = x + pGlyph->xOffset; // offset from character UL to start drawing
dy = y + pGlyph->yOffset;
s = pFont->bitmap + pGlyph->bitmapOffset; // start of bitmap data
// Bitmap drawing loop. Image is MSB first and each pixel is packed next
// to the next (continuing on to the next character line)
iBitOff = 0; // bitmap offset (in bits)
bits = uc = 0; // bits left in this font byte
end_y = dy + pGlyph->height;
if (dy < 0) { // skip these lines
iBitOff += (pGlyph->width * (-dy));
dy = 0;
}
for (ty=dy; ty<=end_y && ty < bb_height; ty++) {
d = &pBackBuffer[ty * bb_pitch]; // internal buffer dest
for (tx=0; tx<pGlyph->width; tx++) {
if (uc == 0) { // need to read more font data
tx += bits; // skip any remaining 0 bits
uc = pgm_read_byte(&s[iBitOff>>3]); // get more font bitmap data
bits = 8 - (iBitOff & 7); // we might not be on a byte boundary
iBitOff += bits; // because of a clipped line
uc <<= (8-bits);
if (tx >= pGlyph->width) {
while(tx >= pGlyph->width) { // rolls into next line(s)
tx -= pGlyph->width;
ty++;
}
if (ty >= end_y) { // we're past the end
tx = pGlyph->width;
continue; // exit this character cleanly
}
d = &pBackBuffer[ty * bb_pitch];
}
} // if we ran out of bits
if (uc & 0x80) { // set pixel
ucMask = 0x80 >> ((dx+tx) & 7);
d[(dx+tx)>>3] |= ucMask;
}
bits--; // next bit
uc <<= 1;
} // for x
} // for y
x += pGlyph->xAdvance; // width of this character
} // while drawing characters
return 0;
} /* tpDrawCustomText() */
//
// Print a string of characters in a custom font to the connected printer
//
int tpPrintCustomText(GFXfont *pFont, int startx, char *szMsg)
{
int i, x, y, end_y, dx, dy, tx, ty, c, iBitOff;
int maxy, miny, height;
uint8_t *s, *d, bits, ucMask, ucClr, uc;
GFXglyph glyph, *pGlyph;
uint8_t ucTemp[80]; // max width of 1 scan line (576 pixels)
int iPrintWidth = iPrinterWidth[ucPrinterType];
if (!bConnected)
return -1;
if (pFont == NULL || x < 0)
return -1;
pGlyph = &glyph;
// Get the size of the rectangle enclosing the text
// tpGetStringBox(pFont, szMsg, &tx, &miny, &maxy);
// height = (maxy - miny) + 1;
tpPreGraphics(iPrintWidth, pFont->yAdvance);
miny = 0 - (pFont->yAdvance * 2)/3; // 2/3 of char is above the baseline
maxy = pFont->yAdvance + miny;
for (y=miny; y<=maxy; y++)
{
i = 0;
x = startx;
memset(ucTemp, 0, sizeof(ucTemp));
while (szMsg[i] && x < iPrintWidth)
{
c = szMsg[i++];
if (c < pFont->first || c > pFont->last) // undefined character
continue; // skip it
c -= pFont->first; // first char of font defined
memcpy_P(&glyph, &pFont->glyph[c], sizeof(glyph));
dx = x + pGlyph->xOffset; // offset from character UL to start drawing
dy = /*y +*/ pGlyph->yOffset;
s = pFont->bitmap + pGlyph->bitmapOffset; // start of bitmap data
// Bitmap drawing loop. Image is MSB first and each pixel is packed next
// to the next (continuing on to the next character line)
iBitOff = 0; // bitmap offset (in bits)
bits = uc = 0; // bits left in this font byte
end_y = dy + pGlyph->height;
for (ty=dy; ty<=end_y; ty++) {
for (tx=0; tx<pGlyph->width; tx++) {
if (uc == 0) { // need to read more font data
tx += bits; // skip any remaining 0 bits
uc = pgm_read_byte(&s[iBitOff>>3]); // get more font bitmap data
bits = 8 - (iBitOff & 7); // we might not be on a byte boundary
iBitOff += bits; // because of a clipped line
uc <<= (8-bits);
if (tx >= pGlyph->width) {
while(tx >= pGlyph->width) { // rolls into next line(s)
tx -= pGlyph->width;
ty++;
}
if (ty >= end_y) { // we're past the end
tx = pGlyph->width;
continue; // exit this character cleanly
}
}
} // if we ran out of bits
if (uc & 0x80 && ty == y) { // set pixel if we're drawing this line
ucMask = 0x80 >> ((dx+tx) & 7);
ucTemp[(dx+tx)>>3] |= ucMask;
}
bits--; // next bit
uc <<= 1;
} // for tx
} // for ty
x += pGlyph->xAdvance; // width of this character
} // while drawing characters
tpSendScanline(ucTemp, (iPrintWidth+7)/8); // send to printer
} // for each line of output
tpPostGraphics();
return 0;
} /* tpPrintCustomText() */
//
// Draw text into the graphics buffer
//
int tpDrawText(int x, int y, char *szMsg, int iFontSize, int bInvert)
{
int i, ty, iFontOff;
unsigned char c, *s, *d, ucTemp[64];
if (x == -1 || y == -1) // use the cursor position
{
x = iCursorX; y = iCursorY;
}
else
{
iCursorX = x; iCursorY = y; // set the new cursor position
}
if (iCursorX >= bb_width || iCursorY >= bb_height-7)
return -1; // can't draw off the display
if (iFontSize == FONT_SMALL) // 8x8 font
{
i = 0;
while (iCursorX < bb_width && szMsg[i] != 0 && iCursorY < bb_height)
{
c = (unsigned char)szMsg[i];
iFontOff = (int)(c-32) * 8;
memcpy(ucTemp, &ucFont[iFontOff], 8);
if (bInvert) InvertBytes(ucTemp, 8);
d = &pBackBuffer[(iCursorY * bb_pitch) + iCursorX/8];
for (ty=0; ty<8; ty++)
{
d[0] = ucTemp[ty];
d += bb_pitch;
}
iCursorX += 8;
if (iCursorX >= bb_width && tp_wrap) // word wrap enabled?
{
iCursorX = 0; // start at the beginning of the next line
iCursorY +=8;
}
i++;
} // while
return 0;
} // 8x8
else if (iFontSize == FONT_LARGE) // 16x32 font
{
i = 0;
while (iCursorX < bb_width && iCursorY < bb_height-31 && szMsg[i] != 0)
{
s = (unsigned char *)&ucBigFont[(unsigned char)(szMsg[i]-32)*64];
memcpy(ucTemp, s, 64);
if (bInvert) InvertBytes(ucTemp, 64);
d = &pBackBuffer[(iCursorY * bb_pitch) + iCursorX/8];
for (ty=0; ty<32; ty++)
{
d[0] = s[0];
d[1] = s[1];
s += 2; d += bb_pitch;
}
iCursorX += 16;
if (iCursorX >= bb_width && tp_wrap) // word wrap enabled?
{
iCursorX = 0; // start at the beginning of the next line
iCursorY += 32;
}
i++;
} // while
return 0;
} // 16x32
return -1;
} /* tpDrawText() */
//
// Set (or clear) an individual pixel
//
int tpSetPixel(int x, int y, uint8_t ucColor)
{
uint8_t *d, mask;
if (pBackBuffer == NULL)
return -1;
d = &pBackBuffer[(bb_pitch * y) + (x >> 3)];
mask = 0x80 >> (x & 7);
if (ucColor)
d[0] |= mask;
else
d[0] &= ~mask;
return 0;
} /* tpSetPixel() */
//
// Load a 1-bpp Windows bitmap into the back buffer
// Pass the pointer to the beginning of the BMP file
// along with a x and y offset (upper left corner)
//
int tpLoadBMP(uint8_t *pBMP, int bInvert, int iXOffset, int iYOffset)
{
int16_t i16;
int iOffBits; // offset to bitmap data
int iPitch;
int16_t cx, cy, x, y;
uint8_t *d, *s, pix;
uint8_t srcmask, dstmask;
uint8_t bFlipped = false;
i16 = pBMP[0] | (pBMP[1] << 8);
if (i16 != 0x4d42) // must start with 'BM'
return -1; // not a BMP file
if (iXOffset < 0 || iYOffset < 0)
return -1;
cx = pBMP[18] + (pBMP[19] << 8);
cy = pBMP[22] + (pBMP[23] << 8);
if (cy > 0) // BMP is flipped vertically (typical)
bFlipped = true;
if (cx + iXOffset > bb_width || cy + iYOffset > bb_height) // too big
return -1;
i16 = pBMP[28] + (pBMP[29] << 8);
if (i16 != 1) // must be 1 bit per pixel
return -1;
iOffBits = pBMP[10] + (pBMP[11] << 8);
iPitch = (cx + 7) >> 3; // byte width
iPitch = (iPitch + 3) & 0xfffc; // must be a multiple of DWORDS
if (bFlipped)
{
iOffBits += ((cy-1) * iPitch); // start from bottom
iPitch = -iPitch;
}
else
{
cy = -cy;
}
// Send it to the gfx buffer
for (y=0; y<cy; y++)
{
s = &pBMP[iOffBits + (y * iPitch)]; // source line
d = &pBackBuffer[((iYOffset+y) * bb_pitch) + iXOffset/8];
srcmask = 0x80; dstmask = 0x80 >> (iXOffset & 7);
pix = *s++;
if (bInvert) pix = ~pix;
for (x=0; x<cx; x++) // do it a bit at a time
{
if (pix & srcmask)
*d |= dstmask;
else
*d &= ~dstmask;
srcmask >>= 1;
if (srcmask == 0) // next pixel
{
srcmask = 0x80;
pix = *s++;
if (bInvert) pix = ~pix;
}
dstmask >>= 1;
if (dstmask == 0)
{
dstmask = 0x80;
d++;
}
} // for x
} // for y
return 0;
} /* tpLoadBMP() */
//
// Connection status
// true = connected, false = disconnected
//
int tpIsConnected(void)
{
if (bConnected == 1) {
// we are/were connected, check...
#ifdef HAL_ESP32_HAL_H_
if (pClient && pClient->isConnected())
return 1;
bConnected = 0; // change status to disconnected
#endif // ESP32
}
return 0; // not connected
} /* tpIsConnected() */
//
// After a successful scan, connect to the printer
// returns 1 if successful, 0 for failure
//
int tpConnect(void)
{
return tpConnect(NULL);
} /* tpConnect() */
//
// After a successful scan, connect to the printer
// returns 1 if successful, 0 for failure
//
int tpConnect(const char *szMacAddress)
{
#ifdef HAL_ESP32_HAL_H_
pClient = BLEDevice::createClient();
if (szMacAddress != NULL) {
if (Server_BLE_Address != NULL) {
delete Server_BLE_Address;
}
Server_BLE_Address = new BLEAddress(String(szMacAddress));
#ifdef DEBUG_OUTPUT
Serial.printf(" - Created client, connecting to %s\n", szMacAddress);
#endif
} else {
#ifdef DEBUG_OUTPUT
Serial.printf(" - Created client, connecting to %s\n", Scanned_BLE_Address.c_str());
#endif
}
// Connect to the BLE Server.
pClient->connect(*Server_BLE_Address);
#ifdef DEBUG_OUTPUT
Serial.println("Came back from connect");
#endif
if (!pClient->isConnected())
{
Serial.println("Connect failed");
return false;
}
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = NULL;
if (ucPrinterType == PRINTER_MTP2 || ucPrinterType == PRINTER_MTP3)
pRemoteService = pClient->getService(SERVICE_UUID0);
else if (ucPrinterType == PRINTER_CAT)
pRemoteService = pClient->getService(SERVICE_UUID1);
else if (ucPrinterType == PRINTER_FOMEMO || ucPrinterType == PRINTER_PERIPAGE || ucPrinterType == PRINTER_PERIPAGEPLUS)
pRemoteService = pClient->getService(SERVICE_UUID2);
if (pRemoteService != NULL)
{
#ifdef DEBUG_OUTPUT
Serial.println(" - Found our service");
#endif
if (pClient->isConnected())
{
pRemoteCharacteristicData = NULL;
if (ucPrinterType == PRINTER_MTP2 || ucPrinterType == PRINTER_MTP3)
pRemoteCharacteristicData = pRemoteService->getCharacteristic(CHAR_UUID_DATA0);
else if (ucPrinterType == PRINTER_CAT)
{
pRemoteCharacteristicData = pRemoteService->getCharacteristic(CHAR_UUID_DATA1);
pRemoteCharacteristicNotify = pRemoteService->getCharacteristic(CHAR_UUID_NOTIFY1);
}
else if (ucPrinterType == PRINTER_FOMEMO || ucPrinterType == PRINTER_PERIPAGE || ucPrinterType == PRINTER_PERIPAGEPLUS)
pRemoteCharacteristicData = pRemoteService->getCharacteristic(CHAR_UUID_DATA2);
if (pRemoteCharacteristicData != NULL)
{
#ifdef DEBUG_OUTPUT
Serial.println("Got data transfer characteristic!");
#endif
if (pRemoteCharacteristicNotify != NULL)
if(pRemoteCharacteristicNotify->canNotify())
pRemoteCharacteristicNotify->registerForNotify(ESP_notify_callback);
bConnected = 1;
return 1;
}
} // if connected
} // if service found
else
{
bConnected = 0;
#ifdef DEBUG_OUTPUT
Serial.println("Data service not found");
#endif
}
return 0;
#endif
#ifdef _ARDUINO_BLE_H_ // Arduino BLE
if (!peripheral)
{
#ifdef DEBUG_OUTPUT
Serial.println("No peripheral");
#endif
return 0; // scan didn't succeed or wasn't run
}
// Connect to the BLE Server.
#ifdef DEBUG_OUTPUT
Serial.println("connection attempt...");
#endif
if (peripheral.connect())
{
#ifdef DEBUG_OUTPUT
Serial.println("Connected!");
#endif
// you MUST discover the service or you won't be able to access it
if (peripheral.discoverService(szServiceNames[ucPrinterType])) {
#ifdef DEBUG_OUTPUT
Serial.print("Service 0x");
Serial.print(szServiceNames[ucPrinterType]);
Serial.println(" discovered");
#endif
} else {
#ifdef DEBUG_OUTPUT
Serial.println("Service discovery failed");
#endif
peripheral.disconnect();
while (1);
}
// Obtain a reference to the service we are after in the remote BLE server.
#ifdef DEBUG_OUTPUT
Serial.println("Trying to get service");
#endif
prtService = peripheral.service(szServiceNames[ucPrinterType]); // get the printer service
if (prtService)
{
#ifdef DEBUG_OUTPUT
Serial.println("Got the service");
#endif
pRemoteCharacteristicData = prtService.characteristic(szCharNames[ucPrinterType]);
if (pRemoteCharacteristicData)
{
#ifdef DEBUG_OUTPUT
Serial.println("Got the characteristic");
#endif
bConnected = 1;
return 1;
}
}
else
{
#ifdef DEBUG_OUTPUT
Serial.println("Didn't get the service");
#endif
}
}
#ifdef DEBUG_OUTPUT
Serial.println("connection failed");
#endif
return 0;
#endif // NANO33
#ifdef ARDUINO_NRF52_ADAFRUIT
Bluefruit.Central.connect(&the_report);
long ulTime = millis();
while (!bConnected && (millis() - ulTime) < 4000) // allow 4 seconds for the connection to occur
{
delay(20);
}
return bConnected;
#endif // ADAFRUIT
} /* tpConnect() */
void tpDisconnect(void)
{
if (!bConnected) return; // nothing to do
#ifdef HAL_ESP32_HAL_H_
if (pClient != NULL)
{
pClient->disconnect();
bConnected = 0;
}
#endif
#ifdef _ARDUINO_BLE_H_
if (peripheral)
{
if (peripheral.connected())
{
peripheral.disconnect();
bConnected = 0;
}
}
#endif
#ifdef ARDUINO_NRF52_ADAFRUIT
{
bConnected = 0;
Bluefruit.disconnect(the_conn_handle);
}
#endif
} /* tpDisconnect() */
//
// Find the printer name from our supported names list
//
static uint8_t tpFindPrinterName(char *szName)
{
int i = 0;
szName[9] = 0; // Need to chop off the name after 'PeriPage+'
// because it includes 2 bytes of the BLE MAC address
while (szPrinterIDs[i].szBLEName != NULL) {
if (strcmp(szName, szPrinterIDs[i].szBLEName) == 0) { // found a supported printer
#ifdef DEBUG_OUTPUT
Serial.print("Found a match for ");
Serial.println(szName);
Serial.print("Printer type = ");
Serial.println(szPrinterIDs[i].ucBLEType, DEC);
#endif
return szPrinterIDs[i].ucBLEType;
} else {
i++;
}
} // while searching our name list
return 255; // invalid name
} /* tpFindPrinterName() */
//
// Parameterless version
// finds supported printers automatically
//
int tpScan(void)
{