COSA
An Object-Oriented Platform for Arduino Programming
GDDRAM.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_GDDRAM_HH
22 #define COSA_GDDRAM_HH
23 
24 #include <Canvas.h>
25 #include "Cosa/SPI.hh"
26 #include "Cosa/OutputPin.hh"
27 
31 class GDDRAM : public Canvas, protected SPI::Driver {
32 public:
40  GDDRAM(uint16_t width,
41  uint16_t height,
44 
50  virtual bool begin();
51 
57  virtual uint8_t set_orientation(uint8_t direction);
58 
65  virtual void draw_pixel(uint16_t x, uint16_t y);
66 
74  virtual void draw_image(uint16_t x, uint16_t y, Image* image);
75 
83  virtual void draw_vertical_line(uint16_t x, uint16_t y, uint16_t length);
84 
92  virtual void draw_horizontal_line(uint16_t x, uint16_t y, uint16_t length);
93 
102  virtual void fill_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
103 
109  virtual bool end();
110 
111 protected:
113  bool m_initiated;
114 
120  virtual const uint8_t* script() = 0;
121 
125  enum Command {
126  NOP = 0x0, // No Operation
127  SWRESET = 0x01, // Software Reset
128  SWDELAY = 0x02, // Software Delay
129  SCRIPTEND = 0x03, // Script End
130  RDDIDIF = 0x04, // Read Display Identification Information
131  RDDST = 0x09, // Read Display Status
132  RDDPM = 0x0A, // Read Display Power Mode
133  RDDMADCTL = 0x0B, // Read Display MADCTL
134  RDDCOLMOD = 0x0C, // Read Display Pixel Format
135  RDDIM = 0x0D, // Read Display Image Mode
136  RDDSM = 0x0E, // Read Display Signal Mode
137  RDDSDR = 0x0F, // Read Display Self-Diagnostic Result
138  SLPIN = 0x10, // Enter Sleep Mode
139  SLPOUT = 0x11, // Exit Sleep Mode
140  PTLON = 0x12, // Partial Mode On
141  NORON = 0x13, // Normal Display Mode On
142  DINVOFF = 0x20, // Display Inversion Off
143  DINVON = 0x21, // Display Inversion On
144  GAMSET = 0x26, // Gamma Set
145  DISPOFF = 0x28, // Display Off
146  DISPON = 0x29, // Display On
147  CASET = 0x2A, // Column Address Set
148  PASET = 0x2B, // Page Address Set
149  RASET = 0x2B, // Page Address Set
150  RAMWR = 0x2C, // Memory Write
151  RGBSET = 0x2D, // Color Setting for 4K, 65K and 262K
152  RAMRD = 0x2E, // Memory Read
153  PTLAR = 0x30, // Partial Area
154  VSCRDEF = 0x33, // Vertical Scrolling Definition
155  TEOFF = 0x34, // Tearing Effect Line Off
156  TEON = 0x35, // Tearing Effect Line On
157  MADCTL = 0x36, // Memory Data Access Control
158  VSCRSADD = 0x37, // Vertical Scrolling Start Address
159  IDMOFF = 0x38, // Idle Mode Off
160  IDMON = 0x39, // Idle Mode On
161  PIXSET = 0x3A, // Pixel Format Set
162  COLMOD = 0x3A, // Set Color Mode
163  RAMWRC = 0x3C, // Write Memory Continue
164  RAMRDC = 0x3E, // Read Memory Continue
165  SETSCANLINE = 0x44, // Set Tear Scanline
166  GETSCANLINE = 0x45, // Get Scanline
167  WRDISBV = 0x51, // Write Display Brightness Value
168  RDDISBV = 0x52, // Read Display Brightness Value
169  WRCTRLD = 0x53, // Write CTRL Display
170  RDCTRLD = 0x54, // Read CTRL Display
171  WRCABC = 0x55, // Write Content Adaptive Brightness Control
172  RDCABC = 0x56, // Read Content Adaptive Brightness Control
173  WRCABCMIN = 0x5E, // Write CABC Minimum Brightness
174  RDCABCMIN = 0x5F, // Read CABC Minimum Brightness
175  RDID1 = 0xDA, // Read ID1 Value
176  RDID2 = 0xDB, // Read ID2 Value
177  RDID3 = 0xDC, // Read ID3 Value
178  IFMODE = 0xB0, // Interface Mode Control
179  FRMCTR1 = 0xB1, // Frame Rate Control, normal mode
180  FRMCTR2 = 0xB2, // Frame Rate Control, idle mode
181  FRMCTR3 = 0xB3, // Frame Rate Control, partial mode
182  INVCTR = 0xB4, // Display Inversion Control
183  PRCTR = 0xB5, // Blanking Porch Control
184  DISCTRL = 0xB6, // Display Function Control
185  ETMOD = 0xB7, // Entry Mode Set
186  BLCTRL1 = 0xB8, // Backlight Control 1
187  BLCTRL2 = 0xB9, // Backlight Control 2
188  BLCTRL3 = 0xBA, // Backlight Control 3
189  BLCTRL4 = 0xBB, // Backlight Control 4
190  BLCTRL5 = 0xBC, // Backlight Control 5
191  BLCTRL6 = 0xBD, // Backlight Control 6
192  BLCTRL7 = 0xBE, // Backlight Control 7
193  BLCTRL8 = 0xBF, // Backlight Control 8
194  PWCTRL1 = 0xC0, // Power Control 1
195  PWCTRL2 = 0xC1, // Power Control 2
196  PWCTRL3 = 0xC2, // Power Control 3, normal mode
197  PWCTRL4 = 0xC3, // Power Control 4, idle mode
198  PWCTRL5 = 0xC4, // Power Control 5, partial mode
199  PWCTRL6 = 0xFC, // Power Control 6, partial mode
200  VMCTRL1 = 0xC5, // VCOM Control 1
201  VMCTRL2 = 0xC7, // VCOM Control 2
202  NVMWR = 0xD0, // NV Memory Write
203  NVMPKEY = 0xD1, // NV Memory Protection Key
204  RDNVM = 0xD2, // NV Memory Status Read
205  RDID4 = 0xD3, // Read ID4
206  PGAMCTRL = 0xE0, // Positive Gamma Correction
207  GMCTRP1 = 0xE0, // Positive Gamma Correction
208  NGAMCTRL = 0xE1, // Negative Gamma Correction
209  GMCTRN1 = 0xE1, // Negative Gamma Correction
210  DGAMCTRL1 = 0xE2, // Digital Gamma Control 1
211  DGAMCTRL2 = 0xE3, // Digital Gamma Control 2
212  IFCTRL = 0xF6, // Interface Control
213  PWCTRLA = 0xCB, // Power Control A
214  PWCTRLB = 0xCF, // Power Control B
215  DTCTRLA = 0xE8, // Driver Timing Control A
216  DTCTRLB = 0xEA, // Driver Timing Control B
217  PWONCTRL = 0xED, // Power On Sequence Control
218  EXTCTRL = 0xF0, // Extension Command Control
219  EN3GAM = 0xF2, // Enable/Disable 3-Gamma
220  PRCTRL = 0xF7, // Pump Ratio Control
221  VCOM4L = 0xFF // VCOM 4 Level Control
222  } __attribute__((packed));
223 
227  enum {
228  MADCTL_MH = 0x04, // Horizontal Refresh order
229  MADCTL_ML = 0x10, // Vertical Refresh order
230  MADCTL_MV = 0x20, // Row / Column Exchange
231  MADCTL_MX = 0x40, // Column Address order
232  MADCTL_MY = 0x80, // Row Address order
233  MADCTL_BGR = 0x08, // BGR order
234  MADCTL_RGB = 0x00 // RGB order
235  } __attribute__((packed));
236 
241  void write(uint16_t data)
242  __attribute__((always_inline))
243  {
244  spi.transfer_start(data >> 8);
245  spi.transfer_next(data);
247  }
248 
255  void write(uint16_t data, uint16_t count)
256  __attribute__((always_inline))
257  {
258  uint8_t high = data >> 8;
259  uint8_t low = data;
260  spi.transfer_start(high);
261  while (--count) {
262  spi.transfer_next(low);
263  spi.transfer_next(high);
264  };
265  spi.transfer_next(low);
267  }
268 
273  void write(Command cmd)
274  __attribute__((always_inline))
275  {
276  asserted(m_dc) {
277  spi.transfer(cmd);
278  }
279  }
280 
286  void write(Command cmd, uint8_t data)
287  __attribute__((always_inline))
288  {
289  asserted(m_dc) {
290  spi.transfer(cmd);
291  }
292  spi.transfer(data);
293  }
294 
300  void write(Command cmd, uint16_t data)
301  __attribute__((always_inline))
302  {
303  asserted(m_dc) {
304  spi.transfer(cmd);
305  }
306  spi.transfer_start(data >> 8);
307  spi.transfer_next(data);
309  }
310 
317  void write(Command cmd, uint16_t x, uint16_t y)
318  __attribute__((always_inline))
319  {
320  asserted(m_dc) {
321  spi.transfer(cmd);
322  }
323  spi.transfer_start(x >> 8);
324  spi.transfer_next(x);
325  spi.transfer_next(y >> 8);
326  spi.transfer_next(y);
328  }
329 };
330 
331 #endif
332 
virtual void draw_image(uint16_t x, uint16_t y, Image *image)
Definition: GDDRAM.cpp:99
uint8_t transfer(uint8_t data)
Definition: SOFT_SPI.cpp:87
OutputPin m_dc
Data/Command select pin.
Definition: GDDRAM.hh:112
void write(Command cmd)
Definition: GDDRAM.hh:273
#define asserted(pin)
Definition: Pin.hh:275
void write(Command cmd, uint16_t data)
Definition: GDDRAM.hh:300
GDDRAM(uint16_t width, uint16_t height, Board::DigitalPin cs, Board::DigitalPin dc)
Definition: GDDRAM.cpp:23
bool m_initiated
Initialization state.
Definition: GDDRAM.hh:113
virtual const uint8_t * script()=0
virtual void fill_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition: GDDRAM.cpp:170
uint8_t transfer_next(uint8_t data)
Definition: SPI.hh:261
void write(uint16_t data, uint16_t count)
Definition: GDDRAM.hh:255
Definition: GDDRAM.hh:31
virtual void draw_horizontal_line(uint16_t x, uint16_t y, uint16_t length)
Definition: GDDRAM.cpp:148
virtual bool begin()
Definition: GDDRAM.cpp:35
virtual uint8_t set_orientation(uint8_t direction)
Definition: GDDRAM.cpp:62
void write(Command cmd, uint8_t data)
Definition: GDDRAM.hh:286
virtual bool end()
Definition: GDDRAM.cpp:195
uint8_t transfer_await()
Definition: SPI.hh:249
virtual void draw_vertical_line(uint16_t x, uint16_t y, uint16_t length)
Definition: GDDRAM.cpp:126
void transfer_start(uint8_t data)
Definition: SPI.hh:238
SPI spi
Definition: SPI.cpp:29
void write(Command cmd, uint16_t x, uint16_t y)
Definition: GDDRAM.hh:317
void draw_pixel()
Definition: Canvas.hh:596
Definition: Canvas.hh:44
void write(uint16_t data)
Definition: GDDRAM.hh:241
Command
Definition: GDDRAM.hh:125