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.

882 lines
27KB

  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. namespace juce
  18. {
  19. DrumPadGridProgram::DrumPadGridProgram (Block& b) : Program (b) {}
  20. int DrumPadGridProgram::getPadIndex (float posX, float posY) const
  21. {
  22. posX = juce::jmin (0.99f, posX / block.getWidth());
  23. posY = juce::jmin (0.99f, posY / block.getHeight());
  24. const uint32 offset = block.getDataByte (visiblePads_byte) ? numColumns1_byte : numColumns0_byte;
  25. const int numColumns = block.getDataByte (offset + numColumns0_byte);
  26. const int numRows = block.getDataByte (offset + numRows0_byte);
  27. return int (posX * numColumns) + int (posY * numRows) * numColumns;
  28. }
  29. void DrumPadGridProgram::startTouch (float startX, float startY)
  30. {
  31. const auto padIdx = getPadIndex (startX, startY);
  32. for (size_t i = 0; i < 4; ++i)
  33. {
  34. if (block.getDataByte (touchedPads_byte + i) == 0)
  35. {
  36. block.setDataByte (touchedPads_byte + i, static_cast<uint8> (padIdx + 1));
  37. break;
  38. }
  39. }
  40. }
  41. void DrumPadGridProgram::endTouch (float startX, float startY)
  42. {
  43. const auto padIdx = getPadIndex (startX, startY);
  44. for (size_t i = 0; i < 4; ++i)
  45. if (block.getDataByte (touchedPads_byte + i) == (padIdx + 1))
  46. block.setDataByte (touchedPads_byte + i, 0);
  47. }
  48. void DrumPadGridProgram::sendTouch (float x, float y, float z, LEDColour colour)
  49. {
  50. Block::ProgramEventMessage e;
  51. e.values[0] = 0x20000000
  52. + (juce::jlimit (0, 255, juce::roundToInt (x * (255.0f / block.getWidth()))) << 16)
  53. + (juce::jlimit (0, 255, juce::roundToInt (y * (255.0f / block.getHeight()))) << 8)
  54. + juce::jlimit (0, 255, juce::roundToInt (z * 255.0f));
  55. e.values[1] = (int32) colour.getARGB();
  56. block.sendProgramEvent (e);
  57. }
  58. //==============================================================================
  59. void DrumPadGridProgram::setGridFills (int numColumns, int numRows, const juce::Array<GridFill>& fills)
  60. {
  61. uint8 visiblePads = block.getDataByte (visiblePads_byte);
  62. setGridFills (numColumns, numRows, fills, visiblePads * numColumns1_byte);
  63. }
  64. void DrumPadGridProgram::setGridFills (int numColumns, int numRows, const juce::Array<GridFill>& fills, uint32 byteOffset)
  65. {
  66. jassert (numColumns * numRows == fills.size());
  67. block.setDataByte (byteOffset + numColumns0_byte, (uint8) numColumns);
  68. block.setDataByte (byteOffset + numRows0_byte, (uint8) numRows);
  69. uint32 i = 0;
  70. for (auto fill : fills)
  71. {
  72. if (i >= maxNumPads)
  73. {
  74. jassertfalse;
  75. break;
  76. }
  77. const uint32 colourOffsetBytes = byteOffset + colours0_byte + i * colourSizeBytes;
  78. const uint32 colourOffsetBits = colourOffsetBytes * 8;
  79. block.setDataBits (colourOffsetBits, 5, fill.colour.getRed() >> 3);
  80. block.setDataBits (colourOffsetBits + 5, 6, fill.colour.getGreen() >> 2);
  81. block.setDataBits (colourOffsetBits + 11, 5, fill.colour.getBlue() >> 3);
  82. block.setDataByte (byteOffset + fillTypes0_byte + i, static_cast<uint8> (fill.fillType));
  83. ++i;
  84. }
  85. }
  86. void DrumPadGridProgram::triggerSlideTransition (int newNumColumns, int newNumRows,
  87. const juce::Array<GridFill>& newFills, SlideDirection direction)
  88. {
  89. uint8 newVisible = block.getDataByte (visiblePads_byte) ? 0 : 1;
  90. setGridFills (newNumColumns, newNumRows, newFills, newVisible * numColumns1_byte);
  91. block.setDataByte (visiblePads_byte, newVisible);
  92. block.setDataByte (slideDirection_byte, (uint8) direction);
  93. }
  94. //==============================================================================
  95. void DrumPadGridProgram::setPadAnimationState (uint32 padIdx, double loopTimeSecs, double currentProgress)
  96. {
  97. // Only 16 animated pads are supported.
  98. jassert (padIdx < 16);
  99. // Compensate for bluetooth latency & led resolution, tweaked by eye for POS app.
  100. currentProgress = std::fmod (currentProgress + 0.1, 1.0);
  101. uint16 aniValue = uint16 (juce::roundToInt ((255 << 8) * currentProgress));
  102. uint16 aniIncrement = loopTimeSecs > 0.0 ? uint16 (juce::roundToInt (((255 << 8) / 25.0) / loopTimeSecs)) : 0;
  103. uint32 offset = 8 * animationTimers_byte + 32 * padIdx;
  104. block.setDataBits (offset, 16, aniValue);
  105. block.setDataBits (offset + 16, 16, aniIncrement);
  106. }
  107. void DrumPadGridProgram::suspendAnimations()
  108. {
  109. for (uint32 i = 0; i < 16; ++i)
  110. {
  111. uint32 offset = 8 * animationTimers_byte + 32 * i;
  112. block.setDataBits (offset + 16, 16, 0);
  113. }
  114. // Hijack touch dimming
  115. block.setDataByte (touchedPads_byte, 255);
  116. }
  117. void DrumPadGridProgram::resumeAnimations()
  118. {
  119. // Unhijack touch dimming
  120. block.setDataByte (touchedPads_byte, 0);
  121. }
  122. //==============================================================================
  123. juce::String DrumPadGridProgram::getLittleFootProgram()
  124. {
  125. if (block.versionNumber.isEmpty() || block.versionNumber.compare ("0.2.5") < 0)
  126. return getLittleFootProgramPre25();
  127. return getLittleFootProgramPost25();
  128. }
  129. juce::String DrumPadGridProgram::getLittleFootProgramPre25() const
  130. {
  131. // Uses its own heatmap, not the one provided in newer firmware
  132. // Also can't use blocks config, introduced in 2.5.
  133. return R"littlefoot(
  134. #heapsize: 1351
  135. int dimFactor;
  136. int dimDelay;
  137. int slideAnimationProgress;
  138. int lastVisiblePads;
  139. int getGridColour (int index, int colourMapOffset)
  140. {
  141. int bit = (2 + colourMapOffset) * 8 + index * 16;
  142. return makeARGB (255,
  143. getHeapBits (bit, 5) << 3,
  144. getHeapBits (bit + 5, 6) << 2,
  145. getHeapBits (bit + 11, 5) << 3);
  146. }
  147. // Returns the current progress and also increments it for next frame
  148. int getAnimationProgress (int index)
  149. {
  150. // Only 16 animated pads supported
  151. if (index > 15)
  152. return 0;
  153. int offsetBits = 162 * 8 + index * 32;
  154. int currentProgress = getHeapBits (offsetBits, 16);
  155. int increment = getHeapBits (offsetBits + 16, 16);
  156. int nextFrame = currentProgress + increment;
  157. // Set incremented 16 bit number.
  158. setHeapByte (162 + index * 4, nextFrame & 0xff);
  159. setHeapByte (163 + index * 4, nextFrame >> 8);
  160. return currentProgress;
  161. }
  162. void outlineRect (int colour, int x, int y, int w)
  163. {
  164. fillRect (colour, x, y, w, 1);
  165. fillRect (colour, x, y + w - 1, w, 1);
  166. fillRect (colour, x, y + 1, 1, w - 1);
  167. fillRect (colour, x + w - 1, y + 1, 1, w - 1);
  168. }
  169. void drawPlus (int colour, int x, int y, int w)
  170. {
  171. fillRect (colour, x, y + (w / 2), w, 1);
  172. fillRect (colour, x + (w / 2), y, 1, w);
  173. }
  174. void fillGradientRect (int colour, int x, int y, int w)
  175. {
  176. if (colour != 0xff000000)
  177. {
  178. int divisor = w + w - 1;
  179. for (int yy = 0; yy < w; ++yy)
  180. {
  181. for (int xx = yy; xx < w; ++xx)
  182. {
  183. int gradColour = blendARGB (colour, makeARGB (((xx + yy) * 250) / divisor, 0, 0, 0));
  184. setLED (x + xx, y + yy, gradColour);
  185. setLED (x + yy, y + xx, gradColour);
  186. }
  187. }
  188. }
  189. }
  190. // TODO: Tom M: This is massaged to work with 3x3 pads and for dots to sync
  191. // with Apple POS loop length. Rework to be more robust & flexible.
  192. void drawPizzaLED (int colour, int x, int y, int w, int progress)
  193. {
  194. --w;
  195. x += 1;
  196. int numToDo = ((8 * progress) / 255) + 1;
  197. int totalLen = w * 4;
  198. for (int i = 1; i <= numToDo; ++i)
  199. {
  200. setLED (x, y, colour);
  201. if (i < w)
  202. ++x;
  203. else if (i < (w * 2))
  204. ++y;
  205. else if (i < (w * 3))
  206. --x;
  207. else if (i < totalLen)
  208. --y;
  209. }
  210. }
  211. void drawPad (int padX, int padY, int padW,
  212. int colour, int fill, int animateProgress)
  213. {
  214. animateProgress >>= 8; // 16 bit to 8 bit
  215. int halfW = padW / 2;
  216. if (fill == 0) // Gradient fill
  217. {
  218. fillGradientRect (colour, padX, padY, padW);
  219. }
  220. else if (fill == 1) // Filled
  221. {
  222. fillRect (colour, padX, padY, padW, padW);
  223. }
  224. else if (fill == 2) // Hollow
  225. {
  226. outlineRect (colour, padX, padY, padW);
  227. }
  228. else if (fill == 3) // Hollow with plus
  229. {
  230. outlineRect (colour, padX, padY, padW);
  231. drawPlus (0xffffffff, padX, padY, padW);
  232. }
  233. else if (fill == 4) // Pulsing dot
  234. {
  235. int pulseCol = blendARGB (colour, makeARGB (animateProgress, 0, 0, 0));
  236. setLED (padX + halfW, padY + halfW, pulseCol);
  237. }
  238. else if (fill == 5) // Blinking dot
  239. {
  240. int blinkCol = animateProgress > 64 ? makeARGB (255, 0, 0, 0) : colour;
  241. setLED (padX + halfW, padY + halfW, blinkCol);
  242. }
  243. else if (fill == 6) // Pizza filled
  244. {
  245. outlineRect (blendARGB (colour, makeARGB (220, 0, 0, 0)), padX, padY, padW); // Dim outline
  246. setLED (padX + halfW, padY + halfW, colour); // Bright centre
  247. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  248. }
  249. else if (fill == 7) // Pizza hollow
  250. {
  251. outlineRect (blendARGB (colour, makeARGB (220, 0, 0, 0)), padX, padY, padW); // Dim outline
  252. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  253. return;
  254. }
  255. }
  256. void fadeHeatMap()
  257. {
  258. for (int i = 0; i < 225; ++i)
  259. {
  260. int colourOffset = 226 + i * 4;
  261. int colour = getHeapInt (colourOffset);
  262. int alpha = (colour >> 24) & 0xff;
  263. if (alpha > 0)
  264. {
  265. alpha -= getHeapByte (1126 + i);
  266. setHeapInt (colourOffset, alpha < 0 ? 0 : ((alpha << 24) | (colour & 0xffffff)));
  267. }
  268. }
  269. }
  270. void addToHeatMap (int x, int y, int colour)
  271. {
  272. if (x >= 0 && y >= 0 && x < 15 && y < 15)
  273. {
  274. int offset = 226 + 4 * (x + y * 15);
  275. colour = blendARGB (getHeapInt (offset), colour);
  276. setHeapInt (offset, colour);
  277. int decay = ((colour >> 24) & 0xff) / 14; // change divisor to change trail times
  278. offset = 1126 + (x + y * 15);
  279. setHeapByte (offset, decay > 0 ? decay : 1);
  280. }
  281. }
  282. int getHeatmapColour (int x, int y)
  283. {
  284. return getHeapInt (226 + 4 * (x + y * 15));
  285. }
  286. int isPadActive (int index)
  287. {
  288. if (getHeapInt (158) == 0) // None active
  289. return 0;
  290. ++index;
  291. return index == getHeapByte (158) ||
  292. index == getHeapByte (159) ||
  293. index == getHeapByte (160) ||
  294. index == getHeapByte (161);
  295. }
  296. void updateDimFactor()
  297. {
  298. if (getHeapInt (158) == 0)
  299. {
  300. if (--dimDelay <= 0)
  301. {
  302. dimFactor -= 12;
  303. if (dimFactor < 0)
  304. dimFactor = 0;
  305. }
  306. }
  307. else
  308. {
  309. dimFactor = 180;
  310. dimDelay = 12;
  311. }
  312. }
  313. void drawPads (int offsetX, int offsetY, int colourMapOffset)
  314. {
  315. int padsPerSide = getHeapByte (0 + colourMapOffset);
  316. if (padsPerSide < 2)
  317. return;
  318. int blockW = 15 / padsPerSide;
  319. int blockPlusGapW = blockW + (15 - padsPerSide * blockW) / (padsPerSide - 1);
  320. for (int padY = 0; padY < padsPerSide; ++padY)
  321. {
  322. for (int padX = 0; padX < padsPerSide; ++padX)
  323. {
  324. int ledX = offsetX + padX * blockPlusGapW;
  325. int ledY = offsetY + padY * blockPlusGapW;
  326. if (ledX < 15 &&
  327. ledY < 15 &&
  328. (ledX + blockW) >= 0 &&
  329. (ledY + blockW) >= 0)
  330. {
  331. int padIdx = padX + padY * padsPerSide;
  332. bool padActive = isPadActive (padIdx);
  333. int blendCol = padActive ? 255 : 0;
  334. int blendAmt = padActive ? dimFactor >> 1 : dimFactor;
  335. int colour = blendARGB (getGridColour (padIdx, colourMapOffset),
  336. makeARGB (blendAmt, blendCol, blendCol, blendCol));
  337. int fillType = getHeapByte (colourMapOffset + 52 + padIdx);
  338. int animate = getAnimationProgress (padIdx);
  339. drawPad (ledX, ledY, blockW, colour, fillType, animate);
  340. }
  341. }
  342. }
  343. }
  344. void slideAnimatePads()
  345. {
  346. int nowVisible = getHeapByte (155);
  347. if (lastVisiblePads != nowVisible)
  348. {
  349. lastVisiblePads = nowVisible;
  350. if (slideAnimationProgress <= 0)
  351. slideAnimationProgress = 15;
  352. }
  353. // If animation is complete, draw normally.
  354. if (slideAnimationProgress <= 0)
  355. {
  356. drawPads (0, 0, 78 * nowVisible);
  357. slideAnimationProgress = 0;
  358. }
  359. else
  360. {
  361. int direction = getHeapByte (156);
  362. slideAnimationProgress -= 1;
  363. int inPos = nowVisible == 0 ? 0 : 78;
  364. int outPos = nowVisible == 0 ? 78 : 0;
  365. if (direction == 0) // Up
  366. {
  367. drawPads (0, slideAnimationProgress - 16, outPos);
  368. drawPads (0, slideAnimationProgress, inPos);
  369. }
  370. else if (direction == 1) // Down
  371. {
  372. drawPads (0, 16 - slideAnimationProgress, outPos);
  373. drawPads (0, 0 - slideAnimationProgress, inPos);
  374. }
  375. else if (direction == 2) // Left
  376. {
  377. drawPads (16 - slideAnimationProgress, 0, outPos);
  378. drawPads (slideAnimationProgress, 0, inPos);
  379. }
  380. else if (direction == 3) // Right
  381. {
  382. drawPads (16 - slideAnimationProgress, 0, outPos);
  383. drawPads (0 - slideAnimationProgress, 0, inPos);
  384. }
  385. else // None
  386. {
  387. drawPads (0, 0, 78 * nowVisible);
  388. slideAnimationProgress = 0;
  389. }
  390. }
  391. }
  392. void repaint()
  393. {
  394. // showErrorOnFail, showRepaintTime, showMovingDot
  395. //enableDebug (true, true, false);
  396. // Clear LEDs to black, update dim animation
  397. fillRect (0xff000000, 0, 0, 15, 15);
  398. updateDimFactor();
  399. // Does the main painting of pads
  400. slideAnimatePads();
  401. // Overlay heatmap
  402. for (int y = 0; y < 15; ++y)
  403. for (int x = 0; x < 15; ++x)
  404. blendLED (x, y, getHeatmapColour (x, y));
  405. fadeHeatMap();
  406. }
  407. // DrumPadGridProgram::sendTouch results in this callback, giving
  408. // us more touch updates per frame and therefore smoother trails.
  409. void handleMessage (int pos, int colour, int xx)
  410. {
  411. handleMessage (pos, colour);
  412. }
  413. void handleMessage (int pos, int colour)
  414. {
  415. if ((pos >> 24) != 0x20)
  416. return;
  417. int tx = ((pos >> 16) & 0xff) - 13;
  418. int ty = ((pos >> 8) & 0xff) - 13;
  419. int tz = pos & 0xff;
  420. tz = tz > 30 ? tz : 30;
  421. int ledCentreX = tx >> 4;
  422. int ledCentreY = ty >> 4;
  423. int adjustX = (tx - (ledCentreX << 4)) >> 2;
  424. int adjustY = (ty - (ledCentreY << 4)) >> 2;
  425. for (int dy = -2; dy <= 2; ++dy)
  426. {
  427. for (int dx = -2; dx <= 2; ++dx)
  428. {
  429. int distance = dx * dx + dy * dy;
  430. int level = distance == 0 ? 255 : (distance == 1 ? 132 : (distance < 5 ? 9 : (distance == 5 ? 2 : 0)));
  431. level += (dx * adjustX);
  432. level += (dy * adjustY);
  433. level = (tz * level) >> 8;
  434. if (level > 0)
  435. addToHeatMap (ledCentreX + dx, ledCentreY + dy,
  436. makeARGB (level, colour >> 16, colour >> 8, colour));
  437. }
  438. }
  439. }
  440. )littlefoot";
  441. }
  442. juce::String DrumPadGridProgram::getLittleFootProgramPost25() const
  443. {
  444. // Uses heatmap provided in firmware (so the program's smaller)
  445. // Initialises config items introduced in firmware 2.5
  446. return R"littlefoot(
  447. #heapsize: 256
  448. int dimFactor;
  449. int dimDelay;
  450. int slideAnimationProgress;
  451. int lastVisiblePads;
  452. bool gammaCorrected;
  453. void initialise()
  454. {
  455. for (int i = 0; i < 32; ++i)
  456. setLocalConfigActiveState (i, true, true);
  457. // Enable gamma correction if supported on hardware
  458. setLocalConfig (33, 1);
  459. gammaCorrected = getLocalConfig (33) > 0;
  460. }
  461. int getGridColour (int index, int colourMapOffset)
  462. {
  463. int bit = (2 + colourMapOffset) * 8 + index * 16;
  464. return makeARGB (255,
  465. getHeapBits (bit, 5) << 3,
  466. getHeapBits (bit + 5, 6) << 2,
  467. getHeapBits (bit + 11, 5) << 3);
  468. }
  469. // Returns the current progress and also increments it for next frame
  470. int getAnimationProgress (int index)
  471. {
  472. // Only 16 animated pads supported
  473. if (index > 15)
  474. return 0;
  475. int offsetBits = 162 * 8 + index * 32;
  476. int currentProgress = getHeapBits (offsetBits, 16);
  477. int increment = getHeapBits (offsetBits + 16, 16);
  478. int nextFrame = currentProgress + increment;
  479. // Set incremented 16 bit number.
  480. setHeapByte (162 + index * 4, nextFrame & 0xff);
  481. setHeapByte (163 + index * 4, nextFrame >> 8);
  482. return currentProgress;
  483. }
  484. void outlineRect (int colour, int x, int y, int w)
  485. {
  486. fillRect (colour, x, y, w, 1);
  487. fillRect (colour, x, y + w - 1, w, 1);
  488. fillRect (colour, x, y + 1, 1, w - 1);
  489. fillRect (colour, x + w - 1, y + 1, 1, w - 1);
  490. }
  491. void drawPlus (int colour, int x, int y, int w)
  492. {
  493. fillRect (colour, x, y + (w / 2), w, 1);
  494. fillRect (colour, x + (w / 2), y, 1, w);
  495. }
  496. void fillGradientRect (int colour, int x, int y, int w)
  497. {
  498. if (colour != 0xff000000)
  499. {
  500. int divisor = w + w - 1;
  501. for (int yy = 0; yy < w; ++yy)
  502. {
  503. for (int xx = yy; xx < w; ++xx)
  504. {
  505. int gradColour = blendARGB (colour, makeARGB (((xx + yy) * 250) / divisor, 0, 0, 0));
  506. fillPixel (gradColour, x + xx, y + yy);
  507. fillPixel (gradColour, x + yy, y + xx);
  508. }
  509. }
  510. }
  511. }
  512. // TODO: Tom M: This is massaged to work with 3x3 pads and for dots to sync
  513. // with Apple POS loop length. Rework to be more robust & flexible.
  514. void drawPizzaLED (int colour, int x, int y, int w, int progress)
  515. {
  516. --w;
  517. x += 1;
  518. int numToDo = ((8 * progress) / 255) + 1;
  519. int totalLen = w * 4;
  520. for (int i = 1; i <= numToDo; ++i)
  521. {
  522. fillPixel (colour, x, y);
  523. if (i < w)
  524. ++x;
  525. else if (i < (w * 2))
  526. ++y;
  527. else if (i < (w * 3))
  528. --x;
  529. else if (i < totalLen)
  530. --y;
  531. }
  532. }
  533. void drawPad (int padX, int padY, int padW,
  534. int colour, int fill, int animateProgress)
  535. {
  536. animateProgress >>= 8; // 16 bit to 8 bit
  537. int halfW = padW / 2;
  538. if (fill == 0) // Gradient fill
  539. {
  540. fillGradientRect (colour, padX, padY, padW);
  541. }
  542. else if (fill == 1) // Filled
  543. {
  544. fillRect (colour, padX, padY, padW, padW);
  545. }
  546. else if (fill == 2) // Hollow
  547. {
  548. outlineRect (colour, padX, padY, padW);
  549. }
  550. else if (fill == 3) // Hollow with plus
  551. {
  552. outlineRect (colour, padX, padY, padW);
  553. drawPlus (0xffffffff, padX, padY, padW);
  554. }
  555. else if (fill == 4) // Pulsing dot
  556. {
  557. int pulseCol = blendARGB (colour, makeARGB (animateProgress, 0, 0, 0));
  558. fillPixel (pulseCol, padX + halfW, padY + halfW);
  559. }
  560. else if (fill == 5) // Blinking dot
  561. {
  562. int blinkCol = animateProgress > 64 ? 0xff000000 : colour;
  563. fillPixel (blinkCol, padX + halfW, padY + halfW);
  564. }
  565. else if (fill == 6) // Pizza filled
  566. {
  567. outlineRect (blendARGB (colour, 0xdc000000), padX, padY, padW); // Dim outline
  568. fillPixel (colour, padX + halfW, padY + halfW); // Bright centre
  569. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  570. }
  571. else // Pizza hollow
  572. {
  573. outlineRect (blendARGB (colour, 0xdc000000), padX, padY, padW); // Dim outline
  574. drawPizzaLED (colour, padX, padY, padW, animateProgress);
  575. }
  576. }
  577. int isPadActive (int index)
  578. {
  579. if (getHeapInt (158) == 0) // None active
  580. return 0;
  581. ++index;
  582. return index == getHeapByte (158) ||
  583. index == getHeapByte (159) ||
  584. index == getHeapByte (160) ||
  585. index == getHeapByte (161);
  586. }
  587. void updateDimFactor()
  588. {
  589. if (getHeapInt (158) == 0)
  590. {
  591. if (--dimDelay <= 0)
  592. {
  593. dimFactor -= 12;
  594. if (dimFactor < 0)
  595. dimFactor = 0;
  596. }
  597. }
  598. else
  599. {
  600. dimFactor = gammaCorrected ? 100 : 180;
  601. dimDelay = 12;
  602. }
  603. }
  604. void drawPads (int offsetX, int offsetY, int colourMapOffset)
  605. {
  606. int padsPerSide = getHeapByte (0 + colourMapOffset);
  607. if (padsPerSide < 2)
  608. return;
  609. int blockW = 15 / padsPerSide;
  610. int blockPlusGapW = blockW + (15 - padsPerSide * blockW) / (padsPerSide - 1);
  611. for (int padY = 0; padY < padsPerSide; ++padY)
  612. {
  613. for (int padX = 0; padX < padsPerSide; ++padX)
  614. {
  615. int ledX = offsetX + padX * blockPlusGapW;
  616. int ledY = offsetY + padY * blockPlusGapW;
  617. if (ledX < 15 &&
  618. ledY < 15 &&
  619. (ledX + blockW) >= 0 &&
  620. (ledY + blockW) >= 0)
  621. {
  622. int padIdx = padX + padY * padsPerSide;
  623. bool padActive = isPadActive (padIdx);
  624. int blendCol = padActive ? 255 : 0;
  625. int blendAmt = padActive ? dimFactor >> 1 : dimFactor;
  626. int colour = blendARGB (getGridColour (padIdx, colourMapOffset),
  627. makeARGB (blendAmt, blendCol, blendCol, blendCol));
  628. int fillType = getHeapByte (colourMapOffset + 52 + padIdx);
  629. int animate = getAnimationProgress (padIdx);
  630. drawPad (ledX, ledY, blockW, colour, fillType, animate);
  631. }
  632. }
  633. }
  634. }
  635. void slideAnimatePads()
  636. {
  637. int nowVisible = getHeapByte (155);
  638. if (lastVisiblePads != nowVisible)
  639. {
  640. lastVisiblePads = nowVisible;
  641. if (slideAnimationProgress <= 0)
  642. slideAnimationProgress = 15;
  643. }
  644. // If animation is complete, draw normally.
  645. if (slideAnimationProgress <= 0)
  646. {
  647. drawPads (0, 0, 78 * nowVisible);
  648. slideAnimationProgress = 0;
  649. }
  650. else
  651. {
  652. int direction = getHeapByte (156);
  653. slideAnimationProgress -= 1;
  654. int inPos = nowVisible == 0 ? 0 : 78;
  655. int outPos = nowVisible == 0 ? 78 : 0;
  656. if (direction == 0) // Up
  657. {
  658. drawPads (0, slideAnimationProgress - 16, outPos);
  659. drawPads (0, slideAnimationProgress, inPos);
  660. }
  661. else if (direction == 1) // Down
  662. {
  663. drawPads (0, 16 - slideAnimationProgress, outPos);
  664. drawPads (0, 0 - slideAnimationProgress, inPos);
  665. }
  666. else if (direction == 2) // Left
  667. {
  668. drawPads (16 - slideAnimationProgress, 0, outPos);
  669. drawPads (slideAnimationProgress, 0, inPos);
  670. }
  671. else if (direction == 3) // Right
  672. {
  673. drawPads (16 - slideAnimationProgress, 0, outPos);
  674. drawPads (0 - slideAnimationProgress, 0, inPos);
  675. }
  676. else // None
  677. {
  678. drawPads (0, 0, 78 * nowVisible);
  679. slideAnimationProgress = 0;
  680. }
  681. }
  682. }
  683. void repaint()
  684. {
  685. // showErrorOnFail, showRepaintTime, showMovingDot
  686. //enableDebug (true, true, false);
  687. // Clear LEDs to black, update dim animation
  688. fillRect (0xff000000, 0, 0, 15, 15);
  689. updateDimFactor();
  690. // Does the main painting of pads
  691. slideAnimatePads();
  692. // Overlay heatmap
  693. drawPressureMap();
  694. fadePressureMap();
  695. }
  696. // DrumPadGridProgram::sendTouch results in this callback, giving
  697. // us more touch updates per frame and therefore smoother trails.
  698. void handleMessage (int pos, int colour, int dummy)
  699. {
  700. if ((pos >> 24) != 0x20)
  701. return;
  702. int tx = (pos >> 16) & 0xff;
  703. int ty = (pos >> 8) & 0xff;
  704. int tz = pos & 0xff;
  705. addPressurePoint (colour,
  706. tx * (2.0 / (256 + 20)),
  707. ty * (2.0 / (256 + 20)),
  708. tz * (1.0 / 3.0));
  709. }
  710. )littlefoot";
  711. }
  712. } // namespace juce