RPi.GPIO.Lua Workshop

Pi and More 5

28.06.2014


Eigenschaften von Lua

Hello World

print ("hi")

Installation auf dem Pi (Raspbian)

Lua und LuaRocks

sudo apt-get install lua5.1 liblua5.1-0-dev
sudo apt-get install luarocks
	

RPi.GPIO.Lua

sudo luarocks install rpi-gpio

# für PWM und Events:
sudo luarocks install copas
sudo luarocks install darksidesync

# für das Modul GPIO.lcd-hd44780:
sudo luarocks install bit32
sudo luarocks install luasocket
	

Lua Crashkurs

Interpreter starten

lua [-e, -i] [Eingabedatei]
Tip: Werte im interaktiven Modus mit '=' ausgeben

Beispiel-Code herunterladen

wget sgz.andre-simon.de -O code.tar.gz
	

Typen und Werte

x=1
print (type(x))

x="1"
print (type(x))

x=false
print (type(x))

print (type(y))

x={}
print (type(x))

x=print
x (type(x))

Kommentare

-- print ("Einzeiliger Kommentar")

--[[
print ("Block")
]]

--[[
print ("Ein und aus")
--]]
	

Zahlenwerte

print(2^4)
print(1/9)
print(math.pi)
	
Strings
print ("Hallo") print ('Welt\n')
print ([[und 
so 
weiter]])
x="1234".."5678" 
print (#x)
	

Boolesche Werte

x=false;

if (x) then print ("true") else print ("false") end
print (x and "true" or "false")
	

Tabellen

a={1, "zwei", 3}
print (a[1])

a["x"] = 123
print (a.x)
print (#a)
	

Funktionen

f = function (a)
  return 2*a
end

g = function (func, p)
  return func(p)
end

print ( f(4) )
print ( g(f, 4) )
	

if-then-else

fib = function(n)

    if n <= 1 then
        return n
    else
	return fib(n-1) + fib(n-2)
    end

end

print (fib(8))
	

Schleifen

a = {"eins", "zwei"}
i=1
while a[i] do
  print (a[i]); i=i+1
end

i=1
repeat
  print (a[i]); i=i+1
until not a[i]

for i=1, #a do print (a[i]) end

for key, value in pairs(a) do print (value) end

Das Modul RPi.GPIO.Lua

Pin-Funktionen:

Interrupts und Events:

PWM:

Ein- und Ausgabe (Aufbau)

Eingabe - Polling (Code)

gpio = require("GPIO")
gpio.setmode(gpio.BOARD)
btn, led1, led2 = 12, 11, 13

sleep = function(seconds)
  os.execute("sleep "..tostring(seconds).."s")
end

gpio.setup(btn, gpio.IN, gpio.PUD_DOWN)
gpio.setup(led1, gpio.OUT)
gpio.setup(led2, gpio.OUT)

cnt=0
gpio.output(led1, 1)

while cnt < 10 do
   if gpio.input(btn)  then
      cnt = cnt + 1
      gpio.output(led1, 0)
      gpio.output(led2, 1)
      led1, led2 = led2, led1
   end
   sleep(0.2)
end

gpio.output(led1, 0)
gpio.output(led2, 0)

gpio.cleanup()

Eingabe - Interrupt (Code)

gpio = require("GPIO")
gpio.setmode(gpio.BOARD)
btn, led1, led2 = 12, 11, 13

gpio.setup(btn, gpio.IN, gpio.PUD_DOWN)
gpio.setup(led1, gpio.OUT)
gpio.setup(led2, gpio.OUT)

cnt=0
gpio.output(led1, 1)

while cnt < 10 do
   gpio.wait_for_edge(btn, gpio.RISING)
   cnt = cnt + 1
   gpio.output(led1, 0)
   gpio.output(led2, 1)
   led1, led2 = led2, led1
end

gpio.output(led1, 0)
gpio.output(led2, 0)

gpio.cleanup()

Eingabe - Callback (Code)

gpio = require("GPIO")
dss = require("dss")
copas = require("copas")

gpio.setmode(gpio.BOARD)
btn, led1, led2 = 12, 11, 13

gpio.setup(btn, gpio.IN, gpio.PUD_DOWN)
gpio.setup(led1, gpio.OUT)
gpio.setup(led2, gpio.OUT)

cnt=0
gpio.output(led1, 1)

cb_btn_press = function(channel)
   cnt = cnt + 1
   if cnt == 10 then
     print ("Finished")
     gpio.output(led1, 0)
     gpio.output(led2, 0)
     gpio.cleanup()	
     os.exit()
   end
   gpio.output(led1, 0)
   gpio.output(led2, 1)
   led1, led2 = led2, led1
end

gpio.add_event_detect(btn, gpio.RISING, cb_btn_press, 200)

-- install the DSS socket receiver/handler that will 
-- do the hard work and make sure our callbacks are called
copas.addserver(
   dss.getsocket(), function(skt)
     skt = copas.wrap(skt)
     local hdlr = dss.gethandler()
     while true do
       hdlr(skt)
     end
   end)

copas.loop()  -- will never return

PWM (Aufbau)

PWM (Code)

gpio = require("GPIO")

BUZZER_OUT=18
LED_OUT=25

local sleep = function(seconds)
  os.execute("sleep "..tostring(seconds).."s")
end

gpio.setmode(gpio.BCM)
gpio.setup(BUZZER_OUT, gpio.OUT)
gpio.setup(LED_OUT, gpio.OUT)

-- SW-PWM instabil ab 500 Hz
dimmer = gpio.newPWM(LED_OUT, 100)  -- Hertz  
dimmer:start(0)                 -- duty cycle off

for cycle =100, 0, -1 do      
    dimmer:ChangeDutyCycle(cycle)
    sleep(0.04)
end

buzzer = gpio.newPWM(BUZZER_OUT, 5)  -- Hertz  
buzzer:start(0)                 -- duty cycle off

for cycle =100, 0, -10 do      
    buzzer:ChangeDutyCycle(cycle)
    sleep(0.5)
end
gpio.output(BUZZER_OUT, 0)

buzzer:stop()
dimmer:stop()

gpio.cleanup()

LCD Ansteuerung (Aufbau)

LCD Ansteuerung (Code)

GPIO = require("GPIO")

pin_rs = 22               -- register select pin
pin_e  = 18               -- enable pin
pin_db = {16, 11, 13, 15} -- data pins 1,2,3,4

hd44780 = require("module.lcd-hd44780")
GPIO.setmode(GPIO.BOARD)
lcd = hd44780.initialize(pin_rs, pin_e, pin_db)
lcd:begin(8,2)

lcd:clear()

--lcd:blink() --blinking cursor
lcd:message("LCD TEST\n12345678")
hd44780.delayMicroseconds(10000 * 500)

scroll1="Say hello to a scrolling text"

lcd:clear()
lcd:message(scroll1)
for j=0, #scroll1 do
  lcd:scrollDisplayLeft()
  hd44780.delayMicroseconds(1000 * 250)
end
lcd:clear()

lcd:message(scroll1)
for j=0, #scroll1 do
  lcd:scrollDisplayRight()
  hd44780.delayMicroseconds(1000 * 250)
end

lcd:clear()

lcd:setCursor(3,1)
lcd:message( "XX"  )
 
hd44780.delayMicroseconds(10000 * 500)
lcd:clear()

Das Simon-Spiel (Aufbau)

Das Simon-Spiel (Code)

GPIO=require "GPIO"

btnExit = 21
buttons = {7, 15, 13, 11, btnExit}
leds = {12, 16, 18, 26}

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
math.randomseed(os.time())

for k,v in pairs(buttons) do
  GPIO.setup(v, GPIO.IN,  GPIO.PUD_DOWN)
end

for k,v in pairs(leds) do
  GPIO.setup(v, GPIO.OUT)
end

flashLED = function (cycles) 
  for x=1, cycles*2 do
    for k,v in pairs(leds) do 
      GPIO.output(v, x%2); 
    end
    os.execute("sleep 0.2")
  end
end

beep = function(num, wait)
  GPIO.output(leds[num], 1)
  os.execute("ogg123 --quiet -K1 beep"..num..".ogg")
  GPIO.output(leds[num], 0)
  if wait==true then os.execute("sleep 0.1") end
end

flashLED(3)

playlist = {}

while true do
  
  playlist[#playlist+1] = math.random(#leds)
  
  for k,v in pairs (playlist) do
    beep(v, true)
  end
  
  for k,v in pairs (playlist) do
    waitForUser=true
    while waitForUser do
      
      for kb,vb in pairs(buttons) do 
        if GPIO.input(vb) == true then
          if (vb~=buttons[v]) then 
            if vb~=btnExit then os.execute("ogg123 --quiet gameover.ogg") end
            GPIO.cleanup()
            os.exit()
          else 
            beep(v, false)
          end
          waitForUser=false
        end
      end
    end
  end
  
  flashLED(1) 
end

os.exit()