# Introduction

@cutos/device-tspl-printer is a JavaScript library that provides a unified interface for accessing and controlling printers
that support the TSPL command set.
Developers can use this interface to send and receive data, configure printer parameters, and handle printer events.

# TSPL Printer SDK

### Installation

`
npm install @cutos/core
`

`
npm install @cutos/device-tspl-printer
`

### Import dependencies

```js
import {CoreAPI} from '@cutos/core';
import {DeviceTSPLPrinter} from '@cutos/device-tspl-printer';
```

### DeviceTSPLPrinter

Constructor, create TSPL printer device instance

```js
let devTSPLPrinter = new DeviceTSPLPrinter(name);
```

* name: TSPL Printer device name

##### Example:

```js
devTSPLPrinter = new DeviceTSPLPrinter('device-tspl-printer');
```

### DeviceTSPLPrinter.init

TSPL Printer device initialization

```js
devTSPLPrinter.init(callback);
```

* callback: callback function

##### Example:

```js
devTSPLPrinter.init((result, error) => {
	if (!error) {
		console.log('onDeviceCreate', result)
	} else {
		console.log(error)
	}
});
```

### DeviceTSPLPrinter.connect

Connect TSPL Printer

```js
devTSPLPrinter.connect(path, callback);
```

* path: device port
* callback: callback function

##### Example:

```js
 devTSPLPrinter.connect('/ttyS1', (response) => {
	console.log(response)
})
```

- Return response example:

```json
{
  "status": true,
  "msg": "already connected"
}
```

### DeviceTSPLPrinter.setLabelSize

Set the label size (width and height) for printing

```js
devTSPLPrinter.setLabelSize(width, height);
```

* width: label width
* height: label height

### DeviceTSPLPrinter.setLabelGap

Configures the gap between consecutive labels (for gap-sensing printers)

```js
devTSPLPrinter.setLabelGap(gap);
```

* gap: label gap,unit:mm

### DeviceTSPLPrinter.setDirection

Defines the printout direction and mirror image.

```js
devTSPLPrinter.setDirection(direction, mirror)
```

* direction: Feed Direction 0 or 1
* mirror: 0:Print normal image;1: Print mirror image

##### Example:

```js
devTSPLPrinter.setDirection(0, 0)
```

### DeviceTSPLPrinter.setClearBuff

Clears the printer's command buffer

```js
devTSPLPrinter.setClearBuff()
```

### DeviceTSPLPrinter.addBox

Draws a rectangle/box on the label.

```js
devTSPLPrinter.addBox(x, y, width, height, thick)
```

* x:Specify x-coordinate of upper left corner (in dots)
* y:Specify y-coordinate of upper left corner (in dots)
* width:Specify x-coordinate of lower right corner (in dots)
* height:Specify y-coordinate of lower right corner (in dots)
* thick:Line thickness (in dots)

##### Example:

```js
devTSPLPrinter.addBox(0, 0, 46 * 8, 37 * 8, 8)
```

### DeviceTSPLPrinter.addText

```js
devTSPLPrinter.addText(text, x, y)
```

* text: prints text on label
* x:The x-coordinate of the text
* y:The y-coordinate of the text

##### Example:

```js
devTSPLPrinter.addText("hello world", 64, 224)
```

### DeviceTSPLPrinter.addQRCode

```js
devTSPLPrinter.addQRCode(content, x, y)
```

* content: QR code content
* x:The upper left corner x-coordinate of the QR code
* y:The upper left corner y-coordinate of the QR code

##### Example:

```js
devTSPLPrinter.addQRCode('POWERED BY CUTOS', 136, 50)
```

### DeviceTSPLPrinter.print

Print content

```js
devTSPLPrinter.print(callback)
```

* callback: callback function

##### Example:

```js
devTSPLPrinter.print((result, error) => {
	console.log(result, error)
})
```
