libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
oled_ssd1351.h
Go to the documentation of this file.
1#pragma once
2
3#include "per/spi.h"
4#include "per/gpio.h"
5#include "sys/system.h"
6
7#define oled_white 0xffff
8#define oled_black 0x0000
9#define oled_red 0x00f1
10#define oled_green 0xe007
11#define oled_blue 0x1f00
12#define oled_cyan (oled_green | oled_blue)
13#define oled_yellow (oled_green | oled_red)
14#define oled_magenta (oled_red | oled_blue)
15
16namespace daisy
17{
22{
23 public:
59 void Init(const Config& config)
60 {
61 // Initialize both GPIO
62 pin_reset_.Init(config.pin_config.reset, GPIO::Mode::OUTPUT);
63 pin_dc_.Init(config.pin_config.dc, GPIO::Mode::OUTPUT);
64
65 // Initialize SPI
66 spi_.Init(config.spi_config);
67
68 // Reset and Configure OLED.
69 pin_reset_.Write(false);
70 System::Delay(10);
71 pin_reset_.Write(true);
72 System::Delay(10);
73 };
75 {
76 pin_dc_.Write(false);
77 spi_.BlockingTransmit(&cmd, 1);
78 };
79
80 void SendData(uint8_t* buff, size_t size)
81 {
82 pin_dc_.Write(true);
84 };
85
86 void SendData(uint8_t data)
87 {
88 pin_dc_.Write(true);
89 spi_.BlockingTransmit(&data, 1);
90 };
91
92 private:
93 SpiHandle spi_;
94 GPIO pin_reset_;
95 GPIO pin_dc_;
96};
97
98
102template <size_t width, size_t height, typename Transport>
104{
105 public:
106 struct Config
107 {
108 typename Transport::Config transport_config;
109 };
110
112 {
115 transport_.Init(config.transport_config);
116
117 transport_.SendCommand(0xfd); // lock IC
118 transport_.SendData(0x12);
119 transport_.SendCommand(0xfd); // unlock IC
120 transport_.SendData(0xb1); //
121
122 transport_.SendCommand(0xae); // display off
123
124 transport_.SendCommand(0x15); // set column address
125 transport_.SendData(0x00); // column address start 00
126 transport_.SendData(0x7f); // column address end 127
127
128 transport_.SendCommand(0x75); // set row address
129 transport_.SendData(0x00); // row address start 00
130 transport_.SendData(0x7f); // row address end 127
131
132 transport_.SendCommand(
133 0xB3); // Set Front Clock Divider / Oscillator Frequency
134 transport_.SendData(0xF1);
135
136 transport_.SendCommand(0xCA); // Set Multiplex Ratio
137 transport_.SendData(0x7F);
138
139 transport_.SendCommand(0xa0); // Set Re-map & Dual COM Line Mode
140 transport_.SendData(
141 0x74); // color mode 64k, enable com split, reverse com scan, color swapped, hz scan
142
143 transport_.SendCommand(0xa1); // set display start line
144 transport_.SendData(0x00); // line 0
145
146 transport_.SendCommand(0xa2); // set display offset
147 transport_.SendData(0x00); // column 0
148
149 transport_.SendCommand(0xAB); // Function Selection
150 transport_.SendData(0x01);
151
152 transport_.SendCommand(0xB4); // Set Segment Low Voltage
153 transport_.SendData(0xA0);
154 transport_.SendData(0xB5);
155 transport_.SendData(0x55);
156
157 transport_.SendCommand(0xC1); // Set Contrast Current for Color A,B,C
158 transport_.SendData(0xC8);
159 transport_.SendData(0x80);
160 transport_.SendData(0xC0);
161
162 transport_.SendCommand(0xC7); // Master Contrast Current Control
163 transport_.SendData(0x0F);
164
165 transport_.SendCommand(
166 0xB1); // Set Reset (Phase 1) / Pre-charge (Phase 2) period
167 transport_.SendData(0x32);
168
169 transport_.SendCommand(0xB2); // Display Enhancement
170 transport_.SendData(0xA4);
171 transport_.SendData(0x00);
172 transport_.SendData(0x00);
173
174 transport_.SendCommand(0xBB); // Set Pre-charge voltage
175 transport_.SendData(0x17);
176
177 transport_.SendCommand(0xB6); // Set Second Precharge Period
178 transport_.SendData(0x01);
179
180 transport_.SendCommand(0xBE); // Set VCOMH Voltage
181 transport_.SendData(0x05);
182
183 transport_.SendCommand(0xA6); // Normal display
184
185 System::Delay(300); // wait 300ms
186 transport_.SendCommand(0xaf); // turn on display
187 Fill(false);
188 };
189
190 size_t Width() const { return width; };
191 size_t Height() const { return height; };
192
194 {
195 if((x >= width) || (y >= height))
196 return;
197
198 if(on)
199 {
200 buffer_[(y * width) + x] = fg_color_;
201 }
202 else
203 {
204 buffer_[(y * width) + x] = bg_color_;
205 }
206 };
207
208 void Fill(bool on)
209 {
210 for(size_t i = 0; i < sizeof(buffer_) / 2; i++)
211 {
213 }
214 };
215
219 void Update()
220 {
221 transport_.SendCommand(0x15); // column
222 transport_.SendData(0x00);
223 transport_.SendData(width - 1);
224
225 transport_.SendCommand(0x75); // row
226 transport_.SendData(0x00);
227 transport_.SendData(height - 1);
228
229 transport_.SendCommand(0x5c); // write display buffer
230 transport_.SendData((uint8_t*)buffer_, sizeof(buffer_));
231 };
232
234 {
235 uint16_t t1, t2;
236
237 fg_color_ = (red & 0x1f) << 11 | (green & 0x3f) << 5 | (blue & 0x1f);
238 t1 = (fg_color_ >> 8) & 0xff;
239 t2 = (fg_color_ & 0xff);
240 fg_color_ = t2 << 8 | t1;
241 };
242
244 {
245 uint16_t t1, t2;
246
247 bg_color_ = (red & 0x1f) << 11 | (green & 0x3f) << 5 | (blue & 0x1f);
248 t1 = (bg_color_ >> 8) & 0xff;
249 t2 = (bg_color_ & 0xff);
250 bg_color_ = t2 << 8 | t1;
251 };
252
253 protected:
258};
259
265
266}; // namespace daisy
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.
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_ssd1351.h:22
void SendCommand(uint8_t cmd)
Definition oled_ssd1351.h:74
void Init(const Config &config)
Definition oled_ssd1351.h:59
void SendData(uint8_t data)
Definition oled_ssd1351.h:86
void SendData(uint8_t *buff, size_t size)
Definition oled_ssd1351.h:80
Definition oled_ssd1351.h:104
void SetColorFG(uint8_t red, uint8_t green, uint8_t blue)
Definition oled_ssd1351.h:233
uint16_t bg_color_
Definition oled_ssd1351.h:257
size_t Height() const
Definition oled_ssd1351.h:191
size_t Width() const
Definition oled_ssd1351.h:190
void SetColorBG(uint8_t red, uint8_t green, uint8_t blue)
Definition oled_ssd1351.h:243
void DrawPixel(uint_fast8_t x, uint_fast8_t y, bool on)
Definition oled_ssd1351.h:193
void Init(Config config)
Definition oled_ssd1351.h:111
Transport transport_
Definition oled_ssd1351.h:254
void Update()
Definition oled_ssd1351.h:219
void Fill(bool on)
Definition oled_ssd1351.h:208
uint16_t buffer_[width *height]
Definition oled_ssd1351.h:255
uint16_t fg_color_
Definition oled_ssd1351.h:256
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)
Hardware defines and helpers for daisy field platform.
Definition index.h:2
@ PORTX
Definition daisy_core.h:188
@ PORTB
Definition daisy_core.h:178
@ PORTG
Definition daisy_core.h:183
#define oled_black
Definition oled_ssd1351.h:8
#define oled_white
Definition oled_ssd1351.h:7
representation of hardware port/pin combination
Definition daisy_core.h:193
Definition oled_ssd1351.h:25
Pin dc
Definition oled_ssd1351.h:34
Config()
Definition oled_ssd1351.h:26
struct daisy::SSD13514WireSpiTransport::Config::@12 pin_config
void Defaults()
Definition oled_ssd1351.h:37
SpiHandle::Config spi_config
Definition oled_ssd1351.h:31
Pin reset
Definition oled_ssd1351.h:35
Definition oled_ssd1351.h:107
Transport::Config transport_config
Definition oled_ssd1351.h:108
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