ph1p / ikea-led-obegraensad

ESP32/Arduino hack for the ikea OBEGRÄNSAD led wall lamp
MIT License
579 stars 78 forks source link

Simpler rotations #2

Open atesgoral opened 1 year ago

atesgoral commented 1 year ago

While reading through your work (THANK YOU! ❤️ ) to understand the scan pattern, I noticed that there could be some room for making things easier to maintain and bringing support for flipping.

Instead of mutating the position array, you could do a fast transformation matrix multiplication during rendering. Bonus: the same transformation matrix can also be used for horizontal/vertical flipping.

Here's how I use a transformation matrix in my FREKVENS hack: https://github.com/atesgoral/node-omega-frekvens/blob/master/lib/frekvens/src/OmegaDriver.cpp#L51-L58

        char colT = (colCx2
          + colNx2 * m_transform[0]
          + rowNx2 * m_transform[1])
          >> 1;
        char rowT = (rowCx2
          + colNx2 * m_transform[2]
          + rowNx2 * m_transform[3])
          >> 1;

And here's how that transformation matrix is set up, based on rotation and flip:

https://github.com/atesgoral/node-omega-frekvens/blob/master/index.js#L181-L205

  let transform = Int8Array.from([
    1, 0,
    0, 1
  ]);

  if (process.env.ROTATE) {
    const turns = parseInt(process.env.ROTATE);
    const theta = Math.PI / 2 * turns;
    const sin = Math.round(Math.sin(theta));
    const cos = Math.round(Math.cos(theta));

    transform = Int8Array.from([
      cos, -sin,
      sin, cos
    ]);
  }

  switch (process.env.FLIP) {
    case 'H':
      transform[0] = -transform[0];
      break;
    case 'V':
      transform[3] = -transform[3];
      break;
  }