Skip to content

Commit 14c9c88

Browse files
committed
using 16bit transfer in driver::ili9341
1 parent 5463e76 commit 14c9c88

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/modm/driver/display/ili9341_impl.hpp

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,9 @@ void
202202
Ili9341<Interface, Reset, Backlight, BufferSize>::drawHorizontalLine(
203203
glcd::Point start, uint16_t length)
204204
{
205-
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.color) };
206205
auto minLength { std::min(std::size_t(length), BufferSize) };
207206
uint16_t *buffer16 { reinterpret_cast<uint16_t *>(buffer) };
208-
std::fill(buffer16, buffer16+minLength, pixelValue);
207+
std::fill(buffer16, buffer16+minLength, foregroundColor.color);
209208

210209
BatchHandle h(*this);
211210

@@ -223,10 +222,9 @@ void
223222
Ili9341<Interface, Reset, Backlight, BufferSize>::drawVerticalLine(
224223
glcd::Point start, uint16_t length)
225224
{
226-
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.color) };
227225
auto minLength { std::min(std::size_t(length), BufferSize) };
228226
uint16_t *buffer16 { reinterpret_cast<uint16_t *>(buffer) };
229-
std::fill(buffer16, buffer16+minLength, pixelValue);
227+
std::fill(buffer16, buffer16+minLength, foregroundColor.color);
230228

231229
BatchHandle h(*this);
232230

@@ -248,10 +246,9 @@ Ili9341<Interface, Reset, Backlight, BufferSize>::fillRectangle(
248246
auto const y { upperLeft.getY() };
249247
std::size_t pixelCount { std::size_t(width) * std::size_t(height) };
250248

251-
uint16_t const pixelValue { modm::toBigEndian(foregroundColor.color) };
252249
auto minLength { std::min(std::size_t(pixelCount), BufferSize) };
253250
uint16_t *buffer16 { reinterpret_cast<uint16_t *>(buffer) };
254-
std::fill(buffer16, buffer16+minLength, pixelValue);
251+
std::fill(buffer16, buffer16+minLength, foregroundColor.color);
255252

256253
BatchHandle h(*this);
257254

@@ -270,9 +267,6 @@ void
270267
Ili9341<Interface, Reset, Backlight, BufferSize>::fillCircle(
271268
glcd::Point center, uint16_t radius)
272269
{
273-
uint8_t const setColor[] { uint8_t((foregroundColor.color >> 8) & 0xff),
274-
uint8_t(foregroundColor.color & 0xff) };
275-
276270
int16_t f = 1 - radius;
277271
int16_t ddF_x = 0;
278272
int16_t ddF_y = -2 * radius;
@@ -283,7 +277,7 @@ Ili9341<Interface, Reset, Backlight, BufferSize>::fillCircle(
283277

284278
setClipping(center.getX() - radius, center.getY(), 2 * radius, 1);
285279
for (std::size_t i = 0; i < 2 * radius; ++i)
286-
this->writeData(setColor, 2);
280+
this->writeData(foregroundColor.color);
287281

288282
while(x < y)
289283
{
@@ -299,16 +293,16 @@ Ili9341<Interface, Reset, Backlight, BufferSize>::fillCircle(
299293

300294
setClipping(center.getX() - x, center.getY() - y, 2 * x, 1);
301295
for (std::size_t i = 0; i < 2 * x; ++i)
302-
this->writeData(setColor, 2);
296+
this->writeData(foregroundColor.color);
303297
setClipping(center.getX() - y, center.getY() - x, 2 * y, 1);
304298
for (std::size_t i = 0; i < 2 * y; ++i)
305-
this->writeData(setColor, 2);
299+
this->writeData(foregroundColor.color);
306300
setClipping(center.getX() - x, center.getY() + y, 2 * x, 1);
307301
for (std::size_t i = 0; i < 2 * x; ++i)
308-
this->writeData(setColor, 2);
302+
this->writeData(foregroundColor.color);
309303
setClipping(center.getX() - y, center.getY() + x, 2 * y, 1);
310304
for (std::size_t i = 0; i < 2 * y; ++i)
311-
this->writeData(setColor, 2);
305+
this->writeData(foregroundColor.color);
312306
}
313307
}
314308

@@ -317,11 +311,6 @@ void
317311
Ili9341<Interface, Reset, Backlight, BufferSize>::drawImageRaw(glcd::Point upperLeft,
318312
uint16_t width, uint16_t height, modm::accessor::Flash<uint8_t> data)
319313
{
320-
uint8_t const setColor[] { uint8_t((foregroundColor.color >> 8) & 0xff),
321-
uint8_t(foregroundColor.color & 0xff) };
322-
uint8_t const clearColor[] { uint8_t((backgroundColor.color >> 8) & 0xff),
323-
uint8_t(backgroundColor.color & 0xff) };
324-
325314
BatchHandle h(*this);
326315

327316
setClipping(upperLeft.getX(), upperLeft.getY(), width, height);
@@ -333,9 +322,9 @@ Ili9341<Interface, Reset, Backlight, BufferSize>::drawImageRaw(glcd::Point upper
333322
{
334323
uint8_t byte = data[(r / 8) * width + w];
335324
if (byte & bit)
336-
this->writeData(setColor, 2);
325+
this->writeData(foregroundColor.color);
337326
else
338-
this->writeData(clearColor, 2);
327+
this->writeData(backgroundColor.color);
339328
}
340329
// TODO: optimize, use ROL (rotate left)
341330
bit <<= 1;
@@ -351,13 +340,8 @@ Ili9341<Interface, Reset, Backlight, BufferSize>::drawRaw(glcd::Point upperLeft,
351340
{
352341
BatchHandle h(*this);
353342

354-
uint16_t* buffer = (uint16_t*)data;
355-
for(size_t i = 0; i < size_t(width*height); i++) {
356-
buffer[i] = modm::fromBigEndian(buffer[i]);
357-
}
358-
359343
setClipping(upperLeft.getX(), upperLeft.getY(), width, height);
360-
this->writeData((uint8_t*)buffer, width * height * 2);
344+
this->writeData(data, width * height);
361345
}
362346

363347
template <class Interface, class Reset, class Backlight, std::size_t BufferSize>
@@ -391,13 +375,10 @@ void
391375
Ili9341<Interface, Reset, Backlight, BufferSize>::setColoredPixel(
392376
int16_t x, int16_t y, color::Rgb565 const &color)
393377
{
394-
auto const pixelColor { color };
395-
uint8_t const setColor[] { uint8_t((pixelColor.color >> 8) & 0xff), uint8_t(pixelColor.color & 0xff) };
396-
397378
BatchHandle h(*this);
398379

399380
this->setClipping(x, y, 1, 1);
400-
this->writeData(setColor, 2);
381+
this->writeData(foregroundColor.color);
401382
}
402383

403384
template <class Interface, class Reset, class Backlight, std::size_t BufferSize>

src/modm/driver/display/ili9341_spi.hpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,66 @@ namespace modm
2121
template<class SPI, class Cs, class Dc>
2222
class Ili9341SPIInterface: public ili9341, public modm::SpiDevice<SPI>
2323
{
24+
static constexpr auto configuration8bit = []() {
25+
SpiMaster::setDataMode(SpiMaster::DataMode::Mode0);
26+
SpiMaster::setDataOrder(SpiMaster::DataOrder::MsbFirst);
27+
SpiMaster::setDataSize(SpiMaster::DataSize::Bit8);
28+
};
29+
30+
static constexpr auto configuration16bit = []() {
31+
SpiMaster::setDataMode(SpiMaster::DataMode::Mode0);
32+
SpiMaster::setDataOrder(SpiMaster::DataOrder::MsbFirst);
33+
SpiMaster::setDataSize(SpiMaster::DataSize::Bit16);
34+
};
35+
2436
public:
2537
Ili9341SPIInterface()
2638
{
27-
this->attachConfigurationHandler([]() {
28-
SPI::setDataMode(SPI::DataMode::Mode0);
29-
SPI::setDataOrder(SPI::DataOrder::MsbFirst);
30-
});
39+
this->attachConfigurationHandler(configuration16bit);
3140
Cs::setOutput(modm::Gpio::High);
3241
Dc::setOutput();
3342
}
3443

3544
__attribute__((noinline)) void
3645
writeCommand(Command command)
3746
{
47+
this->attachConfigurationHandler(configuration8bit);
3848
Dc::reset(); // enable command
3949
SPI::transferBlocking(i(command));
4050
Dc::set(); // reset to data
51+
this->attachConfigurationHandler(configuration16bit);
4152
}
4253
__attribute__((noinline)) void
4354
writeCommand(Command command, uint8_t const *args, std::size_t length)
4455
{
56+
this->attachConfigurationHandler(configuration8bit);
4557
Dc::reset(); // enable command
4658
SPI::transferBlocking(i(command));
4759
Dc::set(); // reset to data
4860
if (length != 0)
4961
{
5062
SPI::transferBlocking(const_cast<unsigned char *>(args), nullptr, length);
5163
}
64+
this->attachConfigurationHandler(configuration16bit);
5265
}
5366
void
5467
writeData(uint8_t const *data, std::size_t length)
5568
{
69+
this->attachConfigurationHandler(configuration8bit);
5670
SPI::transferBlocking(const_cast<unsigned char *>(data), nullptr, length);
71+
this->attachConfigurationHandler(configuration16bit);
5772
}
73+
74+
writeData(color::Rgb565 data)
75+
{
76+
SPI::transferBlocking(data.value);
77+
}
78+
79+
writeData(color::Rgb565 const *data, std::size_t length)
80+
{
81+
SPI::transferBlocking((uint16_t*)(data), nullptr, length);
82+
}
83+
5884
void
5985
writeCommandValue8(Command command, uint8_t value)
6086
{

0 commit comments

Comments
 (0)