In my last post about GPIO and LEDs in lua I wrote about a nice feature in Linux when working on embedded systems, such as the possibility to access hardware devices using the file system.
In this post I’ll be using this feature to build the game ‘Simon says’ in lua. I’ll be using a BeagleBoard rev C4 with ubuntu 11.04 installed.
We made this project with my colleagues for an embedded robotics course, so the credit is also shared with them 🙂
GPIOs in the Beagleboard Note
According to the Beagleboard datasheet, the processor can be configured to use all the pins in the expansion port as GPIOs, but this is a more advanced task which requires changing the MUX registers in the processor, which can be achieved by recompiling the kernel changing the kernel headers.
The Ubuntu image for Beagleboard already has several pins configured as GPIO. Thanks to Michael Shiloh, he found the pins that can be used as GPIO by default. This is the list of GPIOs you can use (and the corresponding pin in the expansion port):
- 157 (pin 22)
- 159 (pin 18)
- 161 (pin 16)
- 162 (pin 14)
- 136 (pin 9)
- 137 (pin 7)
- 138 (pin 5)
- 139 (pin 3)
For a reference on the pin number for each of these GPIOs read the Beagleboard datasheet.
For this game I only used 3 LEDS, because at that time I didn’t have more of these 🙂 but it’s very easy to add more.
I used 3 GPIO ports to connect the LEDs and 3 for the buttons. Using the default ubuntu installation I could plug 4 leds and 4 buttons. If I wanted more, I should tune the kernel as said earlier in the post.
Connecting the leds and buttons
Since the picture does not explain this clearly
I used pins 3,5,7 (GPIO 139,138,137 respectively) as input for the buttons and pins 12,14,16 (GPIO 158,162,161 respectively) as output GPIOs for the LEDs.
The diagram below shows how to connect the buttons and leds.
The source code is available in Github https://github.com/rsisto/simonSaysLuaGpio
The source code is quite simple, The GPIO library explained in the GPIO post was used to interface with the leds and buttons.
The rest of the code is just a main loop implementing the game logic. When the user loses, the leds blink several times and the application quits.
That’s it for now. Let me know if you have any comments or if the code was any use for you! Please share! 😀