Chapter 6: Program Design

Richard Kenneth Eng
Learn How To Program
3 min readJun 4, 2017

--

Let’s establish the basic logic of the program by trying to satisfy the requirements. We want to query the hardware for vital information and display the information on the screen, but we want to do it repeatedly and periodically without end (which implies an endless loop). To express this logic colloquially, we use a kind of natural language description of the algorithm called pseudocode. (An algorithm is a procedure or formula for solving a problem, typically based on performing a sequence of specified actions.) The pseudocode:

initialize the application window
loop indefinitely
query hardware for information and
display information on screen
sleep for 2 seconds
continue with next loop

The action “sleep for 2 seconds” means that the program will idly wait for 2 seconds before continuing with the next loop. It also means that during this time, the program cannot do anything else — it is essentially blocked.

The solution is to create a concurrent thread for this process. (A “thread” is a unit of execution in a program. There is usually only one thread per program, but in certain situations it is desirable to have more than one thread.) “Concurrent” means that it executes independently and simultaneously with your main thread. In Smalltalk, you do this with the fork message.

The C Language Interface

See Addendum: C Library (following Chapter 10) for details of the programming interface. Our application will issue calls to the library which will return the various pieces of information. However, our application will still have to convert the values into easily readable form. For example, the system uptime, which is in seconds, needs to be displayed as hours, minutes, and seconds.

We will create a class for accessing the C library. It will be a singleton, since we only need one instantiation of the class.

The Display Screen

An ImageMorph will be created to which will be attached text string Morphs. Some of these Morphs can have their text strings updated with the latest information from the hardware. The following image will serve as the background for the main window Morph. Of course, the image can be scaled as appropriate for your display (I’ve chosen to make the image 450 x 600 to fit nicely on my 1824 x 984 monitor).

ImageMorph serving as main window

Textual information will be presented in the above window starting from the top left corner and moving down the page. The text will be white, as I believe that will look most appealing.

To keep the code orderly, we will create a class for a label and information field pair. This class will also contain the exact position in the main window.

So here’s the screen layout:

== System ==
System name: ####
Node name: ####
Release: ####
Version: ####
Machine: ####
== Time ==
Uptime: #### hours #### minutes #### seconds
== Memory ==
Free mem: #### KB
Total mem: #### KB
Cached mem: #### KB
Buffer mem: #### KB
Free swap: #### KB
Buffer swap: #### KB
# of processes: ####
== SD card ==
Free space: #### MB
Total space: #### MB
== CPU ==
CPU usage: #### %
Frequency: #### MHz
CPU/GPU temp: #### C
# of cores: ####

We want to centre this block of information vertically because it looks most appealing.

Before we start coding, let’s make sure that the requisite files are in the Pharo folder. We will need the hot air balloon jpeg file (download from here). We will also need the C shared library file (see the Addendum for how to build it).

--

--