We program currency symbols for the customer display

  • Tutorial
I offer a little guidance to those who, like Squier , the author of the article Automating a Store or How to Compete with Supermarkets , do automation in a store using free software. As an illustrative example, I will tell and show how, by connecting the customer’s display to the Raspberry Pi, load currency symbols on it for subsequent withdrawal during sales.





Connection


Unlike Windows, where drivers are needed, Linux is simple, if you have a display with a USB interface, you just need to connect it to the Raspberry Pi, after which the new device will be available through /dev/ttyUSB0. At the same time, the customer’s display acts as a slave, and the control protocol programmed in the controller of the display itself is used to control and display information on the display screen. There are several different communication protocols for controlling the customer’s display, but de facto they are all based on the ESC / POS command format from Epson.

To get started, initialize the customer display by clearing the memory and display screen with the command ESC @:
$ echo -n -e \\x1B\\x40 > /dev/ttyUSB0

And the standard check:
$ echo -n -e Hello, world!\\n > /dev/ttyUSB0

If everything is done correctly, the line will be displayed.

Display


For indication in customer displays, a VFD or LCD panel is usually used, containing 2 lines of 20 characters each. Each character, depending on the display model, may consist of a matrix of 5 * 7 to 8 * 8 points. For this example, a display with a character size of 5 dots horizontally and 7 dots vertically is used.

The characters are encoded in the ROM of the display and are available for output in the range from 0x20 to 0xFF bytes. And if the range is from 0x20 to 0x7F, this is the standard ANSI code page, then the range from 0x80 to 0xFF is allocated for additional code pages, for example Cyrillic. To switch code pages, use the command ESC t nwhere n, where is the code page number. Example:
$ echo -n -e \\x1B\\x74\\x07 > /dev/ttyUSB0
$ echo -n -e \\x87\\xA4\\xE0\\xA0\\xA2\\xE1\\xE2\\xA2\\xE3\\xA9\\x2C\\x20\\x95\\xA0\\xA1\\xE0\\x21\\n > /dev/ttyUSB0



Painting


It’s good if all the characters are already programmed into displays, but very often, having bought a display in China, one has to deal with the fact that the ROM is completely filled with hieroglyphs, and there is no place left for the Cyrillic alphabet. Then you can use a special command ESC & s n m [a (p1..pa)]*m-nto load the missing characters in the RAM display. This command is also suitable for us for loading currency symbols into memory for subsequent display on sales. The character programming command ESC &contains arguments that can be divided into two parts, the first ones are constants, the second are variables.

The constant part consists of three arguments s, nand m, where: s- the number of bytes in the row of the matrix is ​​characters, in our example the value is 1;n — байт места в кодовой странице для программирования первого символа; m — байт места для программирования последнего символа, и если программируется один символ, то n=m.

Переменная часть состоит из блоков a p1...pa, где количество блоков зависит от количества программируемых символов. Каждый блок начинается с значения a, это количества вертикальных столбцов в матрице символа, в нашем примере оно может быть в диапазон от 1 до 5. После значения a идут по-байтно закодированные в p1, p2, p3, p4 и p5 значения состояния диодов каждого столбца матрицы символа, где старший бит верхняя строка диодов, а младший соответственно нижняя.

This year, the symbol of the Russian ruble (₽, code 20BD) was officially included in Unicode, but 5 years earlier, the symbol of the Kazakhstani tenge, which was dear to me since November 1993 (ной, code 20B8), was adopted. So let's start programming with it. If you represent the tenge symbol in the form of a 5 * 8 bit matrix, then it will look like the one shown in the figure below. The top line is supplemented by the high 8th bit, but in this example it will not be displayed.

As a result, to replace the dollar symbols (code 0x26 in the symbol table of our display) with the tenge symbol with the same code, we need an ESC / POS command of the following form:
$ echo -n -e \\x1b\\x26\\x01\\x24\\x24\\x05\\x50\\x50\\x5f\\x50\\x50 > /dev/ttyUSB0

We include loaded characters with the command ESC % n, where the value is none or zero, depending on whether or not the loaded characters are included:
$ echo -n -e \\x1b\\x25\\x01 > /dev/ttyUSB0

Check the work:
$ echo -n -e 1500$ > /dev/ttyUSB0

Result on the screen:


Programming


Now it remains to make the initialization of the Cyrillic alphabet and loading of the tenge symbol automatic when connected. To do this, we need to set the udevrule to automatically run the bash script when the customer display is connected to the Raspberry Pi. The team will udevadmfind out the vendor code and model of our display.
$ udevadm info -q all -n /dev/ttyUSB0  | grep -E -i -w '.*VENDOR_ID.*|.*MODEL_ID.*'

E: ID_MODEL_ID=2303
E: ID_VENDOR_ID=067b

Create rules 90-init-vfd-display.rules:
$ sudo nano /etc/udev/rules.d/90-init-vfd-display.rules

, the following content:
ACTION=="add", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", RUN+="/usr/local/bin/init-vfd-display.sh"

and script:
$ sudo nano /usr/local/bin/init-vfd-display.sh

#!/bin/bash
sleep 20
echo -n -e \\x1B\\x40 > /dev/ttyUSB0
echo -n -e \\x1B\\x74\\x07 > /dev/ttyUSB0
echo -n -e \\x1B\\x26\\x01\\x24\\x24\\x05\\x50\\x50\\x5F\\x50\\x50 > /dev/ttyUSB0
sleep 3

We make the bash script executable and activate our rules:
$ sudo chmod +x /usr/local/bin/init-vfd-display.sh
$ sudo udevadm control --reload-rules
$ sudo udevadm trigger --action=add
$ sudo service udev restart

We try to use the dollar and tenge symbols at the same time, switching them one by one.
$ echo -n -e 1$ =\\x1B\\x25\\x01 181,95$ \\x1B\\x25\\x00 > /dev/ttyUSB0



Conclusion


The example given here is not necessary to execute using the Raspberry Pi, all the described commands are available regardless of the Linux distribution, the main thing is the presence in it of a service udevfor automatically executing a script loading characters into the customer’s display. As a fixing material, I propose to program the symbols of two more currencies from the Unicode table, the Russian ruble and the Ukrainian hryvnia, according to the matrices in the figures, but you can already make the euro sign yourself.

Also popular now: