Lua module for Pi

Lua module for Raspberry Pi
This package provides a Lua module to control the GPIO pins on a Raspberry Pi.
The main functionality is provided by the RPi.GPIO Python Module of Ben Croston.
The following functions have been implemented for Lua:
Pin functions:
- setup
- cleanup
- input
- output
- setmode
- gpio_function
- setwarnings
Interrupts and events:
- wait_for_edge
- event_detected
- add_event_detect
- remove_event_detect
- add_event_callback
PWM:
- newPWM
- start
- ChangeFrequency
- ChangeDutyCycle
- stop
Installation (manual)
Download the two tarballs referenced below and follow the inclided instructions.
Installation (luarocks package, instructions for Raspbian)
sudo apt-get install lua5.1 liblua5.1-0-dev sudo apt-get install luarocks sudo luarocks install rpi-gpio # for PWM and events: sudo luarocks install copas sudo luarocks install darksidesync # for the GPIO.lcd-hd44780 module: sudo luarocks install bit32
Example: Simon-Game
Simon is a electronic memory game: Four LEDs are lighting up in random order, each LEDs comes along with a specific sound. The player presses a button for each LED. If the sequence was entered correctly, the next round will add a new LED.Preparation:
- connect 4 LEDs and 5 push buttons to the Pi GPIO connector (see the arrays buttons and leds)
- connect speakers to your Pi
- copy beep*.ogg files from ~/python_games to your script directory
- adjust sound volume:
amixer cset numid=1 -- 100%
- install ogg123:
sudo apt-get install vorbis-tools
- if Unknown PCM cards.pcm.front is logged: Replace "pcm.front cards.pcm.front" by "pcm.front cards.pcm.default" in /usr/share/alsa/alsa.conf
01 GPIO=require "GPIO" 02 03 btnExit = 21 04 buttons = {7, 15, 13, 11, btnExit} 05 leds={12, 16, 18, 26} 06 07 GPIO.setmode(GPIO.BOARD) 08 GPIO.setwarnings(False) 09 math.randomseed(os.time()) 10 11 for k,v in pairs(buttons) do 12 GPIO.setup(v, GPIO.IN, GPIO.PUD_DOWN) 13 end 14 15 for k,v in pairs(leds) do 16 GPIO.setup(v, GPIO.OUT) 17 end 18 19 flashLED = function (cycles) 20 for x=1, cycles*2 do 21 for k,v in pairs(leds) do 22 GPIO.output(v, x%2); 23 end 24 os.execute("sleep 0.2") 25 end 26 end 27 28 beep = function(num, wait) 29 GPIO.output(leds[num], 1) 30 os.execute("ogg123 --quiet -K1 beep"..num..".ogg") 31 GPIO.output(leds[num], 0) 32 if wait==true then os.execute("sleep 0.1") end 33 end 34 35 quit=function() 36 GPIO.cleanup() 37 os.exit() 38 end 39 40 flashLED(3) 41 42 playlist = {} 43 44 while true do 45 46 playlist[#playlist+1] = math.random(4) 47 48 for k,v in pairs (playlist) do 49 beep(v, true) 50 end 51 52 for k,v in pairs (playlist) do 53 waitForUser=true 54 while waitForUser do 55 56 for kb,vb in pairs(buttons) do 57 if GPIO.input(vb) == true then 58 if (vb~=buttons[v]) then 59 if vb~=btnExit then os.execute("ogg123 --quiet gameover.ogg") end 60 quit() 61 else 62 beep(v, false) 63 end 64 waitForUser=false 65 end 66 end 67 end 68 end 69 70 flashLED(1) 71 end
Downloads (0.1)
RPi.GPIO.Lua module (5 KB)
Downloads (Fork including PWM and Events, 0.2.x)
- http://sourceforge.net/projects/raspberry-gpio-python/
- https://github.com/Tieske/rpi-gpio/tree/master/lua/scripts
- https://github.com/Tieske/rpi-gpio/
- https://github.com/Tieske/DarkSideSync
- https://github.com/Tieske/rpi-gpio/blob/master/lua/scripts/test6_callbacks.lua
- https://github.com/Tieske/rpi-gpio/blob/master/lua/README.md
Pi&More 5 presentation
Slides and sample code (1.1 MB)