diff --git a/examples/BLOCKS/BlocksSynth/Source/MainComponent.h b/examples/BLOCKS/BlocksSynth/Source/MainComponent.h index 482b47e1f3..68cd2ec22e 100644 --- a/examples/BLOCKS/BlocksSynth/Source/MainComponent.h +++ b/examples/BLOCKS/BlocksSynth/Source/MainComponent.h @@ -57,10 +57,10 @@ struct SynthGrid Array gridFillArray; Colour baseGridColour = Colours::green; - Colour touchColour = Colours::cyan; + Colour touchColour = Colours::red; Array tonics = { 4, 12, 20 }; - Array notes = { 1, 3, 6, 7, 9, 11, 14, 15, 17, 19, 22, 24 }; + Array notes = { 1, 3, 6, 7, 9, 11, 14, 15, 17, 19, 22, 24 }; //============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SynthGrid) @@ -73,8 +73,7 @@ struct SynthGrid class MainComponent : public Component, public TopologySource::Listener, private TouchSurface::Listener, - private ControlButton::Listener, - private Timer + private ControlButton::Listener { public: MainComponent() @@ -145,7 +144,7 @@ private: /** Overridden from TouchSurface::Listener. Called when a Touch is received on the Lightpad */ void touchChanged (TouchSurface&, const TouchSurface::Touch& touch) override { - if (currentMode == waveformSelectionMode && touch.isTouchStart && allowTouch) + if (currentMode == waveformSelectionMode && touch.isTouchStart) { // Change the displayed waveshape to the next one ++waveshapeMode; @@ -154,9 +153,6 @@ private: waveshapeMode = 0; waveshapeProgram->setWaveshapeType (waveshapeMode); - - allowTouch = false; - startTimer (500); } else if (currentMode == playMode) { @@ -243,12 +239,13 @@ private: { if (currentMode == waveformSelectionMode) { - // Create a new BitmapLEDProgram for the LEDGrid + // Create a new WaveshapeProgram for the LEDGrid waveshapeProgram = new WaveshapeProgram (*grid); // Set the LEDGrid program grid->setProgram (waveshapeProgram); + // Initialise the program waveshapeProgram->setWaveshapeType (waveshapeMode); waveshapeProgram->generateWaveshapes(); } @@ -267,11 +264,6 @@ private: } } - void timerCallback() override - { - allowTouch = true; - } - enum BlocksSynthMode { waveformSelectionMode = 0, @@ -292,15 +284,11 @@ private: Array touchMessageTimesInLastSecond; - Colour waveshapeColour = Colours::red; - int waveshapeMode = 0; float scaleX = 0.0; float scaleY = 0.0; - bool allowTouch = true; - //============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent) }; diff --git a/examples/BLOCKS/BlocksSynth/Source/WaveshapeProgram.h b/examples/BLOCKS/BlocksSynth/Source/WaveshapeProgram.h index 457def258e..3f0b98e4c8 100644 --- a/examples/BLOCKS/BlocksSynth/Source/WaveshapeProgram.h +++ b/examples/BLOCKS/BlocksSynth/Source/WaveshapeProgram.h @@ -14,10 +14,16 @@ class WaveshapeProgram : public LEDGrid::Program { public: - WaveshapeProgram (LEDGrid& lg) : Program (lg) + WaveshapeProgram (LEDGrid& lg) : Program (lg) {} + + /** Sets the waveshape type to display on the grid */ + void setWaveshapeType (uint8 type) { + ledGrid.setDataByte (0, type); } + /** Generates the Y coordinates for 1.5 cycles of each of the four waveshapes and stores them + at the correct offsets in the shared data heap. */ void generateWaveshapes() { uint8 sineWaveY[45]; @@ -39,14 +45,14 @@ public: if (currentPhase < double_Pi) { if (x == 0) - squareWaveY[x] = 20; + squareWaveY[x] = 255; else squareWaveY[x] = 1; } else { if (squareWaveY[x - 1] == 1) - squareWaveY[x - 1] = 20; + squareWaveY[x - 1] = 255; squareWaveY[x] = 13; } @@ -54,8 +60,8 @@ public: // Saw wave output, set flags for when vertical line should be drawn sawWaveY[x] = 14 - ((x / 2) % 15); - if (sawWaveY[x] == 0 && sawWaveY[x - 1] != 20) - sawWaveY[x] = 20; + if (sawWaveY[x] == 0 && sawWaveY[x - 1] != 255) + sawWaveY[x] = 255; // Triangle wave output triangleWaveY[x] = x < 15 ? x : 14 - (x % 15); @@ -73,31 +79,16 @@ public: currentPhase += phaseInc; } + // Store the values for each of the waveshapes at the correct offsets in the shared data heap for (int i = 0; i < 45; ++i) { - ledGrid.setDataByte (sineWaveOffset + i, sineWaveY[i]); - int sineByte = ledGrid.getDataByte (sineWaveOffset + i); - jassert (sineByte == sineWaveY[i]); - - ledGrid.setDataByte (squareWaveOffset + i, squareWaveY[i]); - int squareByte = ledGrid.getDataByte (squareWaveOffset + i); - jassert (squareByte == squareWaveY[i]); - - ledGrid.setDataByte (sawWaveOffset + i, sawWaveY[i]); - int sawByte = ledGrid.getDataByte (sawWaveOffset + i); - jassert (sawByte == sawWaveY[i]); - + ledGrid.setDataByte (sineWaveOffset + i, sineWaveY[i]); + ledGrid.setDataByte (squareWaveOffset + i, squareWaveY[i]); + ledGrid.setDataByte (sawWaveOffset + i, sawWaveY[i]); ledGrid.setDataByte (triangleWaveOffset + i, triangleWaveY[i]); - int triangleByte = ledGrid.getDataByte (triangleWaveOffset + i); - jassert (triangleByte == triangleWaveY[i]); } } - void setWaveshapeType (uint8 type) - { - ledGrid.setDataByte (0, type); - } - uint32 getHeapSize() override { return totalDataSize; @@ -150,14 +141,17 @@ public: // Get the waveshape type int type = getHeapByte (0); + + // Calculate the heap offset int offset = 1 + (type * 45) + yOffset; for (int x = 0; x < 15; ++x) { + // Get the corresponding Y coordinate for each X coordinate int y = getHeapByte (offset + x); // Draw a vertical line if flag is set or draw an LED circle - if (y == 20) + if (y == 255) { for (int i = 0; i < 15; ++i) drawLEDCircle (x, i); @@ -168,15 +162,19 @@ public: } } + // Increment and wrap the Y offset to draw a 'moving' waveshape if (++yOffset == 30) yOffset = 0; - } )littlefoot"; } private: + //============================================================================== + /** Shared data heap is laid out as below. There is room for the waveshape type and + the Y coordinates for 1.5 cycles of each of the four waveshapes. */ + static constexpr uint32 waveshapeType = 0; // 1 byte static constexpr uint32 sineWaveOffset = 1; // 1 byte * 45 static constexpr uint32 squareWaveOffset = 46; // 1 byte * 45