Skip to content

Commit

Permalink
Added more supported printer models
Browse files Browse the repository at this point in the history
  • Loading branch information
mazinsw committed Jul 24, 2018
1 parent 06db8f9 commit 7dd0ed4
Show file tree
Hide file tree
Showing 34 changed files with 761 additions and 116 deletions.
Binary file added docs/Diebold/Manual-de-comandos-TSP143.pdf
Binary file not shown.
31 changes: 8 additions & 23 deletions src/Thermal/Buffer/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,25 @@ public function __construct($codepage)

public static function getSupportedCodePages()
{
$list = array_flip(mb_list_encodings());
if (isset($list['Windows-1252'])) {
$list['Windows-1252'] = 'CP1252';
}
$list = mb_list_encodings();
return $list;
}

public function setCodePage($codepage)
{
$codes = self::getSupportedCodePages();
if (!isset($codes[$codepage])) {
$codes = array_flip($codes);
if (!isset($codes[$codepage])) {
throw new \Exception(
sprintf('Codepage "%s" not supported by mb_string', $codepage),
404
);
} else {
$this->codepage = $codes[$codepage];
}
} else {
$this->codepage = $codepage;
if (\array_search($codepage, $codes) === false) {
throw new \Exception(
sprintf('Codepage "%s" not supported by mb_string', $codepage),
404
);
}
$this->codepage = $codepage;
return $this;
}

public function getCodePage($printer_code = false)
public function getCodePage()
{
if ($printer_code) {
$codes = self::getSupportedCodePages();
if (\is_string($codes[$this->codepage])) {
return $codes[$this->codepage];
}
}
return $this->codepage;
}

Expand Down
61 changes: 47 additions & 14 deletions src/Thermal/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Thermal;

use Thermal\Profile\Profile;
use Thermal\Profile\Daruma;
use Thermal\Profile\Diebold;
use Thermal\Profile\Elgin;
use Thermal\Profile\EscBema;
use Thermal\Profile\EscPOS;
use Thermal\Profile\Generic;
use Thermal\Profile\Perto;
use Thermal\Profile\Profile;

class Model
{
Expand All @@ -18,33 +23,53 @@ public function __construct($name)
$data = self::loadCapabilities();
if (is_array($name)) {
$capabilities = $name;
$name = isset($capabilities['model']) ? $capabilities['model'] : 'Unknow';
} else {
if (!isset($data['models'][$name])) {
throw new \Exception(sprintf('Printer model "%s" not supported', $name), 404);
}
$capabilities = $data['models'][$name];
$capabilities['model'] = $name;
}
// fill inherited fields
$profile = $capabilities['profile'];
$profile = \is_array($profile) ? $profile : ['inherited' => $profile];
$profile_name = $profile['inherited'];
while (isset($profile['inherited'])) {
$inherited = $profile['inherited'];
unset($profile['inherited']);
$parent = $data['profiles'][$inherited];
$profile = \array_merge($parent, $profile);
}
$capabilities['profile'] = $profile;
$this->profile = self::newProfile($profile_name, $capabilities);
list($profile, $capabilities) = self::expandCapabilities($name, $capabilities, $data);
$this->profile = self::newProfile($profile, $capabilities);
}
}

private static function expandCapabilities($model, $capabilities, $data)
{
$capabilities = \is_array($capabilities) ? $capabilities : ['profile' => $capabilities];
$capabilities['model'] = $model;
// fill inherited fields
$profile = $capabilities['profile'];
while (isset($capabilities['profile'])) {
$inherited = $capabilities['profile'];
unset($capabilities['profile']);
$parent = $data['profiles'][$inherited];
$capabilities = \array_merge($parent, $capabilities);
}
return [$profile, $capabilities];
}

private static function newProfile($profile_name, $capabilities)
{
if ($profile_name == 'escbema') {
return new EscBema($capabilities);
}
if ($profile_name == 'elgin') {
return new Elgin($capabilities);
}
if ($profile_name == 'daruma') {
return new Daruma($capabilities);
}
if ($profile_name == 'diebold') {
return new Diebold($capabilities);
}
if ($profile_name == 'generic') {
return new Generic($capabilities);
}
if ($profile_name == 'perto') {
return new Perto($capabilities);
}
// default profile
return new EscPOS($capabilities);
}
Expand All @@ -57,6 +82,11 @@ private static function loadCapabilities()
public static function getAll()
{
$data = self::loadCapabilities();
foreach ($data['models'] as $model => $capabilities) {
list($profile, $capabilities) = self::expandCapabilities($model, $capabilities, $data);
$data['models'][$model] = $capabilities;
$data['models'][$model]['profile'] = $profile;
}
return $data['models'];
}

Expand All @@ -65,6 +95,9 @@ public function getName()
return $this->profile->getName();
}

/**
* @return \Thermal\Profile\Profile
*/
public function getProfile()
{
return $this->profile;
Expand Down
70 changes: 70 additions & 0 deletions src/Thermal/Profile/Daruma.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Thermal\Profile;

use Thermal\Printer;

class Daruma extends Elgin
{
/**
* @param int $number drawer id
* @param int $on_time time in milliseconds that activate the drawer
* @param int $off_time time in milliseconds that deactivate the drawer
*/
public function drawer($number, $on_time, $off_time)
{
$index = [
Printer::DRAWER_1 => "p"
];
if (!isset($index[$number])) {
throw new \Exception(
sprintf('Drawer %d not available for printer "%s"', $this->getName(), intval($number)),
404
);
}
$this->getConnection()->write("\e" . $index[$number]);
return $this;
}

protected function setAlignment($align)
{
$cmd = [
Printer::ALIGN_LEFT => "\ej0",
Printer::ALIGN_CENTER => "\ej1",
Printer::ALIGN_RIGHT => "\ej2"
];
$this->getConnection()->write($cmd[$align]);
}

protected function setMode($mode, $enable)
{
if ($enable) {
// enable styles
if (Printer::STYLE_DOUBLE_WIDTH & $mode) {
$this->getConnection()->write("\x0E");
}
if (Printer::STYLE_DOUBLE_HEIGHT & $mode) {
$this->getConnection()->write("\ew1");
}
} else {
// disable styles
if (Printer::STYLE_DOUBLE_HEIGHT & $mode) {
$this->getConnection()->write("\ew0");
}
if (Printer::STYLE_DOUBLE_WIDTH & $mode) {
$this->getConnection()->write("\x14");
}
}
return $this;
}

protected function fontChanged($new_font, $old_font)
{
if ($new_font['name'] == 'Font A') {
$this->getConnection()->write("\e!\x00");
} elseif ($new_font['name'] == 'Font B') {
$this->getConnection()->write("\e!\x01");
}
return $this;
}
}
30 changes: 30 additions & 0 deletions src/Thermal/Profile/Diebold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Thermal\Profile;

use Thermal\Printer;

class Diebold extends Elgin
{
/**
* @param int $number drawer id
* @param int $on_time time in milliseconds that activate the drawer
* @param int $off_time time in milliseconds that deactivate the drawer
*/
public function drawer($number, $on_time, $off_time)
{
$index = [
Printer::DRAWER_1 => '0'
];
if (!isset($index[$number])) {
throw new \Exception(
sprintf('Drawer %d not available for printer "%s"', $this->getName(), intval($number)),
404
);
}
$on_time = min((int)($on_time / 2), 65);
$off_time = min((int)($off_time / 2), 65);
$this->getConnection()->write("\e&" . $index[$number] . chr($on_time) . chr($off_time));
return $this;
}
}
84 changes: 84 additions & 0 deletions src/Thermal/Profile/Elgin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Thermal\Profile;

use Thermal\Printer;

class Elgin extends EscPOS
{
public function cutter($mode)
{
if ($mode == Printer::CUT_FULL) {
$this->getConnection()->write("\ew");
return $this;
}
return parent::cutter($mode);
}

public function buzzer()
{
$this->getConnection()->write("\e(A\x04\x00\x01\xFF\x00\xFF");
return $this;
}

/**
* @param int $number drawer id
* @param int $on_time time in milliseconds that activate the drawer
* @param int $off_time time in milliseconds that deactivate the drawer
*/
public function drawer($number, $on_time, $off_time)
{
$index = [
Printer::DRAWER_1 => "v"
];
if (!isset($index[$number])) {
throw new \Exception(
sprintf('Drawer %d not available for printer "%s"', $this->getName(), intval($number)),
404
);
}
$on_time = max(min($on_time, 200), 50);
$this->getConnection()->write("\e" . $index[$number] . chr($on_time));
return $this;
}

protected function setStyle($style, $enable)
{
if ($enable) {
// enable styles
if (Printer::STYLE_BOLD == $style) {
$this->getConnection()->write("\eE");
return $this;
}
} else {
// disable styles
if (Printer::STYLE_BOLD == $style) {
$this->getConnection()->write("\eF");
return $this;
}
}
return parent::setStyle($style, $enable);
}

protected function setMode($mode, $enable)
{
if ($enable) {
// enable styles
if (Printer::STYLE_DOUBLE_WIDTH & $mode) {
$this->getConnection()->write("\eW\x01");
}
if (Printer::STYLE_DOUBLE_HEIGHT & $mode) {
$this->getConnection()->write("\ed\x01");
}
} else {
// disable styles
if (Printer::STYLE_DOUBLE_HEIGHT & $mode) {
$this->getConnection()->write("\ed\x00");
}
if (Printer::STYLE_DOUBLE_WIDTH & $mode) {
$this->getConnection()->write("\eW\x00");
}
}
return $this;
}
}
2 changes: 2 additions & 0 deletions src/Thermal/Profile/EscBema.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function buzzer()

/**
* @param int $number drawer id
* @param int $on_time time in milliseconds that activate the drawer
* @param int $off_time time in milliseconds that deactivate the drawer
*/
public function drawer($number, $on_time, $off_time)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Thermal/Profile/EscPOS.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function cutter($mode)

/**
* @param int $number drawer id
* @param int $on_time time in milliseconds that activate the drawer
* @param int $off_time time in milliseconds that deactivate the drawer
*/
public function drawer($number, $on_time, $off_time)
{
Expand Down
30 changes: 30 additions & 0 deletions src/Thermal/Profile/Generic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Thermal\Profile;

use Thermal\Printer;

class Generic extends EscPOS
{
protected function setMode($mode, $enable)
{
if ($enable) {
// enable styles
if (Printer::STYLE_DOUBLE_WIDTH & $mode) {
$this->getConnection()->write("\x0E");
}
if (Printer::STYLE_DOUBLE_HEIGHT & $mode) {
$this->getConnection()->write("\ed1");
}
} else {
// disable styles
if (Printer::STYLE_DOUBLE_HEIGHT & $mode) {
$this->getConnection()->write("\ed0");
}
if (Printer::STYLE_DOUBLE_WIDTH & $mode) {
$this->getConnection()->write("\x14");
}
}
return $this;
}
}
Loading

0 comments on commit 7dd0ed4

Please sign in to comment.