Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6 | mjames | 1 | /* |
2 | * oledDisplay.H |
||
3 | * |
||
4 | * Created on: 23 Mar 2018 |
||
5 | * Author: Mike |
||
6 | */ |
||
7 | #pragma once |
||
8 | |||
9 | #define swap(x,y) { typeof(x)t = x; x=y; y=t; } |
||
10 | #define abs(x) ((x)>0?(x):-(x)) |
||
11 | |||
12 | enum colour_t { BLACK,WHITE, INVERT }; |
||
13 | |||
14 | #include "spiInterface.H" |
||
15 | #include "SSD13O6.h" |
||
16 | class oledDisplayBase |
||
17 | { |
||
18 | |||
19 | }; |
||
20 | |||
21 | template < uint16_t width, uint16_t height, uint16_t ramWidth> class oledDisplay : public oledDisplayBase |
||
22 | { |
||
23 | public: |
||
24 | oledDisplay(); |
||
25 | |||
26 | uint8_t getRotation() { return rotation; }; |
||
27 | |||
28 | int16_t getWidth() { |
||
29 | switch (rotation) { |
||
30 | case 0: |
||
31 | return width; |
||
32 | break; |
||
33 | case 1: |
||
34 | return width; |
||
35 | break; |
||
36 | case 2: |
||
37 | return height; |
||
38 | break; |
||
39 | case 3: |
||
40 | return -width; |
||
41 | break; |
||
42 | } |
||
43 | return 0; |
||
44 | } |
||
45 | |||
46 | int16_t getHeight() { |
||
47 | switch (rotation) { |
||
48 | case 0: |
||
49 | return height; |
||
50 | break; |
||
51 | case 1: |
||
52 | return height; |
||
53 | break; |
||
54 | case 2: |
||
55 | return width; |
||
56 | break; |
||
57 | case 3: |
||
58 | return -height; |
||
59 | break; |
||
60 | } |
||
61 | return 0; |
||
62 | } |
||
63 | |||
64 | |||
65 | // the most basic function, set a single pixel |
||
66 | void drawPixel(int16_t x, int16_t y, uint16_t color) { |
||
67 | if ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight())) |
||
68 | return; |
||
69 | |||
70 | // check rotation, move pixel around if necessary |
||
71 | switch (getRotation()) { |
||
72 | case 1: |
||
73 | swap(x, y); |
||
74 | x = width - x - 1; |
||
75 | break; |
||
76 | case 2: |
||
77 | x = width - x - 1; |
||
78 | y = height - y - 1; |
||
79 | break; |
||
80 | case 3: |
||
81 | swap(x, y) |
||
82 | ; |
||
83 | y = height - y - 1; |
||
84 | break; |
||
85 | } |
||
86 | |||
87 | // x is which column |
||
88 | switch (color) { |
||
89 | case BLACK: |
||
90 | displayBuffer[x + (y / 8) * width] &= ~(1 << (y & 7)); |
||
91 | break; |
||
92 | |||
93 | default: |
||
94 | case WHITE: |
||
95 | displayBuffer[x + (y / 8) * width] |= (1 << (y & 7)); |
||
96 | break; |
||
97 | |||
98 | case INVERT: |
||
99 | displayBuffer[x + (y / 8) * width] ^= (1 << (y & 7)); |
||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | |||
105 | void display(void) { |
||
106 | |||
107 | |||
108 | // select entire display as window to write into |
||
109 | ssd1306commandSPIwrite(SSD1306_COLUMNADDR); |
||
110 | ssd1306commandSPIwrite(0); // Column start address (0 = reset) |
||
111 | ssd1306commandSPIwrite(ramWidth-1); // Column end address (127 = reset) |
||
112 | |||
113 | ssd1306commandSPIwrite(SSD1306_PAGEADDR); |
||
114 | ssd1306commandSPIwrite(0); // Page start address (0 = reset) |
||
115 | ssd1306commandSPIwrite((height == 64) ? 7 : 3); // Page end address |
||
116 | |||
117 | int row; |
||
118 | |||
119 | int col = ramWidth == 132 ? 2 : 0; |
||
120 | for (row = 0; row < height / 8; row++) { |
||
121 | // set the cursor to |
||
122 | ssd1306commandSPIwrite(0xB0 + row); //set page address |
||
123 | ssd1306commandSPIwrite(col & 0xf); //set lower column address |
||
124 | ssd1306commandSPIwrite(0x10 | (col >> 4)); //set higher column address |
||
125 | |||
126 | ssd1306SendDisplay( |
||
127 | (uint8_t *) (&displayBuffer[0]) + row * width, |
||
128 | width); |
||
129 | |||
130 | } |
||
131 | |||
132 | } |
||
133 | |||
134 | // clear everything |
||
135 | void clearDisplay(void) { |
||
136 | memset(&displayBuffer, 0, (width * height / 8)); |
||
137 | } |
||
138 | |||
139 | |||
140 | private: |
||
141 | |||
142 | uint8_t displayBuffer[width * height |
||
143 | / 8]; |
||
144 | |||
145 | uint8_t rotation; |
||
146 | }; |
||
147 | |||
148 | |||
149 | class ssd1106display : public oledDisplay <128,64,128> |
||
150 | { |
||
151 | |||
152 | }; |
||
153 | |||
154 | class ssd1306display : public oledDisplay <128,64,132> |
||
155 | { |
||
156 | |||
157 | |||
158 | }; |