Wiki

Clone wiki

Pipsta / Christmas Banner

Difficulty Level

pipsta_mono.pngpipsta_mono_empty.pngpipsta_mono_empty.pngpipsta_mono_empty.pngpipsta_mono_empty.png

• No Linux or Raspberry Pi knowledge is required

• A fully assembled and set-up Pipsta and an internet connection are required

• The script presented is essentially a variant of the previously released banner.py

• The section How it Works gives an optional insight into the script's functionality for those wishing to deep-dive

Time to Complete

pipsta_mono.pngpipsta_mono_empty.pngpipsta_mono_empty.pngpipsta_mono_empty.pngpipsta_mono_empty.png

• The tutorial covers the operation of the script in overview

Overview

Pipsta banners make use of the Python Imaging Library to create an image from a font. This project uses an intricate and beautiful Christmas font created by Kevin King to create festive and fun decorations.

Pre-requisites

This project assumes you have a fully working and configured Pipsta. There are wiki guides on how to assemble and set-up your Pipsta.

Step-by-Step Guide

1) If you haven't done so already, create a new directory under '/home/pi/pipsta/Examples' called 'christmas',

2) Download the file '4_ChristmasBanner.zip' from here to the 'christmas' directory,

3) Right-click the file and select 'Extract Here'

4) You should now see:

• File: xmas_banner.py - the script for this project

• File: Kingthings Christmas 2.2.ttf - the font used for the project

• File: kingthingsEULA.txt - the licence for the font

• File: allfontsinfo.txt - an index of fonts created by Kevin King.

5) Once you have navigated to '/home/pi/pipsta/Examples/christmas/4_ChristmasBanner', press F4 to bring up the terminal

6) In the terminal, type:

#!python

python xmas_banner.py

7) This will run the script and produce the default Christmas banner.

8) The script can also take a command-line argument (a variable that can be supplied to the script from the command line) to allow other messages to be printed in this font:

#!python

python xmas_banner.py 'Peace '

(note the space after the word!)

9) This means you can use this script to produce Place Setting Name Cards for your family gatherings over Christmas!

10) Note that --in order to allow the font characters to flow into the next character-- the font needs to know that it has reached the end of a word. Issuing:

#!python

python xmas_banner.py 'Peace'

(with no space after the word)

...will give a truncated (shortened) image:

truncated.png

...whereas suppling a terminator such as a whitespace or punctuation allows the character to be completed properly, in this case with a little robin perched on the end of the 'e':

untruncated.png

How It Works

1) This script works in much the same way as the standard banner project, covered in overview here and described in detail here.

2) The script has two entry points:

  • main() - where the script is run directly from the command line
  • send_to_printer() - where the script is invoked as a module in another script

In this tutorial, we are launching the script from the command line, so we will enter through main()

Note: the bit of code often seen down at the bottom of Python scripts is responsible for allowing these two entry points:

#!python

if __name__ == '__main__':
    main()

If called from the command line, an attribute called 'name' is set to 'main' which steers the script execution (in this case, into main()). If called by other means, i.e. as a module by another script, main() is not executed.

3) Being invoked from main(), the script will have the opportunity to parse_arguments(). In this script, this will allow a single parameter to be supplied which specifies the text to use in the generation of the banner. In a departure from the functionality in the standard banner.py script, we have fixed the font to Kevin King's font --partly because it's so amazing(!)-- but also to simplify the command line.

4) Once armed with the text to be printed, __send_to_printer() is called.

5) __send_to_printer():

  • initialises the USB, looking for the Pipsta Printer and connecting to it,

  • sets the LED flashing, to indicate the Pipsta is processing a print job,

  • scales the font for best fit on the paper, and

  • uses the Python Imaging Library to draw the font into a new image and rotate it so it is oriented along the length of the paper roll

...before calling convert_image()

6) convert_image() works in exactly the same way as the standard banner.py script by:

  • using the Python module bitarray to turn the bitmap image into an array of bits and then pack these bits into bytes.

7) Back in __send_to_printer(), print_image() is then called. This:

  • sets the print darkness (so the image is not over- or under-developed),

  • sets the font mode to 3 (which favours graphics rather than text by ensuring there are no gaps between character lines),

  • packs the byte array of data into dot line commands of data to send to the printer. See the section, Single Dot Line Graphics below for more detail on this graphics command.

Note: The Pipsta printer has a 10KB receive buffer. As the buffer fills with data, the printer goes 'busy' to signal to the Raspberry Pi that sending more data will result in data being lost and the image being corrupted. The latter part of print_image() enquires the busy state of the printer and waits for the printer to be un-busy before sending more data.

8) The script feeds 5 more character lines to present the image for tearing off

9) Finally, the command is issued to stop the LED from flashing, indicating that processing is complete.

Single Dot Line Graphics

The Pipsta printer has a print width of 384 dots, with each dot being 0.125mm wide to give an overall width of 48mm. Note that this leaves a ~5mm unprinted left- and right-margin on (nominal) 58mm paper.

Note: To put the printer into a contiguous graphics mode (i.e. without white-space separation between character lines) the script will issue the SET_FONT_MODE_3 command.

Pipsta scripts that produce graphics prints (rather than pure text) use the printer's "Single Dot Line Graphics" command to render the dot image on the paper. The command to render a complete dot line of 384 dots takes the form:

sdl_cmd.png

Where a '1' bit in the data corresponds to a black dot.

Thus, the function convert_image() simply has to convert the bitmap image to a big-endian bit-array, and then convert this to an array of bytes to make the data sequentially accessible to the nested for-loops in print_image()

[END]

Updated