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.

181 lines
5.9KB

  1. #pragma once
  2. /**
  3. A Program to draw moving waveshapes onto the LEDGrid
  4. */
  5. class WaveshapeProgram : public LEDGrid::Program
  6. {
  7. public:
  8. WaveshapeProgram (LEDGrid& lg) : Program (lg) {}
  9. /** Sets the waveshape type to display on the grid */
  10. void setWaveshapeType (uint8 type)
  11. {
  12. ledGrid.setDataByte (0, type);
  13. }
  14. /** Generates the Y coordinates for 1.5 cycles of each of the four waveshapes and stores them
  15. at the correct offsets in the shared data heap. */
  16. void generateWaveshapes()
  17. {
  18. uint8 sineWaveY[45];
  19. uint8 squareWaveY[45];
  20. uint8 sawWaveY[45];
  21. uint8 triangleWaveY[45];
  22. // Set current phase position to 0 and work out the required phase increment for one cycle
  23. double currentPhase = 0.0;
  24. double phaseInc = (1.0 / 30.0) * (2.0 * double_Pi);
  25. for (int x = 0; x < 30; ++x)
  26. {
  27. // Scale and offset the sin output to the Lightpad display
  28. double sineOutput = sin (currentPhase);
  29. sineWaveY[x] = static_cast<uint8> (roundToInt ((sineOutput * 6.5) + 7.0));
  30. // Square wave output, set flags for when vertical line should be drawn
  31. if (currentPhase < double_Pi)
  32. {
  33. if (x == 0)
  34. squareWaveY[x] = 255;
  35. else
  36. squareWaveY[x] = 1;
  37. }
  38. else
  39. {
  40. if (squareWaveY[x - 1] == 1)
  41. squareWaveY[x - 1] = 255;
  42. squareWaveY[x] = 13;
  43. }
  44. // Saw wave output, set flags for when vertical line should be drawn
  45. sawWaveY[x] = 14 - ((x / 2) % 15);
  46. if (sawWaveY[x] == 0 && sawWaveY[x - 1] != 255)
  47. sawWaveY[x] = 255;
  48. // Triangle wave output
  49. triangleWaveY[x] = x < 15 ? static_cast<uint8> (x) : static_cast<uint8> (14 - (x % 15));
  50. // Add half cycle to end of array so it loops correctly
  51. if (x < 15)
  52. {
  53. sineWaveY[x + 30] = sineWaveY[x];
  54. squareWaveY[x + 30] = squareWaveY[x];
  55. sawWaveY[x + 30] = sawWaveY[x];
  56. triangleWaveY[x + 30] = triangleWaveY[x];
  57. }
  58. // Increment the current phase
  59. currentPhase += phaseInc;
  60. }
  61. // Store the values for each of the waveshapes at the correct offsets in the shared data heap
  62. for (uint8 i = 0; i < 45; ++i)
  63. {
  64. ledGrid.setDataByte (sineWaveOffset + i, sineWaveY[i]);
  65. ledGrid.setDataByte (squareWaveOffset + i, squareWaveY[i]);
  66. ledGrid.setDataByte (sawWaveOffset + i, sawWaveY[i]);
  67. ledGrid.setDataByte (triangleWaveOffset + i, triangleWaveY[i]);
  68. }
  69. }
  70. uint32 getHeapSize() override
  71. {
  72. return totalDataSize;
  73. }
  74. String getLittleFootProgram() override
  75. {
  76. return R"littlefoot(
  77. int yOffset;
  78. int min (int a, int b)
  79. {
  80. if (a > b)
  81. return b;
  82. return a;
  83. }
  84. int max (int a, int b)
  85. {
  86. if (a > b)
  87. return a;
  88. return b;
  89. }
  90. void drawLEDCircle (int x0, int y0)
  91. {
  92. setLED (x0, y0, 0xffff0000);
  93. int minLedIndex = 0;
  94. int maxLedIndex = 14;
  95. setLED (min (x0 + 1, maxLedIndex), y0, 0xff660000);
  96. setLED (max (x0 - 1, minLedIndex), y0, 0xff660000);
  97. setLED (x0, min (y0 + 1, maxLedIndex), 0xff660000);
  98. setLED (x0, max (y0 - 1, minLedIndex), 0xff660000);
  99. setLED (min (x0 + 1, maxLedIndex), min (y0 + 1, maxLedIndex), 0xff1a0000);
  100. setLED (min (x0 + 1, maxLedIndex), max (y0 - 1, minLedIndex), 0xff1a0000);
  101. setLED (max (x0 - 1, minLedIndex), min (y0 + 1, maxLedIndex), 0xff1a0000);
  102. setLED (max (x0 - 1, minLedIndex), max (y0 - 1, minLedIndex), 0xff1a0000);
  103. }
  104. void repaint()
  105. {
  106. // Clear LEDs to black
  107. fillRect (0xff000000, 0, 0, 15, 15);
  108. // Get the waveshape type
  109. int type = getHeapByte (0);
  110. // Calculate the heap offset
  111. int offset = 1 + (type * 45) + yOffset;
  112. for (int x = 0; x < 15; ++x)
  113. {
  114. // Get the corresponding Y coordinate for each X coordinate
  115. int y = getHeapByte (offset + x);
  116. // Draw a vertical line if flag is set or draw an LED circle
  117. if (y == 255)
  118. {
  119. for (int i = 0; i < 15; ++i)
  120. drawLEDCircle (x, i);
  121. }
  122. else if (x % 2 == 0)
  123. {
  124. drawLEDCircle (x, y);
  125. }
  126. }
  127. // Increment and wrap the Y offset to draw a 'moving' waveshape
  128. if (++yOffset == 30)
  129. yOffset = 0;
  130. }
  131. )littlefoot";
  132. }
  133. private:
  134. //==============================================================================
  135. /** Shared data heap is laid out as below. There is room for the waveshape type and
  136. the Y coordinates for 1.5 cycles of each of the four waveshapes. */
  137. static constexpr uint32 waveshapeType = 0; // 1 byte
  138. static constexpr uint32 sineWaveOffset = 1; // 1 byte * 45
  139. static constexpr uint32 squareWaveOffset = 46; // 1 byte * 45
  140. static constexpr uint32 sawWaveOffset = 91; // 1 byte * 45
  141. static constexpr uint32 triangleWaveOffset = 136; // 1 byte * 45
  142. static constexpr uint32 totalDataSize = triangleWaveOffset + 45;
  143. //==============================================================================
  144. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WaveshapeProgram)
  145. };