/*
* oledDisplay.H
*
* Created on: 23 Mar 2018
* Author: Mike
*/
#pragma once
#define swap(x,y) { typeof(x)t = x; x=y; y=t; }
#define abs(x) ((x)>0?(x):-(x))
enum colour_t { BLACK,WHITE, INVERT };
#include "spiInterface.H"
#include "SSD13O6.h"
class oledDisplayBase
{
};
template < uint16_t width, uint16_t height, uint16_t ramWidth> class oledDisplay : public oledDisplayBase
{
public:
oledDisplay();
uint8_t getRotation() { return rotation; };
int16_t getWidth() {
switch (rotation) {
case 0:
return width;
break;
case 1:
return width;
break;
case 2:
return height;
break;
case 3:
return -width;
break;
}
return 0;
}
int16_t getHeight() {
switch (rotation) {
case 0:
return height;
break;
case 1:
return height;
break;
case 2:
return width;
break;
case 3:
return -height;
break;
}
return 0;
}
// the most basic function, set a single pixel
void drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))
return;
// check rotation, move pixel around if necessary
switch (getRotation()) {
case 1:
swap(x, y);
x = width - x - 1;
break;
case 2:
x = width - x - 1;
y = height - y - 1;
break;
case 3:
swap(x, y)
;
y = height - y - 1;
break;
}
// x is which column
switch (color) {
case BLACK:
displayBuffer[x + (y / 8) * width] &= ~(1 << (y & 7));
break;
default:
case WHITE:
displayBuffer[x + (y / 8) * width] |= (1 << (y & 7));
break;
case INVERT:
displayBuffer[x + (y / 8) * width] ^= (1 << (y & 7));
break;
}
}
void display(void) {
// select entire display as window to write into
ssd1306commandSPIwrite(SSD1306_COLUMNADDR);
ssd1306commandSPIwrite(0); // Column start address (0 = reset)
ssd1306commandSPIwrite(ramWidth-1); // Column end address (127 = reset)
ssd1306commandSPIwrite(SSD1306_PAGEADDR);
ssd1306commandSPIwrite(0); // Page start address (0 = reset)
ssd1306commandSPIwrite((height == 64) ? 7 : 3); // Page end address
int row;
int col = ramWidth == 132 ? 2 : 0;
for (row = 0; row < height / 8; row++) {
// set the cursor to
ssd1306commandSPIwrite(0xB0 + row); //set page address
ssd1306commandSPIwrite(col & 0xf); //set lower column address
ssd1306commandSPIwrite(0x10 | (col >> 4)); //set higher column address
ssd1306SendDisplay(
(uint8_t *) (&displayBuffer[0]) + row * width,
width);
}
}
// clear everything
void clearDisplay(void) {
memset(&displayBuffer, 0, (width * height / 8));
}
private:
uint8_t displayBuffer[width * height
/ 8];
uint8_t rotation;
};
class ssd1106display : public oledDisplay <128,64,128>
{
};
class ssd1306display : public oledDisplay <128,64,132>
{
};