forked from adafruit/Adafruit_SHARP_Memory_Display
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_SharpMem.cpp
745 lines (682 loc) · 21.4 KB
/
Adafruit_SharpMem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*********************************************************************
This is an Arduino library for our Monochrome SHARP Memory Displays
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/1393
These displays use SPI to communicate, 3 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/
#include "Adafruit_SharpMem.h"
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) \
{ \
int16_t t = a; \
a = b; \
b = t; \
}
#endif
#ifndef _swap_uint16_t
#define _swap_uint16_t(a, b) \
{ \
uint16_t t = a; \
a = b; \
b = t; \
}
#endif
/**************************************************************************
Sharp Memory Display Connector
-----------------------------------------------------------------------
Pin Function Notes
=== ============== ===============================
1 VIN 3.3-5.0V (into LDO supply)
2 3V3 3.3V out
3 GND
4 SCLK Serial Clock
5 MOSI Serial Data Input
6 CS Serial Chip Select
9 EXTMODE COM Inversion Select (Low = SW clock/serial)
7 EXTCOMIN External COM Inversion Signal
8 DISP Display On(High)/Off(Low)
**************************************************************************/
#define TOGGLE_VCOM \
do { \
_sharpmem_vcom = _sharpmem_vcom ? 0x00 : SHARPMEM_BIT_VCOM; \
} while (0);
/**
* @brief Construct a new Adafruit_SharpMem object with software SPI
*
* @param clk The clock pin
* @param mosi The MOSI pin
* @param cs The display chip select pin - **NOTE** this is ACTIVE HIGH!
* @param width The display width
* @param height The display height
* @param freq The SPI clock frequency desired (unlikely to be that fast in soft
* spi mode!)
*/
Adafruit_SharpMem::Adafruit_SharpMem(uint8_t clk, uint8_t mosi, uint8_t cs,
uint16_t width, uint16_t height,
uint32_t freq)
: Adafruit_GFX(width, height) {
_cs = cs;
if (spidev) {
delete spidev;
}
spidev =
new Adafruit_SPIDevice(cs, clk, -1, mosi, freq, SPI_BITORDER_LSBFIRST);
}
/**
* @brief Construct a new Adafruit_SharpMem object with hardware SPI
*
* @param theSPI Pointer to hardware SPI device you want to use
* @param cs The display chip select pin - **NOTE** this is ACTIVE HIGH!
* @param width The display width
* @param height The display height
* @param freq The SPI clock frequency desired
*/
Adafruit_SharpMem::Adafruit_SharpMem(SPIClass *theSPI, uint8_t cs,
uint16_t width, uint16_t height,
uint32_t freq)
: Adafruit_GFX(width, height) {
_cs = cs;
if (spidev) {
delete spidev;
}
spidev = new Adafruit_SPIDevice(cs, freq, SPI_BITORDER_LSBFIRST, SPI_MODE0,
theSPI);
}
/**
* @brief Start the driver object, setting up pins and configuring a buffer for
* the screen contents
*
* @return boolean true: success false: failure
*/
bool Adafruit_SharpMem::begin(void) {
if (!spidev->begin()) {
return false;
}
// this display is weird in that _cs is active HIGH not LOW like every other
// SPI device
digitalWrite(_cs, LOW);
// Set the vcom bit to a defined state
_sharpmem_vcom = SHARPMEM_BIT_VCOM;
sharpmem_buffer = (uint8_t *)malloc((WIDTH * HEIGHT) / 8);
if (!sharpmem_buffer)
return false;
setRotation(0);
return true;
}
// 1<<n is a costly operation on AVR -- table usu. smaller & faster
static const uint8_t set[] = {1, 2, 4, 8, 16, 32, 64, 128},
clr[] = {(uint8_t)~1, (uint8_t)~2, (uint8_t)~4,
(uint8_t)~8, (uint8_t)~16, (uint8_t)~32,
(uint8_t)~64, (uint8_t)~128};
/**************************************************************************/
/*!
@brief Draws a single pixel in image buffer
@param[in] x
The x position (0 based)
@param[in] y
The y position (0 based)
@param color The color to set:
* **0**: Black
* **1**: White
*/
/**************************************************************************/
void Adafruit_SharpMem::drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((x < 0) || (x >= _width) || (y < 0) || (y >= _height))
return;
switch (rotation) {
case 1:
_swap_int16_t(x, y);
x = WIDTH - 1 - x;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
_swap_int16_t(x, y);
y = HEIGHT - 1 - y;
break;
}
switch (color) {
case 1: // WHITE white
// sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
sharpmem_buffer[(x / 8) + y * ((WIDTH + 7) / 8)] |= set[x & 7];
break;
default:
case 0: // BLACK black
// sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
sharpmem_buffer[(x / 8) + y * ((WIDTH + 7) / 8)] &= clr[x & 7];
break;
case 7:
// line pattern reversed
if (y % 3 == 2 - (x % 3)) {
sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
} else {
sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
}
break;
case 6:
// line pattern
if (y % 3 == x % 3) {
sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
} else {
sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
}
break;
case 5: // PATTERN
// V1
if ((y % 4 == 0 && x % 4 == 2) || // line 0
(x % 2 == 1 && y % 2 == 1) || // line 1 & 3
(y % 4 == 2 && x % 4 == 0) // line 2
) {
// black
sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
} else {
sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
}
break;
case 4: // LIGHT lighter gray
if (y % 2 != 0) {
sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
} else if ((x + 2 * ((y / 2) % 2)) % 4 == 0) { // on
sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
} else {
sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
}
break;
case 3: // DARK darker gray
if (y % 2 != 0) { // off
sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
} else if ((x + 2 * ((y / 2) % 2)) % 4 == 0) { // on
sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
} else {
sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
}
break;
case 2: // GRAY medium gray
if (((x + y) % 2 == 0)) {
sharpmem_buffer[(y * WIDTH + x) / 8] |= set[x & 7];
} else {
sharpmem_buffer[(y * WIDTH + x) / 8] &= clr[x & 7];
}
break;
}
}
/**************************************************************************/
/*!
@brief Gets the value (1 or 0) of the specified pixel from the buffer
@param[in] x
The x position (0 based)
@param[in] y
The y position (0 based)
@return 1 if the pixel is enabled, 0 if disabled
*/
/**************************************************************************/
uint8_t Adafruit_SharpMem::getPixel(uint16_t x, uint16_t y) {
if ((x >= _width) || (y >= _height))
return 0; // <0 test not needed, unsigned
switch (rotation) {
case 1:
_swap_uint16_t(x, y);
x = WIDTH - 1 - x;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
_swap_uint16_t(x, y);
y = HEIGHT - 1 - y;
break;
}
return sharpmem_buffer[(y * WIDTH + x) / 8] & set[x & 7] ? 1 : 0;
}
/**************************************************************************/
/*!
@brief Draw a fat line
*/
/**************************************************************************/
void Adafruit_SharpMem::drawFatLine(int16_t x0, int16_t y0, // first point
int16_t x1, int16_t y1, // second point
int16_t strokeWidth, // stroke width
uint16_t color) {
if (strokeWidth < 1) {
return;
}
// create perpendicular vector
float px = y1 - y0;
float py = -(x1 - x0);
// calculate length to normalize perpendicular vector
float l = sqrt(px * px + py * py);
if (l < 1) {
// do not divide by zero
// do not draw a line too short
return;
}
// normalize and scale to strokewidth
px = (float)strokeWidth * px / l;
py = (float)strokeWidth * py / l;
// finally draw our line!
fillTriangle(x0 + (int)px, y0 + (int)py, // a
x1 + (int)px, y1 + (int)py, // b
x1 - (int)px, y1 - (int)py, // c
color);
fillTriangle(x0 + (int)px, y0 + (int)py, // a
x1 - (int)px, y1 - (int)py, // c
x0 - (int)px, y0 - (int)py, // d
color);
}
/**************************************************************************/
/*!
@brief Clears the screen
*/
/**************************************************************************/
void Adafruit_SharpMem::clearDisplay() {
memset(sharpmem_buffer, 0xff, (WIDTH * HEIGHT) / 8);
spidev->beginTransaction();
// Send the clear screen command rather than doing a HW refresh (quicker)
digitalWrite(_cs, HIGH);
uint8_t clear_data[2] = {(uint8_t)(_sharpmem_vcom | SHARPMEM_BIT_CLEAR),
0x00};
spidev->transfer(clear_data, 2);
TOGGLE_VCOM;
digitalWrite(_cs, LOW);
spidev->endTransaction();
}
/**************************************************************************/
/*!
@brief Renders the contents of the pixel buffer on the LCD
*/
/**************************************************************************/
void Adafruit_SharpMem::refresh(void) {
uint16_t i, currentline;
spidev->beginTransaction();
// Send the write command
digitalWrite(_cs, HIGH);
spidev->transfer(_sharpmem_vcom | SHARPMEM_BIT_WRITECMD);
TOGGLE_VCOM;
uint8_t bytes_per_line = WIDTH / 8;
uint16_t totalbytes = (WIDTH * HEIGHT) / 8;
for (i = 0; i < totalbytes; i += bytes_per_line) {
uint8_t line[bytes_per_line + 2];
// Send address byte
currentline = ((i + 1) / (WIDTH / 8)) + 1;
line[0] = currentline;
// copy over this line
memcpy(line + 1, sharpmem_buffer + i, bytes_per_line);
// Send end of line
line[bytes_per_line + 1] = 0x00;
// send it!
spidev->transfer(line, bytes_per_line + 2);
}
// Send another trailing 8 bits for the last line
spidev->transfer(0x00);
digitalWrite(_cs, LOW);
spidev->endTransaction();
}
/**************************************************************************/
/*!
@brief Clears the display buffer without outputting to the display
*/
/**************************************************************************/
void Adafruit_SharpMem::clearDisplayBuffer() {
memset(sharpmem_buffer, 0xff, (WIDTH * HEIGHT) / 8);
}
/**************************************************************************/
/*!
@brief access to the raw display buffer
*/
/**************************************************************************/
void Adafruit_SharpMem::copyPixelBuffer(uint8_t *bitmap) {
memcpy(bitmap, sharpmem_buffer, (WIDTH * HEIGHT) / 8);
}
/**************************************************************************/
/*!
@brief fills the display buffer with contents of bitmap without outputting
to the display
*/
/**************************************************************************/
void Adafruit_SharpMem::setBitmap(uint8_t *bitmap) {
memcpy(sharpmem_buffer, bitmap, (WIDTH * HEIGHT) / 8);
}
/**************************************************************************/
void Adafruit_SharpMem::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
for (int16_t i = y; i < y + h; i++) {
drawFastHLine(x, i, w, color);
}
}
/**************************************************************************/
void Adafruit_SharpMem::drawFastHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
if (w < 0) { // Convert negative widths to positive equivalent
w *= -1;
x -= w - 1;
if (x < 0) {
w += x;
x = 0;
}
}
// Edge rejection (no-draw if totally off canvas)
if ((y < 0) || (y >= height()) || (x >= width()) || ((x + w - 1) < 0)) {
return;
}
if (x < 0) { // Clip left
w += x;
x = 0;
}
if (x + w >= width()) { // Clip right
w = width() - x;
}
if (getRotation() == 0) {
drawFastRawHLine(x, y, w, color);
} else if (getRotation() == 1) {
int16_t t = x;
x = WIDTH - 1 - y;
y = t;
drawFastRawVLine(x, y, w, color);
} else if (getRotation() == 2) {
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
x -= w - 1;
drawFastRawHLine(x, y, w, color);
} else if (getRotation() == 3) {
int16_t t = x;
x = y;
y = HEIGHT - 1 - t;
y -= w - 1;
drawFastRawVLine(x, y, w, color);
}
}
/**************************************************************************/
void Adafruit_SharpMem::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
int16_t row_bytes = ((WIDTH + 7) / 8);
uint8_t *ptr = &sharpmem_buffer[(x / 8) + y * row_bytes];
if (color > 0) {
uint8_t bit_mask = set[x & 7]; // CHANGED
if (color == 2) { // GRAY
if (y % 2 == 0) {
bit_mask &= 0xAA;
} else {
bit_mask &= 0x55;
}
}
for (int16_t i = 0; i < h; i++) {
*ptr |= bit_mask;
ptr += row_bytes;
}
} else { // BLACK
uint8_t bit_mask = clr[x & 7]; // CHANGED
for (int16_t i = 0; i < h; i++) {
*ptr &= bit_mask;
ptr += row_bytes;
}
}
}
/**************************************************************************/
void Adafruit_SharpMem::drawFastRawHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
int16_t rowBytes = ((WIDTH + 7) / 8);
uint8_t *ptr = &sharpmem_buffer[(x / 8) + y * rowBytes];
size_t remainingWidthBits = w;
// check to see if first byte needs to be partially filled
if ((x & 7) > 0) {
// create bit mask for first byte
uint8_t startByteBitMask = 0x00;
for (int8_t i = (x & 7); ((i < 8) && (remainingWidthBits > 0)); i++) {
startByteBitMask |= set[i]; // CHANGED
remainingWidthBits--;
}
if (color == 7) {
uint8_t pattern = 0x00;
switch (y % 4) {
case 0:
pattern = 0x77;
break;
case 1:
pattern = 0xBB;
break;
case 2:
pattern = 0xDD;
break;
case 3:
pattern = 0xEE;
break;
}
*ptr &= ~(startByteBitMask & ~pattern);
*ptr |= startByteBitMask & pattern;
} else if (color == 6) {
uint8_t pattern = 0x00;
switch (y % 4) {
case 0:
pattern = 0xEE;
break;
case 1:
pattern = 0xDD;
break;
case 2:
pattern = 0xBB;
break;
case 3:
pattern = 0x77;
break;
}
*ptr &= ~(startByteBitMask & ~pattern);
*ptr |= startByteBitMask & pattern;
} else if (color == 5) {
uint8_t pattern = 0x00;
switch (y % 4) {
case 0: // 0x11
pattern = 0xEE;
break;
case 1: // 0x22
pattern = 0x55;
break;
case 2: // 0xBB
pattern = 0xBB;
break;
case 3: // 0x88
pattern = 0x55;
break;
}
*ptr &= ~(startByteBitMask & ~pattern);
*ptr |= startByteBitMask & pattern;
} else if (color == 4) { // LIGHT GRAY
if (y % 2 != 0) { // off
*ptr |= startByteBitMask;
} else { // on every other pixel
*ptr &= ~(startByteBitMask & ~0x55);
*ptr |= startByteBitMask & 0x55;
}
} else if (color == 3) { // DARK GRAY
if (y % 2 != 0) { // off
*ptr &= ~startByteBitMask;
} else { // on every other pixel
*ptr &= ~(startByteBitMask & ~0xAA);
*ptr |= startByteBitMask & 0xAA;
}
} else if (color == 2) { // GRAY
if (y % 2 == 0) {
*ptr &= ~(startByteBitMask & ~0xAA);
*ptr |= startByteBitMask & 0xAA;
} else {
*ptr &= ~(startByteBitMask & ~0x55);
*ptr |= startByteBitMask & 0x55;
}
} else if (color == 1) { // white
*ptr |= startByteBitMask;
} else { // black
*ptr &= ~startByteBitMask;
}
ptr++;
}
// do the next remainingWidthBits bits
if (remainingWidthBits > 0) {
size_t remainingWholeBytes = remainingWidthBits / 8;
size_t lastByteBits = remainingWidthBits % 8;
uint8_t wholeByteColor = color > 0 ? 0xFF : 0x00;
if (color == 7) {
switch (y % 4) {
case 0:
wholeByteColor = 0x77;
break;
case 1:
wholeByteColor = 0xBB;
break;
case 2:
wholeByteColor = 0xDD;
break;
case 3:
wholeByteColor = 0xEE;
break;
}
} else if (color == 6) {
switch (y % 4) {
case 0:
wholeByteColor = 0xEE;
break;
case 1:
wholeByteColor = 0xDD;
break;
case 2:
wholeByteColor = 0xBB;
break;
case 3:
wholeByteColor = 0x77;
break;
}
} else if (color == 5) {
switch (y % 4) {
case 0: // 0x11
wholeByteColor = 0xEE;
break;
case 1: // 0x22
wholeByteColor = 0x55;
break;
case 2: // 0xBB
wholeByteColor = 0xBB;
break;
case 3: // 0x88
wholeByteColor = 0x55;
break;
}
} else if (color == 4) {
if (y % 2 != 0) { // off
wholeByteColor = 0xFF;
} else { // on every other pixel
wholeByteColor = 0x55;
}
} else if (color == 3) { // DARK GRAY
if (y % 2 != 0) { // off
wholeByteColor = 0x00;
} else { // on every other pixel
wholeByteColor = 0xAA;
}
} else if (color == 2) { // GRAY
if (y % 2 == 0) {
wholeByteColor = 0xAA;
} else {
wholeByteColor = 0x55;
}
}
memset(ptr, wholeByteColor, remainingWholeBytes);
if (lastByteBits > 0) {
uint8_t lastByteBitMask = 0x00;
for (size_t i = 0; i < lastByteBits; i++) {
lastByteBitMask |= set[i]; // CHANGED
}
ptr += remainingWholeBytes;
if (color == 7) {
uint8_t pattern = 0x00;
switch (y % 4) {
case 0:
pattern = 0x77;
break;
case 1:
pattern = 0xBB;
break;
case 2:
pattern = 0xDD;
break;
case 3:
pattern = 0xEE;
break;
}
*ptr &= ~(lastByteBitMask & ~pattern);
*ptr |= lastByteBitMask & pattern;
} else if (color == 6) {
uint8_t pattern = 0x00;
switch (y % 4) {
case 0:
pattern = 0xEE;
break;
case 1:
pattern = 0xDD;
break;
case 2:
pattern = 0xBB;
break;
case 3:
pattern = 0x77;
break;
}
*ptr &= ~(lastByteBitMask & ~pattern);
*ptr |= lastByteBitMask & pattern;
} else if (color == 5) {
uint8_t pattern = 0x00;
switch (y % 4) {
case 0: // 0x11
pattern = 0xEE;
break;
case 1: // 0x22
pattern = 0x55;
break;
case 2: // 0xBB
pattern = 0xBB;
break;
case 3: // 0x88
pattern = 0x55;
break;
}
*ptr &= ~(lastByteBitMask & ~pattern);
*ptr |= lastByteBitMask & pattern;
} else if (color == 4) { // LIGHT GRAY
if (y % 2 != 0) { // off
*ptr |= lastByteBitMask;
} else { // on every other pixel
*ptr &= ~(lastByteBitMask & ~0x55);
*ptr |= lastByteBitMask & 0x55;
}
} else if (color == 3) { // DARK GRAY
if (y % 2 != 0) { // off
*ptr &= ~lastByteBitMask;
} else { // on every other pixel
*ptr &= ~(lastByteBitMask & ~0xAA);
*ptr |= lastByteBitMask & 0xAA;
}
} else if (color == 2) {
if (y % 2 == 0) {
*ptr &= ~(lastByteBitMask & ~0xAA);
*ptr |= lastByteBitMask & 0xAA;
} else {
*ptr &= ~(lastByteBitMask & ~0x55); // black pixels
*ptr |= lastByteBitMask & 0x55; // white pixels
}
} else if (color == 1) {
*ptr |= lastByteBitMask;
} else {
*ptr &= ~lastByteBitMask;
}
}
}
}