Rev 8 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 8 | Rev 11 | ||
|---|---|---|---|
| Line 46... | Line 46... | ||
| 46 | uint8_t const SSD1306_CHARGEPUMP = 0x8D; |
46 | uint8_t const SSD1306_CHARGEPUMP = 0x8D; |
| 47 | 47 | ||
| 48 | uint8_t const SSD1306_EXTERNALVCC = 0x1; |
48 | uint8_t const SSD1306_EXTERNALVCC = 0x1; |
| 49 | uint8_t const SSD1306_SWITCHCAPVCC = 0x2; |
49 | uint8_t const SSD1306_SWITCHCAPVCC = 0x2; |
| 50 | 50 | ||
| 51 | // Scrolling #defines |
51 | // Scrolling #defines |
| 52 | uint8_t const SSD1306_ACTIVATE_SCROLL = 0x2F; |
52 | uint8_t const SSD1306_ACTIVATE_SCROLL = 0x2F; |
| 53 | uint8_t const SSD1306_DEACTIVATE_SCROLL = 0x2E; |
53 | uint8_t const SSD1306_DEACTIVATE_SCROLL = 0x2E; |
| 54 | uint8_t const SSD1306_SET_VERTICAL_SCROLL_AREA = 0xA3; |
54 | uint8_t const SSD1306_SET_VERTICAL_SCROLL_AREA = 0xA3; |
| 55 | uint8_t const SSD1306_RIGHT_HORIZONTAL_SCROLL = 0x26; |
55 | uint8_t const SSD1306_RIGHT_HORIZONTAL_SCROLL = 0x26; |
| 56 | uint8_t const SSD1306_LEFT_HORIZONTAL_SCROLL = 0x27; |
56 | uint8_t const SSD1306_LEFT_HORIZONTAL_SCROLL = 0x27; |
| 57 | uint8_t const SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29; |
57 | uint8_t const SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29; |
| 58 | uint8_t const SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A; |
58 | uint8_t const SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A; |
| 59 | 59 | ||
| 60 | template<class T> |
60 | template <class T> |
| 61 | void |
61 | void |
| 62 | swap (T &x, T &y) |
62 | swap(T &x, T &y) |
| 63 | { |
63 | { |
| 64 | T temp = x; |
64 | T temp = x; |
| 65 | x = y; |
65 | x = y; |
| 66 | y = temp; |
66 | y = temp; |
| 67 | } |
67 | } |
| 68 | 68 | ||
| 69 | template<class T> |
69 | template <class T> |
| 70 | T |
- | |
| 71 | abs (T x) |
70 | T abs(T x) |
| 72 | { |
71 | { |
| 73 | return x < 0 ? -x : x; |
72 | return x < 0 ? -x : x; |
| 74 | } |
73 | } |
| 75 | 74 | ||
| 76 | } |
75 | } |
| 77 | // provided to allow a destructor to destroy something not deleted |
76 | // provided to allow a destructor to destroy something not deleted |
| 78 | // this is only OK because destructors shouldnt be needed |
77 | // this is only OK because destructors shouldnt be needed |
| 79 | void |
- | |
| 80 | operator delete (void *data, unsigned int f) |
78 | void operator delete(void *data, unsigned int f) |
| 81 | { |
79 | { |
| 82 | (void) data; |
80 | (void)data; |
| 83 | (void) f; |
81 | (void)f; |
| 84 | } |
82 | } |
| 85 | 83 | ||
| 86 | // provided to implement an "error handler" on pure virtual |
84 | // provided to implement an "error handler" on pure virtual |
| 87 | extern "C" void |
85 | extern "C" void |
| 88 | __cxa_pure_virtual () |
86 | __cxa_pure_virtual() |
| 89 | { |
87 | { |
| 90 | while (1) |
88 | while (1) |
| 91 | ; |
89 | ; |
| 92 | } |
90 | } |
| 93 | 91 | ||
| 94 | display_t::display_t (int const width, int const height, int const ramwidth, |
92 | display_t::display_t(int const width, int const height, int const ramwidth, |
| 95 | uint8_t *const data) : |
- | |
| 96 | m_width (width), m_height (height), m_ramwidth (ramwidth), m_cursor_x (0), m_cursor_y ( |
93 | uint8_t *const data) : m_width(width), m_height(height), m_ramwidth(ramwidth), m_cursor_x(0), m_cursor_y(0), m_rotation(0), m_colour(WHITE), m_data(data) |
| 97 | 0), m_rotation (0), m_colour (WHITE), m_data (data) |
- | |
| 98 | { |
94 | { |
| 99 | } |
95 | } |
| 100 | 96 | ||
| 101 | display_t::~display_t () |
97 | display_t::~display_t() |
| 102 | { |
98 | { |
| 103 | - | ||
| 104 | } |
99 | } |
| 105 | 100 | ||
| 106 | void display_t::reset() |
101 | void display_t::reset() |
| 107 | { |
102 | { |
| 108 | oledReset(); |
103 | oledReset(); |
| 109 | } |
104 | } |
| 110 | 105 | ||
| 111 | void |
- | |
| 112 | display_t::init () |
106 | void display_t::init() |
| 113 | { |
107 | { |
| 114 | uint8_t const vccstate = SSD1306_EXTERNALVCC; |
108 | uint8_t const vccstate = SSD1306_EXTERNALVCC; |
| 115 | 109 | ||
| 116 | - | ||
| 117 | oledSetCD (0); |
110 | oledSetCD(0); |
| 118 | 111 | ||
| 119 | // Init sequence for 128x32 or 128x64 OLED module |
112 | // Init sequence for 128x32 or 128x64 OLED module |
| 120 | oledWrite (SSD1306_DISPLAYOFF); // 0xAE |
113 | oledWrite(SSD1306_DISPLAYOFF); // 0xAE |
| 121 | oledWrite (SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 |
114 | oledWrite(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 |
| 122 | oledWrite (0x80); // the suggested ratio 0x80 |
115 | oledWrite(0x80); // the suggested ratio 0x80 |
| 123 | oledWrite (SSD1306_SETMULTIPLEX); // 0xA8 |
116 | oledWrite(SSD1306_SETMULTIPLEX); // 0xA8 |
| 124 | oledWrite (m_height - 1); |
117 | oledWrite(m_height - 1); |
| 125 | oledWrite (SSD1306_SETDISPLAYOFFSET); // 0xD3 |
118 | oledWrite(SSD1306_SETDISPLAYOFFSET); // 0xD3 |
| 126 | oledWrite (0x0); // no offset |
119 | oledWrite(0x0); // no offset |
| 127 | oledWrite (SSD1306_SETSTARTLINE | 0x0); // line #0 |
120 | oledWrite(SSD1306_SETSTARTLINE | 0x0); // line #0 |
| 128 | oledWrite (SSD1306_CHARGEPUMP); // 0x8D |
121 | oledWrite(SSD1306_CHARGEPUMP); // 0x8D |
| 129 | oledWrite (vccstate == SSD1306_EXTERNALVCC ? 0x10 : 0x14); |
122 | oledWrite(vccstate == SSD1306_EXTERNALVCC ? 0x10 : 0x14); |
| 130 | oledWrite (SSD1306_MEMORYMODE); // 0x20 |
123 | oledWrite(SSD1306_MEMORYMODE); // 0x20 |
| 131 | oledWrite (0x00); // 0x0 act like ks0108 |
124 | oledWrite(0x00); // 0x0 act like ks0108 |
| 132 | oledWrite (SSD1306_SEGREMAP | 0x1); |
125 | oledWrite(SSD1306_SEGREMAP | 0x1); |
| 133 | oledWrite (SSD1306_COMSCANDEC); |
126 | oledWrite(SSD1306_COMSCANDEC); |
| 134 | oledWrite (SSD1306_SETCOMPINS); // 0xDA |
127 | oledWrite(SSD1306_SETCOMPINS); // 0xDA |
| 135 | oledWrite (m_height == 32 ? 0x02 : 0x12); |
128 | oledWrite(m_height == 32 ? 0x02 : 0x12); |
| 136 | oledWrite (SSD1306_SETCONTRAST); // 0x81 |
129 | oledWrite(SSD1306_SETCONTRAST); // 0x81 |
| 137 | oledWrite (vccstate == SSD1306_EXTERNALVCC ? 0x9F : 0xCF); |
130 | oledWrite(vccstate == SSD1306_EXTERNALVCC ? 0x9F : 0xCF); |
| 138 | oledWrite (SSD1306_SETPRECHARGE); // 0xd9 |
131 | oledWrite(SSD1306_SETPRECHARGE); // 0xd9 |
| 139 | oledWrite (vccstate == SSD1306_EXTERNALVCC ? 0x22 : 0xF1); |
132 | oledWrite(vccstate == SSD1306_EXTERNALVCC ? 0x22 : 0xF1); |
| 140 | oledWrite (SSD1306_SETVCOMDETECT); // 0xDB |
133 | oledWrite(SSD1306_SETVCOMDETECT); // 0xDB |
| 141 | oledWrite (0x40); |
134 | oledWrite(0x40); |
| 142 | oledWrite (SSD1306_DISPLAYALLON_RESUME); // 0xA4 |
135 | oledWrite(SSD1306_DISPLAYALLON_RESUME); // 0xA4 |
| 143 | oledWrite (SSD1306_NORMALDISPLAY); // 0xA6 |
136 | oledWrite(SSD1306_NORMALDISPLAY); // 0xA6 |
| 144 | - | ||
| 145 | oledWrite (SSD1306_DISPLAYON); //--turn on oled panel |
- | |
| 146 | 137 | ||
| 147 | clearDisplay (); |
138 | oledWrite(SSD1306_DISPLAYON); //--turn on oled panel |
| 148 | 139 | ||
| - | 140 | clearDisplay(); |
|
| 149 | } |
141 | } |
| 150 | 142 | ||
| 151 | uint8_t |
143 | uint8_t |
| 152 | display_t::getRotation (void) |
144 | display_t::getRotation(void) |
| 153 | { |
145 | { |
| 154 | return m_rotation; |
146 | return m_rotation; |
| 155 | } |
147 | } |
| 156 | 148 | ||
| 157 | int16_t |
149 | int16_t |
| 158 | display_t::width (void) |
150 | display_t::width(void) |
| 159 | { |
151 | { |
| 160 | switch (m_rotation) |
152 | switch (m_rotation) |
| 161 | { |
153 | { |
| 162 | case 0: |
154 | case 0: |
| 163 | return m_width; |
155 | return m_width; |
| 164 | break; |
156 | break; |
| 165 | case 1: |
157 | case 1: |
| 166 | return m_width; |
158 | return m_width; |
| 167 | break; |
159 | break; |
| 168 | case 2: |
160 | case 2: |
| 169 | return m_height; |
161 | return m_height; |
| 170 | break; |
162 | break; |
| 171 | case 3: |
163 | case 3: |
| 172 | return -m_width; |
164 | return -m_width; |
| 173 | break; |
165 | break; |
| 174 | } |
166 | } |
| 175 | return 0; |
167 | return 0; |
| 176 | } |
168 | } |
| 177 | 169 | ||
| 178 | int16_t |
170 | int16_t |
| 179 | display_t::height (void) |
171 | display_t::height(void) |
| 180 | { |
172 | { |
| 181 | switch (m_rotation) |
173 | switch (m_rotation) |
| 182 | { |
174 | { |
| 183 | case 0: |
175 | case 0: |
| 184 | return m_height; |
176 | return m_height; |
| 185 | break; |
177 | break; |
| 186 | case 1: |
178 | case 1: |
| 187 | return m_height; |
179 | return m_height; |
| 188 | break; |
180 | break; |
| 189 | case 2: |
181 | case 2: |
| 190 | return m_width; |
182 | return m_width; |
| 191 | break; |
183 | break; |
| 192 | case 3: |
184 | case 3: |
| 193 | return -m_height; |
185 | return -m_height; |
| 194 | break; |
186 | break; |
| 195 | } |
187 | } |
| 196 | return 0; |
188 | return 0; |
| 197 | } |
189 | } |
| 198 | 190 | ||
| 199 | // the most basic function, set a single pixel |
191 | // the most basic function, set a single pixel |
| 200 | void |
- | |
| 201 | display_t::drawPixel (int16_t x, int16_t y, bool pixel) |
192 | void display_t::drawPixel(int16_t x, int16_t y, bool pixel) |
| 202 | { |
193 | { |
| 203 | if ((x < 0) || (x >= m_width) || (y < 0) || (y >= m_height)) |
194 | if ((x < 0) || (x >= m_width) || (y < 0) || (y >= m_height)) |
| 204 | return; |
195 | return; |
| 205 | 196 | ||
| 206 | // check rotation, move pixel around if necessary |
197 | // check rotation, move pixel around if necessary |
| 207 | switch (m_rotation) |
198 | switch (m_rotation) |
| 208 | { |
199 | { |
| 209 | case 1: |
200 | case 1: |
| 210 | swap (x, y); |
201 | swap(x, y); |
| 211 | x = m_width - x - 1; |
202 | x = m_width - x - 1; |
| 212 | break; |
203 | break; |
| 213 | case 2: |
204 | case 2: |
| 214 | x = m_width - x - 1; |
205 | x = m_width - x - 1; |
| 215 | y = m_height - y - 1; |
206 | y = m_height - y - 1; |
| 216 | break; |
207 | break; |
| 217 | case 3: |
208 | case 3: |
| 218 | swap (x, y); |
209 | swap(x, y); |
| 219 | y = m_height - y - 1; |
210 | y = m_height - y - 1; |
| 220 | break; |
211 | break; |
| 221 | } |
212 | } |
| 222 | 213 | ||
| 223 | // x is which column |
214 | // x is which column |
| 224 | // BLACK, and 0, invert 0 |
215 | // BLACK, and 0, invert 0 |
| 225 | // WHITE, and 0, invert 1 |
216 | // WHITE, and 0, invert 1 |
| 226 | // OVERLAY, and 1 (preserve) , invert 0/ |
217 | // OVERLAY, and 1 (preserve) , invert 0/ |
| 227 | // INVERT, and 1, (preserve) , invert 1 |
218 | // INVERT, and 1, (preserve) , invert 1 |
| 228 | 219 | ||
| 229 | switch (m_colour) |
220 | switch (m_colour) |
| 230 | { |
221 | { |
| 231 | case BLACK: |
222 | case BLACK: |
| 232 | case WHITE: |
223 | case WHITE: |
| 233 | m_data[x + (y / 8) * m_width] &= ~(1 << (y & 7)); |
224 | m_data[x + (y / 8) * m_width] &= ~(1 << (y & 7)); |
| 234 | break; |
225 | break; |
| 235 | default: |
226 | default: |
| 236 | break; |
227 | break; |
| 237 | } |
228 | } |
| 238 | uint8_t pixData = 0; |
229 | uint8_t pixData = 0; |
| 239 | switch (m_colour) |
230 | switch (m_colour) |
| 240 | { |
231 | { |
| 241 | case BLACK: |
232 | case BLACK: |
| 242 | pixData = pixel ? 0 : 1; |
233 | pixData = pixel ? 0 : 1; |
| 243 | break; |
234 | break; |
| 244 | case WHITE: |
235 | case WHITE: |
| 245 | case OVERLAY: |
236 | case OVERLAY: |
| 246 | case INVERT: |
237 | case INVERT: |
| 247 | pixData = pixel ? 1 : 0; |
238 | pixData = pixel ? 1 : 0; |
| 248 | break; |
239 | break; |
| 249 | } |
240 | } |
| 250 | 241 | ||
| 251 | m_data[x + (y / 8) * m_width] ^= (pixData << (y & 7)); |
242 | m_data[x + (y / 8) * m_width] ^= (pixData << (y & 7)); |
| 252 | - | ||
| 253 | } |
243 | } |
| 254 | 244 | ||
| 255 | void |
- | |
| 256 | display_t::invertDisplay (uint8_t i) |
245 | void display_t::invertDisplay(uint8_t i) |
| 257 | { |
246 | { |
| 258 | oledSetCD (0); |
247 | oledSetCD(0); |
| 259 | oledWrite (i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY); |
248 | oledWrite(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY); |
| 260 | } |
249 | } |
| 261 | 250 | ||
| 262 | // startscrollright |
251 | // startscrollright |
| 263 | // Activate a right handed scroll for rows start through stop |
252 | // Activate a right handed scroll for rows start through stop |
| 264 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
253 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
| 265 | // display.scrollright(0x00, 0x0F) |
254 | // display.scrollright(0x00, 0x0F) |
| 266 | void |
- | |
| 267 | display_t::startscrollright (uint8_t start, uint8_t stop) |
255 | void display_t::startscrollright(uint8_t start, uint8_t stop) |
| 268 | { |
256 | { |
| 269 | oledSetCD (0); |
257 | oledSetCD(0); |
| 270 | oledWrite (SSD1306_RIGHT_HORIZONTAL_SCROLL); |
258 | oledWrite(SSD1306_RIGHT_HORIZONTAL_SCROLL); |
| 271 | oledWrite (0X00); |
259 | oledWrite(0X00); |
| 272 | oledWrite (start); |
260 | oledWrite(start); |
| 273 | oledWrite (0X00); |
261 | oledWrite(0X00); |
| 274 | oledWrite (stop); |
262 | oledWrite(stop); |
| 275 | oledWrite (0X00); |
263 | oledWrite(0X00); |
| 276 | oledWrite (0XFF); |
264 | oledWrite(0XFF); |
| 277 | oledWrite (SSD1306_ACTIVATE_SCROLL); |
265 | oledWrite(SSD1306_ACTIVATE_SCROLL); |
| 278 | } |
266 | } |
| 279 | 267 | ||
| 280 | // startscrollleft |
268 | // startscrollleft |
| 281 | // Activate a right handed scroll for rows start through stop |
269 | // Activate a right handed scroll for rows start through stop |
| 282 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
270 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
| 283 | // display.scrollright(0x00, 0x0F) |
271 | // display.scrollright(0x00, 0x0F) |
| 284 | void |
- | |
| 285 | display_t::startscrollleft (uint8_t start, uint8_t stop) |
272 | void display_t::startscrollleft(uint8_t start, uint8_t stop) |
| 286 | { |
273 | { |
| 287 | oledSetCD (0); |
274 | oledSetCD(0); |
| 288 | oledWrite (SSD1306_LEFT_HORIZONTAL_SCROLL); |
275 | oledWrite(SSD1306_LEFT_HORIZONTAL_SCROLL); |
| 289 | oledWrite (0X00); |
276 | oledWrite(0X00); |
| 290 | oledWrite (start); |
277 | oledWrite(start); |
| 291 | oledWrite (0X00); |
278 | oledWrite(0X00); |
| 292 | oledWrite (stop); |
279 | oledWrite(stop); |
| 293 | oledWrite (0X00); |
280 | oledWrite(0X00); |
| 294 | oledWrite (0XFF); |
281 | oledWrite(0XFF); |
| 295 | oledWrite (SSD1306_ACTIVATE_SCROLL); |
282 | oledWrite(SSD1306_ACTIVATE_SCROLL); |
| 296 | } |
283 | } |
| 297 | 284 | ||
| 298 | // startscrolldiagright |
285 | // startscrolldiagright |
| 299 | // Activate a diagonal scroll for rows start through stop |
286 | // Activate a diagonal scroll for rows start through stop |
| 300 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
287 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
| 301 | // display.scrollright(0x00, 0x0F) |
288 | // display.scrollright(0x00, 0x0F) |
| 302 | void |
- | |
| 303 | display_t::startscrolldiagright (uint8_t start, uint8_t stop) |
289 | void display_t::startscrolldiagright(uint8_t start, uint8_t stop) |
| 304 | { |
290 | { |
| 305 | oledSetCD (0); |
291 | oledSetCD(0); |
| 306 | oledWrite (SSD1306_SET_VERTICAL_SCROLL_AREA); |
292 | oledWrite(SSD1306_SET_VERTICAL_SCROLL_AREA); |
| 307 | oledWrite (0X00); |
293 | oledWrite(0X00); |
| 308 | oledWrite (m_height); |
294 | oledWrite(m_height); |
| 309 | oledWrite (SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL); |
295 | oledWrite(SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL); |
| 310 | oledWrite (0X00); |
296 | oledWrite(0X00); |
| 311 | oledWrite (start); |
297 | oledWrite(start); |
| 312 | oledWrite (0X00); |
298 | oledWrite(0X00); |
| 313 | oledWrite (stop); |
299 | oledWrite(stop); |
| 314 | oledWrite (0X01); |
300 | oledWrite(0X01); |
| 315 | oledWrite (SSD1306_ACTIVATE_SCROLL); |
301 | oledWrite(SSD1306_ACTIVATE_SCROLL); |
| 316 | } |
302 | } |
| 317 | 303 | ||
| 318 | // startscrolldiagleft |
304 | // startscrolldiagleft |
| 319 | // Activate a diagonal scroll for rows start through stop |
305 | // Activate a diagonal scroll for rows start through stop |
| 320 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
306 | // Hint, the display is 16 rows tall. To scroll the whole display, run: |
| 321 | // display.scrollright(0x00, 0x0F) |
307 | // display.scrollright(0x00, 0x0F) |
| 322 | void |
- | |
| 323 | display_t::startscrolldiagleft (uint8_t start, uint8_t stop) |
308 | void display_t::startscrolldiagleft(uint8_t start, uint8_t stop) |
| 324 | { |
309 | { |
| 325 | oledSetCD (0); |
310 | oledSetCD(0); |
| 326 | oledWrite (SSD1306_SET_VERTICAL_SCROLL_AREA); |
311 | oledWrite(SSD1306_SET_VERTICAL_SCROLL_AREA); |
| 327 | oledWrite (0X00); |
312 | oledWrite(0X00); |
| 328 | oledWrite (m_height); |
313 | oledWrite(m_height); |
| 329 | oledWrite (SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL); |
314 | oledWrite(SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL); |
| 330 | oledWrite (0X00); |
315 | oledWrite(0X00); |
| 331 | oledWrite (start); |
316 | oledWrite(start); |
| 332 | oledWrite (0X00); |
317 | oledWrite(0X00); |
| 333 | oledWrite (stop); |
318 | oledWrite(stop); |
| 334 | oledWrite (0X01); |
319 | oledWrite(0X01); |
| 335 | oledWrite (SSD1306_ACTIVATE_SCROLL); |
320 | oledWrite(SSD1306_ACTIVATE_SCROLL); |
| 336 | } |
321 | } |
| 337 | 322 | ||
| 338 | void |
- | |
| 339 | display_t::stopscroll (void) |
323 | void display_t::stopscroll(void) |
| 340 | { |
324 | { |
| 341 | oledSetCD (0); |
325 | oledSetCD(0); |
| 342 | oledWrite (SSD1306_DEACTIVATE_SCROLL); |
326 | oledWrite(SSD1306_DEACTIVATE_SCROLL); |
| 343 | } |
327 | } |
| 344 | 328 | ||
| 345 | // Dim the display |
329 | // Dim the display |
| 346 | // dim = true: display is dimmed |
330 | // dim = true: display is dimmed |
| 347 | // dim = false: display is normal |
331 | // dim = false: display is normal |
| 348 | void |
- | |
| 349 | display_t::dim (uint8_t contrast) |
332 | void display_t::dim(uint8_t contrast) |
| 350 | { |
333 | { |
| 351 | 334 | ||
| 352 | // the range of contrast to too small to be really useful |
335 | // the range of contrast to too small to be really useful |
| 353 | // it is useful to dim the display |
336 | // it is useful to dim the display |
| 354 | 337 | ||
| 355 | oledSetCD (0); |
338 | oledSetCD(0); |
| 356 | oledWrite (SSD1306_SETCONTRAST); |
339 | oledWrite(SSD1306_SETCONTRAST); |
| 357 | oledWrite (contrast); |
340 | oledWrite(contrast); |
| 358 | } |
341 | } |
| 359 | 342 | ||
| 360 | void |
- | |
| 361 | display_t::display (void) |
343 | void display_t::display(void) |
| 362 | { |
344 | { |
| 363 | oledSetCD (0); |
345 | oledSetCD(0); |
| 364 | // select entire display as window to write into |
346 | // select entire display as window to write into |
| 365 | oledWrite (SSD1306_COLUMNADDR); |
347 | oledWrite(SSD1306_COLUMNADDR); |
| 366 | oledWrite (0); // Column start address (0 = reset) |
348 | oledWrite(0); // Column start address (0 = reset) |
| 367 | oledWrite (m_ramwidth - 1); // Column end address (127 = reset) |
349 | oledWrite(m_ramwidth - 1); // Column end address (127 = reset) |
| 368 | 350 | ||
| 369 | oledWrite (SSD1306_PAGEADDR); |
351 | oledWrite(SSD1306_PAGEADDR); |
| 370 | oledWrite (0); // Page start address (0 = reset) |
352 | oledWrite(0); // Page start address (0 = reset) |
| 371 | oledWrite ((m_height == 64) ? 7 : 3); // Page end address |
353 | oledWrite((m_height == 64) ? 7 : 3); // Page end address |
| 372 | 354 | ||
| 373 | int row; |
355 | int row; |
| 374 | 356 | ||
| 375 | int col = m_ramwidth == 132 ? 2 : 0; |
357 | int col = m_ramwidth == 132 ? 2 : 0; |
| 376 | for (row = 0; row < m_height / 8; row++) |
358 | for (row = 0; row < m_height / 8; row++) |
| 377 | { |
359 | { |
| 378 | oledSetCD (0); |
360 | oledSetCD(0); |
| 379 | // set the cursor to |
361 | // set the cursor to |
| 380 | oledWrite (0xB0 + row); //set page address |
362 | oledWrite(0xB0 + row); // set page address |
| 381 | oledWrite (col & 0xf); //set lower column address |
363 | oledWrite(col & 0xf); // set lower column address |
| 382 | oledWrite (0x10 | (col >> 4)); //set higher column address |
364 | oledWrite(0x10 | (col >> 4)); // set higher column address |
| 383 | 365 | ||
| 384 | oledSetCD (1); |
366 | oledSetCD(1); |
| 385 | oledWrite (m_data + row * m_width, m_width); |
367 | oledWrite(m_data + row * m_width, m_width); |
| 386 | - | ||
| 387 | } |
368 | } |
| 388 | - | ||
| 389 | } |
369 | } |
| 390 | 370 | ||
| 391 | // clear everything |
371 | // clear everything |
| 392 | void |
- | |
| 393 | display_t::clearDisplay (colour_t colour) |
372 | void display_t::clearDisplay(colour_t colour) |
| 394 | { |
373 | { |
| 395 | switch (colour) |
374 | switch (colour) |
| 396 | { |
375 | { |
| 397 | case WHITE: |
376 | case WHITE: |
| 398 | case OVERLAY: |
377 | case OVERLAY: |
| 399 | memset (m_data, 255, dataSize (m_width, m_height)); |
378 | memset(m_data, 255, dataSize(m_width, m_height)); |
| 400 | break; |
379 | break; |
| 401 | case BLACK: |
380 | case BLACK: |
| 402 | memset (m_data, 0, dataSize (m_width, m_height)); |
381 | memset(m_data, 0, dataSize(m_width, m_height)); |
| 403 | break; |
382 | break; |
| 404 | case INVERT: |
383 | case INVERT: |
| 405 | for (size_t i = 0; i < dataSize (m_width, m_height); i++) |
384 | for (size_t i = 0; i < dataSize(m_width, m_height); i++) |
| 406 | m_data[i] ^= 255; |
385 | m_data[i] ^= 255; |
| 407 | break; |
386 | break; |
| 408 | } |
387 | } |
| 409 | - | ||
| 410 | } |
388 | } |
| 411 | 389 | ||
| 412 | void |
- | |
| 413 | display_t::drawRectangle (int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
390 | void display_t::drawRectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
| 414 | colour_t color) |
391 | colour_t color) |
| 415 | { |
392 | { |
| 416 | for (int16_t x = x1; x <= x2; x++) |
393 | for (int16_t x = x1; x <= x2; x++) |
| 417 | for (int16_t y = y1; y < y2; y++) |
394 | for (int16_t y = y1; y < y2; y++) |
| - | 395 | { |
|
| - | 396 | switch (color) |
|
| 418 | { |
397 | { |
| 419 | switch (color) |
- | |
| 420 | { |
- | |
| 421 | case BLACK: |
398 | case BLACK: |
| 422 | m_data[x + (y / 8) * m_width] &= ~(1 << (y & 7)); |
399 | m_data[x + (y / 8) * m_width] &= ~(1 << (y & 7)); |
| 423 | break; |
400 | break; |
| 424 | 401 | ||
| 425 | default: |
402 | default: |
| 426 | case WHITE: |
403 | case WHITE: |
| 427 | case OVERLAY: |
404 | case OVERLAY: |
| 428 | m_data[x + (y / 8) * m_width] |= (1 << (y & 7)); |
405 | m_data[x + (y / 8) * m_width] |= (1 << (y & 7)); |
| 429 | break; |
406 | break; |
| 430 | 407 | ||
| 431 | case INVERT: |
408 | case INVERT: |
| 432 | m_data[x + (y / 8) * m_width] ^= (1 << (y & 7)); |
409 | m_data[x + (y / 8) * m_width] ^= (1 << (y & 7)); |
| 433 | break; |
410 | break; |
| 434 | } |
- | |
| 435 | } |
411 | } |
| - | 412 | } |
|
| 436 | } |
413 | } |
| 437 | 414 | ||
| 438 | /* using Bresenham draw algorithm */ |
415 | /* using Bresenham draw algorithm */ |
| 439 | void |
- | |
| 440 | display_t::drawLine (int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
416 | void display_t::drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
| 441 | colour_t colour) |
417 | colour_t colour, int8_t pattern) |
| 442 | { |
418 | { |
| 443 | int16_t x, y, dx, dy, //deltas |
419 | int16_t x, y, dx, dy, // deltas |
| 444 | dx2, dy2, //scaled deltas |
420 | dx2, dy2, // scaled deltas |
| 445 | ix, iy, //increase rate on the x and y axis |
421 | ix, iy, // increase rate on the x and y axis |
| 446 | err; //the error term |
422 | err; // the error term |
| 447 | - | ||
| - | 423 | int8_t patt = pattern; // drawing pattern bit mask 1= solid 10= dots , 111000 equal dot/dash |
|
| 448 | 424 | ||
| 449 | uint16_t i; //looping variable |
425 | uint16_t i; // looping variable |
| 450 | 426 | ||
| 451 | setPixelMode(colour); |
427 | setPixelMode(colour); |
| 452 | 428 | ||
| 453 | // identify the first pixel |
429 | // identify the first pixel |
| 454 | x = x1; |
430 | x = x1; |
| Line 461... | Line 437... | ||
| 461 | // calculate direction of the vector and store in ix and iy |
437 | // calculate direction of the vector and store in ix and iy |
| 462 | if (dx >= 0) |
438 | if (dx >= 0) |
| 463 | ix = 1; |
439 | ix = 1; |
| 464 | 440 | ||
| 465 | if (dx < 0) |
441 | if (dx < 0) |
| 466 | { |
442 | { |
| 467 | ix = -1; |
443 | ix = -1; |
| 468 | dx = abs (dx); |
444 | dx = abs(dx); |
| 469 | } |
445 | } |
| 470 | 446 | ||
| 471 | if (dy >= 0) |
447 | if (dy >= 0) |
| 472 | iy = 1; |
448 | iy = 1; |
| 473 | 449 | ||
| 474 | if (dy < 0) |
450 | if (dy < 0) |
| 475 | { |
451 | { |
| 476 | iy = -1; |
452 | iy = -1; |
| 477 | dy = abs (dy); |
453 | dy = abs(dy); |
| 478 | } |
454 | } |
| 479 | 455 | ||
| 480 | // scale deltas and store in dx2 and dy2 |
456 | // scale deltas and store in dx2 and dy2 |
| 481 | dx2 = dx * 2; |
457 | dx2 = dx * 2; |
| 482 | dy2 = dy * 2; |
458 | dy2 = dy * 2; |
| 483 | 459 | ||
| 484 | // all variables are set and it's time to enter the main loop. |
460 | // all variables are set and it's time to enter the main loop. |
| - | 461 | ||
| - | 462 | if (dx > dy) // dx is the major axis |
|
| - | 463 | { |
|
| - | 464 | // initialize the error term |
|
| - | 465 | err = dy2 - dx; |
|
| 485 | 466 | ||
| 486 | if (dx > dy) // dx is the major axis |
467 | for (i = 0; i <= dx; i++) |
| 487 | { |
468 | { |
| - | 469 | // draw pattern based on using pattern as a sequential bit mask. |
|
| 488 | // initialize the error term |
470 | if (patt & 1) |
| 489 | err = dy2 - dx; |
471 | drawPixel(x, y, 1); |
| - | 472 | patt = (patt <= 1) ? pattern : patt >> 1; |
|
| 490 | 473 | ||
| 491 | for (i = 0; i <= dx; i++) |
- | |
| 492 | { |
- | |
| 493 | drawPixel (x, y, 1); |
- | |
| 494 | if (err >= 0) |
474 | if (err >= 0) |
| 495 | { |
475 | { |
| 496 | err -= dx2; |
476 | err -= dx2; |
| 497 | y += iy; |
477 | y += iy; |
| 498 | } |
478 | } |
| 499 | err += dy2; |
479 | err += dy2; |
| 500 | x += ix; |
480 | x += ix; |
| 501 | } |
- | |
| 502 | } |
481 | } |
| - | 482 | } |
|
| 503 | 483 | ||
| 504 | else // dy is the major axis |
484 | else // dy is the major axis |
| 505 | { |
485 | { |
| 506 | // initialize the error term |
486 | // initialize the error term |
| 507 | err = dx2 - dy; |
487 | err = dx2 - dy; |
| 508 | 488 | ||
| 509 | for (i = 0; i <= dy; i++) |
489 | for (i = 0; i <= dy; i++) |
| 510 | { |
490 | { |
| - | 491 | // draw pattern based on using pattern as a sequential bit mask. |
|
| - | 492 | if (patt & 1) |
|
| 511 | drawPixel (x, y, 1); |
493 | drawPixel(x, y, 1); |
| - | 494 | patt = (patt <= 1) ? pattern : patt >> 1; |
|
| 512 | if (err >= 0) |
495 | if (err >= 0) |
| 513 | { |
496 | { |
| 514 | err -= dy2; |
497 | err -= dy2; |
| 515 | x += ix; |
498 | x += ix; |
| 516 | } |
499 | } |
| 517 | err += dx2; |
500 | err += dx2; |
| 518 | y += iy; |
501 | y += iy; |
| 519 | } |
- | |
| 520 | } |
502 | } |
| - | 503 | } |
|
| 521 | } |
504 | } |
| 522 | - | ||