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.

185 lines
6.0KB

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