Solutions to exercise 5: Accessing the real world
Exercise 5.2: The gpio command
The gpio command has many options of which
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.
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
You can find the shell script here:
https://iotworkshop.africa/pub/Embedded_Systems/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
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
And here is the definition of the new function which provides the graceful exit. We just switch off the LED before exiting the program.
And here is the full program:
https://iotworkshop.africa/pub/Embedded_Systems/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://iotworkshop.africa/pub/Embedded_Systems/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.
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://iotworkshop.africa/pub/Embedded_Systems/Exercise5Solutions/sos.c
https://iotworkshop.africa/pub/Embedded_Systems/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://iotworkshop.africa/pub/Embedded_Systems/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://iotworkshop.africa/pub/Embedded_Systems/Exercise5Solutions/trafficLightTest.c