PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD)...

38
PiFace Control and Display (CAD) Documentation Release 2.0.7 Thomas Preston November 05, 2014

Transcript of PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD)...

Page 1: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD)Documentation

Release 2.0.7

Thomas Preston

November 05, 2014

Page 2: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes
Page 3: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

Contents

1 Installation 31.1 SysInfo Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Examples 52.1 Basic usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 IR Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.3 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3 Included Examples 93.1 Internet Radio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 Hangman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.3 Traintimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.4 Tweets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.5 Weather . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

4 Creating Custom Bitmaps 13

5 Tools 155.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155.2 Scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

6 LIRC 176.1 Setting up the Infrared Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.2 Configuring LIRC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186.3 irexec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196.4 python-lirc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206.5 PiFace CAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

7 Reference (PiFace CAD) 217.1 pifacecad.core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217.2 pifacecad.ir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217.3 pifacecad.lcd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

8 Reference (PiFace CAD Tools) 258.1 pifacecad.tools.question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258.2 pifacecad.tools.scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

9 Indices and tables 29

i

Page 4: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

Python Module Index 31

ii

Page 5: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

The pifacecad Python module provides functions and classes for interacting with PiFace Control and Display.

Note: You may need to fiddle with the contrast settings in order to see characters on the screen. Turn the small screwlocated by the GPIO pins on PiFaceCAD.

PiFace Control and Display has eight inputs switches, and IR receiver and an HD44780 LCD.

Links:

• Blog

• GitHub

• C Library

• Custom Bitmaps Generator

Contents:

Contents 1

Page 6: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

2 Contents

Page 7: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 1

Installation

Make sure you are using the lastest version of Raspbian:

$ sudo apt-get update$ sudo apt-get upgrade

Install pifacecad (for Python 3 and 2) with the following command:

$ sudo apt-get install python{,3}-pifacecad

Test by running the sysinfo.py program:

$ python3 /usr/share/doc/python3-pifacecad/examples/sysinfo.py

You will need to configure the IR receiver yourself. More examples can be found in /usr/share/doc/python3-pifacecad/examples/ (which may need unzipping using gunzip).

1.1 SysInfo Service

You can run sysinfo as a service using:

$ sudo service pifacecadsysinfo start

You can stop the service with:

$ sudo service pifacecadsysinfo stop

You can enable the service to run at boot (handy for headless Raspberry Pi’s):

$ sudo update-rc.d pifacecadsysinfo defaults

3

Page 8: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

4 Chapter 1. Installation

Page 9: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 2

Examples

2.1 Basic usage

Hello, World!:

>>> import pifacecad

>>> cad = pifacecad.PiFaceCAD() # create PiFace Control and Display object>>> cad.lcd.backlight_on() # turns the backlight on>>> cad.lcd.write("Hello, world!") # writes hello world on to the LCD

Reading the switches:

>>> cad.switches[3].value # reads the value of switch 31>>> cad.switch_port.value # reads the value of the switch port4

Cursor control:

>>> cad.lcd.set_cursor(4, 1) # set the cursor to col 4 on the second row>>> cad.lcd.cursor_off() # turns the cursor off>>> cad.lcd.write("3.141592") # writes 𝜋 to the LCD

>>> cad.lcd.blink_off() # turns the blinking off>>> cad.lcd.cursor_on() # turns the cursor on>>> cad.lcd.home() # send the cursor home

>>> cad.lcd.write("PiFace Control\nand Display") # ’\n’ starts a new line>>> cad.lcd.clear() # clear the screen (also sends the cursor home)

The LCD has more RAM than just the 16x2 characters that you can see:

>>> cad.lcd.write("Something really, really long.")>>> cad.lcd.move_right()>>> cad.lcd.move_left()>>> cad.lcd.see_cursor() # move the display so that we can see the cursor

The viewport_corner variable describes which column the top left display character is showing from RAM:

>>> cad.lcd.viewport_corner # inspect the viewport_corner variable8>>> cad.lcd.viewport_corner = 15 # set the viewport_corner variable

You can also create your own custom bitmaps.

5

Page 10: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

2.2 IR Receiver

You need to add your LIRC client (your program) to ~/.lircrc:

$ cat ~/.lircrcbegin

prog = pifacecadexamplebutton = 1config = one

end

beginprog = pifacecadexamplebutton = 2config = two

end

Then, register IR codes to functions using IREventListener:

>>> import pifacecad

>>> def print_ir_code(event):... print(event.ir_code)...>>> pifacecad.PiFaceCAD() # initialise a PiFace Control and Display board>>> listener = pifacecad.IREventListener(prog="pifacecadexample")>>> listener.register(’one’, print_ir_code)>>> listener.register(’two’, print_ir_code)>>> listener.activate()

Now when you press 1 or 2 on your remote, “one” or “two” is printed.

2.3 Interrupts

Instead of polling the switches we can use the SwitchEventListener to register actions that we wish to be calledon certain switch events.

>>> import pifacecad>>> def update_pin_text(event):... event.chip.lcd.set_cursor(13, 0)... event.chip.lcd.write(str(event.pin_num))...>>> cad = pifacecad.PiFaceCAD()>>> cad.lcd.write("You pressed: ")>>> listener = pifacecad.SwitchEventListener(chip=cad)>>> for i in range(8):... listener.register(i, pifacecad.IODIR_FALLING_EDGE, update_pin_text)>>> listener.activate()

The screen should update as buttons are pressed. To stop the listener, call it’s deactivate method:

>>> listener.deactivate()

The Event object has some interesting attributes. You can access them like so:

>>> import pifacecad>>> cad = pifacecad.PiFaceCAD()

6 Chapter 2. Examples

Page 11: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

>>> listener = pifacecad.SwitchEventListener(chip=cad)>>> listener.register(0, pifacecad.IODIR_RISING_EDGE, print)>>> listener.activate()

This would print out the event informaion whenever you unpress switch 0:

interrupt_flag: 0b1interrupt_capture: 0b11111111pin_num: 0direction: 1chip: <pifacecad.core.PiFaceCAD object at 0xb682dab0>timestamp: 1380893579.447889

2.3. Interrupts 7

Page 12: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

8 Chapter 2. Examples

Page 13: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 3

Included Examples

The PiFace Control and Display Python libraries include some example programs at/usr/share/doc/python3-pifacecad/examples/. You are encouraged to modify and improvethem.

You can also find the examples on GitHub.

Note: Some of the included examples are zipped (.gz) and require unzipping before they can be used.

3.1 Internet Radio

An internet radio with IR remote control features.

Note: In order to use the infrared remote control features of the radio you must first configure LIRC and configurethe buttons in /usr/share/doc/python3-pifacecad/examples/radiolircrc so that they match thebuttons on your remote (in /etc/lirc/lircd.conf).

The internet radio requires the program mplayer to be installed in order to play audio streams. Install it with:

$ sudo apt-get install mplayer

Unzip the radio:

$ gunzip /usr/share/doc/python3-pifacecad/examples/radio.py.gz

Run the radio:

$ python3 /usr/share/doc/python3-pifacecad/examples/radio.py

Button FunctionNavigation left Previous stationNavigation right Next stationNavigation in Stop/Start playingControl 0 Station Preset 0Control 1 Station Preset 1Control 2 Station Preset 2Control 3 Station Preset 3Control 4 Exit

9

Page 14: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

3.2 Hangman

A game of hangman. Guess which letters are in the word. To many incorrect guesses and, well, you know the rest.

Unzip and run hangman:

$ gunzip /usr/share/doc/python3-pifacecad/examples/radio.py.gz$ python3 /usr/share/doc/python3-pifacecad/examples/radio.py

Button FunctionNavigation left Move cursor/Change letterNavigation right Move cursor/Change letterNavigation in Change mode/enter

See hangman in action on YouTube.

3.3 Traintimes

Shows UK train times from Manchester Picadilly (MAN) to various other stations. Have a peek at the code to the alterwhich station you are departing from.

traintimes.py depends on Beautiful Soup 4. Install it with:

$ sudo apt-get install python3-bs4

Then unzip and run traintimes:

$ gunzip /usr/share/doc/python3-pifacecad/examples/traintimes.py.gz$ python3 /usr/share/doc/python3-pifacecad/examples/traintimes.py

Button FunctionNavigation left Previous destination stationNavigation right Next destination stationNavigation in RefreshControl 4 Exit

3.4 Tweets

Shows latest tweets.

Twitter requires the Python Twitter Tools module to be installed:

$ sudo apt-get install easy_install3$ sudo easy_install3 twitter

Unzip and run tweets:

$ gunzip /usr/share/doc/python3-pifacecad/examples/tweets.py.gz$ python3 /usr/share/doc/python3-pifacecad/examples/tweets.py

Button FunctionNavigation left Previous tweetNavigation right Next tweetNavigation in RefreshControl 4 Exit

10 Chapter 3. Included Examples

Page 15: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

3.5 Weather

Shows current weather information. Unzip and run weather:

$ gunzip /usr/share/doc/python3-pifacecad/examples/weather.py.gz$ python3 /usr/share/doc/python3-pifacecad/examples/weather.py

Button FunctionNavigation left Previous locationNavigation right Next locationNavigation in RefreshControl 4 Exit

3.5. Weather 11

Page 16: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

12 Chapter 3. Included Examples

Page 17: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 4

Creating Custom Bitmaps

You can store up to eight custom bitmaps using PiFace Control and Display. Each bitmap describes a single characteron the LCD screen (5x8 pixels).

To create a custom bitmap you instantiate an LCDBitmap which takes a list of binary values each describing a line.For example:

>>> quaver = pifacecad.LCDBitmap([0, 2, 3, 2, 14, 30, 12, 0])

or as hex:

>>> quaver = pifacecad.LCDBitmap([0x0, 0x2, 0x3, 0x2, 0xe, 0x1e, 0xc, 0x0])

or as binary (you can almost see the bitmap image here):

>>> quaver = pifacecad.LCDBitmap([0b00000,... 0b00010,... 0b00011,... 0b00010,... 0b01110,... 0b11110,... 0b01100,... 0b00000])

Note: You can use this tool to help design custom bitmaps. Make sure you select 5x8.

You store and display custom bitmaps with the following commands:

>>> cad = pifacecad.PiFaceCAD()>>> cad.lcd.store_custom_bitmap(0, quaver)>>> cad.lcd.write_custom_bitmap(0)

Here is a complete code example:

>>> import pifacecad>>> cad = pifacecad.PiFaceCAD()>>> quaver = pifacecad.LCDBitmap([0x0, 0x2, 0x3, 0x2, 0xe, 0x1e, 0xc, 0x0])>>> cad.lcd.store_custom_bitmap(0, quaver)>>> cad.lcd.write_custom_bitmap(0)

13

Page 18: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

14 Chapter 4. Creating Custom Bitmaps

Page 19: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 5

Tools

There are some tools provided with PiFace Control and Display which simplify getting input from a user.

5.1 Question

pifacecad.tools.question.LCDQuestion will display a question on the top row and an answer on thebottom. The user can cycle through answers by moving the navigation switch left and right and select an answer bypressing the navigation switch in.

It can be used like this:

>>> import pifacecad>>> from pifacecad.tools.question import LCDQuestion

>>> question = LCDQuestion(question="What is 5 x 2?", answers=["1", "10", "12"])>>> answer_index = question.ask()

You can pass in an already initialised Control and Display object and also specify a custom selector:

>>> import pifacecad>>> from pifacecad.tools.question import LCDQuestion

>>> customcad = pifacecad.PiFaceCAD()>>> customcad.lcd.cursor_off()>>> customcad.lcd.blink_off()

>>> question = LCDQuestion(question="What is 5 x 2?",... answers=["1", "10", "12"],... selector="#",... cad=customcad)>>> answer_index = question.ask()

5.2 Scanf

pifacecad.tools.scanf.LCDScanf will display a custom string that can be modified using the navigationswitch. In ‘select’ mode, moving the navigation switch left or right (switches 6 and 7) selects a character. Change to‘edit’ mode by pressing the navigation switch in (switch 5). In edit mode, moving the navigation switch left or rightchanges the character.

To return the input, move the cursor under the ‘enter’ arrow and press the navigation switch in.

15

Page 20: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

The LCDScanf class can be used like this:

>>> import pifacecad>>> from pifacecad.tools.scanf import LCDScanf

>>> scanner = LCDScanf("Text: %c%2i%.%r")>>> print(scanner.scan()) # user enters things on PiFace C&D[’a’, ’13’, ’!’]

The format string defines editable variables using a % symbol. The format specification is:

c: CharactersC: Capital Charactersi: Integersd: Integersx: HexadecimalX: Capital Hexadecimal.: Punctuationm: Custom (specifed by ‘‘custom_values‘‘ in init args)r: Return (switch 5 to submit string)

To add multiple characters in the same variable you can specify a number after the % symbol. For example, to requestthat the user enter a two digit integer:

>>> print(LCDScanf("%2i%r").scan())[’42’]

You can use the m specifier to enter custom variable values:

>>> scanner = LCDScanf("Animal: %m%r", custom_values=(’cat’, ’dog’, ’fish’))>>> print(scanner.scan())[’fish’]

16 Chapter 5. Tools

Page 21: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 6

LIRC

This page contains some information about Linux Infrared Remote Control (LIRC) and how to set it up with PiFaceCAD and the Raspberry Pi.

LIRC looks something like this:

6.1 Setting up the Infrared Receiver

Download and run the setup script:

$ wget https://raw.github.com/piface/pifacecad/master/bin/setup_pifacecad_lirc.sh$ chmod +x setup_pifacecad_lirc.sh$ sudo ./setup_pifacecad_lirc.sh

Alternatively you can set up the receiver manually.

Note: The following instructions are for a manual set up of the above script. If you have run the script above, youcan skip straight to Configuring LIRC.

First, install lirc:

$ sudo apt-get install lirc

The latest version of Raspbian should contain the lirc_rpi kernel module. It allows you to specify which GPIOpin the Infrared Receiver is attached to. On PiFace CAD, the Infrared Receiver is connected to GPIO pin 23. To loadthe module type:

$ sudo modprobe lirc_rpi gpio_in_pin=23

You can test that it works with the mode2 program:

17

Page 22: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

$ sudo kill $(pidof lirc) # stop lirc using /dev/lirc0$ mode2 -d /dev/lirc0

If, after pressing some buttons, you get a series of pulse/space lengths then your Infrared Receiver is working. Tomake sure that the module is loaded each time you boot, add the following lines to /etc/modules:

lirc_devlirc_rpi gpio_in_pin=23

Update /etc/lirc/hardware.conf to contain the following:

# /etc/lirc/hardware.conf## Arguments which will be used when launching lircdLIRCD_ARGS="--uinput"

#Don’t start lircmd even if there seems to be a good config file#START_LIRCMD=false

#Don’t start irexec, even if a good config file seems to exist.#START_IREXEC=false

#Try to load appropriate kernel modulesLOAD_MODULES=true

# Run "lircd --driver=help" for a list of supported drivers.DRIVER="default"# usually /dev/lirc0 is the correct setting for systems using udevDEVICE="/dev/lirc0"MODULES="lirc_rpi"

# Default configuration files for your hardware if anyLIRCD_CONF=""LIRCMD_CONF=""

Finally, reboot your Rasbperry Pi:

$ sudo reboot

6.2 Configuring LIRC

See also: http://www.lirc.org/html/configure.html

6.2.1 lircd.conf

The /etc/lirc/lircd.conf file tells LIRC about your remote control. Since every remote control is different,you need to generate a different config for each remote. Alternatively you could try and find your remote controlconfig file here: http://lirc.sourceforge.net/remotes/.

To generate your own configuration run:

$ sudo irrecord -f -d /dev/lirc0 /etc/lirc/lircd.conf

and carefully follow the on-screen instructions. At some point it will ask you to enter the commands for each buttonyou press. You can list the available commands (in another terminal) with:

18 Chapter 6. LIRC

Page 23: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

$ irrecord --list-namespace

After you have finished, restart the lirc daemon (or reboot) and test your remote by running:

$ irw

Your commands should appear in the console.

6.2.2 ~/.lircrc (or /etc/lirc/lircrc)

The ~/.lircrc file is used to configure what other programs see from LIRC. See examples in irexec and python-lirc.

See also: http://www.lirc.org/html/configure.html#lircrc_format

6.3 irexec

irexec is a program that runs commands mapped to IR signals. It is configured using the ~/.lircrc file.

Simple ~/.lircrc:

beginprog = irexecbutton = KEY_1config = echo "You pressed one"repeat = 0

end

Now when you run irexec and press 1 on the remote control You pressed one will be printed to the console:

$ irexecYou pressed oneYou pressed oneYou pressed one

Here is another example that uses mpc to control Music Player Daemon:

beginprog = irexecbutton = KEY_PREVIOUSSONGconfig = mpc prevrepeat = 0

endbegin

prog = irexecbutton = KEY_NEXTSONGconfig = mpc nextrepeat = 0

endbegin

prog = irexecbutton = KEY_PLAYconfig = mpc playrepeat = 1

end

6.3. irexec 19

Page 24: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

6.4 python-lirc

python-lirc is a Python extension that allows us to access configs in ~/.lircrc when LIRC receives a signal.

First we need to add more configurations to our ~/.lircrc:

beginprog = irexecbutton = KEY_1config = echo "You pressed one"repeat = 0

end

beginprog = myprogrambutton = KEY_1config = one

end

beginprog = myprogrambutton = KEY_1config = two

end

Then we can wait for IR codes in Python:

$ python3>>> import lirc>>> sockid = lirc.init("myprogram")>>> lirc.nextcode() # press 1 on remote after this[’one’]>>> lirc.nextcode() # press 2 on remote after this[’two’]

6.5 PiFace CAD

PiFace Control and Display provides a wrapper around python-lirc. An example of how to use it can be found inExamples.

20 Chapter 6. LIRC

Page 25: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 7

Reference (PiFace CAD)

7.1 pifacecad.core

class pifacecad.core.PiFaceCAD(hardware_addr=0, bus=0, chip_select=1, init_board=True)A PiFace Control and Display board.

Attribute switch_port – See pifacecommon.mcp23s17.MCP23S17RegisterNeg.

Attribute switches – list containing pifacecommon.mcp23s17.MCP23S17RegisterBitNeg.

Attribute lcd – See pifacecad.lcd.PiFaceLCD.

Example:

>>> cad = pifacecad.PiFaceCAD()>>> hex(cad.switch_port.value)0x02>>> cad.switches[1].value1>>> cad.lcd.write("Hello, PiFaceLCD!")>>> cad.lcd.backlight_on()

class pifacecad.core.SwitchEventListener(chip=None)Listens for events on the switches and calls the mapped callback functions.

>>> def print_flag(event):... print(event.interrupt_flag)...>>> listener = pifacecad.SwitchEventListener()>>> listener.register(0, pifacecad.IODIR_ON, print_flag)>>> listener.activate()

7.2 pifacecad.ir

class pifacecad.ir.IREvent(ir_code)An IR event.

class pifacecad.ir.IREventListener(prog, lircrc=None)Listens for IR events and calls the registered functions. prog specifies

21

Page 26: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

>>> def print_ir_code(event):... print(event.ir_code)...>>> listener = pifacecad.IREventListener(prog="myprogram")>>> listener.register(’one’, print_ir_code)>>> listener.activate()

activate()When activated the IREventListener will run callbacks associated with IR codes.

deactivate()When deactivated the IREventListener will not run anything.

register(ir_code, callback)Registers an ir_code to a callback function.

Parameters

• ir_code (int) – The IR code.

• callback (function) – The function to run when event is detected.

class pifacecad.ir.IRFunctionMap(ir_code, callback)Maps an IR code to callback function.

pifacecad.ir.watch_ir_events(event_queue)Waits for IR code events and places them on the event queue.

Parameters event_queue (multiprocessing.Queue) – A queue to put events on.

7.3 pifacecad.lcd

class pifacecad.lcd.HD44780ControlPort(chip)Control Port for an HD44780 LCD display. Must have the following properties:

•backlight_pin

•read_write_pin

•register_select_pin

•enable_pin

class pifacecad.lcd.HD44780DataPort(chip)Data Port for an HD44780 LCD display. Must have the following properties:

•value

class pifacecad.lcd.HD44780LCD(control_port, data_port, init_lcd=True)Component part of an HD4780, must be combined with a 4 or 8 bit mixin.

backlight_off()Turn on the backlight.

backlight_on()Turn on the backlight.

blink_off()Turns off the blinking cursor.

blink_on()Turns on the blinking cursor.

22 Chapter 7. Reference (PiFace CAD)

Page 27: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

char_bank_in_range_or_error(char_bank)Raises an exception if char_bank is out of bounds. Returns True otherwise.

Parameters char_bank (int) – The address to check.

clear()Clears the display.

colrow2address(col, row)Returns address of column and row.

Parameters

• col – The column.

• col – int

• row – The row.

• row – int

Returns The address of the column and row.

cursor_off()Turns the underline cursor off.

cursor_on()Turns the underline cursor on.

display_off()Turns the display off (quickly).

display_on()Turns the display on (quickly).

get_cursor()Returns the current column and row of the cursor. Also fixes internal value.

Returns (int, int) – A tuple containing the column and row.

home()Moves the cursor to the home position.

left_justify()Left justifies text from the cursor.

left_to_right()Sets the text to flow from left to right.

move_left()Scrolls the display without changing the RAM.

move_right()Scrolls the display without changing the RAM.

pulse_clock()Pulse the LCD clock for reading data.

right_justify()Right justifies text from the cursor.

right_to_left()Sets the text to flow from right to left.

see_cursor(col=None)Moves the viewport so that the cursor is visible.

7.3. pifacecad.lcd 23

Page 28: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

send_command(command)Send command byte to LCD.

Parameters command (int) – The command byte to be sent.

send_data(data)Send data byte to LCD.

Parameters data (int) – The data byte to be sent.

set_cgram_address(address=0)Start using CGRAM at the given address.

Parameters address (int) – The address to start at (default: 0)

set_cursor(col, row)Places the cursor at the specified column and row.

Parameters

• col (int) – The column.

• row (int) – The row.

set_ddram_address(address=None)Start using DDRAM at the given address.

Parameters address (int) – The address to start at (default: current)

store_custom_bitmap(char_bank, bitmap)Stores a custom bitmap bitmap at char_bank.

Parameters

• char_bank (int) – The CGRAM address to use.

• bitmap (LCDBitmap) – The bitmap to store.

update_display_control()Update the display control to reflect the displaycontrol.

update_entry_mode()Update entrymodeset to reflect the displaymode.

update_function_set()Updates the function set to reflect the current displayfunction.

viewport_cornerThe top left corner of the current viewport.

write(text)Writes a string to the LCD screen.

Parameters text (string) – The text that will be written.

class pifacecad.lcd.LCDBitmap(lines=[])A custom bitmap for the LCD screen.

class pifacecad.lcd.PiFaceLCD(control_port, data_port, init_lcd=True)An HD44780 LCD in 4-bit mode.

24 Chapter 7. Reference (PiFace CAD)

Page 29: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 8

Reference (PiFace CAD Tools)

8.1 pifacecad.tools.question

class pifacecad.tools.question.LCDQuestion(question, answers, selector=’>’, cad=None)Asks a question on the LCD”

Parameters

• question (string) – The question to be asked.

• answers (list) – The answers to choose from.

• selector (string) – The selector displayed in front of each answer.

• cad (PiFaceCAD) – An already initialised PiFaceCAD object.

ask()Asks the question using the LCD screen.

Returns int – index of the answer selected.

8.2 pifacecad.tools.scanf

class pifacecad.tools.scanf.LCDScanf(format, custom_values=None, cad=None)Allows the user to input text using the LCD.

To change mode from moving and editing press switch 5 (navigation switch in). Move the navigation switchside to side (switches 6 and 7) to change character.

The available character set is specified using a format string similar to printf. Supported character specifiers are:

c: CharactersC: Capital Charactersi: Integersd: Integersx: HexadecimalX: Capital Hexadecimal.: Punctuationm: Custom (specifed by ‘‘custom_values‘‘ in init args)r: Return (switch 5 to submit string)

You must prefix them with a % symbol and you can also specify a number argument. Each specifier is returnedas an element in a list.

25

Page 30: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

For example:

>>> scanner = pifacecad.tools.LCDScanf("Text: %c%2i%.%r")>>> print(scanner.scan()) # user enters things on PiFaceCAD[’a’, ’13’, ’!’]

You can also specify custom values:

>>> scanner = pifacecad.tools.LCDScanf(... "Animal: %m%r",... custom_values=(’cat’, ’dog’, ’fish’)... )>>> print(scanner.scan())[’fish’]

move_cursor_left()move cursor left, roll around string, scroll screen. Only place cursor on ValueSelect, ignore plain text.

move_cursor_right()move cursor right, roll around string, scroll screen. Only place cursor on ValueSelect, ignore plain text.

class pifacecad.tools.scanf.MultiValueSelect(multiplier, value_select, cus-tom_values=None)

A list of ValueSelects representing a single value.

class pifacecad.tools.scanf.ValueSelect(values=[], value_index=0)A character in a specified range

longest_lenReturn the length of the longest value in this list. Example:

ValueSelect(["one", "two", "three"]).longest_len() == 5

class pifacecad.tools.scanf.ValueSelectString(format, custom_values=None)A list of ValueSelect’s and characters, representing a string.

instanceindex(instance_type)Returns the first index of an instance of instance_type

selected_valuesReturns a list of currently selected values.

value_at(string_index)Returns the ValueSelect or character at the string index. The string index is the index of this ValueSelect-String as if it is being treated as a string.

Example:

cs0 == CustomValueSelect == ["bb", "cc"]cs1 == CustomValueSelect == ["dd", "ee"]vss == ValueSelectString == ["a", cs0, cs1, "f"]vss == ["a", ["bb", "cc"], ["dd", "ee"], "f"]str(vss) == "abbddf"vss.value_at(0) == "a"vss.value_at(1) == cs0vss.value_at(2) == cs0vss.value_at(3) == cs1vss.value_at(5) == "f"str(vss.value_at(2)) == "bb"

pifacecad.tools.scanf.char_range(c1, c2)Generates the characters from c1 to c2, inclusive.

26 Chapter 8. Reference (PiFace CAD Tools)

Page 31: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

pifacecad.tools.scanf.is_number(character)Returns True if given character is a number, False otherwise.

pifacecad.tools.scanf.is_selectable_character(display_string, col)Returns True if the character can be selected

8.2. pifacecad.tools.scanf 27

Page 32: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

28 Chapter 8. Reference (PiFace CAD Tools)

Page 33: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

CHAPTER 9

Indices and tables

• genindex

• modindex

• search

29

Page 34: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

30 Chapter 9. Indices and tables

Page 35: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

Python Module Index

ppifacecad.core, 21pifacecad.ir, 21pifacecad.lcd, 22pifacecad.tools.question, 25pifacecad.tools.scanf, 25

31

Page 36: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

32 Python Module Index

Page 37: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

Index

Aactivate() (pifacecad.ir.IREventListener method), 22ask() (pifacecad.tools.question.LCDQuestion method),

25

Bbacklight_off() (pifacecad.lcd.HD44780LCD method),

22backlight_on() (pifacecad.lcd.HD44780LCD method), 22blink_off() (pifacecad.lcd.HD44780LCD method), 22blink_on() (pifacecad.lcd.HD44780LCD method), 22

Cchar_bank_in_range_or_error() (pi-

facecad.lcd.HD44780LCD method), 22char_range() (in module pifacecad.tools.scanf), 26clear() (pifacecad.lcd.HD44780LCD method), 23colrow2address() (pifacecad.lcd.HD44780LCD method),

23cursor_off() (pifacecad.lcd.HD44780LCD method), 23cursor_on() (pifacecad.lcd.HD44780LCD method), 23

Ddeactivate() (pifacecad.ir.IREventListener method), 22display_off() (pifacecad.lcd.HD44780LCD method), 23display_on() (pifacecad.lcd.HD44780LCD method), 23

Gget_cursor() (pifacecad.lcd.HD44780LCD method), 23

HHD44780ControlPort (class in pifacecad.lcd), 22HD44780DataPort (class in pifacecad.lcd), 22HD44780LCD (class in pifacecad.lcd), 22home() (pifacecad.lcd.HD44780LCD method), 23

Iinstanceindex() (pifacecad.tools.scanf.ValueSelectString

method), 26IREvent (class in pifacecad.ir), 21

IREventListener (class in pifacecad.ir), 21IRFunctionMap (class in pifacecad.ir), 22is_number() (in module pifacecad.tools.scanf), 26is_selectable_character() (in module pi-

facecad.tools.scanf), 27

LLCDBitmap (class in pifacecad.lcd), 24LCDQuestion (class in pifacecad.tools.question), 25LCDScanf (class in pifacecad.tools.scanf), 25left_justify() (pifacecad.lcd.HD44780LCD method), 23left_to_right() (pifacecad.lcd.HD44780LCD method), 23longest_len (pifacecad.tools.scanf.ValueSelect attribute),

26

Mmove_cursor_left() (pifacecad.tools.scanf.LCDScanf

method), 26move_cursor_right() (pifacecad.tools.scanf.LCDScanf

method), 26move_left() (pifacecad.lcd.HD44780LCD method), 23move_right() (pifacecad.lcd.HD44780LCD method), 23MultiValueSelect (class in pifacecad.tools.scanf), 26

PPiFaceCAD (class in pifacecad.core), 21pifacecad.core (module), 21pifacecad.ir (module), 21pifacecad.lcd (module), 22pifacecad.tools.question (module), 25pifacecad.tools.scanf (module), 25PiFaceLCD (class in pifacecad.lcd), 24pulse_clock() (pifacecad.lcd.HD44780LCD method), 23

Rregister() (pifacecad.ir.IREventListener method), 22right_justify() (pifacecad.lcd.HD44780LCD method), 23right_to_left() (pifacecad.lcd.HD44780LCD method), 23

Ssee_cursor() (pifacecad.lcd.HD44780LCD method), 23

33

Page 38: PiFace Control and Display (CAD) Documentation · 2019-04-02 · PiFace Control and Display (CAD) Documentation, Release 2.0.7 The pifacecad Python module provides functions and classes

PiFace Control and Display (CAD) Documentation, Release 2.0.7

selected_values (pifacecad.tools.scanf.ValueSelectStringattribute), 26

send_command() (pifacecad.lcd.HD44780LCD method),23

send_data() (pifacecad.lcd.HD44780LCD method), 24set_cgram_address() (pifacecad.lcd.HD44780LCD

method), 24set_cursor() (pifacecad.lcd.HD44780LCD method), 24set_ddram_address() (pifacecad.lcd.HD44780LCD

method), 24store_custom_bitmap() (pifacecad.lcd.HD44780LCD

method), 24SwitchEventListener (class in pifacecad.core), 21

Uupdate_display_control() (pifacecad.lcd.HD44780LCD

method), 24update_entry_mode() (pifacecad.lcd.HD44780LCD

method), 24update_function_set() (pifacecad.lcd.HD44780LCD

method), 24

Vvalue_at() (pifacecad.tools.scanf.ValueSelectString

method), 26ValueSelect (class in pifacecad.tools.scanf), 26ValueSelectString (class in pifacecad.tools.scanf), 26viewport_corner (pifacecad.lcd.HD44780LCD attribute),

24

Wwatch_ir_events() (in module pifacecad.ir), 22write() (pifacecad.lcd.HD44780LCD method), 24

34 Index