Pixels
Pixels_TEMPLATE.h
1 /*
2  * Pixels. Graphics library for TFT displays.
3  *
4  * Copyright (C) 2012-2014
5  *
6  * The code is written in C/C++ for Arduino and can be easily ported to any microcontroller by rewritting the low level pin access functions.
7  *
8  * Text output methods of the library rely on Pixelmeister's font data format. See: http://pd4ml.com/pixelmeister
9  *
10  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
11  *
12  * This library includes some code portions and algoritmic ideas derived from works of
13  * - Andreas Schiffler -- aschiffler at ferzkopp dot net (SDL_gfx Project)
14  * - K. Townsend http://microBuilder.eu (lpc1343codebase Project)
15  */
16 
17 
18 // There are 5 TODO steps:
19 
20 // 1. TODO replace all occurences of "TEMPLATE" pattern with a controller name (i.e. PIXELS_TEMPLATE_H -> PIXELS_SSD1289_H)
21 
22 /*
23  * Pixels port to TEMPLATE controller
24  */
25 
26 #include "Pixels.h"
27 
28 #ifndef PIXELS_TEMPLATE_H
29 #define PIXELS_TEMPLATE_H
30 #define PIXELS_MAIN
31 
32 #if defined(PIXELS_ANTIALIASING_H)
33 #define PixelsBase PixelsAntialiased
34 #endif
35 
36 class Pixels : public PixelsBase
37 #if defined(PIXELS_SPISW_H)
38  , public SPIsw
39 #elif defined(PIXELS_SPIHW_H)
40  , public SPIhw
41 #elif defined(PIXELS_PPI8_H)
42  , public PPI8
43 #elif defined(PIXELS_PPI16_H)
44  , public PPI16
45 #endif
46 {
47 protected:
48  void deviceWriteData(uint8_t high, uint8_t low) {
49  writeData(high, low);
50  }
51 
52  void setRegion(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
53  void quickFill(int b, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
54  void setFillDirection(uint8_t direction);
55 
56  void scrollCmd();
57 
58 public:
59 
60 // 2. TODO Adjust device resolution and default pins below
61 
62  Pixels() : PixelsBase(320, 480) {
63  scrollSupported = true;
64  setSpiPins(4, 3, 7, 5, 6); // dummy code in PPI case
65  setPpiPins(38, 39, 40, 41, 0); // dummy code in SPI case
66  }
67 
68  Pixels(uint16_t width, uint16_t height) : PixelsBase( width, height) {
69  scrollSupported = true;
70  setSpiPins(4, 3, 7, 5, 6); // dummy code in PPI case
71  setPpiPins(38, 39, 40, 41, 0); // dummy code in SPI case
72  }
73 
74  void init();
75 };
76 
77 #if defined(PIXELS_ANTIALIASING_H)
78 #undef PixelsBase
79 #endif
80 
81 void Pixels::init() {
82 
83  initInterface();
84 
85  chipSelect();
86 
87 // 3. TODO put device initialization code here
88 // The simplest way would be to copy-paste it from a demo application (hopefully) came with the display module.
89 // Use existing device driver .h files as a command syntax reference
90 
91  chipDeselect();
92 }
93 
94 void Pixels::scrollCmd() {
95  chipSelect();
96 
97 // 4. TODO put scrolling command(s) here. It is a rare case the code is given in demo applications or
98 // in alternative open source libraries. A datasheet usually helps.
99 
100  chipDeselect();
101 }
102 
103 void Pixels::setFillDirection(uint8_t direction) {
104  fillDirection = direction;
105 }
106 
107 void Pixels::quickFill (int color, int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
108  chipSelect();
109 
110  setRegion(x1, y1, x2, y2);
111  int32_t counter = (int32_t)(x2 - x1 + 1) * (y2 - y1 + 1);
112 
113  registerSelect();
114 
115  uint8_t lo = lowByte(color);
116  uint8_t hi = highByte(color);
117 
118  for (int16_t i = 0; i < counter / 20; i++) {
119  writeData(hi, lo);
120  writeData(hi, lo);
121  writeData(hi, lo);
122  writeData(hi, lo);
123  writeData(hi, lo);
124  writeData(hi, lo);
125  writeData(hi, lo);
126  writeData(hi, lo);
127  writeData(hi, lo);
128  writeData(hi, lo);
129  writeData(hi, lo);
130  writeData(hi, lo);
131  writeData(hi, lo);
132  writeData(hi, lo);
133  writeData(hi, lo);
134  writeData(hi, lo);
135  writeData(hi, lo);
136  writeData(hi, lo);
137  writeData(hi, lo);
138  writeData(hi, lo);
139  }
140  for (int32_t i = 0; i < counter % 20; i++) {
141  writeData(hi, lo);
142  }
143 
144  chipDeselect();
145 }
146 
147 void Pixels::setRegion(int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
148  if ( orientation != PORTRAIT ) {
149  int16_t buf;
150  switch( orientation ) {
151  case LANDSCAPE:
152  buf = x1;
153  x1 = deviceWidth - y1 - 1;
154  y1 = buf;
155  buf = x2;
156  x2 = deviceWidth - y2 - 1;
157  y2 = buf;
158  break;
159  case PORTRAIT_FLIP:
160  y1 = deviceHeight - y1 - 1;
161  y2 = deviceHeight - y2 - 1;
162  x1 = deviceWidth - x1 - 1;
163  x2 = deviceWidth - x2 - 1;
164  break;
165  case LANDSCAPE_FLIP:
166  buf = y1;
167  y1 = deviceHeight - x1 - 1;
168  x1 = buf;
169  buf = y2;
170  y2 = deviceHeight - x2 - 1;
171  x2 = buf;
172  break;
173  }
174 
175  if (x2 < x1) {
176  swap(x1, x2);
177  }
178  if (y2 < y1) {
179  swap(y1, y2);
180  }
181  }
182 
183 // 5. TODO put memory region bounds definition commands here. The code can be taken
184 // from a demo application, supplied with a device.
185 
186 }
187 #endif
void init()
void setFillDirection(uint8_t direction)
PixelsBase(uint16_t width, uint16_t height)