Quantcast Led - eLua Wiki
Locked History Attachments

Led

Runs on

  • All platforms

Code

<<EmbedCode: execution failed [socket error] (see also the log)>>

Code comments

  • This first "block" of code is actually not related to blinking the led but to configuring the used GPIO pins. It allows the code to be completely portable among platforms, even using different ports and pins.

Don't be impressed by the number of lines it can take. This ends up being a simple "pattern" in eLua, to identify the underlining platform running your program and assigning ports, pins and devices accordingly.

pd.board() is a function from the "Platform Data" eLua module It returns the name of the platform where the program is currently running, so you can check it at run-time and assign the pins/variables acordingly. On this example, all we want is to identify in which port and pin the LED is connected.

   1 local uartid, invert, ledpin = 0, false 
   2 if pd.board() == "SAM7-EX256" then
   3   ledpin = pio.PB_20
   4 elseif pd.board() == "EK-LM3S8962" or pd.board() == "EK-LM3S6965" then
   5   ledpin = pio.PF_0
   6 elseif pd.board() == "EAGLE-100" then
   7   ledpin = pio.PE_1
   8 elseif pd.board() == "STR9-COMSTICK" then
   9   ledpin = pio.P9_0
  10 elseif pd.board() == "LPC-H2888" then
  11   ledpin = pio.P2_1
  12 elseif pd.board() == "MOD711" then
  13   ledpin = pio.P1_7 uartid = 1
  14 elseif pd.board() == "ATEVK1100" then
  15   ledpin = pio.PB_27 invert = true
  16 elseif pd.board() == "STR-E912" then
  17   ledpin = pio.P6_4
  18 elseif pd.board() == "ELUA-PUC" then
  19   ledpin = pio.P1_20
  20 elseif pd.board() == "ET-STM32" then
  21   ledpin = pio.PA_5
  22 elseif pd.board() == "MBED" then
  23  ledpin = mbed.pio.LED1
  24 else
  25  print( "\nError: Unknown board " .. pd.board() .. " !" ) return
  26 end
  27 

The folowing function simply sets the ledpin assigned above high (on), delays for sometime so we can see it lit and sets it low (off) again. It has a little extra logic, controlled by the "invert" variable, to handle led blinking on some platfoms that controls them in an "inverted" way, that is, to turn them on with a low/zero on the pin output instead of a high/one. This allows you to make sure not only the led will blink properly but that it will stay off when the program ends. This function will be called later by the main program loop.

   1 function cycle()
   2   if not invert then
   3     pio.pin.sethigh( ledpin )
   4   else
   5     pio.pin.setlow( ledpin )
   6   end 
   7   tmr.delay( 0, 500000 )
   8   if not invert then
   9     pio.pin.setlow( ledpin )
  10   else
  11     pio.pin.sethigh( ledpin )
  12   end
  13   tmr.delay( 0, 500000 )
  14 end
  15 

This is the code that configure the GPIO (General Purpose Input/Output) pin as we need it. We will use it as an output control pin, so we configure it with the "setdir" function as a "pio.OUTPUT" pin mode. This function belongs to the eLua "pio" module, that can configure individual pins with the functions of the table "pin" as used here and also all the pins of a port, with the functions of the "port" (pio.port....) table, used in some other examples that you will check soon.

   1 pio.pin.setdir( pio.OUTPUT, ledpin )  -- configure ledpin as an GPIO Output pin 
   2 print( "Hello from eLua on " .. pd.board() ) 
   3 print "Watch your LED blinking :)" 
   4 print "Press any key to end this demo.\n"
   5 

Now we come to the main program loop, which is simply a continuous call to the blinking function "cycle()" explained above, checking each time if the user pressed any key on the terminal keyboard. When/if it detects a keypress, it simply ends the loop and the program.

   1 while uart.getchar( uartid, 0 ) == "" do
   2   cycle()
   3 end
   4 

To learn more about the eLua modules and functions, please refer to our "Generic Modules" sub-menu entries on [http://www.eluaproject.net the official eLua site.]

Files