add support for rbg565 color oled
[openblackhole/openblackhole-enigma2.git] / lib / gdi / lcd.cpp
1 #include <lib/gdi/lcd.h>
2
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6
7 #include <dbox/fp.h>
8 #include <dbox/lcd-ks0713.h>
9
10 #include <lib/gdi/esize.h>
11 #include <lib/base/init.h>
12 #include <lib/base/init_num.h>
13 #include <lib/gdi/glcddc.h>
14
15 eDBoxLCD *eDBoxLCD::instance;
16
17 eLCD::eLCD()
18 {
19         lcdfd = -1;
20         locked=0;
21 }
22
23 void eLCD::setSize(int xres, int yres, int bpp)
24 {
25         res = eSize(xres, yres);
26         _buffer=new unsigned char[xres * yres * bpp/8];
27         memset(_buffer, 0, res.height()*res.width()*bpp/8);
28         _stride=res.width()*bpp/8;
29         eDebug("lcd buffer %p %d bytes, stride %d", _buffer, xres*yres*bpp/8, _stride);
30 }
31
32 eLCD::~eLCD()
33 {
34         delete [] _buffer;
35 }
36
37 int eLCD::lock()
38 {
39         if (locked)
40                 return -1;
41
42         locked=1;
43         return lcdfd;
44 }
45
46 void eLCD::unlock()
47 {
48         locked=0;
49 }
50
51 eDBoxLCD::eDBoxLCD()
52 {
53         int xres=132, yres=64, bpp=8;
54         is_oled = 0;
55 #ifndef NO_LCD
56         lcdfd = open("/dev/dbox/oled0", O_RDWR);
57         if (lcdfd < 0)
58         {
59                 FILE *f=fopen("/proc/stb/fp/oled_brightness", "w");
60                 if (f)
61                 {
62                         is_oled = 2;
63                         fclose(f);
64                 }
65                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
66         } else
67         {
68                 eDebug("found OLED display!");
69                 is_oled = 1;
70         }
71 #else
72         lcdfd = -1;
73 #endif
74         instance=this;
75
76         if (lcdfd<0)
77                 eDebug("couldn't open LCD - load lcd.o!");
78         else
79         {
80                 int i=LCD_MODE_BIN;
81                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
82                 inverted=0;
83                 FILE *f = fopen("/proc/stb/lcd/xres", "r");
84                 if (f)
85                 {
86                         int tmp;
87                         if (fscanf(f, "%x", &tmp) == 1)
88                                 xres = tmp;
89                         fclose(f);
90                         f = fopen("/proc/stb/lcd/yres", "r");
91                         if (f)
92                         {
93                                 if (fscanf(f, "%x", &tmp) == 1)
94                                         yres = tmp;
95                                 fclose(f);
96                                 f = fopen("/proc/stb/lcd/bpp", "r");
97                                 if (f)
98                                 {
99                                         if (fscanf(f, "%x", &tmp) == 1)
100                                                 bpp = tmp;
101                                         fclose(f);
102                                 }
103                         }
104                         is_oled = 3;
105                 }
106         }
107         setSize(xres, yres, bpp);
108 }
109
110 void eDBoxLCD::setInverted(unsigned char inv)
111 {
112         inverted=inv;
113         update();
114 }
115
116 int eDBoxLCD::setLCDContrast(int contrast)
117 {
118         int fp;
119         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
120         {
121                 eDebug("[LCD] can't open /dev/dbox/fp0");
122                 return(-1);
123         }
124
125         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
126         {
127                 eDebug("[LCD] can't set lcd contrast");
128         }
129         close(fp);
130         return(0);
131 }
132
133 int eDBoxLCD::setLCDBrightness(int brightness)
134 {
135         eDebug("setLCDBrightness %d", brightness);
136         FILE *f=fopen("/proc/stb/fp/oled_brightness", "w");
137         if (f)
138         {
139                 if (fprintf(f, "%d", brightness) == 0)
140                         eDebug("write /proc/stb/fp/oled_brightness failed!! (%m)");
141                 fclose(f);
142         }
143         else
144         {
145                 int fp;
146                 if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
147                 {
148                         eDebug("[LCD] can't open /dev/dbox/fp0");
149                         return(-1);
150                 }
151
152                 if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
153                         eDebug("[LCD] can't set lcd brightness (%m)");
154                 close(fp);
155         }
156         return(0);
157 }
158
159 eDBoxLCD::~eDBoxLCD()
160 {
161         if (lcdfd>=0)
162         {
163                 close(lcdfd);
164                 lcdfd=-1;
165         }
166 }
167
168 eDBoxLCD *eDBoxLCD::getInstance()
169 {
170         return instance;
171 }
172
173 void eDBoxLCD::update()
174 {
175         if (lcdfd >= 0)
176         {
177                 if (!is_oled || is_oled == 2)
178                 {
179                         unsigned char raw[132*8];
180                         int x, y, yy;
181                         for (y=0; y<8; y++)
182                         {
183                                 for (x=0; x<132; x++)
184                                 {
185                                         int pix=0;
186                                         for (yy=0; yy<8; yy++)
187                                         {
188                                                 pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
189                                         }
190                                         raw[y*132+x]=(pix^inverted);
191                                 }
192                         }
193                         write(lcdfd, raw, 132*8);
194                 }
195                 else if (is_oled == 3) {
196                         FILE *f = fopen("/tmp/bla", "w+");
197                         if (f) {
198                                 fwrite(_buffer, _stride * res.height(), 1, f);
199                                 fclose(f);
200                         }
201                         write(lcdfd, _buffer, _stride * res.height());
202                 }
203                 else
204                 {
205                         unsigned char raw[64*64];
206                         int x, y;
207                         memset(raw, 0, 64*64);
208                         for (y=0; y<64; y++)
209                         {
210                                 int pix=0;
211                                 for (x=0; x<128 / 2; x++)
212                                 {
213                                         pix = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
214                                         if (inverted)
215                                                 pix = 0xFF - pix;
216                                         raw[y*64+x] = pix;
217                                 }
218                         }
219                         write(lcdfd, raw, 64*64);
220                 }
221         }
222 }
223