COSA
An Object-Oriented Platform for Arduino Programming
GDDRAM.cpp
Go to the documentation of this file.
1 
21 #include "GDDRAM.hh"
22 
23 GDDRAM::GDDRAM(uint16_t width,
24  uint16_t height,
26  Board::DigitalPin dc) :
27  Canvas(width, height),
28  SPI::Driver(cs, SPI::ACTIVE_LOW, SPI::DIV2_CLOCK, 3, SPI::MSB_ORDER, NULL),
29  m_dc(dc, 1),
30  m_initiated(false)
31 {
32 }
33 
34 bool
36 {
37  if (m_initiated) return (false);
38  const uint8_t* bp = script();
39  uint8_t count;
40  uint8_t cmd;
41  spi.acquire(this);
42  spi.begin();
43  while ((cmd = pgm_read_byte(bp++)) != SCRIPTEND) {
44  count = pgm_read_byte(bp++);
45  if (cmd == SWDELAY) {
46  DELAY(count);
47  }
48  else {
49  asserted(m_dc) {
50  spi.transfer(cmd);
51  }
52  while (count--) spi.transfer(pgm_read_byte(bp++));
53  }
54  }
55  spi.end();
56  spi.release();
57  m_initiated = true;
58  return (true);
59 }
60 
61 uint8_t
62 GDDRAM::set_orientation(uint8_t direction)
63 {
64  uint8_t previous = m_direction;
65  uint8_t setting = 0;
66  m_direction = direction;
67  uint16_t temp = WIDTH;
68  WIDTH = HEIGHT;
69  HEIGHT = temp;
70  if (direction == LANDSCAPE) {
71  setting = (MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR);
72  }
73  else {
74  setting = (MADCTL_MX | MADCTL_BGR);
75  }
76  spi.acquire(this);
77  spi.begin();
78  write(MADCTL, setting);
79  spi.end();
80  spi.release();
81  return (previous);
82 }
83 
84 void
85 GDDRAM::draw_pixel(uint16_t x, uint16_t y)
86 {
87  const color16_t color = get_pen_color();
88  spi.acquire(this);
89  spi.begin();
90  write(CASET, x, x + 1);
91  write(PASET, y, y + 1);
92  write(RAMWR);
93  write(color.rgb);
94  spi.end();
95  spi.release();
96 }
97 
98 void
99 GDDRAM::draw_image(uint16_t x, uint16_t y, Image* image)
100 {
101  uint16_t width = image->WIDTH;
102  uint16_t height = image->HEIGHT;
103  spi.acquire(this);
104  spi.begin();
105  write(CASET, x, x + width - 1);
106  write(PASET, y, y + height - 1);
107  write(RAMWR);
108  spi.end();
109  spi.release();
110  for (uint16_t i = 0; i < height; i++) {
112  size_t count;
113  for (uint16_t j = 0; j < width; j += count) {
114  count = (width - j > Image::BUFFER_MAX) ? Image::BUFFER_MAX : width - j;
115  image->read(buf, count);
116  spi.acquire(this);
117  spi.begin();
118  for (uint16_t k = 0; k < count; k++) write(buf[k]);
119  spi.end();
120  spi.release();
121  }
122  }
123 }
124 
125 void
126 GDDRAM::draw_vertical_line(uint16_t x, uint16_t y, uint16_t length)
127 {
128  if (UNLIKELY((x >= WIDTH) || (length == 0))) return;
129  if (y >= HEIGHT) {
130  uint16_t h = y + length;
131  if (h >= HEIGHT) return;
132  length = h;
133  y = 0;
134  }
135  if ((y + length - 1) >= HEIGHT) length = HEIGHT - y;
136  const color16_t color = get_pen_color();
137  spi.acquire(this);
138  spi.begin();
139  write(CASET, x, x);
140  write(PASET, y, y + length - 1);
141  write(RAMWR);
142  write(color.rgb, length);
143  spi.end();
144  spi.release();
145 }
146 
147 void
148 GDDRAM::draw_horizontal_line(uint16_t x, uint16_t y, uint16_t length)
149 {
150  if (UNLIKELY((y >= HEIGHT) || (length == 0))) return;
151  if (x >= WIDTH) {
152  uint16_t w = x + length;
153  if (w >= WIDTH) return;
154  length = w;
155  x = 0;
156  }
157  if ((x + length - 1) >= WIDTH) length = WIDTH - x;
158  const color16_t color = get_pen_color();
159  spi.acquire(this);
160  spi.begin();
161  write(CASET, x, x + length - 1);
162  write(PASET, y, y);
163  write(RAMWR);
164  write(color.rgb, length);
165  spi.end();
166  spi.release();
167 }
168 
169 void
170 GDDRAM::fill_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
171 {
172  if (UNLIKELY((width == 0) || (height == 0))) return;
173  if (UNLIKELY((x >= WIDTH) || (y >= HEIGHT))) return;
174  if ((x + width - 1) >= WIDTH) width = WIDTH - x;
175  if ((y + height - 1) >= HEIGHT) height = HEIGHT - y;
176  const color16_t color = get_pen_color();
177  spi.acquire(this);
178  spi.begin();
179  write(CASET, x, x + width - 1);
180  write(PASET, y, y + height - 1);
181  write(RAMWR);
182  if (width > height) {
183  for (y = 0; y < height; y++)
184  write(color.rgb, width);
185  }
186  else {
187  for (x = 0; x < width; x++)
188  write(color.rgb, height);
189  }
190  spi.end();
191  spi.release();
192 }
193 
194 bool
196 {
197  return (true);
198 }
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
#define asserted(pin)
Definition: Pin.hh:275
Definition: SPI.hh:57
void acquire(Driver *dev)
Definition: SOFT_SPI.cpp:43
GDDRAM(uint16_t width, uint16_t height, Board::DigitalPin cs, Board::DigitalPin dc)
Definition: GDDRAM.cpp:23
#define NULL
Definition: Types.h:101
static const size_t BUFFER_MAX
Definition: Canvas.hh:363
bool m_initiated
Initialization state.
Definition: GDDRAM.hh:113
color16_t color(uint8_t red, uint8_t green, uint8_t blue)
Definition: Canvas.hh:549
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
color16_t get_pen_color() const
Definition: Canvas.hh:441
#define DELAY(us)
Definition: Types.h:280
uint16_t rgb
Definition: Canvas.hh:50
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
uint16_t HEIGHT
Definition: Canvas.hh:340
void begin()
Definition: SPI.hh:216
virtual bool end()
Definition: GDDRAM.cpp:195
void end()
Definition: SPI.hh:226
virtual void draw_vertical_line(uint16_t x, uint16_t y, uint16_t length)
Definition: GDDRAM.cpp:126
uint16_t WIDTH
Definition: Canvas.hh:337
virtual bool read(color16_t *buf, size_t count)=0
SPI spi
Definition: SPI.cpp:29
uint16_t HEIGHT
Definition: Canvas.hh:370
void release()
Definition: SOFT_SPI.cpp:64
#define UNLIKELY(x)
Definition: Types.h:153
void draw_pixel()
Definition: Canvas.hh:596
Definition: Canvas.hh:44
void write(uint16_t data)
Definition: GDDRAM.hh:241
uint16_t WIDTH
Definition: Canvas.hh:369
uint8_t m_direction
Definition: Canvas.hh:1015