mozaic_tips_and_tricks

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
mozaic_tips_and_tricks [2020/03/01 22:06] – Fixed NOTOC _kimozaic_tips_and_tricks [2020/07/12 21:46] – Added: Using Inc and Dec in Expressions _ki
Line 14: Line 14:
   * [[#Use a Knob to toggle 16 Pads View to HELP View]]   * [[#Use a Knob to toggle 16 Pads View to HELP View]]
   * [[#Knob double-tap Support]]   * [[#Knob double-tap Support]]
 +  * [[#Using Inc and Dec in Expressions]]
   * [[#Calculate Standard Chords from a Root Note]]   * [[#Calculate Standard Chords from a Root Note]]
  
Line 224: Line 225:
  
  
-If a knob is only used to toggle between two states like a switch, the code for double-tap detection would look like:+If a knob is only used to toggle between two states like a switch (like the variable //isOn// in the example), the code for double-tap detection would look like:
 <code> <code>
 +@OnKnobChange
 +  _knob = LastKnob
 +  _val  = GetKnobValue _knob
 +  
 +  if _knob = TOGGLE_KNOB
 +    if _val = 64
 +      isOn = not isOn
 +      SetKnobValue TOGGLE_KNOB,48 + 32*isOn
 +    else
 +      isOn = _val > 64  
 +    endif
 +    
 +    if isOn
 +      LabelKnob TOGGLE_KNOB,  {ON}
 +    else
 +      LabelKnob TOGGLE_KNOB,  {OFF}
 +    endif
 +  endif
 +@End
 +
 @OnLoad @OnLoad
   ShowLayout 2   ShowLayout 2
Line 246: Line 267:
   else   else
     LabelKnob TOGGLE_KNOB,  {OFF}     LabelKnob TOGGLE_KNOB,  {OFF}
-  endif 
-@End 
- 
-@OnKnobChange 
-  _knob = LastKnob 
-  _val  = GetKnobValue _knob 
-   
-  if _knob = TOGGLE_KNOB 
-    if _val = 64 
-      isOn = not isOn 
-      SetKnobValue TOGGLE_KNOB,48 + 32*isOn 
-    else 
-      isOn = _val > 64   
-    endif 
-     
-    if isOn 
-      LabelKnob TOGGLE_KNOB,  {ON} 
-    else 
-      LabelKnob TOGGLE_KNOB,  {OFF} 
-    endif 
   endif   endif
 @End @End
Line 275: Line 276:
     * //isOn = _val > 64// assigns either 0 or 1 to //isOn// depending on _val     * //isOn = _val > 64// assigns either 0 or 1 to //isOn// depending on _val
  
 +
 +\\ 
 +===== Using Inc and Dec in Expressions ===== 
 +<html><p align = "right"><small><i>From -ki</i></small></p></html>
 +
 +TheOriginalPaulB discovered that Inc and Dec are working as functions. Both Inc and Dec are ‚pre-increment‘ operations like ++var  in C, as the function returns the already incremented value. This allows for several interesting language constructs:
 +
 +In case of a ring buffer index, one could use
 +<code>
 +  index = (Inc index) % 64
 +</code>
 +instead of the longer construct
 +
 +<code>
 +  Inc index
 +  index = index % 64
 +</code>
 +
 +\\ 
 +Sometimes this trick is also applicable in array initialization, but one has to compensate for the pre-increment.
 +<code>
 +  idx = -1
 +  for i = 10 to 19
 +    array[Inc idx] = 2*i
 +  endfor
 +</code>
 +The example fills array[0] to array[9] with the values 20 to 38. To compensate for pre-increment, idx needs to start at -1 since it will be incremented to zero on first usage
 +
 +\\ 
 +It is also possible to construct a post-increment operation by using the inc in the expression itself, but discarding it using multiplication with 0:
 +
 +<code>
 +  idx = 0
 +  for i = 10 to 19
 +    array[idx] = 2*i  + 0*(Inc idx)
 +  endfor
 +</code>
  
 \\  \\ 
  • mozaic_tips_and_tricks.txt
  • Last modified: 2024/04/23 19:07
  • by _ki