Solutions to exercise 5: Accessing the real world

Exercise 5.2: The gpio command

The gpio command has many options of which

  • mode
  • write
  • blink
are the ones we are interested here. First we must set the pin, to which the LED is connected, to output mode. Then we can switch the LED on or off. In the example below I use the "-g" option which allows me to use the pin numbers as they are written on the cobbler.

gpioCmd.png

Exercise 5.3 The bash script

In the bash script be must essentially write the same gpio commands as given on the command line in exercise 5.2. We have to add a loop though. I save the script into a file named blink.sh You can run the shell script with the command: sh blink.sh. Even nicer: You can change its permissions to make it executable and run it directly:

chmod +x blink.sh
./blink.sh

blink.sh.png

You can find the shell script here:

https://afnog.iotworkshop.africa/pub/UCC_Course_2018/Exercise5Solutions/blink.sh

Exercise 5.4: A C program making the LED blink

In the C program we will have to:

  • include the stdio.h and wiringPi.h include files
  • initialize the wiringPi library with wiringPiSetup (I use wiringPiSetupGpio, which allows me to use the Broadcom pin numbers)
  • set the pin mode to output
  • write the logic level to the pin switching the LED on and off
  • wait for some time between switching the logic levels
blink.c.png

Exercise 5.5: Graceful exit

In order to capture the "^C" which normally just terminates the program we must

  • include stdlib.h (here the exit function is defined) and signal.h (here all system signals including SIGINT, which corresponds to "^C" are defined)
  • create a function which is called when "^C" is seen
  • tell the system to call this function when "^C" is seen
Here is the code that informs the system that we want to be notified when a SIGINT signal is sent to the program

signal.png

And here is the definition of the new function which provides the graceful exit. We just switch off the LED before exiting the program.

blinkExit.png

And here is the full program:

https://afnog.iotworkshop.africa/pub/UCC_Course_2018/Exercise5Solutions/blink2.c

I was asked by a student if it was also possible to gracefully exit the shell script, which I did not know myself. Some research showed that it is possible. This code shows how:

https://afnog.iotworkshop.africa/pub/UCC_Course_2018/Exercise5Solutions/blink1.sh

Exercise 5.6: SOS

In the SOS program we use a function to implement a single light pulse. This function take a parameter which defines how long the LED will be switched on and how long it will be off afterwards.

sos.png

This function is then called in the main program. Here is the full code and its Makefile. The Makefiles for the blink programs are very similar.

https://afnog.iotworkshop.africa/pub/UCC_Course_2018/Exercise5Solutions/sos.c

https://afnog.iotworkshop.africa/pub/UCC_Course_2018/Exercise5Solutions/Makefile

Exercise 5.7: The traffic light simulator

The solution to this exercise shows how a program can be driven by data. We build a table in which we define states describing which traffic light is switched on (green an/or yellow and/or red) and for how long this state is going to last. Then the program goes through this tables and switches the lights as defined by the current entry. This is the implementation of a simple state machine.

The sequence is as follows:

duration Traffic light 1 Traffic light 2
long red green
short red yellow
short red red
long green red
short yellow red
short red red

Here are the states

duration red 1 yellow 1 green 1 red 2 yellow 2 green 2 hex table entry
1 1 0 0 0 0 1 0x61
0 1 0 0 0 1 0 0x22
0 1 0 0 1 0 0 0x24
1 0 0 1 1 0 0 0x4c
0 0 1 0 1 0 0 0x14
0 1 0 0 1 0 0 0x24
Switching on the lights defined in the table (e.g green on traffic light 1 and red on traffic light 2 for a long duration as defined in the first state of the table) and going to the next state once the duration has elapsed, will control the traffic lights in the correct fashion. After the last state we switch to the first one again.

Here is the full program:

https://afnog.iotworkshop.africa/pub/UCC_Course_2018/Exercise5Solutions/trafficLight.c

If you want to test the table driven program on the PC without the need of the LED hardware, here is the program which prints the light states instead of lighting the LEDs.

https://afnog.iotworkshop.africa/pub/UCC_Course_2018/Exercise5Solutions/trafficLightTest.c

-- Uli Raich - 2018-10-30

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatEXT Makefile r1 manage 0.1 K 2018-11-20 - 11:07 UliRaich  
C source code filec blink.c r1 manage 0.4 K 2018-11-20 - 10:12 UliRaich  
PNGpng blink.c.png r1 manage 50.4 K 2018-11-20 - 10:12 UliRaich  
Unix shell scriptsh blink.sh r1 manage 0.3 K 2018-11-20 - 09:55 UliRaich  
PNGpng blink.sh.png r1 manage 28.9 K 2018-11-20 - 09:54 UliRaich  
Unix shell scriptsh blink1.sh r1 manage 0.6 K 2018-11-20 - 16:31 UliRaich  
C source code filec blink2.c r1 manage 0.8 K 2018-11-20 - 10:44 UliRaich  
PNGpng blinkExit.png r1 manage 13.9 K 2018-11-20 - 10:36 UliRaich  
PNGpng gpioCmd.png r1 manage 20.5 K 2018-11-20 - 09:35 UliRaich  
PNGpng signal.png r1 manage 8.2 K 2018-11-20 - 10:36 UliRaich  
C source code filec sos.c r1 manage 0.6 K 2018-11-20 - 11:07 UliRaich  
PNGpng sos.png r1 manage 17.6 K 2018-11-20 - 11:00 UliRaich  
C source code filec trafficLight.c r1 manage 1.3 K 2018-11-20 - 12:16 UliRaich  
C source code filec trafficLightTest.c r1 manage 1.6 K 2018-11-20 - 14:07 UliRaich  
Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r4 - 2018-11-20 - UliRaich
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback