Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MultiMote committed Nov 12, 2024
1 parent d1a9ce6 commit 6f4b4fc
Show file tree
Hide file tree
Showing 26 changed files with 221 additions and 40 deletions.
18 changes: 11 additions & 7 deletions docs/documents/niimbot_hardware_interfacing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
title: NIIMBOT hardware interfacing
---

# Hardware interfacing
# NIIMBOT hardware interfacing

## Bluetooth

NIIMBOT printers have two bluetooth addresses.

In case of D110 :

* `26:03:03:c3:f9:11` - low energy
* `26:03:03:C3:F9:11` - low energy
* `03:26:03:C3:F9:11` - classic

### Bluetooth Low Energy
Expand All @@ -19,7 +19,7 @@ You can interact with printer through a specific BLE characteristic.
To find what characteristic is suitable for this:

1. Find services which have UUID length > 4.
2. Find characteristic in these services which have `NOTIFY` and `WRITE_WITHOUT_RESPONSE` properties.
2. Find characteristic in these services which have `NOTIFY` and `WRITE_NO_RESPONSE` properties.

![](proto/characteristic.png)

Expand All @@ -32,10 +32,14 @@ To send data, write a value without response.

### Bluetooth Classic

Use bluetooth serial.
Use bluetooth serial. The only problem is that packets may be fragmented.

## Serial (USB)
For example, packet `5555d9091f90044c000001000016aaaa` can be received as `5555d9091f90044c000001000016` `aaaa`.

Packet format is same as Bluetooth. The only problem is that packets may be fragmented.
Android [Serial Bluetooth Terminal](https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal) test:

For example, packet `5555d9091f90044c000001000016aaaa` can be received as `5555d9091f90044c000001000016` `aaaa`.
![](proto/bluetooh_terminal.jpg)

## Serial (USB)

Packet format is same as Bluetooth. Packets may be fragmented.
7 changes: 7 additions & 0 deletions docs/documents/niimbot_proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ WIP
| 0xdc | Heartbeat | 0xde, 0xdf, 0xdd, 0xd9 |
| 0xe3 | PageEnd | 0xe4 |
| 0xf3 | PrintEnd | 0xf4 |

## Packets example

* `55 55 40 01 0b 4a aa aa` - get device serial number
* `55 55 1a 01 01 1a aa aa` - get rfid data
* `55 55 58 03 01 01 01 5a aa aa` - enable Bluetooth connection sound
* `55 55 58 03 01 01 00 5b aa aa` - disable Bluetooth connection sound
Binary file added docs/documents/proto/bluetooh_terminal.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion src/client/bluetooth_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class BleConfiguration {
];
}

/** Uses Web Bluetooth API */
/**
* Uses [Web Bluetooth API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API)
*
* @category Client
*/
export class NiimbotBluetoothClient extends NiimbotAbstractClient {
private gattServer?: BluetoothRemoteGATTServer = undefined;
private channel?: BluetoothRemoteGATTCharacteristic = undefined;
Expand Down
6 changes: 6 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { findPrintTask, PrintTaskName } from "../print_tasks";

/**
* Represents the connection result information.
*
* @category Client
*/
export type ConnectionInfo = {
deviceName?: string;
Expand All @@ -22,6 +24,8 @@ export type ConnectionInfo = {

/**
* Interface representing printer information.
*
* @category Client
*/
export interface PrinterInfo {
connectResult?: ConnectResult;
Expand All @@ -40,6 +44,8 @@ export interface PrinterInfo {
/**
* Abstract class representing a client with common functionality for interacting with a printer.
* Hardware interface must be defined after extending this class.
*
* @category Client
*/
export abstract class NiimbotAbstractClient extends EventEmitter<ClientEventMap> {
public readonly abstraction: Abstraction;
Expand Down
6 changes: 5 additions & 1 deletion src/client/serial_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { NiimbotPacket } from "../packets/packet";
import { ConnectResult, PrinterErrorCode, PrintError, ResponseCommandId } from "../packets";
import { Utils, Validators } from "../utils";

/** Uses Web Serial API */
/**
* Uses [Web Serial API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API)
*
* @category Client
**/
export class NiimbotSerialClient extends NiimbotAbstractClient {
private port?: SerialPort = undefined;
private writer?: WritableStreamDefaultWriter<Uint8Array> = undefined;
Expand Down
59 changes: 48 additions & 11 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ConnectionInfo, PrinterInfo, NiimbotAbstractClient, AbstractPrintTask, HeartbeatData, NiimbotPacket } from ".";

/** Base client event */
/**
* Base client event
* @category Events
*/
export class NiimbotEvent {
readonly type: string;

Expand All @@ -9,7 +12,10 @@ export class NiimbotEvent {
}
}

/** Fired when client connected to printer and fetched it's information. */
/**
* Fired when client connected to printer and fetched it's information.
* @category Events
*/
export class ConnectEvent extends NiimbotEvent {
info: ConnectionInfo;
constructor(info: ConnectionInfo) {
Expand All @@ -18,14 +24,20 @@ export class ConnectEvent extends NiimbotEvent {
}
}

/** Fired when client disconnected from printer. */
/**
* Fired when client disconnected from printer.
* @category Events
*/
export class DisconnectEvent extends NiimbotEvent {
constructor() {
super("disconnect");
}
}

/** Fired when packet received, converted to object and validated (head, tail, checksum). */
/**
* Fired when packet received, converted to object and validated (head, tail, checksum).
* @category Events
*/
export class PacketReceivedEvent extends NiimbotEvent {
packet: NiimbotPacket;
constructor(packet: NiimbotPacket) {
Expand All @@ -34,7 +46,10 @@ export class PacketReceivedEvent extends NiimbotEvent {
}
}

/** Fired when packet object sent. */
/**
* Fired when packet object sent.
* @category Events
*/
export class PacketSentEvent extends NiimbotEvent {
packet: NiimbotPacket;
constructor(packet: NiimbotPacket) {
Expand All @@ -43,7 +58,10 @@ export class PacketSentEvent extends NiimbotEvent {
}
}

/** Fired when raw packet sent to printer. */
/**
* Fired when raw packet sent to printer.
* @category Events
*/
export class RawPacketSentEvent extends NiimbotEvent {
data: Uint8Array;
constructor(data: Uint8Array) {
Expand All @@ -52,7 +70,10 @@ export class RawPacketSentEvent extends NiimbotEvent {
}
}

/** Fired when raw packet received from printer. */
/**
* Fired when raw packet received from printer.
* @category Events
*/
export class RawPacketReceivedEvent extends NiimbotEvent {
data: Uint8Array;
constructor(data: Uint8Array) {
Expand All @@ -61,7 +82,10 @@ export class RawPacketReceivedEvent extends NiimbotEvent {
}
}

/** Fired when heartbeat packet received and parsed. */
/**
* Fired when heartbeat packet received and parsed.
* @category Events
*/
export class HeartbeatEvent extends NiimbotEvent {
data: HeartbeatData;
constructor(data: HeartbeatData) {
Expand All @@ -70,7 +94,10 @@ export class HeartbeatEvent extends NiimbotEvent {
}
}

/** Fired when no response received after heartbeat packet sent. */
/**
* Fired when no response received after heartbeat packet sent.
* @category Events
*/
export class HeartbeatFailedEvent extends NiimbotEvent {
failedAttempts: number;
constructor(failedAttempts: number) {
Expand All @@ -79,7 +106,10 @@ export class HeartbeatFailedEvent extends NiimbotEvent {
}
}

/** Fired when info fetched from printer (after {@link NiimbotAbstractClient.fetchPrinterInfo} finished). */
/**
* Fired when info fetched from printer (after {@link NiimbotAbstractClient.fetchPrinterInfo} finished).
* @category Events
*/
export class PrinterInfoFetchedEvent extends NiimbotEvent {
info: PrinterInfo;
constructor(info: PrinterInfo) {
Expand All @@ -88,7 +118,10 @@ export class PrinterInfoFetchedEvent extends NiimbotEvent {
}
}

/** Fired progress received (during {@link AbstractPrintTask.waitForFinished}). */
/**
* Fired progress received (during {@link AbstractPrintTask.waitForFinished}).
* @category Events
*/
export class PrintProgressEvent extends NiimbotEvent {
/** 0 – n */
page: number;
Expand All @@ -108,6 +141,10 @@ export class PrintProgressEvent extends NiimbotEvent {
}
}

/**
* Event list for {@link NiimbotAbstractClient}.
* @category Events
*/
export type ClientEventMap = {
connect: (event: ConnectEvent) => void;
disconnect: (event: DisconnectEvent) => void;
Expand Down
7 changes: 7 additions & 0 deletions src/image_encoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Utils } from ".";

/** @category Image encoder */
export type ImageRow = {
dataType: "void" | "pixels";
rowNumber: number;
Expand All @@ -8,14 +9,20 @@ export type ImageRow = {
rowData?: Uint8Array;
};

/** @category Image encoder */
export type EncodedImage = {
cols: number;
rows: number;
rowsData: ImageRow[];
};

/** @category Image encoder */
export type PrintDirection = "left" | "top";

/**
* @category Helpers
* @category Image encoder
*/
export class ImageEncoder {
/** printDirection = "left" rotates image for 90 degrees clockwise */
public static encodeCanvas(canvas: HTMLCanvasElement, printDirection: PrintDirection = "left"): EncodedImage {
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @module API
*/

export * from "./client";
export * from "./packets";
export * from "./events";
Expand Down
26 changes: 23 additions & 3 deletions src/packets/abstraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { SequentialDataReader } from "./data_reader";
import { NiimbotPacket } from "./packet";
import { PacketGenerator } from "./packet_generator";

/**
* @category Packets
*/
export class PrintError extends Error {
public readonly reasonId: number;

Expand All @@ -27,6 +30,9 @@ export class PrintError extends Error {
}
}

/**
* @category Packets
*/
export interface PrintStatus {
/** 0 – n */
page: number;
Expand All @@ -35,7 +41,9 @@ export interface PrintStatus {
/** 0 – 100 */
pageFeedProgress: number;
}

/**
* @category Packets
*/
export interface RfidInfo {
tagPresent: boolean;
uuid: string;
Expand All @@ -46,26 +54,38 @@ export interface RfidInfo {
consumablesType: LabelType;
}

/** closingState inverted on some printers */
/**
* @category Packets
**/
export interface HeartbeatData {
paperState: number;
rfidReadState: number;
lidClosed: boolean;
powerLevel: BatteryChargeLevel;
}

/**
* @category Packets
*/
export interface SoundSettings {
category: SoundSettingsType;
item: SoundSettingsItemType;
value: boolean;
}

/**
* @category Packets
*/
export interface PrinterStatusData {
supportColor: number;
protocolVersion: number;
}

/** Not sure for name. */
/**
* Packet sender and parser.
*
* @category Packets
*/
export class Abstraction {
private readonly DEFAULT_PACKET_TIMEOUT: number = 1_000;
private client: NiimbotAbstractClient;
Expand Down
18 changes: 15 additions & 3 deletions src/packets/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/** Commands IDs from client to printer */
/**
* Commands IDs from client to printer
*
* @category Packets
**/
export enum RequestCommandId {
Invalid = -1,
/** Entire packet should be prefixed with 0x03 */
Expand Down Expand Up @@ -39,7 +43,11 @@ export enum RequestCommandId {
PrintTestPage = 0x5a,
}

/** Commands IDs from printer to client */
/**
* Commands IDs from printer to client
*
* @category Packets
**/
export enum ResponseCommandId {
Invalid = -1,
In_NotSupported = 0x00,
Expand Down Expand Up @@ -95,7 +103,11 @@ export enum ResponseCommandId {
import TX = RequestCommandId;
import RX = ResponseCommandId;

/** Map request id to response id. null meant no response expected (one way). */
/**
* Map request id to response id. null meant no response expected (one way).
*
* @category Packets
**/
export const commandsMap: Record<RequestCommandId, ResponseCommandId[] | null> = {
[TX.Invalid]: null,
[TX.PrintBitmapRow]: null,
Expand Down
Loading

0 comments on commit 6f4b4fc

Please sign in to comment.