Commit bbf5ce66 authored by Andrey's avatar Andrey

Added printCol function.

parent f8ddcfa7
......@@ -7,4 +7,5 @@
library esc_pos_printer;
export './src/enums.dart';
export './src/exceptions.dart';
export './src/printer.dart';
......@@ -31,3 +31,20 @@ class PosBeepDuration {
static const beep400ms = PosBeepDuration._internal(8);
static const beep450ms = PosBeepDuration._internal(9);
}
// class PosCol {
// const PosCol._internal(this.value);
// final int value;
// static const col1 = PosCol._internal(1);
// static const col2 = PosCol._internal(2);
// static const col3 = PosCol._internal(3);
// static const col4 = PosCol._internal(4);
// static const col5 = PosCol._internal(5);
// static const col6 = PosCol._internal(6);
// static const col7 = PosCol._internal(7);
// static const col8 = PosCol._internal(8);
// static const col9 = PosCol._internal(9);
// static const col10 = PosCol._internal(10);
// static const col11 = PosCol._internal(11);
// static const col12 = PosCol._internal(12);
// }
class PosRowException implements Exception {
PosRowException(this._msg);
String _msg;
@override
String toString() => 'PosRowException: $_msg';
}
import 'dart:io';
import 'dart:typed_data';
import 'package:hex/hex.dart';
import 'commands.dart';
import 'enums.dart';
import 'exceptions.dart';
/// Printer.
class Printer {
......@@ -57,6 +59,36 @@ class Printer {
reset();
}
void printRow(List<int> cols, List<String> data) {
final validRange = cols.every((val) => val >= 1 && val <= 12);
if (!validRange) {
throw PosRowException('Column width should be between 1..12');
}
final validSum = cols.fold(0, (int sum, cur) => sum + cur) == 12;
if (!validSum) {
throw PosRowException('Total columns width must be equal 12');
}
if (cols.length != data.length) {
throw PosRowException("Columns number doesn't equal to data number");
}
for (int i = 0; i < cols.length; ++i) {
final colInd = cols.sublist(0, i).fold(0, (int sum, cur) => sum + cur);
_printCol(colInd, data[i]);
}
_socket.writeln();
}
void _printCol(int i, String data) {
final int pos = i == 0 ? 0 : (512 * i / 11 - 1).round();
final hexStr = pos.toRadixString(16).padLeft(3, '0');
final hexPair = HEX.decode(hexStr);
// print('dec: $pos \t hex: $hexStr \t pair $hexPair');
_socket.add(Uint8List.fromList([0x1b, 0x24, hexPair[1], hexPair[0]]));
_socket.write(data);
}
void beep(
{int count = 3, PosBeepDuration duration = PosBeepDuration.beep450ms}) {
if (count <= 0) {
......
......@@ -10,6 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
hex: ^0.1.2
dev_dependencies:
flutter_test:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment