1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2016 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
DrumPadGridProgram::DrumPadGridProgram (LEDGrid& lg) : Program (lg) {}
int DrumPadGridProgram::getPadIndex (float posX, float posY) const
{
posX = juce::jmin (0.99f, posX / ledGrid.block.getWidth());
posY = juce::jmin (0.99f, posY / ledGrid.block.getHeight());
const uint32 offset = ledGrid.getDataByte (visiblePads_byte) ? numColumns1_byte : numColumns0_byte;
const int numColumns = ledGrid.getDataByte (offset + numColumns0_byte);
const int numRows = ledGrid.getDataByte (offset + numRows0_byte);
return int (posX * numColumns) + int (posY * numRows) * numColumns;
}
void DrumPadGridProgram::startTouch (float startX, float startY)
{
const auto padIdx = getPadIndex (startX, startY);
for (size_t i = 0; i < 4; ++i)
{
if (ledGrid.getDataByte (touchedPads_byte + i) == 0)
{
ledGrid.setDataByte (touchedPads_byte + i, static_cast<uint8> (padIdx + 1));
break;
}
}
}
void DrumPadGridProgram::endTouch (float startX, float startY)
{
const auto padIdx = getPadIndex (startX, startY);
for (size_t i = 0; i < 4; ++i)
if (ledGrid.getDataByte (touchedPads_byte + i) == (padIdx + 1))
ledGrid.setDataByte (touchedPads_byte + i, 0);
}
void DrumPadGridProgram::sendTouch (float x, float y, float z, LEDColour colour)
{
LEDGrid::ProgramEventMessage e;
e.values[0] = 0x20000000
+ (juce::jlimit (0, 255, juce::roundToInt (x * (255.0f / ledGrid.block.getWidth()))) << 16)
+ (juce::jlimit (0, 255, juce::roundToInt (y * (255.0f / ledGrid.block.getHeight()))) << 8)
+ juce::jlimit (0, 255, juce::roundToInt (z * 255.0f));
e.values[1] = (int32) colour.getARGB();
ledGrid.sendProgramEvent (e);
}
//==============================================================================
void DrumPadGridProgram::setGridFills (int numColumns, int numRows, const juce::Array<GridFill>& fills)
{
uint8 visiblePads = ledGrid.getDataByte (visiblePads_byte);
setGridFills (numColumns, numRows, fills, visiblePads * numColumns1_byte);
}
void DrumPadGridProgram::setGridFills (int numColumns, int numRows, const juce::Array<GridFill>& fills, uint32 byteOffset)
{
jassert (numColumns * numRows == fills.size());
ledGrid.setDataByte (byteOffset + numColumns0_byte, (uint8) numColumns);
ledGrid.setDataByte (byteOffset + numRows0_byte, (uint8) numRows);
uint32 i = 0;
for (auto fill : fills)
{
if (i >= maxNumPads)
{
jassertfalse;
break;
}
const uint32 colourOffsetBytes = byteOffset + colours0_byte + i * colourSizeBytes;
const uint32 colourOffsetBits = colourOffsetBytes * 8;
ledGrid.setDataBits (colourOffsetBits, 5, fill.colour.getRed() >> 3);
ledGrid.setDataBits (colourOffsetBits + 5, 6, fill.colour.getGreen() >> 2);
ledGrid.setDataBits (colourOffsetBits + 11, 5, fill.colour.getBlue() >> 3);
ledGrid.setDataByte (byteOffset + fillTypes0_byte + i, static_cast<uint8> (fill.fillType));
++i;
}
}
void DrumPadGridProgram::triggerSlideTransition (int newNumColumns, int newNumRows,
const juce::Array<GridFill>& newFills, SlideDirection direction)
{
uint8 newVisible = ledGrid.getDataByte (visiblePads_byte) ? 0 : 1;
setGridFills (newNumColumns, newNumRows, newFills, newVisible * numColumns1_byte);
ledGrid.setDataByte (visiblePads_byte, newVisible);
ledGrid.setDataByte (slideDirection_byte, (uint8) direction);
}
//==============================================================================
void DrumPadGridProgram::setPadAnimationState (uint32 padIdx, double loopTimeSecs, double currentProgress)
{
// Only 16 animated pads are supported.
jassert (padIdx < 16);
// Compensate for bluetooth latency & led resolution, tweaked by eye for POS app.
currentProgress = std::fmod (currentProgress + 0.1, 1.0);
uint16 aniValue = uint16 (juce::roundToInt ((255 << 8) * currentProgress));
uint16 aniIncrement = loopTimeSecs > 0.0 ? uint16 (juce::roundToInt (((255 << 8) / 25.0) / loopTimeSecs)) : 0;
uint32 offset = 8 * animationTimers_byte + 32 * padIdx;
ledGrid.setDataBits (offset, 16, aniValue);
ledGrid.setDataBits (offset + 16, 16, aniIncrement);
}
void DrumPadGridProgram::suspendAnimations()
{
for (uint32 i = 0; i < 16; ++i)
{
uint32 offset = 8 * animationTimers_byte + 32 * i;
ledGrid.setDataBits (offset + 16, 16, 0);
}
// Hijack touch dimming
ledGrid.setDataByte (touchedPads_byte, 255);
}
void DrumPadGridProgram::resumeAnimations()
{
// Unhijack touch dimming
ledGrid.setDataByte (touchedPads_byte, 0);
}
//==============================================================================
uint32 DrumPadGridProgram::getHeapSize()
{
return totalDataSize;
}
juce::String DrumPadGridProgram::getLittleFootProgram()
{
return R"littlefoot(
int dimFactor;
int dimDelay;
int slideAnimationProgress;
int lastVisiblePads;
int getGridColour (int index, int colourMapOffset)
{
int bit = (2 + colourMapOffset) * 8 + index * 16;
return makeARGB (255,
getHeapBits (bit, 5) << 3,
getHeapBits (bit + 5, 6) << 2,
getHeapBits (bit + 11, 5) << 3);
}
// Returns the current progress and also increments it for next frame
int getAnimationProgress (int index)
{
// Only 16 animated pads supported
if (index > 15)
return 0;
int offsetBits = 162 * 8 + index * 32;
int currentProgress = getHeapBits (offsetBits, 16);
int increment = getHeapBits (offsetBits + 16, 16);
int nextFrame = currentProgress + increment;
// Set incremented 16 bit number.
setHeapByte (162 + index * 4, nextFrame & 0xff);
setHeapByte (163 + index * 4, nextFrame >> 8);
return currentProgress;
}
void outlineRect (int colour, int x, int y, int w)
{
fillRect (colour, x, y, w, 1);
fillRect (colour, x, y + w - 1, w, 1);
fillRect (colour, x, y + 1, 1, w - 1);
fillRect (colour, x + w - 1, y + 1, 1, w - 1);
}
void drawPlus (int colour, int x, int y, int w)
{
fillRect (colour, x, y + (w / 2), w, 1);
fillRect (colour, x + (w / 2), y, 1, w);
}
void fillGradientRect (int colour, int x, int y, int w)
{
if (colour != 0xff000000)
{
int divisor = w + w - 1;
for (int yy = 0; yy < w; ++yy)
{
for (int xx = yy; xx < w; ++xx)
{
int gradColour = blendARGB (colour, makeARGB (((xx + yy) * 250) / divisor, 0, 0, 0));
setLED (x + xx, y + yy, gradColour);
setLED (x + yy, y + xx, gradColour);
}
}
}
}
// TODO: Tom M: This is massaged to work with 3x3 pads and for dots to sync
// with Apple POS loop length. Rework to be more robust & flexible.
void drawPizzaLED (int colour, int x, int y, int w, int progress)
{
--w;
x += 1;
int numToDo = ((8 * progress) / 255) + 1;
int totalLen = w * 4;
for (int i = 1; i <= numToDo; ++i)
{
setLED (x, y, colour);
if (i < w)
++x;
else if (i < (w * 2))
++y;
else if (i < (w * 3))
--x;
else if (i < totalLen)
--y;
}
}
void drawPad (int padX, int padY, int padW,
int colour, int fill, int animateProgress)
{
animateProgress >>= 8; // 16 bit to 8 bit
int halfW = padW / 2;
if (fill == 0) // Gradient fill
{
fillGradientRect (colour, padX, padY, padW);
}
else if (fill == 1) // Filled
{
fillRect (colour, padX, padY, padW, padW);
}
else if (fill == 2) // Hollow
{
outlineRect (colour, padX, padY, padW);
}
else if (fill == 3) // Hollow with plus
{
outlineRect (colour, padX, padY, padW);
drawPlus (0xffffffff, padX, padY, padW);
}
else if (fill == 4) // Pulsing dot
{
int pulseCol = blendARGB (colour, makeARGB (animateProgress, 0, 0, 0));
setLED (padX + halfW, padY + halfW, pulseCol);
}
else if (fill == 5) // Blinking dot
{
int blinkCol = animateProgress > 64 ? makeARGB (255, 0, 0, 0) : colour;
setLED (padX + halfW, padY + halfW, blinkCol);
}
else if (fill == 6) // Pizza filled
{
outlineRect (blendARGB (colour, makeARGB (220, 0, 0, 0)), padX, padY, padW); // Dim outline
setLED (padX + halfW, padY + halfW, colour); // Bright centre
drawPizzaLED (colour, padX, padY, padW, animateProgress);
}
else if (fill == 7) // Pizza hollow
{
outlineRect (blendARGB (colour, makeARGB (220, 0, 0, 0)), padX, padY, padW); // Dim outline
drawPizzaLED (colour, padX, padY, padW, animateProgress);
return;
}
}
void fadeHeatMap()
{
for (int i = 0; i < 225; ++i)
{
int colourOffset = 226 + i * 4;
int colour = getHeapInt (colourOffset);
int alpha = (colour >> 24) & 0xff;
if (alpha > 0)
{
alpha -= getHeapByte (1126 + i);
setHeapInt (colourOffset, alpha < 0 ? 0 : ((alpha << 24) | (colour & 0xffffff)));
}
}
}
void addToHeatMap (int x, int y, int colour)
{
if (x >= 0 && y >= 0 && x < 15 && y < 15)
{
int offset = 226 + 4 * (x + y * 15);
colour = blendARGB (getHeapInt (offset), colour);
setHeapInt (offset, colour);
int decay = ((colour >> 24) & 0xff) / 14; // change divisor to change trail times
offset = 1126 + (x + y * 15);
setHeapByte (offset, decay > 0 ? decay : 1);
}
}
int getHeatmapColour (int x, int y)
{
return getHeapInt (226 + 4 * (x + y * 15));
}
int isPadActive (int index)
{
if (getHeapInt (158) == 0) // None active
return 0;
++index;
return index == getHeapByte (158) ||
index == getHeapByte (159) ||
index == getHeapByte (160) ||
index == getHeapByte (161);
}
void updateDimFactor()
{
if (getHeapInt (158) == 0)
{
if (--dimDelay <= 0)
{
dimFactor -= 12;
if (dimFactor < 0)
dimFactor = 0;
}
}
else
{
dimFactor = 180;
dimDelay = 12;
}
}
void drawPads (int offsetX, int offsetY, int colourMapOffset)
{
int padsPerSide = getHeapByte (0 + colourMapOffset);
if (padsPerSide < 2)
return;
int blockW = 15 / padsPerSide;
int blockPlusGapW = blockW + (15 - padsPerSide * blockW) / (padsPerSide - 1);
for (int padY = 0; padY < padsPerSide; ++padY)
{
for (int padX = 0; padX < padsPerSide; ++padX)
{
int ledX = offsetX + padX * blockPlusGapW;
int ledY = offsetY + padY * blockPlusGapW;
if (ledX < 15 &&
ledY < 15 &&
(ledX + blockW) >= 0 &&
(ledY + blockW) >= 0)
{
int padIdx = padX + padY * padsPerSide;
bool padActive = isPadActive (padIdx);
int blendCol = padActive ? 255 : 0;
int blendAmt = padActive ? dimFactor >> 1 : dimFactor;
int colour = blendARGB (getGridColour (padIdx, colourMapOffset),
makeARGB (blendAmt, blendCol, blendCol, blendCol));
int fillType = getHeapByte (colourMapOffset + 52 + padIdx);
int animate = getAnimationProgress (padIdx);
drawPad (ledX, ledY, blockW, colour, fillType, animate);
}
}
}
}
void slideAnimatePads()
{
int nowVisible = getHeapByte (155);
if (lastVisiblePads != nowVisible)
{
lastVisiblePads = nowVisible;
if (slideAnimationProgress <= 0)
slideAnimationProgress = 15;
}
// If animation is complete, draw normally.
if (slideAnimationProgress <= 0)
{
drawPads (0, 0, 78 * nowVisible);
slideAnimationProgress = 0;
}
else
{
int direction = getHeapByte (156);
slideAnimationProgress -= 1;
int inPos = nowVisible == 0 ? 0 : 78;
int outPos = nowVisible == 0 ? 78 : 0;
if (direction == 0) // Up
{
drawPads (0, slideAnimationProgress - 16, outPos);
drawPads (0, slideAnimationProgress, inPos);
}
else if (direction == 1) // Down
{
drawPads (0, 16 - slideAnimationProgress, outPos);
drawPads (0, 0 - slideAnimationProgress, inPos);
}
else if (direction == 2) // Left
{
drawPads (16 - slideAnimationProgress, 0, outPos);
drawPads (slideAnimationProgress, 0, inPos);
}
else if (direction == 3) // Right
{
drawPads (16 - slideAnimationProgress, 0, outPos);
drawPads (0 - slideAnimationProgress, 0, inPos);
}
else // None
{
drawPads (0, 0, 78 * nowVisible);
slideAnimationProgress = 0;
}
}
}
void repaint()
{
// showErrorOnFail, showRepaintTime, showMovingDot
//enableDebug (true, true, false);
// Clear LEDs to black, update dim animation
fillRect (0xff000000, 0, 0, 15, 15);
updateDimFactor();
// Does the main painting of pads
slideAnimatePads();
// Overlay heatmap
for (int y = 0; y < 15; ++y)
for (int x = 0; x < 15; ++x)
blendLED (x, y, getHeatmapColour (x, y));
fadeHeatMap();
}
// DrumPadGridProgram::sendTouch results in this callback, giving
// us more touch updates per frame and therefore smoother trails.
void handleMessage (int pos, int colour)
{
if ((pos >> 24) != 0x20)
return;
int tx = ((pos >> 16) & 0xff) - 13;
int ty = ((pos >> 8) & 0xff) - 13;
int tz = pos & 0xff;
tz = tz > 30 ? tz : 30;
int ledCentreX = tx >> 4;
int ledCentreY = ty >> 4;
int adjustX = (tx - (ledCentreX << 4)) >> 2;
int adjustY = (ty - (ledCentreY << 4)) >> 2;
for (int dy = -2; dy <= 2; ++dy)
{
for (int dx = -2; dx <= 2; ++dx)
{
int distance = dx * dx + dy * dy;
int level = distance == 0 ? 255 : (distance == 1 ? 132 : (distance < 5 ? 9 : (distance == 5 ? 2 : 0)));
level += (dx * adjustX);
level += (dy * adjustY);
level = (tz * level) >> 8;
if (level > 0)
addToHeatMap (ledCentreX + dx, ledCentreY + dy,
makeARGB (level, colour >> 16, colour >> 8, colour));
}
}
}
)littlefoot";
}
|