-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStMicroController.kt
More file actions
645 lines (549 loc) · 24.9 KB
/
StMicroController.kt
File metadata and controls
645 lines (549 loc) · 24.9 KB
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
package com.imobile3.pos.hardware.printer.stmicro
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.hardware.usb.UsbConstants
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbDeviceConnection
import android.hardware.usb.UsbEndpoint
import android.hardware.usb.UsbInterface
import android.hardware.usb.UsbManager
import com.imobile3.pos.hardware.printer.stmicro.objects.Logging.TAG
import com.imobile3.pos.hardware.printer.stmicro.objects.StMicroPrinterHelper
import com.imobile3.pos.hardware.printer.stmicro.objects.StMicroPrinterHelper.getPaddedWidth
import com.imobile3.pos.library.interfaces.printer.iM3Printer
import com.imobile3.pos.library.interfaces.printer.iM3PrinterCallbacks
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceipt
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceiptComponent
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceiptComponent.BarcodeType
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceiptComponent.Justification
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceiptComponent.Justification.Center
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceiptComponent.TextFont
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceiptComponent.TextSize
import com.imobile3.pos.library.interfaces.printer.iM3PrinterReceiptComponent.TextStyle
import com.imobile3.pos.library.utils.BarcodeHelper
import com.imobile3.pos.library.utils.ImageHelper
import com.imobile3.pos.library.utils.LogHelper
import com.imobile3.pos.library.utils.PrintFormatHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.io.ByteArrayOutputStream
/**
* Essentially an SDK for the ST-P398C printer that is integrated on the Bleep TS813 POS. The
* ST-P398C is an extremely basic ascii printer connected via usb. Commands are sent to it in the
* form of a [ByteArray].
*/
class StMicroController(
val context: Context?,
val device: SupportedDevice?,
val connectionType: SupportedConnectionType?,
val address: String?,
val isShared: Boolean,
val callbacks: iM3PrinterCallbacks?
) : iM3Printer {
// Android USB classes
private val vendorId = device?.printerSpecs?.usbVendorId
private val productId = device?.printerSpecs?.usbProductId
private var usbInterface: UsbInterface? = null
private var connection: UsbDeviceConnection? = null
private var outputEndpoint: UsbEndpoint? = null
private val buffer = ByteArrayOutputStream()
private val bufferMax = 8 * 1024 // I'm copying this value from the Seiko Controller
private var hasPrinted = false
override fun printText(text: String?, cutPaper: Boolean) {
LogHelper.write(TAG, "StMicroController.printText($text, $cutPaper)")
val connection = getConnectionOrNull()
if (connection != null) {
PrintTextTask(text, cutPaper).apply {
name = "iM3Printer Print Text"
start()
}
} else {
// If the connection is still null after attempting to connect then report an error
val errorMsg = "Failed to establish a connection to the " +
"${device?.supportedHardware?.displayName ?: "Unknown"} printer."
LogHelper.write(TAG, errorMsg)
callbacks?.iM3PrinterOnError(errorMsg)
}
}
override fun printReceipt(receipt: iM3PrinterReceipt?) {
LogHelper.write(TAG, "StMicroController.printReceipt($receipt)")
val connection = getConnectionOrNull()
if (connection != null) {
PrintReceiptTask(receipt).apply {
name = "iM3Printer Print Receipt"
start()
}
} else {
// If the connection is still null after attempting to connect then report an error
val errorMsg = "Failed to establish a connection to the " +
"${device?.supportedHardware?.displayName ?: "Unknown"} printer."
LogHelper.write(TAG, errorMsg)
callbacks?.iM3PrinterOnError(errorMsg)
}
}
override fun openCashDrawer() {
// no-op this printer does not have a cash drawer
}
private fun getConnectionOrNull(): UsbDeviceConnection? {
return connection ?: connect()
}
private fun connect(): UsbDeviceConnection? {
val manager: UsbManager =
context?.getSystemService(Context.USB_SERVICE) as UsbManager
val deviceMap: HashMap<String, UsbDevice> = manager.deviceList
// Look for our printer by it's usb vendor and product id's
for (usbDevice in deviceMap.values) {
if (usbDevice.vendorId == vendorId && usbDevice.productId == productId) {
// Their should only be one interface present
usbInterface = usbDevice.getInterface(0)
usbInterface?.let { usbInterface ->
// Every UsbInterface has multiple endpoints to communicate with different
// purposes. For the bleep printer there are only 2 (input and output) and we are
// only interested in the output endpoint.
for (i in 0 until usbInterface.endpointCount) {
val endpoint = usbInterface.getEndpoint(i)
// Looking for the output endpoint specifically
if (endpoint.type == UsbConstants.USB_ENDPOINT_XFER_BULK
&& endpoint.direction == UsbConstants.USB_DIR_OUT) {
outputEndpoint = endpoint
}
}
// Establish communication with the printer
connection = manager.openDevice(usbDevice)
connection?.claimInterface(usbInterface, true)
}
break
}
}
return connection
}
private inner class PrintTextTask(val textToPrint: String?, val cutPaper: Boolean): Thread() {
override fun run() {
runBlocking {
launch(Dispatchers.IO) {
try {
setCodeTable()
val textComponent = iM3PrinterReceiptComponent().apply {
this.text = textToPrint
justification = Justification.Center
textFont = TextFont.Normal
textSize = TextSize.Normal
textStyle = TextStyle.Normal
}
addText(textComponent)
if (cutPaper) {
addFeedLines(5)
addCut()
}
// Send the buffer of commands to the printer as a ByteArray
flushBuffer()
// Callbacks must be invoked on the main thread
withContext(Dispatchers.Main) {
callbacks?.iM3PrinterOnPrintComplete()
}
} catch (e: Exception) {
LogHelper.write(TAG, "PrintTextTask -> Failed to print text: " +
e.localizedMessage
)
// Callbacks must be invoked on the main thread
withContext(Dispatchers.Main) {
callbacks?.iM3PrinterOnError(e.localizedMessage)
}
}
// Disconnect the printer when we're done with it
disconnect()
}
}
}
}
private inner class PrintReceiptTask(val receipt: iM3PrinterReceipt?) : Thread() {
override fun run() {
runBlocking {
launch(Dispatchers.IO) {
try {
val receipts = receipt?.split(
device?.printerSpecs?.maxComponentsPerReceipt ?: 0)
if (receipts != null) {
for (receipt in receipts) {
setCodeTable()
bufferReceipt(receipt)
if (receipt.isCutReceipt) {
addFeedLines(5)
addCut()
}
}
// Send the buffer of commands to the printer as a ByteArray
flushBuffer()
}
// Callbacks must be invoked on the main thread
withContext(Dispatchers.Main) {
callbacks?.iM3PrinterOnPrintComplete()
}
} catch(e: Exception) {
LogHelper.write(TAG, "PrintReceiptTask() -> Failed to print " +
"receipt: ${e.localizedMessage}")
// Callbacks must be invoked on the main thread
withContext(Dispatchers.Main) {
callbacks?.iM3PrinterOnError(e.localizedMessage)
}
}
// Disconnect the printer when we're done with it
disconnect()
}
}
}
}
private fun addText(component: iM3PrinterReceiptComponent) {
with (component) {
val maxCharWidth = getMaxCharWidth(textFont, textSize)
val formattedText = PrintFormatHelper.getFormattedText(component, maxCharWidth)
setLineSpacing(getTextLineHeight(textFont, textSize))
addFeedLinesIfNeeded(1)
addJustification(justification)
addTextFont(textFont)
addTextSize(textSize)
addTextStyle(textStyle)
buffer(getByteArray(getHex(formattedText)))
}
}
private fun bufferReceipt(receipt: iM3PrinterReceipt) {
for (component in receipt.components) {
with (component) {
when (type) {
iM3PrinterReceiptComponent.Type.Text -> addText(component)
iM3PrinterReceiptComponent.Type.Bitmap -> addBitmap(bitmap, justification)
iM3PrinterReceiptComponent.Type.Barcode -> {
addBarcode(barcodeType, barcodeContent, justification)
}
iM3PrinterReceiptComponent.Type.BarcodeFullWidth -> {
addBarcode(barcodeType, barcodeContent, justification)
}
iM3PrinterReceiptComponent.Type.Divider -> addDivider(dividerHeight)
iM3PrinterReceiptComponent.Type.UnderscoreDivider -> {
addUnderscoreDivider(textFont, textSize, textStyle)
}
iM3PrinterReceiptComponent.Type.LineFeed -> addFeedLines(component.feedLines)
iM3PrinterReceiptComponent.Type.Cut -> addCut()
iM3PrinterReceiptComponent.Type.Qrcode -> addQrcode(qrcodeContent, justification)
null -> {} // no-op
}
}
}
}
private fun setLineSpacing(lineHeight: Int) {
buffer(getByteArray("${AsciiControlCodes.SET_LINE_SPACING}${getHex(lineHeight)}"))
}
private fun getMaxCharWidth(textFont: TextFont, textSize: TextSize): Int {
if (device != null) {
with (device.printerSpecs) {
// If we are printing big or wide text we can only fit half the characters
var widthMultiplier = 1
if (isTextSizeSupported) {
if (textSize == TextSize.Double || textSize == TextSize.Wide) {
widthMultiplier = 2
}
}
// The width will be the total width of the receipt divided by the width of each
// character. So we get the max amount of characters that can fit on one line.
return if (textFont == TextFont.Normal) {
pageWidth / (fontWidthA * widthMultiplier)
} else {
pageWidth / (fontWidthB * widthMultiplier)
}
}
} else {
// The device really shouldn't be null at this point. We need to fix this class if it is.
LogHelper.write(TAG, "getMaxCharWidth -> device is null returning worst " +
"case scenario of 24")
return 24
}
}
private fun getTextLineHeight(textFont: TextFont, textSize: TextSize): Int {
if (device != null) {
with (device.printerSpecs) {
var heightMultiplier = 1
if (isTextSizeSupported) {
if (textSize == TextSize.Tall) {
heightMultiplier = 2
}
}
val heightPadding = heightMultiplier * 5
return if (textFont == TextFont.Normal) {
(fontHeightA * heightMultiplier) + heightPadding
} else {
(fontHeightB * heightMultiplier) + heightPadding
}
}
} else {
// The device really shouldn't be null at this point. We need to fix this class if it is.
LogHelper.write(TAG, "getTextLineHeight -> device is null returning worst " +
"case scenario of 58")
return 58
}
}
private fun addJustification(justification: Justification) {
when (justification) {
Justification.Left -> {
buffer(getByteArray(
"${AsciiControlCodes.SELECT_JUSTIFICATION}${getHex(0)}"))
}
Justification.Center -> {
buffer(getByteArray(
"${AsciiControlCodes.SELECT_JUSTIFICATION}${getHex(1)}"))
}
Justification.Right -> {
buffer(getByteArray(
"${AsciiControlCodes.SELECT_JUSTIFICATION}${getHex(2)}"))
}
else -> {
buffer(getByteArray(
"${AsciiControlCodes.SELECT_JUSTIFICATION}${getHex(0)}"))
}
}
}
private fun addTextFont(textFont: TextFont) {
if (textFont == TextFont.Normal) {
buffer(getByteArray("${AsciiControlCodes.SELECT_MODE}00")) // Font A
} else {
buffer(getByteArray("${AsciiControlCodes.SELECT_MODE}01")) // Font B (Smaller)
}
}
private fun addTextSize(textSize: TextSize) {
// This printer only supports tall and wide settings but not both at the same time. So if
// TextSize is not Tall or Wide then we default to Normal.
when (textSize) {
TextSize.Tall ->
buffer(getByteArray("${AsciiControlCodes.SET_CHARACTER_SIZE}01"))
TextSize.Wide ->
buffer(getByteArray("${AsciiControlCodes.SET_CHARACTER_SIZE}10"))
else ->
buffer(getByteArray("${AsciiControlCodes.SET_CHARACTER_SIZE}00"))
}
}
private fun addTextStyle(textStyle: TextStyle) {
when (textStyle) {
TextStyle.Normal -> {
buffer(getByteArray("${AsciiControlCodes.SET_BOLD}00")) // Turn off bold
buffer(getByteArray("${AsciiControlCodes.SET_UNDERLINE}00")) // Turn off underline
buffer(getByteArray("${AsciiControlCodes.SET_INVERSE}00")) // Turn off inverse
}
TextStyle.Bold -> {
buffer(getByteArray("${AsciiControlCodes.SET_BOLD}01")) // Turn on bold
buffer(getByteArray("${AsciiControlCodes.SET_UNDERLINE}00")) // Turn off underline
buffer(getByteArray("${AsciiControlCodes.SET_INVERSE}00")) // Turn off inverse
}
TextStyle.Underline -> {
buffer(getByteArray("${AsciiControlCodes.SET_BOLD}00")) // Turn off bold
buffer(getByteArray("${AsciiControlCodes.SET_UNDERLINE}02")) // Turn on underline
buffer(getByteArray("${AsciiControlCodes.SET_INVERSE}00")) // Turn off inverse
}
TextStyle.Inverse -> {
// The ST_P398C printer prints white on black but then errors immediately afterwards
// this might be a firmware issue :(
buffer(getByteArray("${AsciiControlCodes.SET_BOLD}00")) // Turn off bold
buffer(getByteArray("${AsciiControlCodes.SET_UNDERLINE}00")) // Turn off underline
buffer(getByteArray("${AsciiControlCodes.SET_INVERSE}00")) // Turn off inverse
}
TextStyle.Italic -> {
// The ST_P398C printer does not support italics :(
buffer(getByteArray("${AsciiControlCodes.SET_BOLD}00")) // Turn off bold
buffer(getByteArray("${AsciiControlCodes.SET_UNDERLINE}00")) // Turn off underline
buffer(getByteArray("${AsciiControlCodes.SET_INVERSE}00")) // Turn off inverse
}
}
}
/**
* Converts a string into hex. The resulting hex string is a string of two digit hex characters.
* Each two digit hex character represents one regular ascii character. For example:
* ```
* val result = getHex("Hello")
* println(result) // prints -> 48656C6C6F
* // H=48, e=65, l=6C, l=6C, o=6F
* ```
* This is step one of a two step process to convert a [String] into an array of [Byte] to be
* sent to the printer. Next we'll call [getByteArray]
*/
private fun getHex(string: String?): String {
string ?: return ""
val sb = StringBuilder()
for (char in string) {
sb.append(String.format("%02X", char.code))
}
return sb.toString()
}
private fun getHex(int: Int): String = String.format("%02X", int)
private fun getByteArray(hex: String): ByteArray {
// Each hexadecimal character is comprised of two hexadecimal digits. So every two
// characters in our hex string will be converted to one Byte.
val bytes = ByteArray(hex.length / 2)
for (i in bytes.indices) {
// Grab two hex characters at a time from our string and split them up
val firstIndex = i * 2
val secondIndex = i * 2 + 1
val firstHalf: String = hex.substring(firstIndex, firstIndex + 1)
val secondHalf: String = hex.substring(secondIndex, secondIndex + 1)
try {
// Each hex character represents 4 bits and we want to combine them to make a byte of
// 8 bits. Start by shifting the first character left 4 bits to make room for the
// second hex character.
val firstInt = Integer.parseInt(firstHalf, 16) shl 4
val secondInt = Integer.parseInt(secondHalf, 16)
// Now that we've bit shifted the first hex we can combine the two blocks of 4 bits
// into one byte with an inclusive "or" operator
bytes[i] = (firstInt or secondInt).toByte()
} catch(e: Exception) {
if (e is NumberFormatException) {
LogHelper.write(TAG, "getByteArray() -> Failed to convert hex character" +
"to Byte with the following error: ${e.localizedMessage}")
} else {
LogHelper.write(TAG, "getByteArray() -> ${e.localizedMessage}")
}
}
}
return bytes
}
private fun getByteArray(hex: String, bitmap: Bitmap): ByteArray {
val pixelArray = StMicroPrinterHelper.decodeBitmap(bitmap)
val bos = ByteArrayOutputStream()
bos.write(getByteArray(hex))
bos.write(pixelArray)
return bos.toByteArray()
}
private fun buffer(bytes: ByteArray?) {
if (bytes == null || bytes.isEmpty()) {
return
}
if ((bytes.size + buffer.size()) >= bufferMax) {
flushBuffer()
}
buffer.write(bytes)
}
private fun flushBuffer() {
if (buffer.size() == 0) {
return
}
val bytes = buffer.toByteArray()
connection?.bulkTransfer(outputEndpoint, bytes, bytes.size, 0)
buffer.reset()
}
private fun setCodeTable() {
// Make sure the [USA, Standard Europe] character code table is selected
buffer(getByteArray("${AsciiControlCodes.SELECT_CODE_TABLE}00"))
}
private fun addFeedLines(lines: Int) {
for (i in 1..lines) {
buffer(getByteArray(AsciiControlCodes.LF))
}
}
private fun addCut() {
buffer(getByteArray(AsciiControlCodes.PARTIAL_CUT))
}
private fun addFeedLinesIfNeeded(lines: Int) {
if (hasPrinted) {
addFeedLines(1)
} else {
hasPrinted = true
}
}
private fun addBitmap(bitmap: Bitmap, justification: Justification) {
// Configuring the bitmap before printing
val widthAfterPadding = getPaddedWidth(bitmap.width)
val maxPageWidth = device?.printerSpecs?.pageWidth ?: 576
var configuredBitmap = bitmap
// Scale the bitmap if it doesn't fit on the page
if (widthAfterPadding > maxPageWidth) {
configuredBitmap = ImageHelper.scaleBitmap(configuredBitmap, maxPageWidth)
}
// printer doesn't like alpha channels, so set to white
configuredBitmap = ImageHelper.alphaToWhite(configuredBitmap)
// make sure we're dealing with RGB_565
configuredBitmap = ImageHelper.reduceConfig(configuredBitmap)
addJustification(justification)
addFeedLinesIfNeeded(1)
bufferBitmap(configuredBitmap)
}
/**
* Adds the command to print a raster image to the buffer. The parameters of the command are:<p>
*
* m xL xH yL yH d1..dk
*
* m Sets optional scaling parameters (we're not gonna use it).
*
* xL Is how many bytes wide our raster image will be.
*
* xH Does essentially the same thing and we won't use it.
*
* xY Is how how many bits (not bytes!) tall our raster image will be.
*
* d1..dk Is a bunch of bits representing the image itself. 0 for white and 1 for black.
*
* There are more details about how a bitmap is converted into a byte array in the Kdoc for
* [StMicroPrinterHelper.decodeBitmap].
*/
private fun bufferBitmap(bitmap: Bitmap) {
val hexString = StringBuilder()
.append(AsciiControlCodes.PRINT_BITMAP)
.append(getHex(0)) // No scaling
.append(getHex(getPaddedWidth(bitmap.width) / 8))// xL = Width after padding / 8;
.append(getHex(0)) // xH = 0
.append(getHex(bitmap.height)) // yL = height
.append(getHex(0)) // yH = 0
.toString()
buffer(getByteArray(hexString, bitmap))
}
private fun addBarcode(type: BarcodeType, content: String, justification: Justification) {
val contentTextSize = 23
var width = contentTextSize * content.length * 2
if (width > (device?.printerSpecs?.pageWidth ?: 576)) {
width = (device?.printerSpecs?.pageWidth ?: 576) - 100
}
val height = 80
val bitmap = BarcodeHelper
.getBarcode(type, content, width, height, false, contentTextSize)
addJustification(justification)
addFeedLinesIfNeeded(1)
bufferBitmap(bitmap)
}
private fun addDivider(height: Int) {
val pageWidth = device?.printerSpecs?.pageWidth ?: 576
val bitmap = Bitmap.createBitmap(pageWidth, height, Bitmap.Config.RGB_565)
bitmap.eraseColor(Color.BLACK)
addFeedLinesIfNeeded(2)
bufferBitmap(bitmap)
}
private fun addUnderscoreDivider(
textFont: TextFont,
textSize: TextSize,
textStyle: TextStyle
) {
val maxWidth = getMaxCharWidth(textFont, textSize)
setLineSpacing(getTextLineHeight(textFont, textSize))
val text = String(CharArray(maxWidth)).replace("\u0000", "_")
addFeedLinesIfNeeded(1)
addJustification(Center)
addTextFont(textFont)
addTextSize(textSize)
addTextStyle(textStyle)
buffer(getByteArray(getHex(text)))
}
private fun addQrcode(content: String, justification: Justification) {
var size = (device?.printerSpecs?.pageWidth ?: 576) / 3
// The manual says the maximum height in pixels is 255 but in practice it fails to print
// anything larger than 192 :(
if (size > 192) {
size = 192
}
val bitmap = BarcodeHelper.getQrCode(content, size)
addBitmap(bitmap, justification)
}
private fun disconnect() {
connection?.releaseInterface(usbInterface)
connection?.close()
connection = null
usbInterface = null
outputEndpoint = null
}
}