A Python interface to a large-format pen plotter

The preview Function

The preview() function is used to see the HPGL file onscreen before committing it to paper. Line 186 initializes the Pygame library, then line 187 creates a Pygame window. The resolution is hard-coded to match /boot/config.txt on the Rasp Pi. Line 188 calls the visual function on the currently selected file and passes a reference to the Pygame window, screen. Line 189 flips the display so that the drawing performed by visual is seen on the screen.

The plot Function

At this point, everything that has been set up actually gets sent to the plotter. Two loops are set up (lines 192-193) that loop over the file list of loaded HPGL files and over the commands in each file.

Line 194 checks whether the command is PU (move with pen up) or PD (move with pen down – draw). These commands make use of the command class's x and y variables. Line 195 reformulates the command string and writes it to the serial port opened for the plotter. If this is not a PU or PD command, then the entire command is stored in the command class's cmd variable so that it is sent to the plotter followed by a ; separator. Next, lines 199-204 update the screen. This is the same set of commands used in the menu() loop, but different variables feed the offsets so that it reflects plot progress, not user navigation.

Lines 207-222 monitor the plotter's buffer space. The plotter can hold 1,024 characters before it has to start ignoring/discarding data. Line 207 calls the plotter's getBuffer() command, which returns how many characters remain in the buffer.

Line 208 calculates the percentage of the buffer that remains and uses that percentage to determine how many characters to draw on the screen as a bar graph (line 209). Line 210 actually draws the graph, then line 211 uses clrtoeol() to "clear to end of line." Line 212 refreshes the screen so that the updated status is visible. At line 214, the program checks whether the buffer is too low to continue plotting – I chose 64 characters as a hold point. Lines 215-220 are the same as lines 207-212, then the program pauses for half a second (line 222) before checking the buffer again. If the available space has gone above 64 characters, the loop will resume sending commands to the plotter.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content