Skip to content

Commit 279b6ce

Browse files
committed
pbio/drv/display_ev3: Fix triplet encoding.
This was causing unintended aliasing issues. Fixes pybricks/support#2440
1 parent 8a53118 commit 279b6ce

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

lib/pbio/drv/display/display_ev3.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ static bool pbdrv_display_user_frame_update_requested;
144144
/**
145145
* Display buffer in the format ready for sending to the st7586s display driver.
146146
*
147-
* Three pixels are encoded in one byte as | A B C | A B C | A B
147+
* Three pixels are encoded in one byte as (MSB) | A B C | A B C | A B | (LSB)
148148
*
149149
* A B (C)
150150
* --------------------
151151
* 0 0 0 Empty
152-
* 1 0 0 Dark Grey
153152
* 0 1 0 Light Grey
153+
* 1 0 0 Dark Grey
154154
* 1 1 1 Black
155155
*
156156
* Column C is essentially redundant, but required for the first and second
@@ -175,16 +175,15 @@ static uint8_t st7586s_send_buf[ST7586S_NUM_COL_TRIPLETS * ST7586S_NUM_ROWS] __a
175175
* @return Encoded triplet.
176176
*/
177177
static uint8_t encode_triplet(uint8_t p0, uint8_t p1, uint8_t p2) {
178-
if (p0 >= 3) {
179-
p0 = 7;
180-
}
181-
if (p1 >= 3) {
182-
p1 = 7;
183-
}
184-
if (p2 >= 3) {
185-
p2 = 7;
186-
}
187-
return p0 << 6 | p1 << 3 | p2;
178+
// As described above, the first two pixels are the normal binary
179+
// representation shifted left by one, with an extra bit set for black.
180+
// The third pixel is not shifted, so contains just two bits.
181+
p0 = p0 >= 3 ? 0b111 : (p0 << 1);
182+
p1 = p1 >= 3 ? 0b111 : (p1 << 1);
183+
p2 = p2 >= 3 ? 0b11 : p2;
184+
185+
// Three pixels are then concatenated to one byte.
186+
return p0 << 5 | p1 << 2 | p2;
188187
}
189188

190189
/**

0 commit comments

Comments
 (0)