Skip to content

Commit

Permalink
Some Lua scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bsongis committed Dec 15, 2021
1 parent 57218c0 commit a659c27
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lua/lcddemo/lcddemo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Lua LCD Test widget

local bitmap, mask

local function paint(widget)
-- Demo of a mask
lcd.color(lcd.RGB(255, 255, 0))
lcd.drawMask(0, 10, mask)

-- Demo of a text
lcd.font(FONT_ITALIC)
lcd.color(lcd.RGB(255,0,255))
lcd.drawText(15, 10, "FONT")

-- Demo of a bitmap
local w2, h2 = lcd.getWindowSize()
lcd.drawBitmap(10, 25, bitmap, w2 - 20, h2 - 35)
end

local function init()
bitmap = lcd.loadBitmap("FLASH:/bitmaps/system/default_glider.png")
mask = lcd.loadMask("FLASH:/bitmaps/system/mask_dot.png")
system.registerWidget({key="lcddemo", name="Lua LCD Demo", paint=paint})
end

return {init=init}
55 changes: 55 additions & 0 deletions lua/rssi/rssi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- Lua RSSI widget

local function create()
return {value=nil}
end

local function paint(widget)
local w, h = lcd.getWindowSize()
local text_w, text_h = lcd.getTextSize("")
if widget.value ~= nil then
lcd.font(FONT_XL)
lcd.drawText(w/2, (h - text_h)/2, "RSSI = "..widget.value, CENTERED)
end
end

local function wakeup(widget)
local sensor = system.getSource("RSSI")
local newValue = nil
if sensor ~= nil then
newValue = sensor:stringValue()
end
if widget.value ~= newValue then
widget.value = newValue
lcd.invalidate()
end
end

local function menu(widget)
return {
{"Lua... playNumber(RSSI)",
function()
local sensor = system.getSource("RSSI")
system.playNumber(sensor:value(), sensor:unit(), sensor:decimals())
end},
}
end

local function event(widget, category, value, x, y)
print("Event received:", category, value, x, y)
if category == EVT_KEY and value == KEY_ENTER_LONG then
print("Board " .. system.getVersion().board .. " Version " .. system.getVersion().version)
print("Date " .. os.date() .. " Time " .. os.time())
lcd.invalidate()
system.killEvent(value)
return true
else
return false
end
end

local function init()
system.registerWidget({key="RSSI", name="Lua RSSI", create=create, paint=paint, event=event, menu=menu, wakeup=wakeup})
end

return {init=init}
169 changes: 169 additions & 0 deletions lua/snake/snake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
-- Lua Snake game

local translations = {en="Lua Snake", fr="Serpent Lua"}

local function name(widget)
local locale = system.getLocale()
return translations[locale] or translations["en"]
end

local wCell = 12
local xMax
local yMax
local game_map = {}
local Head
local Tail
local highscore = 0
local size = 3
local Food = {x=false, y=false}
local direction = "right"
local score = 0

local function create_food()
Food.x, Food.y = math.random(xMax - 1), math.random(yMax - 1)
while game_map[Food.x][Food.y] do
Food.x, Food.y = math.random(xMax - 1), math.random(yMax - 1)
end
game_map[Food.x][Food.y] = "food"
end

local function eat_food()
game_map[Head.x][Head.y] = nil
create_food()
score = score + 1
end

local function check_collision()
if Head.x < 0 or Head.x > xMax then
return true
elseif Head.y < 0 or Head.y > yMax then
return true
elseif ((game_map[Head.x][Head.y]) and (game_map[Head.x][Head.y] ~= "food")) then
return true
end
return false
end

local function move()
if game_map[Tail.x][Tail.y] == "right" then
Tail.dx = 1
Tail.dy = 0
elseif game_map[Tail.x][Tail.y] == "left" then
Tail.dx = -1
Tail.dy = 0
elseif game_map[Tail.x][Tail.y] == "up" then
Tail.dx = 0
Tail.dy = -1
elseif game_map[Tail.x][Tail.y] == "down" then
Tail.dx = 0
Tail.dy = 1
end

game_map[Head.x][Head.y] = direction
Head.x = Head.x + Head.dx
Head.y = Head.y + Head.dy

lcd.invalidate()

if Head.x < 0 or Head.x > xMax or Head.y < 0 or Head.y > yMax then
system.exit()
elseif game_map[Head.x][Head.y] == "food" then
eat_food()
else
game_map[Tail.x][Tail.y] = nil
Tail.x = Tail.x + Tail.dx
Tail.y = Tail.y + Tail.dy
end
end

local function create()
local w, h = lcd.getWindowSize()
xMax = math.floor(w / wCell) - 1
yMax = math.floor(h / wCell) - 1

food = false
size = 3
score = 0
Head = {x=3, y=1, dx=1, dy=0}
Tail = {x=1, y=1, dx=1, dy=0}
direction = "right"

for i = 0, xMax, 1 do
game_map[i] = {}
end

for i = 0, size - 1, 1 do
game_map[Tail.x + (i * Tail.dx)][Tail.y + (i * Tail.dy)] = direction
end

create_food()
end

local function wakeup()
move()
end

local function event(widget, category, value, x, y)
print("Event received:", category, value, x, y, KEY_RIGHT_FIRST)

local dir = direction
if category == EVT_KEY and value == KEY_RIGHT_FIRST and direction ~= "left" then
dir = "right"
Head.dx = 1
Head.dy = 0
end
if category == EVT_KEY and value == KEY_LEFT_FIRST and direction ~= "right" then
dir = "left"
Head.dx = -1
Head.dy = 0
end
if category == EVT_KEY and value == KEY_UP_FIRST and direction ~= "down" then
dir = "up"
Head.dx = 0
Head.dy = -1
end
if category == EVT_KEY and value == KEY_DOWN_FIRST and direction ~= "up" then
dir = "down"
Head.dx = 0
Head.dy = 1
end

direction = dir

return true
end

local function paint()
lcd.color(lcd.RGB(40, 40, 40))

for x = 0, xMax, 1 do
lcd.drawFilledRectangle(x * wCell, 0, 1, yMax * wCell)
end

for y = 0, yMax, 1 do
lcd.drawFilledRectangle(0, y * wCell, xMax * wCell, 1)
end

for x = 0, xMax, 1 do
for y = 0, yMax, 1 do
local cell = game_map[x][y]
if cell ~= nil then
if cell == "food" then
lcd.color(lcd.RGB(79, 195, 247))
else
lcd.color(lcd.RGB(255, 255, 255))
end
lcd.drawFilledRectangle(x * wCell + 1, y * wCell + 1, wCell - 1, wCell - 1)
end
end
end
end

local icon = lcd.loadMask("/scripts/snake.png")

local function init()
system.registerSystemTool({name=name, icon=icon, create=create, wakeup=wakeup, event=event, paint=paint})
end

return {init=init}

Binary file added lua/snake/snake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a659c27

Please sign in to comment.