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.

548 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. DrumPadGridProgram::DrumPadGridProgram (LEDGrid& lg) : Program (lg) {}
  24. int DrumPadGridProgram::getPadIndex (float posX, float posY) const
  25. {
  26. posX = juce::jmin (0.99f, posX / ledGrid.block.getWidth());
  27. posY = juce::jmin (0.99f, posY / ledGrid.block.getHeight());
  28. const uint32 offset = ledGrid.getDataByte (visiblePads_byte) ? numColumns1_byte : numColumns0_byte;
  29. const int numColumns = ledGrid.getDataByte (offset + numColumns0_byte);
  30. const int numRows = ledGrid.getDataByte (offset + numRows0_byte);
  31. return int (posX * numColumns) + int (posY * numRows) * numColumns;
  32. }
  33. void DrumPadGridProgram::startTouch (float startX, float startY)
  34. {
  35. const auto padIdx = getPadIndex (startX, startY);
  36. for (size_t i = 0; i < 4; ++i)
  37. {
  38. if (ledGrid.getDataByte (touchedPads_byte + i) == 0)
  39. {
  40. ledGrid.setDataByte (touchedPads_byte + i, static_cast<uint8> (padIdx + 1));
  41. break;
  42. }
  43. }
  44. }
  45. void DrumPadGridProgram::endTouch (float startX, float startY)
  46. {
  47. const auto padIdx = getPadIndex (startX, startY);
  48. for (size_t i = 0; i < 4; ++i)
  49. if (ledGrid.getDataByte (touchedPads_byte + i) == (padIdx + 1))
  50. ledGrid.setDataByte (touchedPads_byte + i, 0);
  51. }
  52. void DrumPadGridProgram::sendTouch (float x, float y, float z, LEDColour colour)
  53. {
  54. LEDGrid::ProgramEventMessage e;
  55. e.values[0] = 0x20000000
  56. + (juce::jlimit (0, 255, juce::roundToInt (x * (255.0f / ledGrid.block.getWidth()))) << 16)
  57. + (juce::jlimit (0, 255, juce::roundToInt (y * (255.0f / ledGrid.block.getHeight()))) << 8)
  58. + juce::jlimit (0, 255, juce::roundToInt (z * 255.0f));
  59. e.values[1] = (int32) colour.getARGB();
  60. ledGrid.sendProgramEvent (e);
  61. }
  62. //==============================================================================
  63. void DrumPadGridProgram::setGridFills (int numColumns, int numRows, const juce::Array<GridFill>& fills)
  64. {
  65. uint8 visiblePads = ledGrid.getDataByte (visiblePads_byte);
  66. setGridFills (numColumns, numRows, fills, visiblePads * numColumns1_byte);
  67. }
  68. void DrumPadGridProgram::setGridFills (int numColumns, int numRows, const juce::Array<GridFill>& fills, uint32 byteOffset)
  69. {
  70. jassert (numColumns * numRows == fills.size());
  71. ledGrid.setDataByte (byteOffset + numColumns0_byte, (uint8) numColumns);
  72. ledGrid.setDataByte (byteOffset + numRows0_byte, (uint8) numRows);
  73. uint32 i = 0;
  74. for (auto fill : fills)
  75. {
  76. if (i >= maxNumPads)
  77. {
  78. jassertfalse;
  79. break;
  80. }
  81. const uint32 colourOffsetBytes = byteOffset + colours0_byte + i * colourSizeBytes;
  82. const uint32 colourOffsetBits = colourOffsetBytes * 8;
  83. ledGrid.setDataBits (colourOffsetBits, 5, fill.colour.getRed() >> 3);
  84. ledGrid.setDataBits (colourOffsetBits + 5, 6, fill.colour.getGreen() >> 2);
  85. ledGrid.setDataBits (colourOffsetBits + 11, 5, fill.colour.getBlue() >> 3);
  86. ledGrid.setDataByte (byteOffset + fillTypes0_byte + i, static_cast<uint8> (fill.fillType));
  87. ++i;
  88. }
  89. }
  90. void DrumPadGridProgram::triggerSlideTransition (int newNumColumns, int newNumRows,
  91. const juce::Array<GridFill>& newFills, SlideDirection direction)
  92. {
  93. uint8 newVisible = ledGrid.getDataByte (visiblePads_byte) ? 0 : 1;
  94. setGridFills (newNumColumns, newNumRows, newFills, newVisible * numColumns1_byte);
  95. ledGrid.setDataByte (visiblePads_byte, newVisible);
  96. ledGrid.setDataByte (slideDirection_byte, (uint8) direction);
  97. }
  98. //==============================================================================
  99. void DrumPadGridProgram::setPadAnimationState (uint32 padIdx, double loopTimeSecs, double currentProgress)
  100. {
  101. // Only 16 animated pads are supported.
  102. jassert (padIdx < 16);
  103. // Compensate for bluetooth latency & led resolution, tweaked by eye for POS app.
  104. currentProgress = std::fmod (currentProgress + 0.1, 1.0);
  105. uint16 aniValue = uint16 (juce::roundToInt ((255 << 8) * currentProgress));
  106. uint16 aniIncrement = loopTimeSecs > 0.0 ? uint16 (juce::roundToInt (((255 << 8) / 25.0) / loopTimeSecs)) : 0;
  107. uint32 offset = 8 * animationTimers_byte + 32 * padIdx;
  108. ledGrid.setDataBits (offset, 16, aniValue);
  109. ledGrid.setDataBits (offset + 16, 16, aniIncrement);
  110. }
  111. void DrumPadGridProgram::suspendAnimations()
  112. {
  113. for (uint32 i = 0; i < 16; ++i)
  114. {
  115. uint32 offset = 8 * animationTimers_byte + 32 * i;
  116. ledGrid.setDataBits (offset + 16, 16, 0);
  117. }
  118. // Hijack touch dimming
  119. ledGrid.setDataByte (touchedPads_byte, 255);
  120. }
  121. void DrumPadGridProgram::resumeAnimations()
  122. {
  123. // Unhijack touch dimming
  124. ledGrid.setDataByte (touchedPads_byte, 0);
  125. }
  126. //==============================================================================
  127. uint32 DrumPadGridProgram::getHeapSize()
  128. {
  129. return totalDataSize;
  130. }
  131. juce::String DrumPadGridProgram::getLittleFootProgram()
  132. {
  133. return R"littlefoot(
  134. int dimFactor;
  135. int dimDelay;
  136. int slideAnimationProgress;
  137. int lastVisiblePads;
  138. int getGridColour (int index, int colourMapOffset)
  139. {
  140. int bit = (2 + colourMapOffset) * 8 + index * 16;
  141. return makeARGB (255,
  142. getHeapBits (bit, 5) << 3,
  143. getHeapBits (bit + 5, 6) << 2,
  144. getHeapBits (bit + 11, 5) << 3);
  145. }
  146. // Returns the current progress and also increments it for next frame
  147. int getAnimationProgress (int index)
  148. {
  149. // Only 16 animated pads supported
  150. if (index > 15)
  151. return 0;
  152. int offsetBits = 162 * 8 + index * 32;
  153. int currentProgress = getHeapBits (offsetBits, 16);
  154. int increment = getHeapBits (offsetBits + 16, 16);
  155. int nextFrame = currentProgress + increment;
  156. // Set incremented 16 bit number.
  157. setHeapByte (162 + index * 4, nextFrame & 0xff);
  158. setHeapByte (163 + index * 4, nextFrame >> 8);
  159. return currentProgress;
  160. }
  161. void outlineRect (int colour, int x, int y, int w)
  162. {
  163. fillRect (colour, x, y, w, 1);
  164. fillRect (colour, x, y + w - 1, w, 1);
  165. fillRect (colour, x, y + 1, 1, w - 1);
  166. fillRect (colour, x + w - 1, y + 1, 1, w - 1);
  167. }
  168. void drawPlus (int colour, int x, int y, int w)
  169. {
  170. fillRect (colour, x, y + (w / 2), w, 1);
  171. fillRect (colour, x + (w / 2), y, 1, w);
  172. }
  173. void fillGradientRect (int colour, int x, int y, int w)
  174. {
  175. if (colour != 0xff000000)
  176. {
  177. int divisor = w + w - 1;
  178. for (int yy = 0; yy < w; ++yy)
  179. {
  180. for (int xx = yy; xx < w; ++xx)
  181. {
  182. int gradColour = blendARGB (colour, makeARGB (((xx + yy) * 250) / divisor, 0, 0, 0));
  183. setLED (x + xx, y + yy, gradColour);
  184. setLED (x + yy, y + xx, gradColour);
  185. }
  186. }
  187. }
  188. }
  189. // TODO: Tom M: This is massaged to work with 3x3 pads and for dots to sync
  190. // with Apple POS loop length. Rework to be more robust & flexible.
  191. void drawPizzaLED (int colour, int x, int y, int w, int progress)
  192. {
  193. --w;
  194. x += 1;
  195. int numToDo = ((8 * progress) / 255) + 1;
  196. int totalLen = w * 4;
  197. for (int i = 1; i <= numToDo; ++i)
  198. {
  199. setLED (x, y, colour);
  200. if (i < w)
  201. ++x;
  202. else if (i < (w * 2))
  203. ++y;
  204. else if (i < (w * 3))
  205. --x;
  206. else if (i < totalLen)
  207. --y;
  208. }
  209. }
  210. void drawPad (int padX, int padY, int padW,
  211. int colour, int fill, int animateProgress)
  212. {
  213. animateProgress >>= 8; // 16 bit to 8 bit
  214. int halfW = padW / 2;
  215. if (fill == 0) // Gradient fill
  216. {
  217. fillGradientRect (colour, padX, padY, padW);
  218. }
  219. else if (fill == 1) // Filled
  220. {
  221. fillRect (colour, padX, padY, padW, padW);
  222. }
  223. else if (fill == 2) // Hollow
  224. {
  225. outlineRect (colour, padX, padY, padW);
  226. }
  227. else if (fill == 3) // Hollow with plus
  228. {
  229. outlineRect (colour, padX, padY, padW);
  230. drawPlus (0xffffffff, padX, padY, padW);
  231. }
  232. else if (fill == 4) // Pulsing dot
  233. {
  234. int pulseCol = blendARGB (colour, makeARGB (animateProgress, 0, 0, 0));
  235. setLED (padX + halfW, padY + halfW, pulseCol);
  236. }
  237. else if (fill == 5) // Blinking dot
  238. {
  239. int blinkCol = animateProgress > 64 ? makeARGB (255, 0, 0, 0) : colour;
  240. setLED (padX + halfW, padY + halfW, blinkCol);
  241. }
  242. else if (fill == 6) // Pizza filled
  243. {
  244. outlineRect (blendARGB (colour, makeARGB (220, 0, 0, 0)), padX, padY, padW); // Dim outline
  245. setLED (padX + halfW, padY + halfW, colour); // Bright centre
  246. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  247. }
  248. else if (fill == 7) // Pizza hollow
  249. {
  250. outlineRect (blendARGB (colour, makeARGB (220, 0, 0, 0)), padX, padY, padW); // Dim outline
  251. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  252. return;
  253. }
  254. }
  255. void fadeHeatMap()
  256. {
  257. for (int i = 0; i < 225; ++i)
  258. {
  259. int colourOffset = 226 + i * 4;
  260. int colour = getHeapInt (colourOffset);
  261. int alpha = (colour >> 24) & 0xff;
  262. if (alpha > 0)
  263. {
  264. alpha -= getHeapByte (1126 + i);
  265. setHeapInt (colourOffset, alpha < 0 ? 0 : ((alpha << 24) | (colour & 0xffffff)));
  266. }
  267. }
  268. }
  269. void addToHeatMap (int x, int y, int colour)
  270. {
  271. if (x >= 0 && y >= 0 && x < 15 && y < 15)
  272. {
  273. int offset = 226 + 4 * (x + y * 15);
  274. colour = blendARGB (getHeapInt (offset), colour);
  275. setHeapInt (offset, colour);
  276. int decay = ((colour >> 24) & 0xff) / 14; // change divisor to change trail times
  277. offset = 1126 + (x + y * 15);
  278. setHeapByte (offset, decay > 0 ? decay : 1);
  279. }
  280. }
  281. int getHeatmapColour (int x, int y)
  282. {
  283. return getHeapInt (226 + 4 * (x + y * 15));
  284. }
  285. int isPadActive (int index)
  286. {
  287. if (getHeapInt (158) == 0) // None active
  288. return 0;
  289. ++index;
  290. return index == getHeapByte (158) ||
  291. index == getHeapByte (159) ||
  292. index == getHeapByte (160) ||
  293. index == getHeapByte (161);
  294. }
  295. void updateDimFactor()
  296. {
  297. if (getHeapInt (158) == 0)
  298. {
  299. if (--dimDelay <= 0)
  300. {
  301. dimFactor -= 12;
  302. if (dimFactor < 0)
  303. dimFactor = 0;
  304. }
  305. }
  306. else
  307. {
  308. dimFactor = 180;
  309. dimDelay = 12;
  310. }
  311. }
  312. void drawPads (int offsetX, int offsetY, int colourMapOffset)
  313. {
  314. int padsPerSide = getHeapByte (0 + colourMapOffset);
  315. if (padsPerSide < 2)
  316. return;
  317. int blockW = 15 / padsPerSide;
  318. int blockPlusGapW = blockW + (15 - padsPerSide * blockW) / (padsPerSide - 1);
  319. for (int padY = 0; padY < padsPerSide; ++padY)
  320. {
  321. for (int padX = 0; padX < padsPerSide; ++padX)
  322. {
  323. int ledX = offsetX + padX * blockPlusGapW;
  324. int ledY = offsetY + padY * blockPlusGapW;
  325. if (ledX < 15 &&
  326. ledY < 15 &&
  327. (ledX + blockW) >= 0 &&
  328. (ledY + blockW) >= 0)
  329. {
  330. int padIdx = padX + padY * padsPerSide;
  331. bool padActive = isPadActive (padIdx);
  332. int blendCol = padActive ? 255 : 0;
  333. int blendAmt = padActive ? dimFactor >> 1 : dimFactor;
  334. int colour = blendARGB (getGridColour (padIdx, colourMapOffset),
  335. makeARGB (blendAmt, blendCol, blendCol, blendCol));
  336. int fillType = getHeapByte (colourMapOffset + 52 + padIdx);
  337. int animate = getAnimationProgress (padIdx);
  338. drawPad (ledX, ledY, blockW, colour, fillType, animate);
  339. }
  340. }
  341. }
  342. }
  343. void slideAnimatePads()
  344. {
  345. int nowVisible = getHeapByte (155);
  346. if (lastVisiblePads != nowVisible)
  347. {
  348. lastVisiblePads = nowVisible;
  349. if (slideAnimationProgress <= 0)
  350. slideAnimationProgress = 15;
  351. }
  352. // If animation is complete, draw normally.
  353. if (slideAnimationProgress <= 0)
  354. {
  355. drawPads (0, 0, 78 * nowVisible);
  356. slideAnimationProgress = 0;
  357. }
  358. else
  359. {
  360. int direction = getHeapByte (156);
  361. slideAnimationProgress -= 1;
  362. int inPos = nowVisible == 0 ? 0 : 78;
  363. int outPos = nowVisible == 0 ? 78 : 0;
  364. if (direction == 0) // Up
  365. {
  366. drawPads (0, slideAnimationProgress - 16, outPos);
  367. drawPads (0, slideAnimationProgress, inPos);
  368. }
  369. else if (direction == 1) // Down
  370. {
  371. drawPads (0, 16 - slideAnimationProgress, outPos);
  372. drawPads (0, 0 - slideAnimationProgress, inPos);
  373. }
  374. else if (direction == 2) // Left
  375. {
  376. drawPads (16 - slideAnimationProgress, 0, outPos);
  377. drawPads (slideAnimationProgress, 0, inPos);
  378. }
  379. else if (direction == 3) // Right
  380. {
  381. drawPads (16 - slideAnimationProgress, 0, outPos);
  382. drawPads (0 - slideAnimationProgress, 0, inPos);
  383. }
  384. else // None
  385. {
  386. drawPads (0, 0, 78 * nowVisible);
  387. slideAnimationProgress = 0;
  388. }
  389. }
  390. }
  391. void repaint()
  392. {
  393. // showErrorOnFail, showRepaintTime, showMovingDot
  394. //enableDebug (true, true, false);
  395. // Clear LEDs to black, update dim animation
  396. fillRect (0xff000000, 0, 0, 15, 15);
  397. updateDimFactor();
  398. // Does the main painting of pads
  399. slideAnimatePads();
  400. // Overlay heatmap
  401. for (int y = 0; y < 15; ++y)
  402. for (int x = 0; x < 15; ++x)
  403. blendLED (x, y, getHeatmapColour (x, y));
  404. fadeHeatMap();
  405. }
  406. // DrumPadGridProgram::sendTouch results in this callback, giving
  407. // us more touch updates per frame and therefore smoother trails.
  408. void handleMessage (int pos, int colour)
  409. {
  410. if ((pos >> 24) != 0x20)
  411. return;
  412. int tx = ((pos >> 16) & 0xff) - 13;
  413. int ty = ((pos >> 8) & 0xff) - 13;
  414. int tz = pos & 0xff;
  415. tz = tz > 30 ? tz : 30;
  416. int ledCentreX = tx >> 4;
  417. int ledCentreY = ty >> 4;
  418. int adjustX = (tx - (ledCentreX << 4)) >> 2;
  419. int adjustY = (ty - (ledCentreY << 4)) >> 2;
  420. for (int dy = -2; dy <= 2; ++dy)
  421. {
  422. for (int dx = -2; dx <= 2; ++dx)
  423. {
  424. int distance = dx * dx + dy * dy;
  425. int level = distance == 0 ? 255 : (distance == 1 ? 132 : (distance < 5 ? 9 : (distance == 5 ? 2 : 0)));
  426. level += (dx * adjustX);
  427. level += (dy * adjustY);
  428. level = (tz * level) >> 8;
  429. if (level > 0)
  430. addToHeatMap (ledCentreX + dx, ledCentreY + dy,
  431. makeARGB (level, colour >> 16, colour >> 8, colour));
  432. }
  433. }
  434. }
  435. )littlefoot";
  436. }