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.

471 lines
15KB

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