libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
oled_ssd130x.h
Go to the documentation of this file.
1#pragma once
2#ifndef SA_OLED_SSD130X_H
3#define SA_OLED_SSD130X_H
5#include "per/i2c.h"
6#include "per/spi.h"
7#include "per/gpio.h"
8#include "sys/system.h"
9
10namespace daisy
11{
16{
17 public:
37 void Init(const Config& config)
38 {
39 i2c_address_ = config.i2c_address;
40 i2c_.Init(config.i2c_config);
41 };
43 {
44 uint8_t buf[2] = {0X00, cmd};
45 i2c_.TransmitBlocking(i2c_address_, buf, 2, 1000);
46 };
47
48 void SendData(uint8_t* buff, size_t size)
49 {
50 for(size_t i = 0; i < size; i++)
51 {
52 uint8_t buf[2] = {0X40, buff[i]};
53 i2c_.TransmitBlocking(i2c_address_, buf, 2, 1000);
54 }
55 };
56
57 private:
59 uint8_t i2c_address_;
60};
61
66{
67 public:
103 void Init(const Config& config)
104 {
105 // Initialize both GPIO
106 pin_dc_.Init(config.pin_config.dc, GPIO::Mode::OUTPUT);
107 pin_reset_.Init(config.pin_config.reset, GPIO::Mode::OUTPUT);
108
109 // Initialize SPI
110 spi_.Init(config.spi_config);
111
112 // Reset and Configure OLED.
113 pin_reset_.Write(0);
114 System::Delay(10);
115 pin_reset_.Write(1);
116 System::Delay(10);
117 };
119 {
120 pin_dc_.Write(0);
121 spi_.BlockingTransmit(&cmd, 1);
122 };
123
124 void SendData(uint8_t* buff, size_t size)
125 {
126 pin_dc_.Write(1);
128 };
129
130 private:
131 SpiHandle spi_;
132 GPIO pin_reset_;
133 GPIO pin_dc_;
134};
135
140{
141 public:
142 struct Config
143 {
145 {
146 // Initialize using defaults
147 Defaults();
148 }
149 struct
150 {
157 void Defaults()
158 {
159 pin_config.sclk_delay = 0; // fast as possible?!
160 // SPI peripheral config
161 pin_config.sclk = Pin(PORTD, 3);
162 pin_config.mosi = Pin(PORTC, 3);
163 // SSD130x control pin config
164 pin_config.dc = Pin(PORTC, 11); //D2
165 pin_config.reset = Pin(PORTC, 10); //D3
166 }
167 };
168 void Init(const Config& config)
169 {
170 // Initialize both GPIO
171 pin_sclk_.Init(config.pin_config.sclk, GPIO::Mode::OUTPUT);
172 pin_sclk_.Write(1); //ClockPolarity::LOW
173 clk_delay = config.pin_config.sclk_delay;
174 pin_mosi_.Init(config.pin_config.mosi, GPIO::Mode::OUTPUT);
175 pin_mosi_.Write(0);
176
177 pin_dc_.Init(config.pin_config.dc, GPIO::Mode::OUTPUT);
178 pin_reset_.Init(config.pin_config.reset, GPIO::Mode::OUTPUT);
179
180 // Reset and Configure OLED.
181 pin_reset_.Write(0);
182 System::Delay(10);
183 pin_reset_.Write(1);
184 System::Delay(10);
185 };
187 {
188 pin_dc_.Write(0);
189 SoftSpiTransmit(cmd);
190 };
191
192 void SendData(uint8_t* buff, size_t size)
193 {
194 pin_dc_.Write(1);
195 for(size_t i = 0; i < size; i++)
196 SoftSpiTransmit(buff[i]);
197 };
198
199 private:
200 void SoftSpiTransmit(uint8_t val)
201 {
202 // bit flip
203 val = ((val & 0x01) << 7) | ((val & 0x02) << 5) | ((val & 0x04) << 3)
204 | ((val & 0x08) << 1) | ((val & 0x10) >> 1) | ((val & 0x20) >> 3)
205 | ((val & 0x40) >> 5) | ((val & 0x80) >> 7);
206
207 for(uint8_t bit = 0u; bit < 8u; bit++)
208 {
209 pin_mosi_.Write((val & (1 << bit)) ? 1 : 0);
210
211 System::DelayTicks(clk_delay);
212
213 pin_sclk_.Toggle();
214
215 System::DelayTicks(clk_delay);
216
217 pin_sclk_.Toggle();
218 }
219 }
220
221 uint32_t clk_delay;
222 GPIO pin_sclk_;
223 GPIO pin_mosi_;
224 GPIO pin_reset_;
225 GPIO pin_dc_;
226};
227
228
232template <size_t width, size_t height, typename Transport>
234{
235 public:
236 struct Config
237 {
238 typename Transport::Config transport_config;
239 };
240
242 {
243 transport_.Init(config.transport_config);
244
245 // Init routine...
246
247 // Display Off
248 transport_.SendCommand(0xaE);
249 // Dimension dependent commands...
250 switch(height)
251 {
252 case 16:
253 // Display Clock Divide Ratio
254 transport_.SendCommand(0xD5);
255 transport_.SendCommand(0x60);
256 // Multiplex Ratio
257 transport_.SendCommand(0xA8);
258 transport_.SendCommand(0x0F);
259 // COM Pins
260 transport_.SendCommand(0xDA);
261 transport_.SendCommand(0x02);
262 break;
263 case 32:
264 // Display Clock Divide Ratio
265 transport_.SendCommand(0xD5);
266 transport_.SendCommand(0x80);
267 // Multiplex Ratio
268 transport_.SendCommand(0xA8);
269 transport_.SendCommand(0x1F);
270 // COM Pins
271 transport_.SendCommand(0xDA);
272 if(width == 64)
273 {
274 transport_.SendCommand(0x12);
275 }
276 else
277 {
278 transport_.SendCommand(0x02);
279 }
280
281 break;
282 case 48:
283 // Display Clock Divide Ratio
284 transport_.SendCommand(0xD5);
285 transport_.SendCommand(0x80);
286 // Multiplex Ratio
287 transport_.SendCommand(0xA8);
288 transport_.SendCommand(0x2F);
289 // COM Pins
290 transport_.SendCommand(0xDA);
291 transport_.SendCommand(0x12);
292 break;
293 default: // 128
294 // Display Clock Divide Ratio
295 transport_.SendCommand(0xD5);
296 transport_.SendCommand(0x80);
297 // Multiplex Ratio
298 transport_.SendCommand(0xA8);
299 transport_.SendCommand(0x3F);
300 // COM Pins
301 transport_.SendCommand(0xDA);
302 transport_.SendCommand(0x12);
303 break;
304 }
305
306 // Display Offset
307 transport_.SendCommand(0xD3);
308 transport_.SendCommand(0x00);
309 // Start Line Address
310 transport_.SendCommand(0x40);
311 // Normal Display
312 transport_.SendCommand(0xA6);
313 // All On Resume
314 transport_.SendCommand(0xA4);
315 // Charge Pump
316 transport_.SendCommand(0x8D);
317 transport_.SendCommand(0x14);
318 // Set Segment Remap
319 transport_.SendCommand(0xA1);
320 // COM Output Scan Direction
321 transport_.SendCommand(0xC8);
322 // Contrast Control
323 transport_.SendCommand(0x81);
324 transport_.SendCommand(0x8F);
325 // Pre Charge
326 transport_.SendCommand(0xD9);
327 transport_.SendCommand(0x25);
328 // VCOM Detect
329 transport_.SendCommand(0xDB);
330 transport_.SendCommand(0x34);
331
332
333 // Display On
334 transport_.SendCommand(0xAF); //--turn on oled panel
335 };
336
337 size_t Width() const { return width; };
338 size_t Height() const { return height; };
339
341 {
342 if(x >= width || y >= height)
343 return;
344 if(on)
345 buffer_[x + (y / 8) * width] |= (1 << (y % 8));
346 else
347 buffer_[x + (y / 8) * width] &= ~(1 << (y % 8));
348 }
349
350 void Fill(bool on)
351 {
352 for(size_t i = 0; i < sizeof(buffer_); i++)
353 {
354 buffer_[i] = on ? 0xff : 0x00;
355 }
356 };
357
361 void Update()
362 {
363 uint8_t i;
365 switch(height)
366 {
367 case 32: high_column_addr = 0x12; break;
368
369 default: high_column_addr = 0x10; break;
370 }
371 for(i = 0; i < (height / 8); i++)
372 {
373 transport_.SendCommand(0xB0 + i);
374 transport_.SendCommand(0x00);
375 transport_.SendCommand(high_column_addr);
376 transport_.SendData(&buffer_[width * i], width);
377 }
378 };
379
380 protected:
382 uint8_t buffer_[width * height / 8];
383};
384
390
396
402
408
414
420
426
431
436
441
447
448
452template <size_t width, size_t height, typename Transport>
454{
455 public:
456 struct Config
457 {
458 typename Transport::Config transport_config;
459 };
460
462 {
463 transport_.Init(config.transport_config);
464
465 // Init routine...
468 switch(height)
469 {
470 case 64:
471 uDispayOffset = 0x60;
472 uMultiplex = 0x7F;
473 break;
474
475 case 80:
476 uDispayOffset = 0x68;
477 uMultiplex = 0x4F;
478 break;
479
480 case 128:
481 default:
482 uDispayOffset = 0x00;
483 uMultiplex = 0x7F;
484 break;
485 }
486
487 // Display Off
488 transport_.SendCommand(0xaE);
489
490 // Memory Mode
491 transport_.SendCommand(0x20);
492
493 // Normal Display
494 transport_.SendCommand(0xA6);
495
496 // Multiplex Ratio
497 transport_.SendCommand(0xA8);
498 transport_.SendCommand(uMultiplex);
499
500 // All On Resume
501 transport_.SendCommand(0xA4);
502
503 // Display Offset
504 transport_.SendCommand(0xD3);
505 transport_.SendCommand(uDispayOffset);
506
507 // Display Clock Divide Ratio
508 transport_.SendCommand(0xD5);
509 transport_.SendCommand(0x80);
510
511 // Pre Charge
512 transport_.SendCommand(0xD9);
513 transport_.SendCommand(0x22);
514
515 // Com Pins
516 transport_.SendCommand(0xDA);
517 transport_.SendCommand(0x12);
518
519 // VCOM Detect
520 transport_.SendCommand(0xDB);
521 transport_.SendCommand(0x35);
522
523 // Contrast Control
524 transport_.SendCommand(0x81);
525 transport_.SendCommand(0x80);
526
527 // Display On
528 transport_.SendCommand(0xAF);
529 };
530
531 size_t Width() const { return width; };
532 size_t Height() const { return height; };
533
535 {
536 if(x >= width || y >= height)
537 return;
538 if(on)
539 buffer_[x + (y / 8) * width] |= (1 << (y % 8));
540 else
541 buffer_[x + (y / 8) * width] &= ~(1 << (y % 8));
542 }
543
544 void Fill(bool on)
545 {
546 for(size_t i = 0; i < sizeof(buffer_); i++)
547 {
548 buffer_[i] = on ? 0xff : 0x00;
549 }
550 };
551
555 void Update()
556 {
557 uint8_t i;
559 switch(height)
560 {
561 case 32: high_column_addr = 0x12; break;
562
563 default: high_column_addr = 0x10; break;
564 }
565 for(i = 0; i < (height / 8); i++)
566 {
567 transport_.SendCommand(0xB0 + i);
568 transport_.SendCommand(0x00);
569 transport_.SendCommand(high_column_addr);
570 transport_.SendData(&buffer_[width * i], width);
571 }
572 };
573
574 private:
575 Transport transport_;
576 uint8_t buffer_[width * height / 8];
577};
578
584
590
596
602
608
614
615
616}; // namespace daisy
617
618
619#endif
General Purpose I/O control.
Definition gpio.h:22
void Write(bool state)
Changes the state of the GPIO hardware when configured as an OUTPUT.
void Init()
Initialize the GPIO using the internal Config struct.
void Toggle()
flips the current state of the GPIO. If it was HIGH, it will go LOW, and vice versa.
Definition i2c.h:26
Result Init(const Config &config)
Result TransmitBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Definition leddriver.h:33
void Init(I2CHandle i2c, const uint8_t(&addresses)[numDrivers], DmaBuffer dma_buffer_a, DmaBuffer dma_buffer_b, Pin oe_pin=Pin(PORTX, 0))
Definition leddriver.h:65
Definition oled_ssd130x.h:454
void Init(Config config)
Definition oled_ssd130x.h:461
size_t Width() const
Definition oled_ssd130x.h:531
void DrawPixel(uint_fast8_t x, uint_fast8_t y, bool on)
Definition oled_ssd130x.h:534
void Update()
Definition oled_ssd130x.h:555
void Fill(bool on)
Definition oled_ssd130x.h:544
size_t Height() const
Definition oled_ssd130x.h:532
Definition oled_ssd130x.h:140
void SendData(uint8_t *buff, size_t size)
Definition oled_ssd130x.h:192
void SendCommand(uint8_t cmd)
Definition oled_ssd130x.h:186
void Init(const Config &config)
Definition oled_ssd130x.h:168
Definition oled_ssd130x.h:66
void SendCommand(uint8_t cmd)
Definition oled_ssd130x.h:118
void SendData(uint8_t *buff, size_t size)
Definition oled_ssd130x.h:124
void Init(const Config &config)
Definition oled_ssd130x.h:103
Definition oled_ssd130x.h:234
void Update()
Definition oled_ssd130x.h:361
size_t Height() const
Definition oled_ssd130x.h:338
void Fill(bool on)
Definition oled_ssd130x.h:350
size_t Width() const
Definition oled_ssd130x.h:337
void Init(Config config)
Definition oled_ssd130x.h:241
Transport transport_
Definition oled_ssd130x.h:381
void DrawPixel(uint_fast8_t x, uint_fast8_t y, bool on)
Definition oled_ssd130x.h:340
uint8_t buffer_[width *height/8]
Definition oled_ssd130x.h:382
Definition oled_ssd130x.h:16
void Init(const Config &config)
Definition oled_ssd130x.h:37
void SendData(uint8_t *buff, size_t size)
Definition oled_ssd130x.h:48
void SendCommand(uint8_t cmd)
Definition oled_ssd130x.h:42
Definition spi.h:24
Result BlockingTransmit(uint8_t *buff, size_t size, uint32_t timeout=100)
Result Init(const Config &config)
static void Delay(uint32_t delay_ms)
static void DelayTicks(uint32_t delay_ticks)
Hardware defines and helpers for daisy field platform.
Definition index.h:2
@ PORTX
Definition daisy_core.h:188
@ PORTB
Definition daisy_core.h:178
@ PORTC
Definition daisy_core.h:179
@ PORTD
Definition daisy_core.h:180
@ PORTG
Definition daisy_core.h:183
Definition i2c.h:30
struct daisy::I2CHandle::Config::@15 pin_config
Mode mode
Definition i2c.h:65
Speed speed
Definition i2c.h:64
Peripheral periph
Definition i2c.h:57
representation of hardware port/pin combination
Definition daisy_core.h:193
Definition oled_ssd130x.h:457
Transport::Config transport_config
Definition oled_ssd130x.h:458
Definition oled_ssd130x.h:143
void Defaults()
Definition oled_ssd130x.h:157
Config()
Definition oled_ssd130x.h:144
uint32_t sclk_delay
Definition oled_ssd130x.h:151
Pin reset
Definition oled_ssd130x.h:155
Pin mosi
Definition oled_ssd130x.h:153
struct daisy::SSD130x4WireSoftSpiTransport::Config::@10 pin_config
Pin dc
Definition oled_ssd130x.h:154
Pin sclk
Definition oled_ssd130x.h:152
Definition oled_ssd130x.h:69
Pin reset
Definition oled_ssd130x.h:79
Pin dc
Definition oled_ssd130x.h:78
SpiHandle::Config spi_config
Definition oled_ssd130x.h:75
void Defaults()
Definition oled_ssd130x.h:81
struct daisy::SSD130x4WireSpiTransport::Config::@9 pin_config
Config()
Definition oled_ssd130x.h:70
Definition oled_ssd130x.h:237
Transport::Config transport_config
Definition oled_ssd130x.h:238
Definition oled_ssd130x.h:19
uint8_t i2c_address
Definition oled_ssd130x.h:26
void Defaults()
Definition oled_ssd130x.h:27
I2CHandle::Config i2c_config
Definition oled_ssd130x.h:25
Config()
Definition oled_ssd130x.h:20
Definition spi.h:27
ClockPolarity clock_polarity
Definition spi.h:104
Peripheral periph
Definition spi.h:100
Pin nss
Definition spi.h:88
struct daisy::SpiHandle::Config::@18 pin_config
Mode mode
Definition spi.h:101
ClockPhase clock_phase
Definition spi.h:105
BaudPrescaler baud_prescaler
Definition spi.h:107
Direction direction
Definition spi.h:102
unsigned long datasize
Definition spi.h:103