Watchmaker Wiki

The ultimate watch maker for Android Wear!

User Tools

Site Tools


lua

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lua [2015/04/21 10:53] alex_curranlua [2020/07/24 09:32] (current) alexcurran1
Line 16: Line 16:
  
 (Note:  We're just starting to add Lua code examples and explanations, so it may look sparse now, but keep checking back!  Some exciting stuff is on its way!) (Note:  We're just starting to add Lua code examples and explanations, so it may look sparse now, but keep checking back!  Some exciting stuff is on its way!)
 +
  
 ===== Conditional Statements ===== ===== Conditional Statements =====
  
-Conditionals (IF-THEN-ELSE) work differently than in most other programming languages. You build logical expression that is evaluatedand the result is set as your property.+Conditional statements (IF-THEN-ELSE) work in much the same as most languages and also supports "elseif" (there are some examples below), for more information see http://www.lua.org/pil/4.3.1.html  
 + 
 + 
 +===== Conditional Value Assignment ===== 
 +These allow you to conditionally set value to a property based on some conditions, the syntax uses (IF-AND-OR).
  
 It's easy once you know this: It's easy once you know this:
Line 25: Line 30:
 ^ IF <condition> THEN <x> ELSE <y>  | becomes:  ^ (<condition>) and <x> or <y>  ^ ^ IF <condition> THEN <x> ELSE <y>  | becomes:  ^ (<condition>) and <x> or <y>  ^
  
-Simple Example:+A simple Example:
  
   ({dm} % 2 == 0) and 'even' or 'odd'   ({dm} % 2 == 0) and 'even' or 'odd'
  
-More complex example:+A more complex example:
  
   ({dh} < 11) and 'Morning' or ({dh} < 15) and 'Noon' or 'Evening'   ({dh} < 11) and 'Morning' or ({dh} < 15) and 'Noon' or 'Evening'
      
-Note the parentheses.  The AND and OR statements can mean multiple things to Lua.  Sometimes Lua needs help determining whether you're using one of them as a conjunction (and/or) or as an operation (then/else).  If it gets confused, you may get undesired results, so we help out by encapsulating the entire "IF" part of the logic statement in parentheses.  Here's another example of why it's a good idea to add parentheses:+Note the parentheses.  The AND and OR statements can mean multiple things to Lua.  Sometimes Lua needs help determining whether you're using one of them as a conjunction (and/or) or as an operation (then/else).  If it gets confused, you may get undesired results. We help out Lua by encapsulating the entire "IF" part of the logic statement in parentheses. 
 + 
 +Here's another example of why it's a good idea to add parentheses:
  
    ({dh} > 23 or {dh} < 1) and 'Midnight' or ({dh} > 11 and {dh} < 13) and 'Noon' or ({dh} < 12) and 'Morning' or 'Afternoon'    ({dh} > 23 or {dh} < 1) and 'Midnight' or ({dh} > 11 and {dh} < 13) and 'Noon' or ({dh} < 12) and 'Morning' or 'Afternoon'
Line 50: Line 57:
 ===== Strings ===== ===== Strings =====
  
-Texts can be manipulated through the [[http://www.lua.org/manual/5.3/manual.html#6.4|string library]].+Text [[http://www.lua.org/pil/2.4.html|strings]] such as "some text" can be manipulated through the string library ([[http://www.lua.org/manual/5.3/manual.html\#6.4]]). 
 + 
 +You can quote strings with double or single quotes so 'some text' is also a string.
  
 ==== Methods ==== ==== Methods ====
Line 56: Line 65:
 Upper/Lowercase a string: Upper/Lowercase a string:
  
-  string.upper({ddww}) +  string.upper('{ddww}'
-  string.lower({ddww})+  string.lower('{ddww}')
  
 Reverse a string: Reverse a string:
  
-  string.reverse({ddww})+  string.reverse('{ddww}')
  
 Shorten a string (use only first 20 chars): Shorten a string (use only first 20 chars):
Line 132: Line 141:
 You are recommended to keep Tap Action scripts shorter e.g. by running functions in the main script file, as Tap Action scripts are stored in the XML file and not the separate script file. You are recommended to keep Tap Action scripts shorter e.g. by running functions in the main script file, as Tap Action scripts are stored in the XML file and not the separate script file.
  
-===== Run Lua Function Every Second or Millisecond =====+===== Run Lua Function Every Hour, Minute, Second or Millisecond ===== 
 + 
 +    function on_hour(h)           - every hour (h = hour) 
 +    function on_minute(h, m)      - every minute (h = hour, m = minute) 
 +    function on_second(h, m, s)   - every second (h = hour, m = minute, s = second) 
 +    function on_millisecond(dt)   - every millisecond (dt = delta time)
  
 If you have some custom variables that update every millisecond, you can create an //on_millisecond(..)// function in your script file. This function will be called every millisecond. In the example below this will move points around a circle (co-ordinates stored in var_ms_posx and var_ms_posy) : If you have some custom variables that update every millisecond, you can create an //on_millisecond(..)// function in your script file. This function will be called every millisecond. In the example below this will move points around a circle (co-ordinates stored in var_ms_posx and var_ms_posy) :
Line 195: Line 209:
 WatchMaker extends the Lua commandset. Just use the wm_action() or other functions anywhere in your script or tap actions : WatchMaker extends the Lua commandset. Just use the wm_action() or other functions anywhere in your script or tap actions :
  
-    wm_action('sw_start_stop'      -- start or stop the stopwatch +    wm_action('sw_start_stop'         -- start or stop the stopwatch 
-    wm_action('sw_reset'           -- reset stopwatch +    wm_action('sw_reset'              -- reset stopwatch 
-    wm_action('m_update_weather'   -- update weather +    wm_action('m_update_weather'      -- update weather 
-    wm_action('m_task:MyTask'      -- run Tasker task 'MyTask' +    wm_action('m_task:MyTask'         -- run Tasker task 'MyTask' 
 +    wm_action('color_switch_next'     -- Color Switch: Next Color 
 +    wm_action('color_switch_prev'     -- Color Switch: Previous Color 
 +    wm_action('color_switch_select'   -- Color Switch: Select Color 
 +    wm_action('tap_launcher'          -- Launch Tap Launcher 
 +    wm_action('widget_weather'        -- Launch Weather Widget 
 +    wm_action('widget_health'         -- Launch Health Widget 
 +    wm_action('widget_calendar'       -- Launch Calendar Widget 
 +            
     wm_schedule('...'    -- schedule an animation or event (see Scheduling Animations / Functions)     wm_schedule('...'    -- schedule an animation or event (see Scheduling Animations / Functions)
     wm_unschedule_all()    -- unschedule all animations or events     wm_unschedule_all()    -- unschedule all animations or events
 +    wm_vibrate(d, r)       -- vibrate for duration d (milliseconds) and repeat r times 
 +    wm_sfx('sfx_file'    -- play MP3 file with name sfx_file.mp3 (needs to be in /BeautifulWatches/sfx) - supported watches with speaker only
     wm_transition('...'  -- run a transition (see Transitions)     wm_transition('...'  -- run a transition (see Transitions)
 +    wm_anim_set('layerName', 'anim_in', 'Typewriter"    -- set animation in for 'layerName' to Typewriter
 +    wm_anim_set('layerName', 'dur_in', 1.0)     -- set duration in for 'layerName' to 1.0 (or use -1 for default)
 +    wm_anim_start('layerName'   -- start animation on 'layerName'
     wm_tag('...'         -- ONLY use for dynamic variables otherwise will break     wm_tag('...'         -- ONLY use for dynamic variables otherwise will break
                                 return a value of a WatchMaker tag, e.g. wm_tag("{c"..var_myvar.."bp}")                                 return a value of a WatchMaker tag, e.g. wm_tag("{c"..var_myvar.."bp}")
Line 227: Line 253:
 You can write your own tweening function like this : You can write your own tweening function like this :
  
-    function linear_myversion(t, b, c, d)+    function linear_myversion(d, b, c, t)
         return c * t / d + b         return c * t / d + b
     end     end
          
-    function inQuad_myversion(t, b, c, d)+    function inQuad_myversion(d, b, c, t)
         t = t / d         t = t / d
         return c * math.pow(t, 2) + b         return c * math.pow(t, 2) + b
     end     end
          
-    -- t = time == how much time has to pass for the tweening to complete+    -- t = time == running time. How much time has passed *right now*
     -- b = begin == starting property value     -- b = begin == starting property value
     -- c = change == ending - beginning     -- c = change == ending - beginning
-    -- d = duration == running time. How much time has passed *right now*+    -- d = duration == how much time has to pass for the tweening to complete
  
 ===== Transitions ===== ===== Transitions =====
lua.1429613618.txt.gz · Last modified: 2015/04/21 10:53 by alex_curran