Using the curses library to view IoT data

Old School

© Lead Image © Andrey Kiselev, 123RF.com

© Lead Image © Andrey Kiselev, 123RF.com

Article from Issue 232/2020
Author(s):

When you need some quick graphical output, the old school curses library can save you some time and effort.

Many projects require a lot of time building colorful web pages or custom graphical user interfaces (GUIs). In a number of cases, however, only a simple text interface is required, especially for remote connections into a Raspberry Pi when you just want a quick system update.

In this article, I review a 1980s technology called curses [1], which lets you create text-based applications without requiring X windows or web services. In one example I look at C and Python apps that simulate Raspberry Pi sensor data, and in two examples, I output large text presentations and dynamic bars in Python [2].

Python Curses

The curses module is standard in Python and includes features such as:

  • dynamic screen positioning
  • ASCII box-drawing characters
  • basic color support
  • window and pad objects

As a first example, I create an interface with a color background, header, footer, and dynamic text that simulates sensor data to a Raspberry Pi (Figure 1; Listing 1).

Listing 1

curses_text.py

01 import curses , time, random
02
03 # create a curses object
04 stdscr = curses.initscr()
05 height, width = stdscr.getmaxyx() # get the window size
06
07 # define two color pairs, 1- header/footer , 2 - dynamic text, 3 - background
08 curses.start_color()
09 curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
10 curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
11 curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLUE)
12
13 # Write a header and footer, first write colored strip, then write text
14 stdscr.bkgd(curses.color_pair(3))
15 stdscr.addstr(0, 0, " " * width,curses.color_pair(1) )
16 stdscr.addstr(height-1, 0, " " * (width - 1),curses.color_pair(1) )
17 stdscr.addstr(0, 0, " Curses Dynamic Text Example" ,curses.color_pair(1) )
18 stdscr.addstr(height-1, 0, " Key Commands : q - to quit " ,curses.color_pair(1) )
19 stdscr.addstr(3, 5, "RASPBERRY PI SIMULATED SENSOR VALUES" ,curses.A_BOLD )
20 stdscr.refresh()
21
22 # Cycle to update text. Enter a 'q' to quit
23 k = 0
24 stdscr.nodelay(1)
25 while (k != ord('q')):
26     # write 10 lines text with a label and then some random numbers
27     for i in range(1,11):
28         stdscr.addstr(4+ i, 5, "Sensor " + str(i) + " : " ,curses.A_BOLD )
29         stdscr.addstr(4+ i, 20, str(random.randint(10,99)) ,curses.color_pair(2) )
30     time.sleep(2)
31     k = stdscr.getch()
32
33 curses.endwin()
Figure 1: Curses dynamic text example.

The first step in a curses app is to define a main screen object (stdscr, line 4). The next step is to enable color and create some color pairs (lines 8-11). Color pair 3 sets the background to blue (line 14). Lines 15-18 use color pairs and the screen size (with height and width, defined in line 5) to add a header and footer strip.

The stdscr.nodelay command allows the program to cycle until the stdscr.getch() call returns a key stroke. The simulated Rasp Pi values refresh every 10 seconds until the q key is captured (line 25), after which, the terminal settings are returned to normal (curses.endwin()) and the program exits.

This simulated Raspberry Pi example only took about 30 lines of code, which is significantly less than if an equivalent web application were used.

C curses Example

For the C example, I used a Raspberry Pi and the previous Python example. Before you begin, you need to install the curses library:

sudo apt-get install libncurses5-dev

The curses syntax is similar between C and Python, but not identical (Listing 2). For example, in Python the addstr command includes a color pair reference. In C this is not supported, so an on/off attribute (attron/attroff) toggles the color pair. However, C does support a formatted string write command, mvprintw (lines 38 and 42).

Listing 2

c1.c

01 /* c1.c - Basic Curses Example */
02
03 #include <curses.h>
04 #include <stdlib.h>
05 #include <unistd.h>
06
07 int main(void)
08 {
09     int row, col, k;
10 // Create a curses object and define color pairs
11     initscr();
12     getmaxyx(stdscr,row,col);
13     start_color();
14     init_pair(1,COLOR_RED,COLOR_WHITE);
15     init_pair(2,COLOR_GREEN,COLOR_BLACK);
16     init_pair(3,COLOR_WHITE,COLOR_BLUE);
17     curs_set(0);
18     noecho();
19     nodelay(stdscr, TRUE);
20 // Write a header and footer, first write colored strip, then write text
21     bkgd(COLOR_PAIR(3));
22     attron(COLOR_PAIR(1));
23 // Create a top and bottom color strip
24     for (int i = 0; i < col; i++) {
25         mvaddstr(0, i, " ");
26         mvaddstr(row-1, i, " ");
27     }
28     mvaddstr(0, 0, " Curses C Dynamic Text Example");
29     mvaddstr(row-1, 0, " Key Commands: q - to quit");
30     attroff(COLOR_PAIR(1));
31     mvaddstr(2, 5,"RASPBERRY PI SIMULATED SENSOR VALUES" );
32     refresh();
33 // Cycle with new values every 2 seconds until a q key (133) is entered
34     while (k != 113)
35     {
36         attroff(COLOR_PAIR(2));
37         for (int i = 0; i < 10; i++) {
38             mvprintw((4+i), 5, " Sensor %d : ",i);
39         }
40         attron(COLOR_PAIR(2));
41         for (int i = 0; i < 10; i++) {
42             mvprintw((4+i), 20, "%d",rand() %100);
43         }
44         k = getch();
45         sleep(2);
46     }
47     endwin();
48     exit(0);
49 }

To compile and run the program, enter:

gcc -o c1 c1.c -lncurses
./c1

With the exception of the caption in the top line, the C and Python output look identical.

FIGlet for Large Custom Text

To generate large custom text for presentation, you can use the FIGlet library [3], which has an extensive selection of "fonts" created with standard ASCII characters. The FIGlet library is installed in Python with the command:

pip install pyfiglet

An example executed from the Python shell shows how it works:

>>> import pyfiglet
>>> value1 = pyfiglet.figlet_format(         "12.3", font = "starwars" )
>>> print(value1)
 __   ___       ____
/_ | |__ \     |___ \
 | |    ) |      __) |
 | |   / /      |__ <
 | |  / /_   __ ___) |
 |_| |____| (__)____/

By combining curses with FIGlet, you can create some simple Raspberry Pi interfaces. A little bit of trial and error might be required to get a FIGlet font [4] that matches your requirements. I found that the starwars and doom fonts worked well for dynamic text and the small font was good for headings.

Listing 3 shows the code that generates the FIGlet large text in Figure 2. In this example, a get_io() function generates random numbers; for a true Rasp Pi project, this function would return sensor values.

Listing 3

bigtxt.py

01 import curses, time
02 import pyfiglet, random
03
04 def get_io(): # Get a random value. Tweek later with real data
05     global value1
06     testvalue = str(random.randint(100,1000)/10) + " C"
07     value1 = pyfiglet.figlet_format(testvalue, font = "starwars" )
08
09 # Create a string of text based on the Figlet font object
10 title = pyfiglet.figlet_format("Raspi Data", font = "small" )
11
12 stdscr = curses.initscr() # create a curses object
13 # Create a couple of color definitions
14 curses.start_color()
15 curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLACK)
16 curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
17
18 # Write the BIG TITLE text string
19 stdscr.addstr(1,0, title,curses.color_pair(1) )
20 stdscr.addstr(8,0, "Sensor 1: GPIO 7 Temperature Reading" ,curses.A_BOLD)
21
22 # Cycle getting new data, enter a 'q' to quit
23 stdscr.nodelay(1)
24 k = 0
25 while (k != ord('q')):
26     get_io() # get the data values
27     stdscr.addstr(10,0, value1,curses.color_pair(2) )
28     stdscr.refresh()
29     time.sleep(2)
30
31     k = stdscr.getch()
32
33 curses.endwin()
Figure 2: FIGlet large text.

Buy this article as PDF

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

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Nerf Target Game

    A cool Nerf gun game for a neighborhood party provides a lesson in Python coding with multiple processors.

  • Perl: Internet Video Organizer

    Video files are ready for download, but your storage space is limited, and, if you're like most people, you may have a hard time letting go of past favorites. You need a higher authority to decide when it’s time to trim down your collection.

  • Serial Communications

    We explore serial communications, from the electrical specs to protocols and libraries, with an example of serial communication with an Arduino.

  • Perl: Dynamic netstat

    The netstat utility reveals how your Linux box interacts with the local network. With a few Perl modules, you can develop a tool that displays the data dynamically, exactly the way top does.

  • BPython 0.9.6: Python Command Line at Ease

    BPython is an interactive Python command line interface with its own comfort functions. The program is now available in version 0.9.6.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News