Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joshgoebel/d3c42de04f3f61806acdd46a6c5e6f2a to your computer and use it in GitHub Desktop.
Save joshgoebel/d3c42de04f3f61806acdd46a6c5e6f2a to your computer and use it in GitHub Desktop.
// should really use various NOPs here instead of while loop
#define SPOUT(x) SPDR = x; \
while (!(SPSR & _BV(SPIF))) { }
// should really use various NOPs here instead of while loop
#define SPOUT2(x) SPDR = x; \
while (!(SPSR & _BV(SPIF))) { }
// should really use various NOPs here instead of while loop
#define SPOUT3(x) SPDR = x; \
while (!(SPSR & _BV(SPIF))) { }
// For reference, this is the "closed loop" C++ version of paintScreen()
// used prior to the above version.
void Arduboy2Core::paintScreen(uint8_t image[], bool clear)
{
if (clear)
{
blank();
return;
}
uint8_t *buffr;
uint8_t x, y, data;
sendLCDCommand(0x2B); // Row Address Setting (height)
sendLCDData(0x00); // COM0 -> COM160
sendLCDData(0x00); //
sendLCDData(0x00); //
sendLCDData(HEIGHT-1); //HEIGHT-1
for (x=0; x<WIDTH; x++) {
// start at x column of page 0
buffr = &image + x;
sendLCDCommand(0x2A); // Column Address Setting
sendLCDData(0x00);
sendLCDData(x);
sendLCDData(0x00);
sendLCDData(x);
sendLCDCommand(0x2C); // write memory
// iteratate over 8 vertical pages of SSD1306 hardware vertially
for (y=8; y>0; y--) {
data = *buffr;
// 3 pixels horizontally, 2 pixels vertically
// PURPOSSELY unrolling this loop
if (data & 0b00000001) { SPOUT(0xFF); SPOUT2(0xFF); // #1
} else { SPOUT(0); SPOUT2(0); }
if (data & 0b00000010) { SPOUT(0xFF); SPOUT2(0xFF); // #2
} else { SPOUT(0); SPOUT2(0); }
if (data & 0b00000100) { SPOUT(0xFF); SPOUT2(0xFF); // #3
} else { SPOUT(0); SPOUT2(0); }
if (data & 0b00001000) { SPOUT(0xFF); SPOUT2(0xFF); // #4
} else { SPOUT(0); SPOUT2(0); }
if (data & 0b00010000) { SPOUT(0xFF); SPOUT2(0xFF); // #5
} else { SPOUT(0); SPOUT2(0); }
if (data & 0b00100000) { SPOUT(0xFF); SPOUT2(0xFF); // #6
} else { SPOUT(0); SPOUT2(0); }
if (data & 0b01000000) { SPOUT(0xFF); SPOUT2(0xFF); // #7
} else { SPOUT(0); SPOUT2(0); }
if (data & 0b10000000) { SPOUT(0xFF); SPOUT3(0xFF); // #8
} else { SPOUT(0); SPOUT3(0); }
buffr += 128; // skip to next page
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment