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.

479 lines
15KB

  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 (Block& b) : Program (b) {}
  24. int DrumPadGridProgram::getPadIndex (float posX, float posY) const
  25. {
  26. posX = juce::jmin (0.99f, posX / block.getWidth());
  27. posY = juce::jmin (0.99f, posY / block.getHeight());
  28. const uint32 offset = block.getDataByte (visiblePads_byte) ? numColumns1_byte : numColumns0_byte;
  29. const int numColumns = block.getDataByte (offset + numColumns0_byte);
  30. const int numRows = block.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 (block.getDataByte (touchedPads_byte + i) == 0)
  39. {
  40. block.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 (block.getDataByte (touchedPads_byte + i) == (padIdx + 1))
  50. block.setDataByte (touchedPads_byte + i, 0);
  51. }
  52. void DrumPadGridProgram::sendTouch (float x, float y, float z, LEDColour colour)
  53. {
  54. Block::ProgramEventMessage e;
  55. e.values[0] = 0x20000000
  56. + (juce::jlimit (0, 255, juce::roundToInt (x * (255.0f / block.getWidth()))) << 16)
  57. + (juce::jlimit (0, 255, juce::roundToInt (y * (255.0f / block.getHeight()))) << 8)
  58. + juce::jlimit (0, 255, juce::roundToInt (z * 255.0f));
  59. e.values[1] = (int32) colour.getARGB();
  60. block.sendProgramEvent (e);
  61. }
  62. //==============================================================================
  63. void DrumPadGridProgram::setGridFills (int numColumns, int numRows, const juce::Array<GridFill>& fills)
  64. {
  65. uint8 visiblePads = block.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. block.setDataByte (byteOffset + numColumns0_byte, (uint8) numColumns);
  72. block.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. block.setDataBits (colourOffsetBits, 5, fill.colour.getRed() >> 3);
  84. block.setDataBits (colourOffsetBits + 5, 6, fill.colour.getGreen() >> 2);
  85. block.setDataBits (colourOffsetBits + 11, 5, fill.colour.getBlue() >> 3);
  86. block.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 = block.getDataByte (visiblePads_byte) ? 0 : 1;
  94. setGridFills (newNumColumns, newNumRows, newFills, newVisible * numColumns1_byte);
  95. block.setDataByte (visiblePads_byte, newVisible);
  96. block.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. block.setDataBits (offset, 16, aniValue);
  109. block.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. block.setDataBits (offset + 16, 16, 0);
  117. }
  118. // Hijack touch dimming
  119. block.setDataByte (touchedPads_byte, 255);
  120. }
  121. void DrumPadGridProgram::resumeAnimations()
  122. {
  123. // Unhijack touch dimming
  124. block.setDataByte (touchedPads_byte, 0);
  125. }
  126. //==============================================================================
  127. juce::String DrumPadGridProgram::getLittleFootProgram()
  128. {
  129. return R"littlefoot(
  130. #heapsize: 256
  131. int dimFactor;
  132. int dimDelay;
  133. int slideAnimationProgress;
  134. int lastVisiblePads;
  135. int getGridColour (int index, int colourMapOffset)
  136. {
  137. int bit = (2 + colourMapOffset) * 8 + index * 16;
  138. return makeARGB (255,
  139. getHeapBits (bit, 5) << 3,
  140. getHeapBits (bit + 5, 6) << 2,
  141. getHeapBits (bit + 11, 5) << 3);
  142. }
  143. // Returns the current progress and also increments it for next frame
  144. int getAnimationProgress (int index)
  145. {
  146. // Only 16 animated pads supported
  147. if (index > 15)
  148. return 0;
  149. int offsetBits = 162 * 8 + index * 32;
  150. int currentProgress = getHeapBits (offsetBits, 16);
  151. int increment = getHeapBits (offsetBits + 16, 16);
  152. int nextFrame = currentProgress + increment;
  153. // Set incremented 16 bit number.
  154. setHeapByte (162 + index * 4, nextFrame & 0xff);
  155. setHeapByte (163 + index * 4, nextFrame >> 8);
  156. return currentProgress;
  157. }
  158. void outlineRect (int colour, int x, int y, int w)
  159. {
  160. fillRect (colour, x, y, w, 1);
  161. fillRect (colour, x, y + w - 1, w, 1);
  162. fillRect (colour, x, y + 1, 1, w - 1);
  163. fillRect (colour, x + w - 1, y + 1, 1, w - 1);
  164. }
  165. void drawPlus (int colour, int x, int y, int w)
  166. {
  167. fillRect (colour, x, y + (w / 2), w, 1);
  168. fillRect (colour, x + (w / 2), y, 1, w);
  169. }
  170. void fillGradientRect (int colour, int x, int y, int w)
  171. {
  172. if (colour != 0xff000000)
  173. {
  174. int divisor = w + w - 1;
  175. for (int yy = 0; yy < w; ++yy)
  176. {
  177. for (int xx = yy; xx < w; ++xx)
  178. {
  179. int gradColour = blendARGB (colour, makeARGB (((xx + yy) * 250) / divisor, 0, 0, 0));
  180. fillPixel (gradColour, x + xx, y + yy);
  181. fillPixel (gradColour, x + yy, y + xx);
  182. }
  183. }
  184. }
  185. }
  186. // TODO: Tom M: This is massaged to work with 3x3 pads and for dots to sync
  187. // with Apple POS loop length. Rework to be more robust & flexible.
  188. void drawPizzaLED (int colour, int x, int y, int w, int progress)
  189. {
  190. --w;
  191. x += 1;
  192. int numToDo = ((8 * progress) / 255) + 1;
  193. int totalLen = w * 4;
  194. for (int i = 1; i <= numToDo; ++i)
  195. {
  196. fillPixel (colour, x, y);
  197. if (i < w)
  198. ++x;
  199. else if (i < (w * 2))
  200. ++y;
  201. else if (i < (w * 3))
  202. --x;
  203. else if (i < totalLen)
  204. --y;
  205. }
  206. }
  207. void drawPad (int padX, int padY, int padW,
  208. int colour, int fill, int animateProgress)
  209. {
  210. animateProgress >>= 8; // 16 bit to 8 bit
  211. int halfW = padW / 2;
  212. if (fill == 0) // Gradient fill
  213. {
  214. fillGradientRect (colour, padX, padY, padW);
  215. }
  216. else if (fill == 1) // Filled
  217. {
  218. fillRect (colour, padX, padY, padW, padW);
  219. }
  220. else if (fill == 2) // Hollow
  221. {
  222. outlineRect (colour, padX, padY, padW);
  223. }
  224. else if (fill == 3) // Hollow with plus
  225. {
  226. outlineRect (colour, padX, padY, padW);
  227. drawPlus (0xffffffff, padX, padY, padW);
  228. }
  229. else if (fill == 4) // Pulsing dot
  230. {
  231. int pulseCol = blendARGB (colour, makeARGB (animateProgress, 0, 0, 0));
  232. fillPixel (pulseCol, padX + halfW, padY + halfW);
  233. }
  234. else if (fill == 5) // Blinking dot
  235. {
  236. int blinkCol = animateProgress > 64 ? 0xff000000 : colour;
  237. fillPixel (blinkCol, padX + halfW, padY + halfW);
  238. }
  239. else if (fill == 6) // Pizza filled
  240. {
  241. outlineRect (blendARGB (colour, 0xdc000000), padX, padY, padW); // Dim outline
  242. fillPixel (colour, padX + halfW, padY + halfW); // Bright centre
  243. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  244. }
  245. else // Pizza hollow
  246. {
  247. outlineRect (blendARGB (colour, 0xdc000000), padX, padY, padW); // Dim outline
  248. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  249. }
  250. }
  251. int isPadActive (int index)
  252. {
  253. if (getHeapInt (158) == 0) // None active
  254. return 0;
  255. ++index;
  256. return index == getHeapByte (158) ||
  257. index == getHeapByte (159) ||
  258. index == getHeapByte (160) ||
  259. index == getHeapByte (161);
  260. }
  261. void updateDimFactor()
  262. {
  263. if (getHeapInt (158) == 0)
  264. {
  265. if (--dimDelay <= 0)
  266. {
  267. dimFactor -= 12;
  268. if (dimFactor < 0)
  269. dimFactor = 0;
  270. }
  271. }
  272. else
  273. {
  274. dimFactor = 180;
  275. dimDelay = 12;
  276. }
  277. }
  278. void drawPads (int offsetX, int offsetY, int colourMapOffset)
  279. {
  280. int padsPerSide = getHeapByte (0 + colourMapOffset);
  281. if (padsPerSide < 2)
  282. return;
  283. int blockW = 15 / padsPerSide;
  284. int blockPlusGapW = blockW + (15 - padsPerSide * blockW) / (padsPerSide - 1);
  285. for (int padY = 0; padY < padsPerSide; ++padY)
  286. {
  287. for (int padX = 0; padX < padsPerSide; ++padX)
  288. {
  289. int ledX = offsetX + padX * blockPlusGapW;
  290. int ledY = offsetY + padY * blockPlusGapW;
  291. if (ledX < 15 &&
  292. ledY < 15 &&
  293. (ledX + blockW) >= 0 &&
  294. (ledY + blockW) >= 0)
  295. {
  296. int padIdx = padX + padY * padsPerSide;
  297. bool padActive = isPadActive (padIdx);
  298. int blendCol = padActive ? 255 : 0;
  299. int blendAmt = padActive ? dimFactor >> 1 : dimFactor;
  300. int colour = blendARGB (getGridColour (padIdx, colourMapOffset),
  301. makeARGB (blendAmt, blendCol, blendCol, blendCol));
  302. int fillType = getHeapByte (colourMapOffset + 52 + padIdx);
  303. int animate = getAnimationProgress (padIdx);
  304. drawPad (ledX, ledY, blockW, colour, fillType, animate);
  305. }
  306. }
  307. }
  308. }
  309. void slideAnimatePads()
  310. {
  311. int nowVisible = getHeapByte (155);
  312. if (lastVisiblePads != nowVisible)
  313. {
  314. lastVisiblePads = nowVisible;
  315. if (slideAnimationProgress <= 0)
  316. slideAnimationProgress = 15;
  317. }
  318. // If animation is complete, draw normally.
  319. if (slideAnimationProgress <= 0)
  320. {
  321. drawPads (0, 0, 78 * nowVisible);
  322. slideAnimationProgress = 0;
  323. }
  324. else
  325. {
  326. int direction = getHeapByte (156);
  327. slideAnimationProgress -= 1;
  328. int inPos = nowVisible == 0 ? 0 : 78;
  329. int outPos = nowVisible == 0 ? 78 : 0;
  330. if (direction == 0) // Up
  331. {
  332. drawPads (0, slideAnimationProgress - 16, outPos);
  333. drawPads (0, slideAnimationProgress, inPos);
  334. }
  335. else if (direction == 1) // Down
  336. {
  337. drawPads (0, 16 - slideAnimationProgress, outPos);
  338. drawPads (0, 0 - slideAnimationProgress, inPos);
  339. }
  340. else if (direction == 2) // Left
  341. {
  342. drawPads (16 - slideAnimationProgress, 0, outPos);
  343. drawPads (slideAnimationProgress, 0, inPos);
  344. }
  345. else if (direction == 3) // Right
  346. {
  347. drawPads (16 - slideAnimationProgress, 0, outPos);
  348. drawPads (0 - slideAnimationProgress, 0, inPos);
  349. }
  350. else // None
  351. {
  352. drawPads (0, 0, 78 * nowVisible);
  353. slideAnimationProgress = 0;
  354. }
  355. }
  356. }
  357. void repaint()
  358. {
  359. // showErrorOnFail, showRepaintTime, showMovingDot
  360. //enableDebug (true, true, false);
  361. // Clear LEDs to black, update dim animation
  362. fillRect (0xff000000, 0, 0, 15, 15);
  363. updateDimFactor();
  364. // Does the main painting of pads
  365. slideAnimatePads();
  366. // Overlay heatmap
  367. drawPressureMap();
  368. fadePressureMap();
  369. }
  370. // DrumPadGridProgram::sendTouch results in this callback, giving
  371. // us more touch updates per frame and therefore smoother trails.
  372. void handleMessage (int pos, int colour, int dummy)
  373. {
  374. if ((pos >> 24) != 0x20)
  375. return;
  376. int tx = (pos >> 16) & 0xff;
  377. int ty = (pos >> 8) & 0xff;
  378. int tz = pos & 0xff;
  379. addPressurePoint (colour,
  380. tx * (2.0 / (256 + 20)),
  381. ty * (2.0 / (256 + 20)),
  382. tz * (1.0 / 3.0));
  383. }
  384. )littlefoot";
  385. }