1
0
mirror of https://github.com/elua/elua.git synced 2025-01-08 20:56:17 +08:00
elua/doc/pt/examples.html

144 lines
9.3 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Language" content="en-us"><title>Product</title>
<link rel="stylesheet" type="text/css" href="../style.css"></head>
<body style="background-color: rgb(255, 255, 255);">
<h3>Lua Code Examples</h3>eLua distros come bundled with nice and fun
(yes! we have games too! :) Lua programs examples in the File System.
They are also included in the eLua
source code distribution, under the /romfs subdirectory. <br>Lua example programs for eLua are also presented and commented here. <br>As previously explained, you can run them directly from eLua's file
system or you can use the eLua shell and send them via XMODEM, as
described <a href="using.html#shell">here</a>.<br><br>
<h3><a name="hello"></a>hello.lua: the ubiquitous "Hello, World!"</h3>
<p><strong>Runs on:&nbsp;</strong><br>All Targets</p><p><strong></strong>
<strong>Description:<br></strong>To call this a "program" is a gross overstatement, but it's a
tradition, so we respect it :) It just prints "Hello, World!" on the terminal and
returns to the shell. Download it only if you feel too lazy to fire up
the lua interpreter inside eLua and write it yourself :)</p><p><strong>Commented Source Code:</strong><br></p><p>print("Hello World")</p>
<h2><br></h2><h3><a name="info"></a>info.lua: getting the platform data</h3>
<p><strong>Runs on:&nbsp;</strong><br>All targets except i386</p><p><strong>Description:<br></strong>
This isn't really more advanced than "Hello, World!", but it does show
an eLua specific module: the platform data module (pd). You can read
more about the platform modules in the eLua source distribution
(docs/platform_modules.txt). The program will display the platform
name, the CPU name, the board name and the CPU clock and then will exit
to the shell.</p><p><strong>Commented Source Code:</strong></p><p><span style="font-family: Courier New;">-- Uses module pd to access platform data and print it on the Terminal</span><br style="font-family: Courier New;"><span style="font-family: Courier New;">print( "I'm running on platform " .. pd.platform() ) &nbsp;</span><br style="font-family: Courier New;"><span style="font-family: Courier New;">print( "The CPU is a " .. pd.cpu() )</span><br style="font-family: Courier New;"><span style="font-family: Courier New;"><span>print( "The board name is " .. pd.board(</span>) )</span><br></p><p></p>
<h3><a name="led"></a>led.lua: the old LED blinker, the new eLua way</h3>
<p><strong>Runs on: </strong><br>All targets except i386</p><p><strong>Description:<br></strong><strong></strong>
Now we get to do something "more embedded": blink a LED. The code ilustrates a few interesting eLua features:</p>
<ul><li><p>cross platform code: the code assigns a different pin
to the LED starting from the board name. You can see how the platform
data module makes it very easy to write such portable code.</p></li><li><p>uart, pio, tmr, pd modules: they are all used here.</p></li></ul>
<p>Watch it blink, then press any key to return to the eLua shell.</p><p><strong>Commented Source Code:</strong></p>
<h3><a name="hangman"></a>hangman.lua: taking advantage of your terminal</h3>
<p><strong>Runs on:&nbsp;</strong><br>All Targets except i386</p><strong></strong>
<strong></strong><p><strong>Description:<br></strong>By far the geekiest example from the eLua distribution (or would it be morse.lua? :), it makes use of
the term module (docs/terminal_support.txt) to let the user play a
BSD-like "hangman" directly in his terminal emulator. Run the example
and enjoy. Currently it has a very small list of words, as this was
written mainly as a proof of eLua's capabilities, but it's very easy to
add new words/replace the existing ones. A screenshot can be seen <a href="http://elua.berlios.de/other/elua_hangman.png">here</a>.</p><p></p>
<h3><a name="pwmled"></a>pwmled.lua: LED blinker, advanced class</h3><p><strong>Runs on:&nbsp;</strong><br>EK-LM3S8962,&nbsp;EK-LM3S6965</p><strong></strong>
<strong></strong><p><strong>Description:<br></strong>
This uses the PWM module to fade the on-board LED in and out, in an
infinite loop. Not much to say here, the code is very simple, yet the
results are quite spectacular. Press any key to end the sample and
return to the shell.</p><p></p>
<h3><a name="tvbgone"></a>tvbgone.lua: yes, eLua can do real time!</h3>
<p><strong>Runs on:&nbsp;</strong><br>EK-LM3S8962,&nbsp;EK-LM3S6965</p><strong></strong>
<strong></strong><p><strong>Description:<br></strong>This is more complex, but also very important for eLua, because it
proves that real time applications (with relatively strict timing
requirements) can run from eLua directly. It's the famous TV-B-Gone
project adapted from <a href="http://www.ladyada.net/make/tvbgone/">LadyAda's kit</a>.
If you're not familiar with TV-B-Gone, it knows how to do one thing
very well: power off your TV :) Basically it contains a lot of remote
control codes (for a lot of TVs) that are continously sent via an IR
LED. This code uses the PWM module (new in eLua 0.4) and it also does
file I/O from Lua, since the remote control codes are kept in a
separate file (which is also part of the ROM file system). To read the
binary file, the "pack" module (also new in 0.4) is used. To ensure
that we don't get any unexpected delays, the Lua garbage collector is
turned off. Take a look at this sample, it's both a very good proof of
the capabilities of eLua and a good learning resource. To use it on any
of the Cortex boards (EK-LM3S8962 or EK-LM3S6965) connect an IR LED
with a resistor between the "PWM2" and "GND" pins of the extension
connector. Get close to your TV and press the "select" button on your
board to start sending the IR codes. The on-board LED stays lit while
the codes are transmitted and shuts off afterwards. Press the "down"
button on your board to exit the application and return to the shell.</p><p></p>
<h3><a name="piano"></a>piano.lua: because PWM is great</h3><p><strong>Runs on:&nbsp;</strong><br>EK-LM3S8962,&nbsp;EK-LM3S6965, SAM7-EX256</p><strong></strong>
<strong></strong><p><strong>Description:<br></strong>
Yet another use for the PWM module, this sample can be used to "play"
notes via the on-board speaker using the PC keyboard. The on-screen
keyboard shows what keys you must press for different notes, and you
can set your octave and inter-note delay. Press ESC to end your eLua
musical session :) A screenshot can be seen <a href="http://elua.berlios.de/other/elua_piano.png">here</a>.</p><p></p>
<h3><a name="bisect"></a>bisect.lua: floating point at its best</h3>
<p><strong>Runs on:&nbsp;</strong><br>All Targets</p><strong></strong>
<strong></strong><p><strong>Description:<br></strong>This is taken directly from the official Lua distribution, and it's
here to show that eLua can do floating point just like on a desktop
machine, albeit slower. Run it on your target, then run it again, but
this time on the PC, and compare the results. Yes, they are identical.</p><p></p>
<h3><a name="morse"></a>morse.lua: because PWM is great, part II</h3>
<p><strong>Runs on:&nbsp;</strong><br>EK-LM3S8962,&nbsp;EK-LM3S6965, SAM7-EX256</p><strong></strong>
<strong></strong><p><strong>Description:<br></strong>This uses the same PWM module for Morse code generation. Just enter a
text, and listen to it getting Morsed on your board's speaker and on a blinking Led. The
letters and Morse codes are also shown on the terminal. Use '+' and
'-'' to change the frequency, up and down arrows to change the speed,
's' to mute/unmute, and ESC to exit.</p><p></p>
<h3><a name="lhttpd"></a>lhttpd.lua: only with (e)Lua ...</h3>
<p><strong>Runs on:&nbsp;</strong><br>EK-LM3S8962,&nbsp;EK-LM3S6965<strong></strong>
<strong></strong><br></p><p><strong>Description:<br></strong>This is one of those things that can show the real potential of a
language (and hopefully the real potential of eLua in this case). As
you have probably guessed by now, it's a web server&nbsp;written in Lua.
Except that it is much more than this: it's a scripting web server! That's
right, you can embed parts of Lua code into your pages, and the server
will parse them and replace the Lua code with its output. To output
something from Lua code embedded in a web page, simply use "print" in
your Lua code. You can also write your pages completely in Lua (again,
using "print"), the server knows how to handle this too. When is the
last time you heard about a scripting web server in 256k of Flash/64k
of RAM?&nbsp;</p><p>The full list of features is given below:</p>
<ul><li>completely written&nbsp;in Lua</li><li>can handle a single connection at a time (for now)</li><li>can serve text and images (so far)</li><li>gets its files from the ROM file system (this will be extended when more filesystems are added)</li><li>can
execute embedded Lua code and replace it with its output (via "print"
statements). Embed Lua code in your HTML files between tags, make sure
your HTML file extension is ".pht", and the server will preprocess it
and replace the Lua code with its output</li><li>if a file with
".lua" extension is requested, it doesn't send the file, but executes
it and sends its output (via "print" statements)</li></ul>
<p>This is still work in progress, but it already works quite well.
Take a look at romfs/index.pht and romfs/test.lua from the source
distribution for an example of how to include Lua code in your HTML
files.</p><p></p><p></p><p></p></body></html>