The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. /**
  21. A Program to draw moving waveshapes onto the LEDGrid
  22. */
  23. class WaveshapeProgram : public Block::Program
  24. {
  25. public:
  26. WaveshapeProgram (Block& b) : Program (b) {}
  27. /** Sets the waveshape type to display on the grid */
  28. void setWaveshapeType (uint8 type)
  29. {
  30. block.setDataByte (0, type);
  31. }
  32. /** Generates the Y coordinates for 1.5 cycles of each of the four waveshapes and stores them
  33. at the correct offsets in the shared data heap. */
  34. void generateWaveshapes()
  35. {
  36. uint8 sineWaveY[45];
  37. uint8 squareWaveY[45];
  38. uint8 sawWaveY[45];
  39. uint8 triangleWaveY[45];
  40. // Set current phase position to 0 and work out the required phase increment for one cycle
  41. auto currentPhase = 0.0;
  42. auto phaseInc = (1.0 / 30.0) * (2.0 * double_Pi);
  43. for (auto x = 0; x < 30; ++x)
  44. {
  45. // Scale and offset the sin output to the Lightpad display
  46. auto sineOutput = sin (currentPhase);
  47. sineWaveY[x] = static_cast<uint8> (roundToInt ((sineOutput * 6.5) + 7.0));
  48. // Square wave output, set flags for when vertical line should be drawn
  49. if (currentPhase < double_Pi)
  50. {
  51. if (x == 0)
  52. squareWaveY[x] = 255;
  53. else
  54. squareWaveY[x] = 1;
  55. }
  56. else
  57. {
  58. if (squareWaveY[x - 1] == 1)
  59. squareWaveY[x - 1] = 255;
  60. squareWaveY[x] = 13;
  61. }
  62. // Saw wave output, set flags for when vertical line should be drawn
  63. sawWaveY[x] = 14 - ((x / 2) % 15);
  64. if (sawWaveY[x] == 0 && sawWaveY[x - 1] != 255)
  65. sawWaveY[x] = 255;
  66. // Triangle wave output
  67. triangleWaveY[x] = x < 15 ? static_cast<uint8> (x) : static_cast<uint8> (14 - (x % 15));
  68. // Add half cycle to end of array so it loops correctly
  69. if (x < 15)
  70. {
  71. sineWaveY[x + 30] = sineWaveY[x];
  72. squareWaveY[x + 30] = squareWaveY[x];
  73. sawWaveY[x + 30] = sawWaveY[x];
  74. triangleWaveY[x + 30] = triangleWaveY[x];
  75. }
  76. // Increment the current phase
  77. currentPhase += phaseInc;
  78. }
  79. // Store the values for each of the waveshapes at the correct offsets in the shared data heap
  80. for (uint8 i = 0; i < 45; ++i)
  81. {
  82. block.setDataByte (sineWaveOffset + i, sineWaveY[i]);
  83. block.setDataByte (squareWaveOffset + i, squareWaveY[i]);
  84. block.setDataByte (sawWaveOffset + i, sawWaveY[i]);
  85. block.setDataByte (triangleWaveOffset + i, triangleWaveY[i]);
  86. }
  87. }
  88. String getLittleFootProgram() override
  89. {
  90. return R"littlefoot(
  91. #heapsize: 256
  92. int yOffset;
  93. void drawLEDCircle (int x0, int y0)
  94. {
  95. blendPixel (0xffff0000, x0, y0);
  96. int minLedIndex = 0;
  97. int maxLedIndex = 14;
  98. blendPixel (0xff660000, min (x0 + 1, maxLedIndex), y0);
  99. blendPixel (0xff660000, max (x0 - 1, minLedIndex), y0);
  100. blendPixel (0xff660000, x0, min (y0 + 1, maxLedIndex));
  101. blendPixel (0xff660000, x0, max (y0 - 1, minLedIndex));
  102. blendPixel (0xff1a0000, min (x0 + 1, maxLedIndex), min (y0 + 1, maxLedIndex));
  103. blendPixel (0xff1a0000, min (x0 + 1, maxLedIndex), max (y0 - 1, minLedIndex));
  104. blendPixel (0xff1a0000, max (x0 - 1, minLedIndex), min (y0 + 1, maxLedIndex));
  105. blendPixel (0xff1a0000, max (x0 - 1, minLedIndex), max (y0 - 1, minLedIndex));
  106. }
  107. void repaint()
  108. {
  109. // Clear LEDs to black
  110. fillRect (0xff000000, 0, 0, 15, 15);
  111. // Get the waveshape type
  112. int type = getHeapByte (0);
  113. // Calculate the heap offset
  114. int offset = 1 + (type * 45) + yOffset;
  115. for (int x = 0; x < 15; ++x)
  116. {
  117. // Get the corresponding Y coordinate for each X coordinate
  118. int y = getHeapByte (offset + x);
  119. // Draw a vertical line if flag is set or draw an LED circle
  120. if (y == 255)
  121. {
  122. for (int i = 0; i < 15; ++i)
  123. drawLEDCircle (x, i);
  124. }
  125. else if (x % 2 == 0)
  126. {
  127. drawLEDCircle (x, y);
  128. }
  129. }
  130. // Increment and wrap the Y offset to draw a 'moving' waveshape
  131. if (++yOffset == 30)
  132. yOffset = 0;
  133. }
  134. )littlefoot";
  135. }
  136. private:
  137. //==============================================================================
  138. /** Shared data heap is laid out as below. There is room for the waveshape type and
  139. the Y coordinates for 1.5 cycles of each of the four waveshapes. */
  140. static constexpr uint32 waveshapeType = 0; // 1 byte
  141. static constexpr uint32 sineWaveOffset = 1; // 1 byte * 45
  142. static constexpr uint32 squareWaveOffset = 46; // 1 byte * 45
  143. static constexpr uint32 sawWaveOffset = 91; // 1 byte * 45
  144. static constexpr uint32 triangleWaveOffset = 136; // 1 byte * 45
  145. //==============================================================================
  146. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WaveshapeProgram)
  147. };