Examples of use of OneLua
Buttons reading
msg, contador = true, 0 --2 simple variables...
while true do
buttons.read() --That's it... reading the buttons
if buttons.cross then msg = not msg end --Simple X pulsation
if buttons.held.up then contador = contador + 1 end --arrows held pulsation
if buttons.held.down then contador = contador - 1 end
if buttons.released.start then break end --press & release Start to exit
if msg then screen.print(5, 35, "Press X to make me invisible/visible!" ) end
screen.print(5, 5, "Up/Down to modify the counter\ncurrent value: "..contador)
screen.print(5, 50, "Analog\nX: "..buttons.analogx.. "\nY: "..buttons.analogy)
screen.print(5, 95, "Press Start to exit")
screen.flip()
end
Go!Cam
color.loadpalette()
os.cpu(333)
cam_effects = {"Normal", "Negative", "Black & White", "Sepia", "Blue", "Red", "Green"}
camarita=cam.init()
while not cam.available() do
screen.print(5,5,"Please Connect Your camera!",0.6,color.green)
screen.flip()
end
cam.begin()
Brightness,Contrast=cam.brightness(),cam.contrast()
zoom = cam.zoom()
effect = cam.effect()
iihi = 0
reverse = 0
while true do
buttons.read()
cam.render()
screen.print(5,5,"State: "..tostring(cam.state()).. " available: "..tostring(cam.available()) .. " Shot: "..tostring(shot) .. " FPS: ".. screen.fps(),0.6,color.green)
screen.print(5,20,"Direction: "..tostring(cam.direction()),0.6,color.green)
screen.print(5,35,"Zoom: "..tostring(cam.zoom()),0.6,color.green)
screen.print(5,50,"Brightness: "..tostring(cam.brightness()),0.6,color.green)
screen.print(5,65,"Contrast: "..tostring(cam.contrast()),0.6,color.green)
screen.print(5,80,"Sharpness: "..tostring(cam.sharpness()),0.6,color.green)
screen.print(5,95,"Saturation: "..tostring(cam.saturation()),0.6,color.green)
screen.print(5,110,"Exposure level: "..tostring(cam.evlevel()),0.6,color.green)
screen.print(5,125,"FX Mode: "..tostring(cam_effects[math.max(cam.effect(),1)]).." #"..cam.effect(),0.6,color.green)
screen.print(5,140,"Mirror: "..tostring(cam.mirror()),0.6,color.green)
screen.print(5,155,"Autoreverse: "..tostring(cam.autoreverse()),0.6,color.green)
screen.print(5,170," -- SET --",0.6,color.green)
screen.print(5,185,"Zoom -L / R+",0.6,color.green)
screen.print(5,200,"Brightness UP/Down",0.6,color.green)
screen.print(5,215,"Constrast Left/Right",0.6,color.green)
screen.print(5,230,"Fx Mode Select",0.6,color.green)
screen.print(5,245,"Shot Img Cross",0.6,color.green)
if buttons.r and zoom < 5 then zoom+=1; cam.zoom(zoom) end
if buttons.l and zoom > 1 then zoom-=1; cam.zoom(zoom) end
if buttons.square then cam.term() end
if buttons.circle then os.message(tostring(cam.init())) os.message(tostring(cam.begin())) end
if buttons.triangle then
cam.mirror(reverse);
reverse = reverse + 1;
if reverse >1 then
reverse = 0
end
end
if buttons.cross then
shot = cam.shot("MS0:/PICTURE/cam_shot_"..iihi..".png")
iihi = iihi+1
end
if buttons.held.up then
Brightness+=1
cam.brightness(Brightness)
elseif buttons.held.down then
Brightness-=1
cam.brightness(Brightness)
end
if buttons.select then
effect+=1
if effect > 7 then
effect = 1
end
cam.effect(effect)
end
if buttons.held.right then
Contrast+=1
cam.contrast(Contrast)
elseif buttons.held.left then
Contrast-=1
cam.contrast(Contrast)
end
if Brightness > 255 then Brightness=0 end
if Brightness < 0 then Brightness=255 end
if Contrast > 255 then Contrast=0 end
if Contrast < 0 then Contrast=255 end
screen.flip()
end
Using the SIO port
-- Sample + Simple Terminal Serial In Out @ 02/06/2016
os.cpu(333) -- We put the CPU at full power!. / Ponemos la CPU a maxima potencia!.
Baud = 115200 -- Transmission speed serial port. / Velocidad de transmisión por el puerto serial.
line = 18 -- Current line of text. / Línea actual de texto.
text = {} -- Container of text or data. / Contenedor de texto o datos.
for i=1,18 do -- We fill 18 spaces with an empty string. / Llenamos 18 espacios con un string vacio.
tab[i] = ""
end
-- Display a message, while starts and loads the sio. / Mostrar un mensaje, mientras se inicia y carga el sio.
screen.print(10,10,"Starting Out Serial In 'SIO'")
screen.flip()
-- Start and load the sio. / Inicia y carga el sio.
sio.init()
sio.baud(Baud) -- Set transmission speed. / Configura velocidad de transmisión.
while true do -- Main loop! / Bucle principal!
for r=1, sio.available() do -- Check data available. / Revisa datos disponibles.
local recv = sio.read() -- Get a char of ring buffer. / Obtiene un char de buffer de anillo.
if recv == -1 then continue end -- Check error read. / Revisa error de lectura.
power.tick() -- While data arrives, the PSP is not suspended. / Mientras lleguen datos, el psp no se suspende.
if recv == 10 or #text[line] >= 45 then -- If a line break arrives or line contains 45 characters or more, adjusted lines. / Si llega un salto de linea o la linea contiene 45 caracteres o mas, ajustamos lineas.
line += 1 -- We add one to the current line. / Sumamos uno a la línea actual.
if line > 18 then -- If the line is 18 peruse the rows one row back. / Si la línea es mayor de 18 recorremos las lineas una fila para atras.
for i=1,17 do -- Move all lines backspace. / Movemos todas las líneas un espacio atrás.
text[i] = text[i+1]
end
line = 18 -- Set line to 18 - Configura line a 18
text[line] = "" -- Set this line data to empty string. / Configura esa linea de datos a un string vacio.
end
else -- It is a printable character! / Es un caracter imprimible!
text[line] = text[line]..string.char(recv) -- We concatenate the character with the current line. / Concatenamos el caracter con la linea actual.
end
end
for l=1, 18 do -- We print all lines. / Imprimimos todas las líneas.
screen.print(10, l*15, text[l])
end
screen.flip() -- Screen update. / Actualizamos Pantalla.
end
Scrolling text
-- ## Sample Scroll Print ##
-- We assign some variables to the values of the x axis of each print with scroll
scrollx1 = 10 -- axis to scroll left (right to left)
scrollx2 = 450 -- axis to scroll right (left to right)
scrollx3 = 240 -- axis to scroll through (right to left without vanished)
scrollx4 = 240 -- to scroll seesaw axis (center right to left)
-- load palette
color.loadpalette()
while true do
draw.line(240,0,240,272,color.red) --We draw a line right in the middle of the screen to denote positions
--[[The variable previously assigned to the x axis, is passed as an argument and received as a result of the function
all purposes if the space to show bone the last argument "w" is greater than the width of the text,
then the effect will be zero, and should become normal printing]]
--this __SLEFT like the left alignment will print from the axis x to the right with a space to show 90.
scrollx1 = screen.print(scrollx1,10,"test scroll left",0.7,color.white,color.black,__SLEFT,90)
--this __SRIGHT like the right alignment will print from the axis x to the left with a space to show 100.
scrollx2 = screen.print(scrollx2,25,"test scroll right",0.7,color.white,color.black,__SRIGHT,100)
--this __STHROUGH like the left alignment will print from the axis x to the right with a space to show 100.
scrollx3 = screen.print(scrollx3,50,"test scroll throuh",0.7,color.white,color.black,__STHROUGH,100)
--[[this __SSEESAW just like the alignment center will print from the x axis - half the space to display,
of center right and center left with a space to show 100.
Note: If this mode is not in effect, for the reason previously mentioned,
then your alignment will normally left and printing.]]
scrollx4 = screen.print(scrollx4,70,"test scroll seesaw",0.7,color.white,color.black,__SSEESAW,100)
screen.flip() -- screen update
end
Message box
option = {"OK","CANCEL","YES","NO",""}
--variable where we will receive the answer of mjebox
local response = 5
local title = "Test Of Box" -- title
local msg = "Hello From Box...\nOK = 1\nCANCEL = 2\nYES = 3\nNO = 4\nEXIT = 5" -- message
while true do
buttons.read() -- First read pulses.
if buttons.triangle then -- Press /\
-- muestra dos botones por defecto X:OK, O:CANCEL
response = os.messagebox(title,msg)
elseif buttons.circle then --Press O
-- action shows a button and set O: Exit (exit app)
response = os.messagebox(title,msg,__CIRCLE,__EXIT)
elseif buttons.cross then--Press X
-- shows a box with two buttons and actions defined L: No, R: Yes
response = os.messagebox(title,msg,__L,__NO,__R,__YES)
elseif buttons.square then -- Press []
-- shows three buttons and actions defined X: YES, O: NO, triangle Exit (exit app)
response = os.messagebox(title,msg,__CROSS,__YES,__CIRCLE,__CANCEL,__TRIANGLE,__EXIT)
end
screen.print(10,5,"Sample Message Box")
screen.print(10,17,"Press triangle for simple Message Box (X:OK, O:CANCEL)",0.6)
screen.print(10,32,"Press circle for Message Box (O:EXIT)",0.6)
screen.print(10,47,"Press cross for Message Box (L:NO, R:YES)",0.6)
screen.print(10,62,"Press square for Message Box (X:YES, O:CANCEL, triangle:EXIT)",0.6)
-- We review what is the value returned some action :)
screen.print(10,200,"Result: "..response.." Selection: "..option[response])
screen.flip() -- Upgrade screen
end
Colors
color.loadpalette()
img = image.load("img.png")
x,y=100,100
if img then ctx,ctx2 = img:pixel(x,y) else ctx,ctx2 = color.red,0xFFFFFF03 end
function drawPointer(x,y,c)
draw.gradline(x-5,y,x+5,y,color.yellow,color.red)
draw.gradline(x,y-5,x,y+5,color.red,color.yellow)
end
function toHex(num)
local hexstr = '0123456789ABCDEF'
local s = ''
while num > 0 do
local mod = math.fmod(num, 16)
s = string.sub(hexstr, mod+1, mod+1) .. s
num = math.floor(num / 16)
end
if s == '' then s = '0' end
return "0x"..s
end
while true do
buttons.read()
if img then img:blit(0,0) end
if (buttons.held.right or buttons.held.circle) and x<479 then x += ; ctx,ctx2 = img:pixel(x,y) end
if (buttons.held.left or buttons.held.square) and x>1 then x -= 1; ctx,ctx2 = img:pixel(x,y) end
if (buttons.held.up or buttons.held.triangle) and y>1 then y -= 1; ctx,ctx2 = img:pixel(x,y) end
if (buttons.held.down or buttons.held.cross) and y<271 then y += 1; ctx,ctx2 = img:pixel(x,y) end
screen.print(10,10,string.format("X: %d\t Y: %d",x,y),1,ctx,0xFF000000)
screen.print(10,30,string.format("(%s)",toHex(ctx2)),1,color.red,0xFF000000)
drawPointer(x,y,0xFFFFFF00)
screen.flip()
end