cant successfully change Display Orientation
Closed this issue ยท 5 comments
Hi,
I built the Freaq myself using a Nano, and a LED matrix breakout board (max7219 based).
Since I read in your source that there is an option to change orientation, I started my build without testing the diplay before mounting it on my faceplate.
I am using the pins exactly as in your source - however, the display is reversed upside down...
I tried to change the function 'void initialiseDisplay()' in 'MutantFMSynth.ino' from:
void initialiseDisplay()
{
ledDisplay.initialise();
ledDisplay.setOrientation(LEDMATRIX_ORIENTATION_0);
...
to:
void initialiseDisplay()
{
ledDisplay.initialise();
ledDisplay.setOrientation(LEDMATRIX_ORIENTATION_180);
...
but no change.
I also tried changing the setOrientation line in 'void LedMatrix::initialise()' from 'setOrientation(LEDMATRIX_ORIENTATION_0);'
to 'setOrientation(LEDMATRIX_ORIENTATION_180);'
also not change.
I tried with both set to 180 and with only one of each, but no matter what i do, the display is always upside down...
the orientation options dont change the actual orientation at all.
any tips for me what I could do in the software to make the display change orientation?
(I would like to avoid turning the hardware, as that would cause issue with my faceplate...)
Yes sorry - that code was a stub! I never ended up completing the different orientations as I settled on a hardware layout which didn't require it.
you are correct to change the main sketch file like this:
void initialiseDisplay()
{
ledDisplay.initialise();
ledDisplay.setOrientation(LEDMATRIX_ORIENTATION_180);
...
Here are some updated functions for LedMatrix.cpp which add support for a 180degree flip. I don't really want to do 90/270! :) You only need to amend setPixel(), togglePixel() and setRowPixels(). the other functions like displayIcon reuse these functions so once they are fixed it all works (just tested now on my prototype)
/*----------------------------------------------------------------------------------------------------------
* setPixel
* was almost correct - flips the X axis but needs to check for orientation and if required, invert the Y parameter
*----------------------------------------------------------------------------------------------------------
*/
void LedMatrix::setPixel(uint8_t x, uint8_t y, uint8_t value)
{
if (orientation == LEDMATRIX_ORIENTATION_180)
{
y = (LEDMATRIX_MAXROWS - 1) - y;
}
if (x < LEDMATRIX_MAXCOLS && y < LEDMATRIX_MAXROWS)
{
if (value)
{
vRAM[y] = vRAM[y] | displayBit[x];
}
else
{
vRAM[y] = vRAM[y] & ~displayBit[x];
}
rowModified |= 1 << y;
}
}
/*----------------------------------------------------------------------------------------------------------
* togglePixel
* same issue
*----------------------------------------------------------------------------------------------------------
*/
void LedMatrix::togglePixel(uint8_t x, uint8_t y)
{
if (orientation == LEDMATRIX_ORIENTATION_180)
{
y = (LEDMATRIX_MAXROWS - 1) - y;
}
if (y < LEDMATRIX_MAXROWS && x < LEDMATRIX_MAXCOLS)
{
// if pixel is on, switch off
if (vRAM[y] & displayBit[x])
{
vRAM[y] = vRAM[y] & ~displayBit[x];
}
else
{
vRAM[y] = vRAM[y] | displayBit[x];
}
rowModified |= 1 << y;
}
}
/*----------------------------------------------------------------------------------------------------------
* setRowPixels
* add a check for orientation and if == 180 invert the y parameter as above. then rather than copying the bits as-is from the given value, reverse the bitfield
*----------------------------------------------------------------------------------------------------------
*/
void LedMatrix::setRowPixels(uint8_t y, uint8_t value)
{
if (y < LEDMATRIX_MAXROWS)
{
if (orientation == LEDMATRIX_ORIENTATION_180)
{
uint8_t reversedValue = 0;
y = (LEDMATRIX_MAXROWS - 1) - y;
for (int i = 0; i < 8; ++i)
{
reversedValue <<= 1;
reversedValue |= (value & 0x01);
value >>= 1;
}
vRAM[y] = reversedValue;
rowModified |= 1 << y;
}
else
{
vRAM[y] = value;
rowModified |= 1 << y;
}
}
}
As an aside, it was pretty inefficient (and dumb) of me to do this check at runtime, as the orientation of the matrix isn't likely to change unless you design some sort of symmetrical handheld with an accelerometer! hmmm.. that would be cool... ๐
So orientation should really be a precompiler flag and use something like:
#define LED_ORIENTATION 180
...
#if LED_ORIENTATION == 180
// 180 flip version
...
#elif LED_ORIENTATION == 0
// standard version
...
#endif
Hey - thnx for the speedy reply and the code fix!
will test it ASAP and report back :)
worked like a charm!
also had to change this line in 'LedMatrix.h' from:
void setPixel(byte x, byte y, bool value);
to:
void setPixel(byte x, byte y, byte value);
Awesome - much better than reworking the entire enclosure!! when you're finished I would love to see (& hear) your build!