display関数は、ディスプレイを搭載した製品の画面に文字や図形を描画します。
ディスプレイを搭載していない製品ではdisplay関数の呼び出しは無視されます。そのため、どの端末でも同じスクリプトをそのまま実行できます。
OS7.1.0以降
display.print(string)
現在のカーソル位置に文字列を追記します。文字を書くたびにカーソルが進むため、display.print()を繰り返すと、タイプライターのように続きから文字が並びます。
引数
| 引数 | 型 | 説明 |
|---|---|---|
string |
string | 描画する文字列です。 |
display.clear()
画面全体を消去します。
display.raw(table)
フレームバッファを画面へ直接描画します。
tableは各要素に0から255までの1バイトを持つバイト配列です。バイトはディスプレイのフレームバッファ形式にしたがい、縦方向に並びます。
引数
| 引数 | 型 | 説明 |
|---|---|---|
table |
table | フレームバッファのバイト配列です。各要素は0から255までの値です。 |
Example: タイプライター風の表示
以下の例では、メッセージを1文字ずつ表示し、タイプライターのような演出をします。
local messages = {
"Hello from obniz",
"Lua is running on the device.",
"Bye!",
}
local msg_index = 1
local char_index = 0
local tick = 0
local interval = 120 -- 1文字あたりのミリ秒
os.log(" - Lua PowerOn");
display.clear();
function on_offline_loop()
loop()
end
function on_online_loop()
loop()
end
function loop()
if tick + interval < os.getTick() then
tick = os.getTick()
local msg = messages[msg_index]
if char_index < #msg then
-- 次の1文字を追記する
char_index = char_index + 1
display.print(msg:sub(char_index, char_index));
else
-- 次のメッセージへ移る
char_index = 0
msg_index = msg_index + 1
if msg_index > #messages then
msg_index = 1
end
end
end
end