1 /*
2  * Copyright (c) 2015 Gurjant Kalsi <me@gurjantkalsi.com>
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 
9 // 1.33 Inch 3-Bit RGB Sharp Color LCD
10 #include <target/display/LS013B7DH06.h>
11 #include <string.h>
12 #include <assert.h>
13 
14 #define SET_BIT(BUF, BITNUM) ((BUF)[(BITNUM) >> 3] |= (0x1 << ((BITNUM) & 0x07)))
15 
lcd_get_line(struct display_image * image,uint8_t idx,uint8_t * result)16 uint8_t lcd_get_line(struct display_image *image, uint8_t idx, uint8_t *result) {
17     uint8_t *framebuffer = (uint8_t *)image->pixels + image->rowbytes * idx;
18 
19     memset(result, 0, MLCD_BYTES_LINE);
20 
21     if (image->format == IMAGE_FORMAT_RGB_332) {
22         for (int i = 0; i < MLCD_WIDTH; ++i) {
23             uint8_t inpix = framebuffer[i];
24 
25             int j = i * 3;
26 
27             if (inpix & 0x80) {
28                 SET_BIT(result, j);
29             }
30             if (inpix & 0x10) {
31                 SET_BIT(result, j + 1);
32             }
33             if (inpix & 0x02) {
34                 SET_BIT(result, j + 2);
35             }
36         }
37     } else if (image->format == IMAGE_FORMAT_RGB_x111) {
38         int j = 0;
39         for (uint i = 0; i < image->width; i += 2) {
40             uint8_t val = *framebuffer++;
41             uint8_t inpix = val & 0xf;
42 
43             if (inpix & 0x4) {
44                 SET_BIT(result, j);
45             }
46             if (inpix & 0x2) {
47                 SET_BIT(result, j + 1);
48             }
49             if (inpix & 0x1) {
50                 SET_BIT(result, j + 2);
51             }
52 
53             inpix = val >> 4;
54             if (inpix & 0x4) {
55                 SET_BIT(result, j + 3);
56             }
57             if (inpix & 0x2) {
58                 SET_BIT(result, j + 4);
59             }
60             if (inpix & 0x1) {
61                 SET_BIT(result, j + 5);
62             }
63             j += 6;
64         }
65         if (image->width & 1) {
66             uint8_t val = *framebuffer;
67             uint8_t inpix = val & 0xf;
68 
69             if (inpix & 0x4) {
70                 SET_BIT(result, j);
71             }
72             if (inpix & 0x2) {
73                 SET_BIT(result, j + 1);
74             }
75             if (inpix & 0x1) {
76                 SET_BIT(result, j + 2);
77             }
78         }
79     } else {
80         DEBUG_ASSERT(false);
81     }
82 
83     return MLCD_BYTES_LINE;
84 }
85