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.

202 lines
6.7KB

  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. double currentPhase = 0.0;
  42. double phaseInc = (1.0 / 30.0) * (2.0 * double_Pi);
  43. for (int x = 0; x < 30; ++x)
  44. {
  45. // Scale and offset the sin output to the Lightpad display
  46. double 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. int min (int a, int b)
  94. {
  95. if (a > b)
  96. return b;
  97. return a;
  98. }
  99. int max (int a, int b)
  100. {
  101. if (a > b)
  102. return a;
  103. return b;
  104. }
  105. void drawLEDCircle (int x0, int y0)
  106. {
  107. setLED (x0, y0, 0xffff0000);
  108. int minLedIndex = 0;
  109. int maxLedIndex = 14;
  110. setLED (min (x0 + 1, maxLedIndex), y0, 0xff660000);
  111. setLED (max (x0 - 1, minLedIndex), y0, 0xff660000);
  112. setLED (x0, min (y0 + 1, maxLedIndex), 0xff660000);
  113. setLED (x0, max (y0 - 1, minLedIndex), 0xff660000);
  114. setLED (min (x0 + 1, maxLedIndex), min (y0 + 1, maxLedIndex), 0xff1a0000);
  115. setLED (min (x0 + 1, maxLedIndex), max (y0 - 1, minLedIndex), 0xff1a0000);
  116. setLED (max (x0 - 1, minLedIndex), min (y0 + 1, maxLedIndex), 0xff1a0000);
  117. setLED (max (x0 - 1, minLedIndex), max (y0 - 1, minLedIndex), 0xff1a0000);
  118. }
  119. void repaint()
  120. {
  121. // Clear LEDs to black
  122. fillRect (0xff000000, 0, 0, 15, 15);
  123. // Get the waveshape type
  124. int type = getHeapByte (0);
  125. // Calculate the heap offset
  126. int offset = 1 + (type * 45) + yOffset;
  127. for (int x = 0; x < 15; ++x)
  128. {
  129. // Get the corresponding Y coordinate for each X coordinate
  130. int y = getHeapByte (offset + x);
  131. // Draw a vertical line if flag is set or draw an LED circle
  132. if (y == 255)
  133. {
  134. for (int i = 0; i < 15; ++i)
  135. drawLEDCircle (x, i);
  136. }
  137. else if (x % 2 == 0)
  138. {
  139. drawLEDCircle (x, y);
  140. }
  141. }
  142. // Increment and wrap the Y offset to draw a 'moving' waveshape
  143. if (++yOffset == 30)
  144. yOffset = 0;
  145. }
  146. )littlefoot";
  147. }
  148. private:
  149. //==============================================================================
  150. /** Shared data heap is laid out as below. There is room for the waveshape type and
  151. the Y coordinates for 1.5 cycles of each of the four waveshapes. */
  152. static constexpr uint32 waveshapeType = 0; // 1 byte
  153. static constexpr uint32 sineWaveOffset = 1; // 1 byte * 45
  154. static constexpr uint32 squareWaveOffset = 46; // 1 byte * 45
  155. static constexpr uint32 sawWaveOffset = 91; // 1 byte * 45
  156. static constexpr uint32 triangleWaveOffset = 136; // 1 byte * 45
  157. //==============================================================================
  158. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WaveshapeProgram)
  159. };