CookBook
ℹ️
Access gadget’s components
-- To access the gadget's components use
-- gdt.<componentname>
gdt.Led0.State = true
ℹ️
Color functions
-- Every color parameter accepts a color type that can
-- be filled in different ways
-- constants
local white:color = color.white
-- RGB components
local green:color = Color(0,255,0)
-- some HSV color. The first value ranges 0-360, the other two 0-1
local someColor:color = ColorHSV(50, 0.1, 0.1)
-- Color with transparency
local transparentRed:color = ColorRGBA(255,0,0,127)
ℹ️
Change an Led component’s color by code
-- To change an Led Color use the color
-- functions to set the Color property
-- or use a color constant
gdt.Led0.Color = Color(255,255,255)
gdt.Led0.Color = color.white
ℹ️
Drawing a sprite on a screen
-- First, you need:
-- 1) a spritesheet
-- 2) a display component
-- 3) a videochip
-- 4) connect the display component to the videochip
-- 5) a ROM component
local spriteS:SpriteSheet = gdt.ROM.System.SpriteSheets["sprites"]
-- Draw a sprite at 10,10, from SpriteSheet spriteS,
-- using the frame at 0,0 (col, row)
-- with white color multiplier, on a transparent background.
gdt.VideoChip0:DrawSprite( vec2(10,10), spriteS,
0, 0, color.white, color.clear )
ℹ️
Specify variable type
-- When possible specify the variable type.
-- This will let the autocomplete know what to do
local myAwesomeLed:Led = gdt.Led0
local myVideoChip:VideoChip = gdt.VideoChip0
ℹ️
Read the state of a Led button
-- Reads the actual state of a button. True if pressed
if gdt.LedButton0.ButtonState then log("pressed") end
-- Check if the button is pressed.
--True only in the actual frame the button is pressed
if gdt.LedButton0.ButtonDown then log("pressed") end
-- Check if the button is released.
-- True only in the actual frame the button is released
if gdt.LedButton0.ButtonUp then log("released") end
ℹ️
Write a number in a 7segment display
-- this is a function that helps to write numbers
-- in 7-segment displays
-- it works both on single-digits displays and
-- in multiple digits displays
-- Initialize reference table for addressing
-- digits in the 7-segment display
local nb7:{{number}} = {}
nb7[0] ={1,2,3,4,5,6}
nb7[1] ={2,3}
nb7[2] ={1,2,4,5,7}
nb7[3] ={1,2,3,4,7}
nb7[4] ={2,3,6,7}
nb7[5] ={1,3,4,6,7}
nb7[6] ={1,3,4,5,6,7}
nb7[7] ={1,2,3}
nb7[8] ={1,2,3,4,5,6,7}
nb7[9] ={1,2,3,4,6,7}
-- writes a number on a 7-segment display
-- display: the 7 segment display component
-- position: the position of the digit starting from left.
-- 1 for single digits displays
-- num: the number to display
-- ledColor: the color to be used
function writeNumber(display:SegmentDisplay, position:number,
num:number, ledColor:color)
-- get rid of decimals
num = math.abs(math.floor(num))
-- first, clear the previous State
for s=1,7 do
display.States[position][s] = false
display.Colors[position][s] = ledColor
end
for s=1,#nb7[num] do
display.States[position][nb7[num][s]] = true
end
end