Code Walkthrough


In this section we'll briefly touch on the contents of piphone.py. We won't go through all the code, just talk about each section and what it does. You can open up piphone.py here and maybe have it open as you read through this page.

Imports


In this section of code, we import all the python libraries needed to make our script functional. They're all important but the main library is pygame. PyGame is an SDL (Simple DirectMedia Layer wrapper that gives python access to all the wonderful functionality in SDL, giving us access to audio, keyboard, mouse, touch-screens and video hardware. It's because of these libraries, plus the thousands of lines of code in the other included libraries that allows us to implement this application in under 500 lines of python.

UI Classes


These are Phil Burgess' classes for the user interface. They define the Icon class and the Button class, which takes care of our getting our icon images onto the screen.

UI Callbacks


Once a particular button is pressed, a callback function is called. This then allows the application to do what's relevant to that particular button. For example when we press the green dial button, a function is called that tells the FONA to dial the number on the screen.  

Global Stuff


Next we have a bunch of global variables which are needed throughout the application. 

The global section also has the "buttons" array, which defines all our screens. If you want to customise the app for your own use, this is the first place you go to start laying out your own screens. Each button has an x and y co-ordinate for where it should go, and a width and height for where it's touch sensitive. For each button there as also a callback function specified, which gets called when it's pressed.

Assorted Utility Functions


All we have in here is a couple of functions to load an save the settings, if needed. 

 

Initialization


Here's where we get into the core of the application. We first initialize everything, start pygame, init the screen, touch-screen, load the icons and buttons.

Main Loop


An finally we're into the main loop. This is a small loop that gets a touch input, and depending on which screen is being shown, and which button is pressed, calls it's callback. Then we take care of drawing the number on the screeen as it's typed and we finally update the frame-buffer before going around the main loop again.

When the dial (or hangup) icons are pressed, a function is called to tell the FONA to dial or to hang up. 

This guide was first published on Nov 24, 2014. It was last updated on Nov 24, 2014. This page (Code Walkthrough) was last updated on Jul 21, 2019.