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.

191 lines
6.2KB

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