├── README ├── adcpoll └── adcpoll.lua ├── adcscope └── adcscope.lua ├── ds1267 ├── README └── ds1267.lua ├── hello └── hello.lua ├── http-led ├── index.pht ├── lhttpd.lua └── test.lua ├── info └── info.lua ├── int └── inthandlers.lua ├── led └── led.lua ├── life ├── lcdlife.lua ├── life-bit.lua └── life.lua ├── logo ├── logo.bin └── logo.lua └── pwmled └── pwmled.lua /README: -------------------------------------------------------------------------------- 1 | Welcome to eLua examples. 2 | 3 | This is a little project that maintains some basic and intermediate level examples of eLua 4 | code running on it's supported platforms. 5 | Each example is kept separetely in it's own sub-folder. 6 | These examples (and others that became individual projects also found on our github repo) used to be part of our distros, included in the ROMFS. They were removed from the distro to keep it smaller, cleaner and not oriented to a single type of audience. 7 | 8 | You are welcomed to extend these examples to new platfoms and to add new ones too. 9 | Anything that can help new users to quickly understand the basics of eLua is 10 | welcomed here. 11 | For information and tutorials about each of these examples, please refer to their pages on the eLua User Labs wiki at http://wiki.eluaproject.net/Projects 12 | 13 | The main site of the eLua Project is http://www.eluaproject.net 14 | -------------------------------------------------------------------------------- /adcpoll/adcpoll.lua: -------------------------------------------------------------------------------- 1 | -- Acquire ADC samples using a timer with polling for available samples 2 | 3 | if pd.board() == "ET-STM32" then 4 | timer = 2 5 | adcchannels = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 6 | adcsmoothing = {4, 4, 4, 4, 16, 16, 16, 16, 32, 32, 32, 32, 64, 128, 64, 128} 7 | elseif pd.board() == "MBED" then 8 | timer = 1 9 | adcchannels = {0,1,2,3,4,5} 10 | adcsmoothing = {4, 4, 16, 16, 32, 32} 11 | else 12 | timer = 0 13 | adcchannels = {0,1,2,3} 14 | adcsmoothing = {4, 64, 32, 16} 15 | end 16 | 17 | -- Setup ADC and start sampling 18 | for i, v in ipairs(adcchannels) do 19 | adc.setblocking(v,0) -- no blocking on any channels 20 | adc.setsmoothing(v,adcsmoothing[i]) -- set smoothing from adcsmoothing table 21 | adc.setclock(v, 4 ,timer) -- get 4 samples per second, per channel 22 | end 23 | 24 | -- Draw static text on terminal 25 | term.clrscr() 26 | term.print(1,1,"ADC Status:") 27 | term.print(1,3," CH SLEN RES") 28 | term.print(1,#adcchannels+5,"Press ESC to exit.") 29 | 30 | -- start sampling on all channels at the same time 31 | adc.sample(adcchannels,128) 32 | 33 | while true do 34 | for i, v in ipairs(adcchannels) do 35 | -- If samples are not being collected, start 36 | if adc.isdone(v) == 1 then adc.sample(v,128) end 37 | 38 | -- Try and get a sample 39 | tsample = adc.getsample(v) 40 | 41 | -- If we have a new sample, then update display 42 | if not (tsample == nil) then 43 | term.print(1,i+3,string.format("ADC%02d (%03d): %04d\n", v, adcsmoothing[i], tsample)) 44 | end 45 | end 46 | 47 | -- Exit if user hits Escape 48 | key = term.getchar( term.NOWAIT ) 49 | if key == term.KC_ESC then break end 50 | end 51 | 52 | term.clrscr() 53 | term.moveto(1, 1) 54 | -------------------------------------------------------------------------------- /adcscope/adcscope.lua: -------------------------------------------------------------------------------- 1 | -- Acquire ADC samples as quickly as possible, without the use of a timer 2 | -- provides statistics on time and memory usage while running 3 | 4 | if pd.board() == "ET-STM32" then 5 | adcchannels = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 6 | adcsmoothing = {4, 4, 4, 4, 16, 16, 16, 16, 32, 32, 32, 32, 64, 128, 64, 128} 7 | numiter = 50 8 | elseif pd.board() == "MBED" then 9 | adcchannels = {0,1,2,3,4,5} 10 | adcsmoothing = {4, 4, 16, 16, 32, 32} 11 | numiter = 2 12 | else 13 | adcchannels = {0,1,2,3} 14 | adcsmoothing = {4, 16, 64, 128} 15 | numiter = 200 16 | end 17 | 18 | -- Setup ADC 19 | for i, v in ipairs(adcchannels) do 20 | adc.setblocking(v,1) -- block, waiting for samples when an adc.get* function is used 21 | adc.setclock(v,0) -- set clock to zero: no timer, acquire samples as fast as possible 22 | adc.setsmoothing(v,adcsmoothing[i]) -- apply moving average filter using lengths from adcsmoothing 23 | end 24 | 25 | -- Draw static text on terminal 26 | term.clrscr() 27 | term.print(1,1,"ADC Status:") 28 | term.print(1,3," CH SLEN RES") 29 | term.print(1,#adcchannels+7,"Press ESC to exit.") 30 | 31 | -- Use some locals for speed 32 | local adcvals = {} 33 | local key, stime, etime, dtime, i, v 34 | local sample = adc.sample 35 | local insertsamples = adc.insertsamples 36 | local tread = tmr.read 37 | local tstart = tmr.start 38 | 39 | while true do 40 | stime = tstart(0) -- start timer 41 | for j=1,numiter do -- acuire numiter samples 42 | sample(adcchannels, 1) 43 | for i, v in ipairs(adcchannels) do 44 | insertsamples(v,adcvals,i,1) -- for each iteration j, get samples and put them in adcvals 45 | end 46 | end 47 | etime = tread(0) -- get cycle end time 48 | dtime = tmr.gettimediff(0,etime,stime)/numiter -- compute average acquisition time per cycle 49 | 50 | -- draw last acquired samples on the console 51 | term.moveto(1,4) 52 | for i, v in ipairs(adcchannels) do 53 | term.print(string.format("ADC%02d (%03d): %04d\n", v, adcsmoothing[i],adcvals[i])) 54 | term.moveto(1,i+4) 55 | end 56 | 57 | -- draw acquisition statistics 58 | term.print(string.format("Tcyc: %06d (us)\n",dtime)) 59 | term.print(1,#adcchannels+5,string.format("Mem: %03.2f (kB)\n",collectgarbage("count"))) 60 | 61 | key = term.getchar( term.NOWAIT ) 62 | if key == term.KC_ESC then break end -- exit if user hits Escape 63 | end 64 | 65 | term.clrscr() 66 | term.moveto( 1 , 1 ) 67 | -------------------------------------------------------------------------------- /ds1267/README: -------------------------------------------------------------------------------- 1 | This project is described on the eLua User Labs wiki at 2 | http://wiki.eluaproject.net 3 | 4 | -------------------------------------------------------------------------------- /ds1267/ds1267.lua: -------------------------------------------------------------------------------- 1 | -- eLua examples 2 | -- DS1267 Digital Potentiometers SPI controlled by eLua 3 | 4 | clock = spi.setup( 0, spi.MASTER, 1e6, 0, 0, 8 ) 5 | pio.pin.setdir( pio.OUTPUT, pio.PA_4 ) 6 | pio.pin.setlow( pio.PA_4 ) 7 | 8 | function set_ds1267(pos0, pos1) 9 | pio.pin.sethigh( pio.PA_4 ) 10 | spi.write( 0, 0, pos1, pos0 ) 11 | pio.pin.setlow( pio.PA_4) 12 | end 13 | 14 | 15 | -------------------------------------------------------------------------------- /hello/hello.lua: -------------------------------------------------------------------------------- 1 | -- eLua example 2 | -- Hello World, the classic simplest example of a program on a framework 3 | 4 | print( "Hello, World!" ) 5 | 6 | -------------------------------------------------------------------------------- /http-led/index.pht: -------------------------------------------------------------------------------- 1 | 2 | eLua webserver test 3 | 4 |

Welcome to the eLua web server!

5 |
6 |
The right column of the following table is generated by embedded Lua code:
7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
eLua CPU
eLua board
20 |
21 | And now let's test CGI. Use the form below to control the on-board LED.

22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
32 | The LED is now ON
' 37 | elseif reqdata['ledoff'] then 38 | pio.pin.setlow( pio.PF_0 ) 39 | print '
The LED is now OFF
' 40 | elseif next(reqdata) ~= nil then 41 | print '
Invalid CGI request!
' 42 | end 43 | ?> 44 |
45 | 46 | Press here to go to a test page generated completely by a Lua script.
47 |
48 | AND MOST IMPORTANTLY... remember to have fun with eLua! :)
49 |
50 | 51 | -------------------------------------------------------------------------------- /http-led/lhttpd.lua: -------------------------------------------------------------------------------- 1 | -- Check platform 2 | if pd.board() ~= 'EK-LM3S8962' and pd.board() ~= 'EK-LM3S6965' and pd.board() ~= 'EAGLE-100' and pd.board() ~= 'EK-LM3S9B92' then 3 | print( pd.board() .. " not supported by this example" ) 4 | return 5 | end 6 | 7 | -- Mapping between file extension (and request) and HTTP response 8 | local extmap = { 9 | txt = "text/plain", 10 | htm = "text/html", 11 | pht = "text/html", 12 | gif = "image/gif", 13 | jpg = "imge/jpeg", 14 | png = "image/png", 15 | lua = "text/html" 16 | } 17 | 18 | local basedir = "/rom/" 19 | reqdata = {} 20 | 21 | -- Auxiliary function: execute the given code with a substituted "print" 22 | -- that prints everything to a string, return the code output 23 | local function docode(thecode) 24 | local strbuf = {} 25 | local oldprint, newprint = print, function(...) 26 | local total, idx = select('#', ...) 27 | for idx = 1, total do 28 | strbuf[#strbuf + 1] = tostring(select(idx, ...)) .. (idx == total and '\n' or '\t') 29 | end 30 | end 31 | print = newprint 32 | local f = loadstring(thecode) 33 | if f then 34 | f() 35 | else 36 | print ">>> Invalid Lua code <<<" 37 | end 38 | print = oldprint 39 | collectgarbage('collect') 40 | return table.concat(strbuf) 41 | end 42 | 43 | function process(header) 44 | -- look for first line 45 | local s, e = header:find("[^\n]+\n") 46 | local reqstr = header:sub(s, e) 47 | local respheader, respdata = '', '' 48 | 49 | reqdata = {} 50 | -- check if the request is valid, also keep the actual request 51 | local i, valid, req = 0, false 52 | for w in reqstr:gmatch("%S+") do 53 | valid = ( i == 0 and w == "GET" ) or valid 54 | req = ( i == 1 and w ) or req 55 | i = i + 1 56 | end 57 | 58 | -- valid is true if the request is valid, req has the request string 59 | if valid then 60 | -- now look for all parameters in this request (if any) 61 | local fname = "" 62 | if req:find("%?") then 63 | local rest 64 | _, _, fname, rest = req:find("(.*)%?(.*)") 65 | -- small trick: end "rest" with a "&" for easier processing 66 | -- now look for "var=value" pairs in the request (GET encoding) 67 | rest = rest .. "&" 68 | for crtpair in rest:gmatch("[^&]+") do 69 | local _, __, k, v = crtpair:find("(.*)=(.*)") 70 | -- replace all "%xx" characters with their actual value 71 | v = v:gsub("(%%%x%x)", function(s) return string.char(tonumber(s:sub(2, -1), 16)) end) 72 | reqdata[k] = v 73 | end 74 | else 75 | fname = req 76 | end 77 | fname = ( fname == "/" ) and "index.pht" or fname:sub(2, -1) 78 | s, e = fname:find("%.[%a%d]+$") 79 | local ftype = fname:sub(s+1, e):lower() 80 | ftype = (#ftype > 0 and ftype) or "txt" 81 | fname = basedir .. fname 82 | 83 | -- now "fname" has the name of the requested file, and "reqdata" the actual request data 84 | -- also "ftype" holds the file type (actually its extension) 85 | local tf = io.open(fname, "r") 86 | if tf then 87 | respheader = "HTTP/1.1 200 OK\r\nConnection: close\r\nServer: eLua-miniweb\r\nContent-Type: " .. extmap[ftype or "txt"] .. "\r\n\r\n" 88 | -- Preprocess "lua" and "pht" files: run the Lua ones, parse the .htm ones for "" sequences 89 | if ftype == "pht" or ftype == "lua" then 90 | local fdata = tf:read("*a") 91 | if ftype == "lua" then 92 | respdata = docode(fdata) 93 | else 94 | -- Look for patterns and execute them accordingly 95 | respdata = fdata:gsub("<%?lua(.-)%?>", docode) 96 | end 97 | else 98 | respdata = tf:read("*a") 99 | end 100 | tf:close() 101 | else 102 | respheader = "HTTP/1.1 404 Not Found\r\nConnection: close\r\nServer: eLua-miniweb\r\nContent-Type: text/html\r\n\r\nPage not found" 103 | end 104 | end 105 | return respheader .. respdata 106 | end 107 | 108 | while true do 109 | local sock, remoteip, err = net.accept( 80 ) 110 | print( "Got connection on socket", sock ) 111 | print( "Remote ip: " .. net.unpackip( remoteip, "*s" ) ) 112 | local response, err2 = net.recv( sock, 1024 ) 113 | print "Got request" 114 | local httpdata = process( response ) 115 | if #httpdata > 0 then 116 | print "Sending response" 117 | net.send( sock, httpdata ) 118 | end 119 | net.close( sock ) 120 | reqdata = {} 121 | collectgarbage('collect') 122 | end 123 | -------------------------------------------------------------------------------- /http-led/test.lua: -------------------------------------------------------------------------------- 1 | print "" 2 | print "Hello from eLua!
" 3 | print 'Press here to return to the main page.' 4 | print "" 5 | 6 | -------------------------------------------------------------------------------- /info/info.lua: -------------------------------------------------------------------------------- 1 | print( "I'm running on platform " .. pd.platform() ) 2 | print( "The CPU is a " .. pd.cpu() ) 3 | print( "The board name is " .. pd.board() ) 4 | -------------------------------------------------------------------------------- /int/inthandlers.lua: -------------------------------------------------------------------------------- 1 | -- Interrupt handler example 2 | -- From http://www.eluaproject.net/doc/master/en_inthandlers.html 3 | 4 | 5 | local vtmrid = tmr.VIRT0 6 | local to = 1500000 7 | local uartid = 0 8 | 9 | local prev_tmr, new_prev_tmr, prev_gpio 10 | 11 | -- This is the timer interrupt handler 12 | local function tmr_handler( resnum ) 13 | print( string.format( "Timer interrupt for id %d", resnum ) ) 14 | if prev_tmr then prev_tmr( resnum ) end 15 | end 16 | 17 | -- This is the timer interrupt handler that gets set after tmr_handler 18 | local function new_tmr_handler( resnum ) 19 | print( string.format( "NEW HANDLER: timer interrupt for id %d", resnum ) ) 20 | -- This will chain to the previous interrupt handler (tmr_handler above) 21 | if new_prev_tmr then new_prev_tmr( resnum ) end 22 | end 23 | 24 | -- This is the GPIO interrupt on change (falling edge) interrupt 25 | local function gpio_negedge_handler( resnum ) 26 | local port, pin = pio.decode( resnum ) 27 | print( string.format( "GPIO NEGEDGE interrupt on port %d, pin %d", port, pin ) ) 28 | if prev_gpio then prev_gpio( resnum ) end 29 | end 30 | 31 | pio.pin.setdir( pio.INPUT, pio.P0_0 ) 32 | 33 | -- Set timer interrupt handler 34 | prev_tmr = cpu.set_int_handler( cpu.INT_TMR_MATCH, tmr_handler ) 35 | -- Set GPIO interrupt on change (negative edge) interrupt handler 36 | prev_gpio = cpu.set_int_handler( cpu.INT_GPIO_NEGEDGE, gpio_negedge_handler ) 37 | -- Setup periodic timer interrupt for virtual timer 0 38 | tmr.set_match_int( vtmrid, to, tmr.INT_CYCLIC ) 39 | -- Enable GPIO interrupt on change (negative edge) for pin 0 of port 0 40 | cpu.sei( cpu.INT_GPIO_NEGEDGE, pio.P0_0 ) 41 | -- Enable timer match interrupt on virtual timer 0 42 | cpu.sei( cpu.INT_TMR_MATCH, vtmrid ) 43 | 44 | local tmrid, count = 0, 0 45 | while true do 46 | print "Outside interrupt" 47 | for i = 1, 1000 do tmr.delay( tmrid, 1000 ) end 48 | if uart.getchar( uartid, 0 ) ~= "" then break end 49 | count = count + 1 50 | if count == 5 then 51 | print "Changing timer interrupt handler" 52 | new_prev_tmr = cpu.set_int_handler( cpu.INT_TMR_MATCH, new_tmr_handler ) 53 | end 54 | end 55 | 56 | -- Cleanup 57 | -- Stop the timer from generating periodic interrupts 58 | tmr.set_match_int( vtmrid, 0, tmr.INT_CYCLIC ); 59 | -- Disable the GPIO interrupt on change (negative edge) interrupt 60 | cpu.cli( cpu.INT_GPIO_NEGEDGE, pio.P0_0 ) 61 | -- Disable the timer interrupt on match interrupt 62 | cpu.cli( cpu.INT_TMR_MATCH, vtmrid ) 63 | -- Clear the timer interrupt handler 64 | cpu.set_int_handler( cpu.INT_TMR_MATCH, nil ); 65 | -- Clear the GPIO interrupt handler 66 | cpu.set_int_handler( cpu.INT_GPIO_NEGEDGE, nil ); 67 | -------------------------------------------------------------------------------- /led/led.lua: -------------------------------------------------------------------------------- 1 | -- eLua blinking led example, the Hello World of embedded :) 2 | 3 | local uartid, invert, ledpin = 0, false 4 | if pd.board() == "SAM7-EX256" then 5 | ledpin = pio.PB_20 6 | elseif pd.board() == "EK-LM3S8962" or pd.board() == "EK-LM3S6965" then 7 | ledpin = pio.PF_0 8 | elseif pd.board() == "EAGLE-100" then 9 | ledpin = pio.PE_1 10 | elseif pd.board() == "STR9-COMSTICK" then 11 | ledpin = pio.P9_0 12 | elseif pd.board() == "LPC-H2888" then 13 | ledpin = pio.P2_1 14 | elseif pd.board() == "MOD711" then 15 | ledpin = pio.P1_7 16 | uartid = 1 17 | elseif pd.board() == "ATEVK1100" then 18 | ledpin = pio.PB_27 19 | invert = true 20 | elseif pd.board() == "STR-E912" then 21 | ledpin = pio.P6_4 22 | elseif pd.board() == "ELUA-PUC" then 23 | ledpin = pio.P1_20 24 | elseif pd.board() == "ET-STM32" then 25 | ledpin = pio.PA_5 26 | elseif pd.board() == "STM32F4DISCOVERY" then 27 | ledpin = pio.PD_13 28 | uartid = uart.CDC 29 | elseif pd.board() == "MBED" then 30 | ledpin = mbed.pio.LED1 31 | mbed.pio.configpin( ledpin, 0, 0, 0 ) 32 | elseif pd.board() == "MIZAR32" then 33 | ledpin = pio.PB_29 34 | invert = true 35 | else 36 | print( "\nError: Unknown board " .. pd.board() .. " !" ) 37 | return 38 | end 39 | 40 | function cycle() 41 | if not invert then 42 | pio.pin.sethigh( ledpin ) 43 | else 44 | pio.pin.setlow( ledpin ) 45 | end 46 | tmr.delay( 0, 500000 ) 47 | if not invert then 48 | pio.pin.setlow( ledpin ) 49 | else 50 | pio.pin.sethigh( ledpin ) 51 | end 52 | tmr.delay( 0, 500000 ) 53 | end 54 | 55 | pio.pin.setdir( pio.OUTPUT, ledpin ) 56 | print( "Hello from eLua on " .. pd.board() ) 57 | print "Watch your LED blinking :)" 58 | print "Enjoy eLua !" 59 | print "Press any key to end this demo.\n" 60 | 61 | while uart.getchar( uartid, 0 ) == "" do 62 | cycle() 63 | end 64 | 65 | -------------------------------------------------------------------------------- /life/lcdlife.lua: -------------------------------------------------------------------------------- 1 | -- life.lua 2 | -- eLua version by Bogdan Marinescu, www.eluaproject.net 3 | -- original by Dave Bollinger posted to lua-l 4 | -- modified to use ANSI terminal escape sequences 5 | -- modified to use for instead of while 6 | 7 | function ARRAY2D(w,h) 8 | local t = {w=w,h=h} 9 | for y=1,h do 10 | t[y] = {} 11 | for x=1,w do 12 | t[y][x]=0 13 | end 14 | end 15 | return t 16 | end 17 | 18 | _CELLS = {} 19 | 20 | -- give birth to a "shape" within the cell array 21 | function _CELLS:spawn(shape,left,top) 22 | for y=0,shape.h-1 do 23 | for x=0,shape.w-1 do 24 | self[top+y][left+x] = shape[y*shape.w+x+1] 25 | end 26 | end 27 | end 28 | 29 | -- run the CA and produce the next generation 30 | function _CELLS:evolve(next) 31 | local ym1,y,yp1,yi=self.h-1,self.h,1,self.h 32 | while yi > 0 do 33 | local xm1,x,xp1,xi=self.w-1,self.w,1,self.w 34 | while xi > 0 do 35 | local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + 36 | self[y][xm1] + self[y][xp1] + 37 | self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] 38 | next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 39 | xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 40 | end 41 | ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 42 | end 43 | end 44 | 45 | -- output the array to screen 46 | function _CELLS:draw() 47 | local pixval = { 16, 8, 4, 2, 1 } 48 | -- reprogram each user-defined character doing each column from left to right 49 | for charcol = 0, 3 do 50 | for charrow = 0, 1 do 51 | local charval = { 0, 0, 0, 0, 0, 0, 0, 0 } 52 | for pixrow = 1, 8 do 53 | local rowval = 0 -- value to program into this pixel row of the char 54 | local y = pixrow + charrow * 8 55 | for pixcol = 1, 5 do 56 | local x = pixcol + charcol * 5 57 | if self[y][x] > 0 then 58 | rowval = rowval + pixval[pixcol] 59 | end 60 | end 61 | charval[pixrow] = rowval 62 | end 63 | mizar32.lcd.definechar(charcol + charrow*4, charval) 64 | end 65 | end 66 | end 67 | 68 | -- constructor 69 | function CELLS(w,h) 70 | local c = ARRAY2D(w,h) 71 | c.spawn = _CELLS.spawn 72 | c.evolve = _CELLS.evolve 73 | c.draw = _CELLS.draw 74 | return c 75 | end 76 | 77 | -- 78 | -- shapes suitable for use with spawn() above 79 | -- 80 | HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } 81 | GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } 82 | EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } 83 | FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 } 84 | BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 } 85 | 86 | -- the main routine 87 | function LIFE(w,h) 88 | -- create two arrays 89 | local thisgen = CELLS(w,h) 90 | local nextgen = CELLS(w,h) 91 | 92 | -- create some life 93 | -- about 1000 generations of fun, then a glider steady-state 94 | thisgen:spawn(GLIDER,5,4) 95 | thisgen:spawn(EXPLODE,25,10) 96 | thisgen:spawn(FISH,4,12) 97 | 98 | -- run until break 99 | local gen=1 100 | while true do 101 | thisgen:evolve(nextgen) 102 | thisgen,nextgen = nextgen,thisgen 103 | thisgen:draw() 104 | gen=gen+1 105 | if gen>2000 then break end 106 | end 107 | end 108 | 109 | -- We do pixel graphics by printing the 8 user-definable characters as two 110 | -- rows fo four characters, then redefining their bitmap on the fly. 111 | -- 0 1 2 3 112 | -- 4 5 6 7 113 | -- and each character is 5 pixels wide by 8 pixels high. 114 | 115 | -- First, clear all the user-defined characters 116 | mizar32.lcd.reset() 117 | for c = 0,7 do mizar32.lcd.definechar( c, {} ) end 118 | mizar32.lcd.print(0, 1, 2, 3, " LCD") 119 | mizar32.lcd.goto(2,1) 120 | mizar32.lcd.print(4, 5, 6, 7, " Life") 121 | 122 | LIFE(4*5, 2*8) 123 | -------------------------------------------------------------------------------- /life/life-bit.lua: -------------------------------------------------------------------------------- 1 | -- life.lua 2 | -- eLua version by Bogdan Marinescu, www.eluaproject.net 3 | -- original by Dave Bollinger posted to lua-l 4 | -- modified to use ANSI terminal escape sequences 5 | -- modified to use for instead of while 6 | -- modified to be faster than out = out .. '0' using table.concat() 7 | 8 | -- modified to use less memory by using bitarray 9 | 10 | local write=io.write 11 | 12 | ALIVE="O" DEAD="-" 13 | 14 | function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary 15 | for i = 1,500000 do end 16 | end 17 | 18 | function ARRAY2D(w,h) 19 | local t = {w=w,h=h} 20 | for y=1,h do 21 | t[y] = bitarray.new(w, 1, 0) 22 | -- for x=1,w do 23 | -- t[y][x]=0 24 | -- end 25 | end 26 | return t 27 | end 28 | 29 | _CELLS = {} 30 | 31 | -- give birth to a "shape" within the cell array 32 | function _CELLS:spawn(shape,left,top) 33 | for y=0,shape.h-1 do 34 | for x=0,shape.w-1 do 35 | self[top+y][left+x] = shape[y*shape.w+x+1] 36 | end 37 | end 38 | end 39 | 40 | -- run the CA and produce the next generation 41 | function _CELLS:evolve(next) 42 | local ym1,y,yp1,yi=self.h-1,self.h,1,self.h 43 | while yi > 0 do 44 | local xm1,x,xp1,xi=self.w-1,self.w,1,self.w 45 | while xi > 0 do 46 | local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + 47 | self[y][xm1] + self[y][xp1] + 48 | self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] 49 | next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 50 | xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 51 | end 52 | ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 53 | end 54 | end 55 | 56 | -- output the array to screen 57 | function _CELLS:draw() 58 | local out = {} -- accumulate to reduce flicker 59 | for y=1,self.h do 60 | for x=1,self.w do 61 | out[#out + 1] = ((self[y][x]>0) and ALIVE) or DEAD 62 | end 63 | out[#out + 1] = "\n" 64 | end 65 | write(table.concat(out)) 66 | end 67 | 68 | -- constructor 69 | function CELLS(w,h) 70 | local c = ARRAY2D(w,h) 71 | c.spawn = _CELLS.spawn 72 | c.evolve = _CELLS.evolve 73 | c.draw = _CELLS.draw 74 | return c 75 | end 76 | 77 | -- 78 | -- shapes suitable for use with spawn() above 79 | -- 80 | --HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } 81 | GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } 82 | --EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } 83 | --FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 } 84 | --BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 } 85 | --LINE = { 1,1,1,1,1,1,1,1,1,1; w=10,h=1 } 86 | 87 | -- the main routine 88 | function LIFE(w,h) 89 | -- create two arrays 90 | local thisgen = CELLS(w,h) 91 | local nextgen = CELLS(w,h) 92 | 93 | -- create some life 94 | -- about 1000 generations of fun, then a glider steady-state 95 | thisgen:spawn(GLIDER,5,4) 96 | --thisgen:spawn(EXPLODE,25,10) 97 | --thisgen:spawn(FISH,4,12) 98 | --thisgen:spawn(LINE,10,10) 99 | 100 | -- run until break 101 | local gen=1 102 | write("\027[2J") -- ANSI clear screen 103 | while true do 104 | thisgen:evolve(nextgen) 105 | thisgen,nextgen = nextgen,thisgen 106 | write("\027[H") -- ANSI home cursor 107 | thisgen:draw() 108 | write("Life - generation ",gen,", mem ", string.format("%d",collectgarbage('count')), " kB\n") 109 | gen=gen+1 110 | if gen>2000 then break end 111 | delay() -- no delay 112 | end 113 | end 114 | 115 | LIFE(10,16) 116 | -------------------------------------------------------------------------------- /life/life.lua: -------------------------------------------------------------------------------- 1 | -- life.lua 2 | -- eLua version by Bogdan Marinescu, www.eluaproject.net 3 | -- original by Dave Bollinger posted to lua-l 4 | -- modified to use ANSI terminal escape sequences 5 | -- modified to use for instead of while 6 | -- modified to be faster than out = out .. '0' using table.concat() 7 | 8 | local write=io.write 9 | 10 | ALIVE="O" DEAD="-" 11 | 12 | function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary 13 | for i = 1,50000000 do end 14 | end 15 | 16 | function ARRAY2D(w,h) 17 | local t = {w=w,h=h} 18 | for y=1,h do 19 | t[y] = {} 20 | for x=1,w do 21 | t[y][x]=0 22 | end 23 | end 24 | return t 25 | end 26 | 27 | _CELLS = {} 28 | 29 | -- give birth to a "shape" within the cell array 30 | function _CELLS:spawn(shape,left,top) 31 | for y=0,shape.h-1 do 32 | for x=0,shape.w-1 do 33 | self[top+y][left+x] = shape[y*shape.w+x+1] 34 | end 35 | end 36 | end 37 | 38 | -- run the CA and produce the next generation 39 | function _CELLS:evolve(next) 40 | local ym1,y,yp1,yi=self.h-1,self.h,1,self.h 41 | while yi > 0 do 42 | local xm1,x,xp1,xi=self.w-1,self.w,1,self.w 43 | while xi > 0 do 44 | local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + 45 | self[y][xm1] + self[y][xp1] + 46 | self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] 47 | next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 48 | xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 49 | end 50 | ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 51 | end 52 | end 53 | 54 | -- output the array to screen 55 | function _CELLS:draw() 56 | local out = {} -- accumulate to reduce flicker 57 | for y=1,self.h do 58 | for x=1,self.w do 59 | out[#out + 1] = ((self[y][x]>0) and ALIVE) or DEAD 60 | end 61 | out[#out + 1] = "\n" 62 | end 63 | write(table.concat(out)) 64 | end 65 | 66 | -- constructor 67 | function CELLS(w,h) 68 | local c = ARRAY2D(w,h) 69 | c.spawn = _CELLS.spawn 70 | c.evolve = _CELLS.evolve 71 | c.draw = _CELLS.draw 72 | return c 73 | end 74 | 75 | -- 76 | -- shapes suitable for use with spawn() above 77 | -- 78 | HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } 79 | GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } 80 | EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } 81 | FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 } 82 | BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 } 83 | LINE = { 1,1,1,1,1,1,1,1,1,1; w=10,h=1 } 84 | 85 | -- the main routine 86 | function LIFE(w,h) 87 | -- create two arrays 88 | local thisgen = CELLS(w,h) 89 | local nextgen = CELLS(w,h) 90 | 91 | -- create some life 92 | -- about 1000 generations of fun, then a glider steady-state 93 | --thisgen:spawn(GLIDER,5,4) 94 | --thisgen:spawn(EXPLODE,25,10) 95 | --thisgen:spawn(FISH,4,12) 96 | thisgen:spawn(LINE,10,10) 97 | 98 | -- run until break 99 | local gen=1 100 | write("\027[2J") -- ANSI clear screen 101 | while true do 102 | thisgen:evolve(nextgen) 103 | thisgen,nextgen = nextgen,thisgen 104 | write("\027[H") -- ANSI home cursor 105 | thisgen:draw() 106 | write("Life - generation ",gen,", mem ", string.format("%d",collectgarbage('count')), " kB\n") 107 | gen=gen+1 108 | if gen>2000 then break end 109 | delay() -- no delay 110 | end 111 | end 112 | 113 | LIFE(40,20) 114 | -------------------------------------------------------------------------------- /logo/logo.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elua/examples/b2222c5fb27d8c1a870980ee201cc0efb1a02d6c/logo/logo.bin -------------------------------------------------------------------------------- /logo/logo.lua: -------------------------------------------------------------------------------- 1 | local disp = lm3s.disp 2 | 3 | disp.init( 1000000 ) 4 | disp.clear() 5 | 6 | local math = math 7 | local floor = math.floor 8 | local pi = math.pi 9 | 10 | local img = io.open( "/rom/logo.bin", "rb") 11 | if img == nil then 12 | img = io.open( "/mmc/logo.bin", "rb") 13 | end 14 | if img == nil then 15 | print "Unable to load image" 16 | return 0 17 | end 18 | local imgdata = img:read( "*a" ) 19 | img:close() 20 | 21 | local maxx , maxy = 64, 64 22 | local xf, yf = maxx / 2 - 1, maxy / 2 - 1 23 | local ct = yf * maxx + xf + 1 24 | 25 | local function rotate( imgdata, angle ) 26 | local c = math.cos( angle ) 27 | local s = math.sin( angle ) 28 | local newdata = bitarray.new( maxx * maxy, 4 ) 29 | local xc, ys = -xf * c, -yf * s 30 | local xs, yc = -xf * s, -yf * c 31 | local p1, p2 = xc - ys, xs + yc 32 | local p3, p4 = -xc - ys, -xs + yc 33 | local xx1, yy1, xx2, yy2 34 | local widx1, widx2 = 1, 2*xf + 1 35 | local widx3, widx4 = 2 * yf * maxx + 1, 2 * yf * maxx + 2 * xf + 1 36 | local w1, w2, w3, w4 37 | for y = -yf, 0 do 38 | xx1, yy1, xx2, yy2 = p1, p2, p3, p4 39 | w1, w2, w3, w4 = widx1, widx2, widx3, widx4 40 | for x = -xf, 0 do 41 | if ( xx1 >= -xf ) and ( xx1 <= xf ) and ( yy1 >= -yf ) and ( yy1 <= yf ) then 42 | newdata[ w1 ] = imgdata[ floor( yy1 ) * maxx + floor( xx1 ) + ct ] 43 | end 44 | if ( xx2 >= -xf ) and ( xx2 <= xf ) and ( yy2 >= -yf ) and ( yy2 <= yf ) then 45 | newdata[ w2 ] = imgdata[ floor( yy2 ) * maxx + floor( xx2 ) + ct ] 46 | end 47 | if ( -xx2 >= -xf ) and ( -xx2 <= xf ) and ( -yy2 >= -yf ) and ( -yy2 <= yf ) then 48 | newdata[ w3 ] = imgdata[ floor( -yy2 ) * maxx + floor( -xx2 ) + ct ] 49 | end 50 | if ( -xx1 >= -xf ) and ( -xx1 <= xf ) and ( -yy1 >= -yf ) and ( -yy1 <= yf ) then 51 | newdata[ w4 ] = imgdata[ floor( -yy1 ) * maxx + floor( -xx1 ) + ct ] 52 | end 53 | xx1 = xx1 + c ; yy1 = yy1 + s ; xx2 = xx2 - c ; yy2 = yy2 - s 54 | w1 = w1 + 1 ; w2 = w2 - 1 ; w3 = w3 + 1 ; w4 = w4 - 1 55 | end 56 | p1 = p1 - s ; p2 = p2 + c ; p3 = p3 - s ; p4 = p4 + c 57 | widx1 = widx1 + maxx ; widx2 = widx2 + maxx ; widx3 = widx3 - maxx ; widx4 = widx4 - maxx 58 | end 59 | return bitarray.tostring( newdata, "raw" ) 60 | end 61 | 62 | local origx = ( 128 - maxx ) / 2 63 | local origy = ( 96 - maxy ) / 2 64 | disp.draw( imgdata, origx, origy, maxx, maxy ) 65 | local imgd = bitarray.new( imgdata, 4 ) 66 | imgdata = nil 67 | collectgarbage() 68 | 69 | local delta = pi / 8 70 | local angles = { 0, delta, pi / 4, 3 * delta, pi / 2, 5 * delta, 3 * pi / 4, 7 * delta, pi, 71 | 9 * delta, 5 * pi / 4, 11 * delta, 3 * pi / 2, 13 * delta, 7 * pi / 4, 15 * delta } 72 | local index = 2 73 | 74 | while uart.getchar( 0, uart.NO_TIMEOUT ) == "" do 75 | local newimg = rotate( imgd, angles[ index ] ) 76 | disp.draw( newimg, origx, origy, maxx, maxy ) 77 | newimg = nil 78 | collectgarbage() 79 | index = index + 1 80 | index = index > #angles and 1 or index 81 | end 82 | 83 | disp.clear() 84 | 85 | -------------------------------------------------------------------------------- /pwmled/pwmled.lua: -------------------------------------------------------------------------------- 1 | -- eLua examples 2 | -- Control LED intensity with PWM 3 | 4 | local pwmid, tmrid, ledpin 5 | if pd.board() == 'EK-LM3S8962' or pd.board() == 'EK-LM3S6965' or pd.board() == 'ET-STM32' or pd.board() == 'EAGLE-100' then 6 | pwmid, tmrid = 0, 1 7 | pwm.setclock( pwmid, 25000000 ) 8 | elseif pd.board() == 'ELUA-PUC' then 9 | pwmid, tmrid = 7, 0 10 | local psel = cpu.r32( cpu.IO_PINSEL3 ) 11 | psel = bit.bor( bit.band( psel, 0xFFFFFCFF ), 0x00000200 ) 12 | cpu.w32( cpu.IO_PINSEL3, psel ) 13 | elseif pd.board() == 'MBED' then 14 | pwmid, tmrid = 1, 0 15 | mbed.pio.configpin(mbed.pio.LED1, 2, 0, 0) 16 | else 17 | print( pd.board() .. " not supported by this example" ) 18 | return 19 | end 20 | 21 | print "Control LED with PWM (fade up/down)" 22 | print "Press any key to exit" 23 | 24 | local crtduty, incr = 10, 5 25 | tmr.start( tmrid ) 26 | pwm.setup( pwmid, 50000, crtduty ) 27 | pwm.start( pwmid ) 28 | 29 | while uart.getchar( 0, 0 ) == "" do 30 | if crtduty == 95 or crtduty == 5 then 31 | incr = -incr 32 | end 33 | crtduty = crtduty + incr 34 | pwm.setup( pwmid, 50000, crtduty ) 35 | tmr.delay( tmrid, 50000 ) 36 | end 37 | 38 | pwm.stop( pwmid ) 39 | --------------------------------------------------------------------------------