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.

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