Skip to content

Commit cfbec09

Browse files
committed
Indexed Color support
1 parent a2c9795 commit cfbec09

14 files changed

+202
-354
lines changed

src/modm/driver/display/ili9341.hpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class Ili9341 : public Interface,
5353
using ColorType = Rgb565;
5454
static constexpr Resolution resolution{320, 240};
5555

56+
ColorType* colormap;
57+
5658
using BufferLandscape = Buffer<Rgb565, resolution>;
5759
using BufferPortrait = Buffer<Rgb565, resolution.swapped()>;
5860

@@ -109,30 +111,19 @@ class Ili9341 : public Interface,
109111
write(Rectangle rectangle, P pattern);
110112

111113
// Write BufferInterface
112-
template<class C_>
113-
modm::ResumableResult<void>
114-
write(BufferInterface<C_> &buffer, Point origin = {0, 0}) {
115-
RF_BEGIN();
116-
RF_END_RETURN_CALL(writeImage(
117-
ImageAccessor<C_, modm::accessor::Ram>(&buffer),
118-
origin)
119-
);
120-
}
121-
122-
// Write BufferInterface
123-
template<class C_>
114+
template<class C>
124115
modm::ResumableResult<void>
125-
write(BufferInterface<C_> &buffer, ColorType* colormap, Point origin = {0, 0}) {
116+
write(BufferInterface<C> &buffer, Point origin = {0, 0}) {
126117
RF_BEGIN();
127-
RF_END_RETURN_CALL(writeImage(ImageAccessor<C_, modm::accessor::Ram>(&buffer), colormap, origin));
118+
RF_END_RETURN_CALL(writeImage(ImageAccessor<C, modm::accessor::Ram>(&buffer), origin));
128119
}
129120

130121
// Write Flash Image
131122
modm::ResumableResult<void>
132123
write(const uint8_t *addr, Point origin = {0, 0}) {
133124
RF_BEGIN();
134125
RF_END_RETURN_CALL(writeImage(
135-
ImageAccessor<Monochrome, modm::accessor::Flash>(addr),
126+
ImageAccessor<Gray2, modm::accessor::Flash>(addr),
136127
origin)
137128
);
138129
}
@@ -169,19 +160,19 @@ class Ili9341 : public Interface,
169160
* @param decoder Image with Flash or Ram Accessor
170161
* @param origin Placement for the image
171162
*/
172-
template<Color C_, template<typename> class Accessor>
163+
template<Color C, template<typename> class Accessor>
173164
modm::ResumableResult<void>
174-
writeImage(ImageAccessor<C_, Accessor> decoder, Point origin = {0, 0});
165+
writeImage(ImageAccessor<C, Accessor> decoder, Point origin = {0, 0});
175166

176167
/**
177168
* Write ColorGrayBase2 Image using color-mapping
178169
*
179170
* @param decoder Image with Flash or Ram Accessor
180171
* @param origin Placement for the image
181172
*/
182-
template<template<typename> class Accessor>
173+
template<ColorGrayBase2 G, template<typename> class Accessor>
183174
modm::ResumableResult<void>
184-
writeImage(ImageAccessor<Monochrome, Accessor> decoder, const ColorType colors[Monochrome::max + 1], Point origin = {0, 0});
175+
writeImage(ImageAccessor<G, Accessor> decoder, Point origin = {0, 0});
185176

186177
inline void
187178
initialize_write_monochrome(Point origin);
@@ -201,7 +192,7 @@ class Ili9341 : public Interface,
201192

202193
private:
203194
// Static variables for resumable functions
204-
void scannerNextRow() {
195+
void scannerincrementRow() {
205196
p.scanner.x++;
206197
p.scanner.y = this->clipping.topLeft.y;
207198
}

src/modm/driver/display/ili9341_impl.hpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#pragma once
13+
1314
#include "ili9341.hpp"
1415

1516
using namespace modm::shape;
@@ -262,7 +263,7 @@ modm::Ili9341<Interface, Reset, BC>::write(Rectangle rectangle, P pattern) {
262263
p.buffer[p.i] = pattern(p.scanner);
263264

264265
if (++p.scanner.y == this->clipping.bottomRight.y) {
265-
scannerNextRow();
266+
scannerincrementRow();
266267
}
267268
}
268269
// Transfer
@@ -300,7 +301,7 @@ modm::Ili9341<Interface, Reset, BC>::writeImage(ImageAccessor<Rgb565, Accessor>
300301

301302
while(p.pixels >= p.pixels_bulk) {
302303
RF_CALL(this->writeData(d.getPointer() + 1, p.pixels_bulk));
303-
d.nextRow();
304+
d.incrementRow();
304305
p.pixels -= p.pixels_bulk;
305306
}
306307
} else
@@ -339,8 +340,8 @@ modm::Ili9341<Interface, Reset, BC>::writeImage(ImageAccessor<C_, Accessor> deco
339340
// difference to -- write monochrome Image -- below
340341
p.buffer[p.i] = d.nextPixel();
341342
if (++p.scanner.y == this->clipping.bottomRight.y) {
342-
scannerNextRow();
343-
d.nextRow();
343+
scannerincrementRow();
344+
d.incrementRow();
344345
}
345346
}
346347
// Transfer
@@ -354,12 +355,12 @@ modm::Ili9341<Interface, Reset, BC>::writeImage(ImageAccessor<C_, Accessor> deco
354355

355356
// -- Write Gray Image, this supports color-mapping
356357
template<class Interface, class Reset, size_t BC>
357-
template<template<typename> class Accessor>
358+
template<ColorGrayBase2 G, template<typename> class Accessor>
358359
modm::ResumableResult<void>
359-
modm::Ili9341<Interface, Reset, BC>::writeImage(ImageAccessor<Monochrome, Accessor> decoder, const ColorType colors[Monochrome::max + 1], Point origin) {
360+
modm::Ili9341<Interface, Reset, BC>::writeImage(ImageAccessor<G, Accessor> decoder, Point origin) {
360361
// Found no cast for d share the memory between all of the writeImage()-methods.
361362
// This waste of RAM will be history once Resumable Functions become history.
362-
static ImageAccessor<Monochrome, Accessor> d;
363+
static ImageAccessor<G, Accessor> d;
363364

364365
RF_BEGIN();
365366

@@ -378,10 +379,10 @@ modm::Ili9341<Interface, Reset, BC>::writeImage(ImageAccessor<Monochrome, Access
378379
// OPTIMIZE following Line is the only
379380
// difference to -- write colored Image -- above
380381
// IMPLEMENT color-mapping
381-
p.buffer[p.i] = colors[d.nextPixel().getValue()];
382+
p.buffer[p.i] = colormap[d.nextPixel().getValue()];
382383
if (++p.scanner.y == this->clipping.bottomRight.y) {
383-
scannerNextRow();
384-
d.nextRow();
384+
scannerincrementRow();
385+
d.incrementRow();
385386
}
386387
}
387388
// Transfer
@@ -418,6 +419,7 @@ modm::Ili9341<Interface, Reset, BC>::drawBlind(const VLine& vline)
418419
RF_END_RETURN_CALL(drawBlind(Section(vline.start, {vline.start.x + 1, vline.end_y})));
419420
}
420421

422+
#include <limits>
421423
template<class Interface, class Reset, size_t BC>
422424
modm::ResumableResult<void>
423425
modm::Ili9341<Interface, Reset, BC>::drawBlind(const Section& section)
@@ -426,20 +428,15 @@ modm::Ili9341<Interface, Reset, BC>::drawBlind(const Section& section)
426428

427429
this->clipping = section;
428430
RF_CALL(updateClipping());
429-
430-
// OPTIMIZE Make this impossible fast through use of DMA
431-
// See https://github.com/modm-io/modm/issues/666
432-
433-
// Without DMA, at least this could be parallelised to updateClipping(..) above
434-
p.pixels_bulk = std::min<size_t>(BC, p.pixels);
435-
std::fill(p.buffer, p.buffer + p.pixels_bulk, color);
436-
437-
while (p.pixels)
438-
{
431+
432+
// Respect DMAs max bulksize of 2^16-1
433+
while(p.pixels) {
434+
p.pixels_bulk = std::min<uint32_t>(p.pixels, std::numeric_limits<uint16_t>::max());
435+
RF_CALL(this->writeData(color, p.pixels_bulk));
439436
p.pixels -= p.pixels_bulk;
440-
RF_CALL(this->writeData(p.buffer, p.pixels_bulk));
441-
p.pixels_bulk = std::min<size_t>(BC, p.pixels);
442437
}
438+
// RF_CALL(this->writeData(color, p.pixels));
439+
443440
RF_END();
444441
}
445442

src/modm/math/scaling_unsigned.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ScalingUnsigned {
6565
: value(other.value * max / other.max)
6666
{}
6767

68-
/* // Faster construction from monochrome
68+
/* // Faster construction from from single digit
6969
constexpr ScalingUnsigned(const ScalingUnsigned<1> &other) : value(other.value ? bitmask<D>() : 0){}
7070
7171
// constexpr ScalingUnsigned(ScalingUnsigned<1> &&other) : value(other.value ? bitmask<D>() : 0){}

src/modm/ui/color/rgb_stacked.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ class RgbStackedT
5757
: RgbStackedT(rgb.getRed(), rgb.getGreen(), rgb.getBlue()) {}
5858

5959
void setRed(RedType red)
60-
{ value = (value & ~(bitmask<DR>() << (DG + DB))) | red.value << (DG + DB); }
60+
{ value = (value & ~(RedType::mask << (DG + DB))) | red.value << (DG + DB); }
6161
void setGreen(GreenType green)
62-
{ value = (value & ~(bitmask<DG>() << DB)) | green.value << DB; }
62+
{ value = (value & ~(GreenType::mask << DB)) | green.value << DB; }
6363
void setBlue(BlueType blue)
64-
{ value = (value & ~bitmask<DB>()) | blue.value; }
64+
{ value = (value & ~BlueType::mask) | blue.value; }
6565

6666
RedType getRed() const { return value >> (DG + DB); }
67-
GreenType getGreen() const { return value >> DB & bitmask<DG>(); }
68-
BlueType getBlue() const { return value & bitmask<DB>(); }
67+
GreenType getGreen() const { return value >> DB & GreenType::max;}
68+
BlueType getBlue() const { return value & BlueType::max; }
69+
70+
ValueType getValue() const { return value; }
6971

7072
template <typename RgbStackedT_>
7173
constexpr RgbStackedT operator+(RgbStackedT_ &other) {

src/modm/ui/graphic/accessor_image.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ImageAccessor<C, Accessor> : public Accessor<C>
8080
}
8181

8282
void
83-
nextRow()
83+
incrementRow()
8484
{
8585
addr_top += size.y;
8686
this->address = addr_top;

src/modm/ui/graphic/accessor_image_gray.hpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,52 @@ class ImageAccessor<G, Accessor> : public Accessor<typename G::ValueType>
5454
// # ImageAccessor for uncompressed monochrome images
5555
// ####################################################
5656

57-
void
57+
static constexpr int digits = numeric_limits<typename G::ValueType>::digits;
58+
59+
inline void
5860
initialize(const modm::shape::Point origin)
5961
{
6062
const Point topLeft = {origin.x < 0 ? -origin.x : 0, origin.y < 0 ? -origin.y : 0};
6163

62-
addr_top = this->address + (topLeft.y / 8) * size.x + topLeft.x;
63-
bitmask_top = std::rotl(Bit0, topLeft.y % 8 - 1);
64-
nextRow();
64+
addr_top = this->address + (topLeft.y * G::Digits / 8) * size.x + topLeft.x;
65+
rotl_top = topLeft.y * G::Digits % 8;
66+
incrementRow();
67+
}
68+
69+
inline void
70+
incrementRow()
71+
{
72+
rotl = rotl_top;
73+
this->address = addr_top++;
74+
// TODO I'm not sure about this but is propably required
75+
// if (rotl_top == digits)
76+
// this->address -= size.x;
77+
78+
byte = std::rotr(Accessor<uint8_t>::operator*(), rotl_top - G::Digits);
6579
}
6680

6781
inline G
6882
nextPixel()
6983
{
70-
bitmask = std::rotl(bitmask, 1);
71-
if (bitmask == Bit0)
84+
rotl += G::Digits;
85+
if (rotl > digits)
7286
{
7387
this->address += size.x;
7488
byte = Accessor<uint8_t>::operator*();
89+
rotl = G::Digits;
90+
} else {
91+
byte = std::rotr(byte, G::Digits);
7592
}
76-
return byte & bitmask;
77-
}
7893

79-
inline void
80-
nextRow()
81-
{
82-
bitmask = bitmask_top;
83-
this->address = addr_top++;
84-
if (bitmask == Bit7)
85-
this->address -= size.x;
86-
else
87-
byte = Accessor<uint8_t>::operator*();
94+
return byte & G::max;
8895
}
89-
9096
private:
91-
// config
97+
// init state
9298
const G::ValueType* addr_top;
93-
G::ValueType bitmask_top;
99+
int8_t rotl_top;
94100

95-
// cache
101+
// reader state
96102
G::ValueType byte;
97-
G::ValueType bitmask;
103+
int8_t rotl;
98104
};
99105
} // namespace modm::graphic

src/modm/ui/graphic/buffer.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,27 @@ class Buffer
134134
/**
135135
* Write colored Image
136136
*
137-
* @param decoder Image with Flash or Ram Accessor
137+
* @param accessor Image with Flash or Ram Accessor
138138
* @param origin Placement for the image
139139
*/
140140
template<Color C_, template<typename> class Accessor>
141141
void
142-
writeImage(ImageAccessor<C_, Accessor> decoder, Point origin = {0, 0});
142+
writeImage(ImageAccessor<C_, Accessor> accessor, Point origin = {0, 0});
143143

144144
/**
145145
* Write monochrome Image
146146
*
147-
* @param decoder Image with Flash or Ram Accessor
147+
* @param accessor Image with Flash or Ram Accessor
148148
* @param origin Placement for the image
149149
*/
150150
template<template<typename> class Accessor>
151151
void
152-
writeImage(ImageAccessor<Monochrome, Accessor> decoder, Point origin = {0, 0});
152+
writeImage(ImageAccessor<Monochrome, Accessor> accessor, Point origin = {0, 0});
153153

154154
/**
155155
* Write monochrome Glyph
156156
*
157-
* @param decoder Accessor for Image in Flash memory
157+
* @param accessor Accessor for Image in Flash memory
158158
*/
159159
void
160160
writeGlyph(ImageAccessor<Monochrome, modm::accessor::Flash> glyph) final {
@@ -184,4 +184,5 @@ class Buffer
184184
} // namespace modm
185185

186186
#include "buffer_impl.hpp"
187+
#include "buffer_draw_impl.hpp"
187188
#include "buffer_gray.hpp"

0 commit comments

Comments
 (0)