mirror of
https://github.com/elua/elua.git
synced 2025-01-25 01:02:54 +08:00
133 lines
9.5 KiB
HTML
133 lines
9.5 KiB
HTML
<!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>Exemplos de eLua</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../style.css"></head>
|
||
<body style="background-color: rgb(255, 255, 255);">
|
||
<h3>Exemplos de c<>digo Lua</h3>Distribui<EFBFBD><EFBFBD>es eLua trazem <20>timos e interessantes (claro! tem jogos tamb<6D>m! :) exemplos de programas em Lua.
|
||
Eles est<73>o inclu<6C>dos tamb<6D>m na distribui<75><69>o que vem com os c<>digos fontes, no subdiret<65>rio /romfs. <br>Programas Lua para eLua s<>o mostrados e comentados aqui tamb<6D>m. <br>Como j<> foi dito, voc<6F> pode rod<6F>-los a partir do sistema de arquivos de eLua ou pode usar o shell de eLua e envi<76>-los atrav<61>s do XMODEM, como or you can use the eLua shell and send them via XMODEM, as
|
||
descrito <a href="using.html#shell">aqui</a>.<br><br>
|
||
|
||
<h3><a name="hello"></a>hello.lua: o ob<6F>quo "Hello, World!"</h3>
|
||
|
||
<p><strong>Roda em: </strong><br>Todas as arquiteturas</p><p><strong></strong>
|
||
|
||
<strong>Descri<EFBFBD><EFBFBD>o:<br></strong>Chamar isto de um "programa" <20> um grande exagero, mas j<> <20> uma
|
||
tradi<EFBFBD><EFBFBD>o, logo, vamos mant<6E>-la :) Este programa imprime "Hello, World!" em um terminal e
|
||
retorna para o shell. Baixe-o somente se sentir-se muito incomodado em executar o interpretador Lua inserido em eLua e digit<69>-lo voc<6F> mesmo :)</p><p><strong>C<EFBFBD>digo fonte comentado:</strong><br></p><p>print("Hello World")</p>
|
||
|
||
<h2><br></h2><h3><a name="info"></a>info.lua: Obtendo os dados sobre a plataforma</h3>
|
||
|
||
<p><strong>Roda em: </strong><br>Todas as arquiteturas</p><p><strong>Descri<EFBFBD><EFBFBD>o:<br></strong>
|
||
Este programa n<>o <20> muito mais complicado do que o "Hello, World!", por<6F>m ele mostra um m<>dulo espec<65>fico de eLua: o m<>dulo "platform data" (pd). Voc<6F> pode ler
|
||
mais sobre os m<>dulos "platform" na distribui<75><69>o do c<>digo fonte
|
||
(docs/platform_modules.txt). O programa mostrar<61> o nome da plataforma, o nome da CPU, o nome da placa e o clock da CPU e ent<6E>o finaliza e volta para o shell.</p><p><strong>C<EFBFBD>digo fonte comentado:</strong></p><p><span>-- Usa o m<>dulo pd para ter acesso aos dados da plataforma e mostr<74>-lo no Terminal </span><br><span>print( "Estou usando a platforma " .. pd.platform() ) </span><br><span>print( "A CPU <20> uma " .. pd.cpu() )</span><br><span><span>print( "A placa <20> uma " .. pd.board(</span>) )</span><br></p><p></p>
|
||
|
||
<h3><a name="led"></a>led.lua: o velho LED que pisca, jeito novo de fazer com eLua</h3>
|
||
|
||
<p><strong>Roda em: </strong><br>Todas as arquiteturas exceto i386</p><p><strong>Descri<EFBFBD><EFBFBD>o:<br></strong><strong></strong>
|
||
Agora temos que fazer algo com "cara" de embarcado: piscar um LED. O c<>digo a seguir ilustra alguns recursos interessantes de eLua:</p>
|
||
|
||
<ul><li><p>c<EFBFBD>digo port<72>til entre plataformas: o c<>digo atribue um pino diferente
|
||
para o LED come<6D>ando pelo nome da placa. Voc<6F> pode ver como o m<>dulo de dados da plataforma torna a portabilidade do c<>digo muito f<>cil.</p></li><li><p>uart, pio, tmr, pd modules: todas eles s<>o usados aqui.</p></li></ul>
|
||
|
||
<p>Observe ele piscando, ent<6E>o pressione qualquer tecla para voltar ao shell de eLua.</p><p><strong>C<EFBFBD>digo fonte comentado:</strong></p>
|
||
|
||
<h3><a name="hangman"></a>hangman.lua: tirando vantagem de seu terminal</h3>
|
||
|
||
<p><strong>Roda em: </strong><br>Todas as arquiteturas exceto i386</p><strong></strong>
|
||
|
||
<strong></strong><p><strong>Descri<EFBFBD><EFBFBD>o:<br></strong>Muito longe de ser o melhor exemplo da distribui<75><69>o eLua (ou poderia ser o morse.lua? :), ele faz uso do
|
||
m<EFBFBD>dulo term (docs/terminal_support.txt) para deixar o usu<73>rio jogar como no
|
||
BSD "hangman" diretamente no seu emulador de terminal. Rode o exemplo
|
||
e aproveite. Atualmente existe uma pequena lista de palavras, pois este programa foi escrito principalmente com o prop<6F>sito de testar a capacidade de eLua, mas <20> muito f<>cil
|
||
acrescentar e substituir palavras na lista atual. Uma tela de exemplo pode ser vista <a href="http://elua.berlios.de/other/elua_hangman.png">aqui</a>.</p><p></p>
|
||
|
||
<h3><a name="pwmled"></a>pwmled.lua: Piscador de LED, classe avan<61>ada</h3><p><strong>Roda em: </strong><br>EK-LM3S8962, EK-LM3S6965</p><strong></strong>
|
||
|
||
<strong></strong><p><strong>Descri<EFBFBD><EFBFBD>o:<br></strong>
|
||
Este programa usa o m<>dulo PWM para acender/apagar a luz do LED gradualmente, indefinidamente. Nada mais a dizer aqui, o c<>digo <20> bem simples, ainda que os
|
||
resultados sejam bem interessantes. Presione qualquer tecla para finalizar o programa e retornar ao shell.</p><p></p>
|
||
|
||
<h3><a name="tvbgone"></a>tvbgone.lua: yes, eLua can do real time!</h3>
|
||
|
||
<p><strong>Runs on: </strong><br>EK-LM3S8962, 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: </strong><br>EK-LM3S8962, 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: </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: </strong><br>EK-LM3S8962, 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: </strong><br>EK-LM3S8962, 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 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? </p><p>The full list of features is given below:</p>
|
||
|
||
<ul><li>completely written 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> |