Rockbox.org home
Downloads
Release release
Dev builds dev builds
Extras extras
themes themes
Documentation
Manual manual
Wiki wiki
Device Status device status
Support
Forums forums
Mailing lists mailing lists
IRC IRC
Development
Bugs bugs
Patches patches
Dev Guide dev guide
Search



Donate

Rockbox Technical Forums


Login with username, password and session length
Home Help Search Staff List Login Register
News:

Welcome to the Rockbox Technical Forums!

+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Could someone help correct a simple script?
« previous next »
  • Print
Pages: [1]

Author Topic: Could someone help correct a simple script?  (Read 2926 times)

Offline HolyRoller

  • Member
  • *
  • Posts: 18
Could someone help correct a simple script?
« on: June 30, 2019, 07:29:28 PM »
I'm trying to make a simple LUA script that rolls 4 dice simultaneously (one 6-sided, two 10-sided, and one 100-sided).  I can get the following script to work in a LUA compiler, but not on Rockbox for the Sansa Clip Zip:

print(math.random(1,6), math.random(1,10), math.random(1,10), math.random(1,100))

Could someone help me get this corrected?  I'd appreciate it!
Logged

Offline saratoga

  • Developer
  • Member
  • *
  • Posts: 9213
Re: Could someone help correct a simple script?
« Reply #1 on: June 30, 2019, 08:51:33 PM »
Never used lua, but there is no console on rockbox, so I'm not sure you can print.  Take a look at the helloworld program which implements its own printf function:

https://git.rockbox.org/?p=rockbox.git;a=blob;f=apps/plugins/helloworld.lua;h=aa2ef446d3d422c27be7358383764c7cf92d638a;hb=HEAD#l47
Logged

Offline rockbox_dev123

  • Member
  • *
  • Posts: 74
Re: Could someone help correct a simple script?
« Reply #2 on: July 01, 2019, 07:26:40 PM »
https://www.rockbox.org/wiki/PluginDice
Logged

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 876
Re: Could someone help correct a simple script?
« Reply #3 on: July 02, 2019, 12:14:25 AM »
Our lua interpreter has quite a few differences from standard lua

your script should work as follows:

Code: [Select]
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory

math.randomseed(os.time()) -- seeds the random number generator
print.clear()
print.f("%d %d %d %d", math.random(1,6), math.random(1,10), math.random(1,10), math.random(1,100))
rb.sleep(rb.HZ * 5) --waits 5 seconds

I have a few examples floating around on the forum

this one will be very important: it prints the API in a easily browsed list
http://forums.rockbox.org/index.php/topic,51935.msg240367.html

here is a couple advanced examples:
http://forums.rockbox.org/index.php/topic,52352.msg242367.html

and one I forgot:
http://forums.rockbox.org/index.php/topic,52895.0.html


« Last Edit: July 02, 2019, 12:30:51 AM by Bilgus »
Logged

Offline HolyRoller

  • Member
  • *
  • Posts: 18
Re: Could someone help correct a simple script?
« Reply #4 on: July 02, 2019, 06:20:47 PM »
Thanks for your help guys!  I attempted to run the code there but it crashed with an error.  I then noticed that I don't have a print.lua in that directory.  Can I download that somewhere?
Logged

Offline HolyRoller

  • Member
  • *
  • Posts: 18
Re: Could someone help correct a simple script?
« Reply #5 on: July 02, 2019, 07:11:08 PM »
Aha, I just needed the latest dev build.  Thanks!
Logged

Offline HolyRoller

  • Member
  • *
  • Posts: 18
Re: Could someone help correct a simple script?
« Reply #6 on: July 02, 2019, 07:38:46 PM »
If I could trouble you guys for one more.  How could I modify my code to let me hit the back or ok button to exit instead of waiting for the sleep?

Code: [Select]
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory

local r = math.random
local items = {
    "sword",
    "shield",
    "potion"
}

math.randomseed(os.time()) -- seeds the random number generator
print.clear()
print.f("%s", items[r(1, #items)])
rb.sleep(rb.HZ * 5) --waits 5 seconds
« Last Edit: July 02, 2019, 09:30:23 PM by HolyRoller »
Logged

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 876
Re: Could someone help correct a simple script?
« Reply #7 on: July 03, 2019, 12:40:50 AM »
Code: [Select]
require "buttons"
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory

local line = 1, max_line, w, h
local button = 0
local btn = rb.buttons
local r = math.random

local items = {
    "sword",
    "shield",
    "potion"
}

math.randomseed(os.time()) -- seeds the random number generator
print.clear()

repeat

    if button >= 0 and bit.band(button, btn.BUTTON_REL) == 0 then -- make sure to skip sys_events ( < 0 ) and button release

        if line == max_line then
            print.clear()
            line = 1
        end

        line, max_line, w, h = print.f("%d. %s", line, items[r(1, #items)])
    end

    button = rb.button_get(true) -- true = block till pressed
until(button == btn.BUTTON_BACK)
Logged

Offline HolyRoller

  • Member
  • *
  • Posts: 18
Re: Could someone help correct a simple script?
« Reply #8 on: July 03, 2019, 10:27:12 AM »
Alright I ran this code and every button seems to re-roll the result but none exit the program.  Had to do a hard reset.  Do I modify the local button line? 

Also, is there a word wrap feature or code?  With long results, it will display all of it on one line and autoscroll.  I'd like it to wrap if possible.

Thanks for your help, I'm learning a lot!
Logged

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 876
Re: Could someone help correct a simple script?
« Reply #9 on: July 03, 2019, 04:09:18 PM »
your DAP probably doesn't have button_back mapped so use actions instead
Logged

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 876
Re: Could someone help correct a simple script?
« Reply #10 on: July 03, 2019, 05:22:01 PM »
here you go this uses actions and has a word wrap function I just threw it together so tweak it till you get what you want

Code: [Select]
require "actions"
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory

local line = 1, max_line, w, h

local act = rb.actions
local action = act.NONE
local r = math.random
local TIMEOUT_BLOCK = -1
local TIMEOUT_NOBLOCK = 0
local left_margin = rb.font_getstringsize("W ", font or rb.FONT_UI)
local items = {
    "sword",
    "shield",
    "potion",
    "really long text item that needs to word wrap to another line",
    "really really long text item that needs to word wrap to another line and then to additional lines after that"
}

math.randomseed(os.time()) -- seeds the random number generator
print.opt.overflow("none")
print.settings = print.opt.get()
print.clear()

local function word_wrap(item, max_width)
    local item_disp=""
    local chars = 1
    repeat --remove what has alread been displayed
        item_disp = string.sub(item, 1, chars)
        chars = chars + 1
    until(rb.font_getstringsize(item_disp, font or rb.FONT_UI) >= max_width - left_margin)

    local fsp = string.find(item, "%s", chars - 3) or 0x0
    --rb.splash(100, chars .. " " .. fsp)
    if fsp ~= 0 and fsp < chars then chars = fsp + 1 end


    item = string.sub(item, chars)
    line, max_line, w, h = print.f("%s", item)
    if w > max_width then
        return word_wrap(item, max_width) -- function tailcall
    end
end


repeat

    if action == act.PLA_SELECT or action == act.NONE then

        if line == max_line then
            print.clear()
            line = 1
        end
    local item = items[r(1, #items)]
        line, max_line, w, h = print.f("%s", item)
    if w > print.settings.width then
            word_wrap(item, print.settings.width)
    end
    end

    action = rb.get_plugin_action(TIMEOUT_BLOCK) -- block till pressed
until(action == act.PLA_EXIT or action == act.PLA_CANCEL)
Logged

Offline HolyRoller

  • Member
  • *
  • Posts: 18
Re: Could someone help correct a simple script?
« Reply #11 on: July 03, 2019, 06:44:13 PM »
Fantastic that works!  Is there a way I can tweak the cut off?  I noticed that when it wraps, it will repeat the previous 3 or so characters.  Sorta like:

really reall
ally long t
g text line
Logged

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 876
Re: Could someone help correct a simple script?
« Reply #12 on: July 03, 2019, 08:23:46 PM »
push the text through the word_wrap function every time and tweak it to do what you want...

teach a man to fish and whatnot

Logged

  • Print
Pages: [1]
« previous next »
+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Could someone help correct a simple script?
 

  • SMF 2.0.19 | SMF © 2021, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 0.176 seconds with 21 queries.