Differences
This shows you the differences between two versions of the page.
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 _ki | mozaic_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]] | ||
* [[# | * [[# | ||
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 |
< | < | ||
+ | @OnKnobChange | ||
+ | _knob = LastKnob | ||
+ | _val = GetKnobValue _knob | ||
+ | | ||
+ | if _knob = TOGGLE_KNOB | ||
+ | if _val = 64 | ||
+ | isOn = not isOn | ||
+ | SetKnobValue TOGGLE_KNOB, | ||
+ | else | ||
+ | isOn = _val > 64 | ||
+ | endif | ||
+ | | ||
+ | if isOn | ||
+ | LabelKnob TOGGLE_KNOB, | ||
+ | else | ||
+ | LabelKnob TOGGLE_KNOB, | ||
+ | endif | ||
+ | endif | ||
+ | @End | ||
+ | |||
@OnLoad | @OnLoad | ||
ShowLayout 2 | ShowLayout 2 | ||
Line 246: | Line 267: | ||
else | else | ||
LabelKnob TOGGLE_KNOB, | LabelKnob TOGGLE_KNOB, | ||
- | endif | ||
- | @End | ||
- | |||
- | @OnKnobChange | ||
- | _knob = LastKnob | ||
- | _val = GetKnobValue _knob | ||
- | | ||
- | if _knob = TOGGLE_KNOB | ||
- | if _val = 64 | ||
- | isOn = not isOn | ||
- | SetKnobValue TOGGLE_KNOB, | ||
- | else | ||
- | isOn = _val > 64 | ||
- | endif | ||
- | | ||
- | if isOn | ||
- | LabelKnob TOGGLE_KNOB, | ||
- | else | ||
- | LabelKnob TOGGLE_KNOB, | ||
- | 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 ===== | ||
+ | < | ||
+ | |||
+ | 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 | ||
+ | < | ||
+ | index = (Inc index) % 64 | ||
+ | </ | ||
+ | instead of the longer construct | ||
+ | |||
+ | < | ||
+ | Inc index | ||
+ | index = index % 64 | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Sometimes this trick is also applicable in array initialization, | ||
+ | < | ||
+ | idx = -1 | ||
+ | for i = 10 to 19 | ||
+ | array[Inc idx] = 2*i | ||
+ | endfor | ||
+ | </ | ||
+ | The example fills array[0] to array[9] with the values 20 to 38. To compensate for pre-increment, | ||
+ | |||
+ | \\ | ||
+ | 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: | ||
+ | |||
+ | < | ||
+ | idx = 0 | ||
+ | for i = 10 to 19 | ||
+ | array[idx] = 2*i + 0*(Inc idx) | ||
+ | endfor | ||
+ | </ | ||
\\ | \\ |