From 2bfb9d5a5702cd064db73ce36a9b98b8fd65b4e9 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sun, 15 Sep 2019 16:47:14 -0400 Subject: [PATCH] Update API documentation. Add gain.js example. Fix lights of rainbox.js. Make MessageDisplay multi-line. --- README.md | 32 ++++++++++++++++---------------- examples/gain.js | 29 +++++++++++++++++++++++++++++ examples/rainbow.js | 6 ++++-- examples/vco.js | 2 +- src/Prototype.cpp | 17 ++++++++++++++++- 5 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 examples/gain.js diff --git a/README.md b/README.md index eb454b3..d553597 100644 --- a/README.md +++ b/README.md @@ -55,33 +55,33 @@ function process(block) { */ block.bufferSize - /** Voltage of the input port of row `rowIndex`. Read-only. + /** Voltage of the input port of column `i`. Read-only. */ - block.inputs[rowIndex][bufferIndex] // 0.0 + block.inputs[i][bufferIndex] // 0.0 - /** Voltage of the output port of row `rowIndex`. Writable. + /** Voltage of the output port of column `i`. Writable. */ - block.outputs[rowIndex][bufferIndex] // 0.0 + block.outputs[i][bufferIndex] // 0.0 - /** Value of the knob of row `rowIndex`. Between 0 and 1. Read-only. + /** Value of the knob of column `i`. Between 0 and 1. Read-only. */ - block.knobs[rowIndex] // 0.0 + block.knobs[i] // 0.0 - /** Pressed state of the switch of row `rowIndex`. Read-only. + /** Pressed state of the switch of column `i`. Read-only. */ - block.switches[rowIndex] // false + block.switches[i] // false - /** Brightness of the RGB LED of row `rowIndex`, between 0 and 1. Writable. + /** Brightness of the RGB LED of column `i`, between 0 and 1. Writable. */ - block.lights[rowIndex][0] // 0.0 (red) - block.lights[rowIndex][1] // 0.0 (green) - block.lights[rowIndex][2] // 0.0 (blue) + block.lights[i][0] // 0.0 (red) + block.lights[i][1] // 0.0 (green) + block.lights[i][2] // 0.0 (blue) - /** Brightness of the switch RGB LED of row `rowIndex`. Writable. + /** Brightness of the switch RGB LED of column `i`. Writable. */ - block.switchLights[rowIndex][0] // 0.0 (red) - block.switchLights[rowIndex][1] // 0.0 (green) - block.switchLights[rowIndex][2] // 0.0 (blue) + block.switchLights[i][0] // 0.0 (red) + block.switchLights[i][1] // 0.0 (green) + block.switchLights[i][2] // 0.0 (blue) } ``` diff --git a/examples/gain.js b/examples/gain.js new file mode 100644 index 0000000..da3f890 --- /dev/null +++ b/examples/gain.js @@ -0,0 +1,29 @@ +// Simplest possible script using all variables +// by Andrew Belt + +function process(block) { + // Loop through each column + for (var i = 0; i < 6; i++) { + // Get input + var x = block.inputs[i][0] + // Get gain knob + var gain = block.knobs[i] + // Apply gain to input + var y = x * gain + // Set gain light (red = 0) + block.lights[i][0] = gain + // Check mute switch + if (block.switches[i]) { + // Mute output + y = 0 + // Enable mute light (red = 0) + block.switchLights[i][0] = 1 + } + else { + // Disable mute light + block.switchLights[i][0] = 0 + } + // Set output + block.outputs[i][0] = y + } +} diff --git a/examples/rainbow.js b/examples/rainbow.js index dcef69c..4f462b0 100644 --- a/examples/rainbow.js +++ b/examples/rainbow.js @@ -33,8 +33,10 @@ function process(block) { for (var i = 0; i < 6; i++) { var h = (1 - i / 6 + phase) % 1 var rgb = hsvToRgb(h, 1, 1) - block.lights[i] = rgb - block.switchLights[i] = rgb + for (var c = 0; c < 3; c++) { + block.lights[i][c] = rgb[c] + block.switchLights[i][c] = rgb[c] + } block.outputs[i][0] = Math.sin(2 * Math.PI * h) * 5 + 5 } } diff --git a/examples/vco.js b/examples/vco.js index f370c13..887f32d 100644 --- a/examples/vco.js +++ b/examples/vco.js @@ -19,7 +19,7 @@ function process(block) { var freq = 261.6256 * Math.pow(2, pitch) display("Freq: " + freq.toFixed(3) + " Hz") - // Set all output samples in block + // Set all samples in output buffer var deltaPhase = config.frameDivider * block.sampleTime * freq for (var i = 0; i < block.bufferSize; i++) { // Accumulate phase diff --git a/src/Prototype.cpp b/src/Prototype.cpp index 42e202b..50c4860 100644 --- a/src/Prototype.cpp +++ b/src/Prototype.cpp @@ -128,7 +128,7 @@ struct Prototype : Module { // Reset settings frameDivider = 32; frame = 0; - block.bufferSize = 1; + block = ScriptEngine::ProcessBlock(); bufferIndex = 0; } @@ -250,6 +250,20 @@ struct MessageChoice : LedDisplayChoice { void step() override { text = module ? module->message : ""; } + + void draw(const DrawArgs& args) override { + nvgScissor(args.vg, RECT_ARGS(args.clipBox)); + if (font->handle >= 0) { + nvgFillColor(args.vg, color); + nvgFontFaceId(args.vg, font->handle); + nvgTextLetterSpacing(args.vg, 0.0); + nvgTextLineHeight(args.vg, 1.08); + + nvgFontSize(args.vg, 12); + nvgTextBox(args.vg, textOffset.x, textOffset.y, box.size.x - textOffset.x, text.c_str(), NULL); + } + nvgResetScissor(args.vg); + } }; @@ -272,6 +286,7 @@ struct PrototypeDisplay : LedDisplay { MessageChoice* messageChoice = new MessageChoice; messageChoice->box.pos = fileChoice->box.getBottomLeft(); messageChoice->box.size.x = box.size.x; + messageChoice->box.size.y = box.size.y - messageChoice->box.pos.y; messageChoice->module = module; addChild(messageChoice); }