LED Brightness Setup : remove 'brightness at standby menu' and append setLEDDefault
[vuplus_dvbapp] / 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 #if defined(HAVE_DBOX_FP_H) && defined(HAVE_DBOX_LCD_KS0713_H)
8 #include <dbox/fp.h>
9 #include <dbox/lcd-ks0713.h>
10 #else
11 #define NO_LCD 1
12 #endif
13
14 #include <lib/gdi/esize.h>
15 #include <lib/base/init.h>
16 #include <lib/base/init_num.h>
17 #include <lib/gdi/glcddc.h>
18
19 eDBoxLCD *eDBoxLCD::instance;
20
21 eLCD::eLCD()
22 {
23         lcdfd = -1;
24         locked=0;
25 }
26
27 void eLCD::setSize(int xres, int yres, int bpp)
28 {
29         res = eSize(xres, yres);
30         _buffer=new unsigned char[xres * yres * bpp/8];
31         memset(_buffer, 0, res.height()*res.width()*bpp/8);
32         _stride=res.width()*bpp/8;
33         eDebug("lcd buffer %p %d bytes, stride %d", _buffer, xres*yres*bpp/8, _stride);
34 }
35
36 eLCD::~eLCD()
37 {
38         delete [] _buffer;
39 }
40
41 int eLCD::lock()
42 {
43         if (locked)
44                 return -1;
45
46         locked=1;
47         return lcdfd;
48 }
49
50 void eLCD::unlock()
51 {
52         locked=0;
53 }
54
55 eDBoxLCD::eDBoxLCD()
56 {
57         int xres=132, yres=64, bpp=8;
58         is_oled = 0;
59 #ifndef NO_LCD
60         lcdfd = open("/dev/dbox/oled0", O_RDWR);
61         if (lcdfd < 0)
62         {
63                 FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
64                 if (!f)
65                         f = fopen("/proc/stb/fp/oled_brightness", "w");
66                 if (f)
67                 {
68                         is_oled = 2;
69                         fclose(f);
70                 }
71                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
72         } else
73         {
74                 eDebug("found OLED display!");
75                 is_oled = 1;
76         }
77
78         if (lcdfd<0)
79                 eDebug("couldn't open LCD - load lcd.o!");
80         else
81         {
82                 int i=LCD_MODE_BIN;
83                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
84                 inverted=0;
85                 FILE *f = fopen("/proc/stb/lcd/xres", "r");
86                 if (f)
87                 {
88                         int tmp;
89                         if (fscanf(f, "%x", &tmp) == 1)
90                                 xres = tmp;
91                         fclose(f);
92                         f = fopen("/proc/stb/lcd/yres", "r");
93                         if (f)
94                         {
95                                 if (fscanf(f, "%x", &tmp) == 1)
96                                         yres = tmp;
97                                 fclose(f);
98                                 f = fopen("/proc/stb/lcd/bpp", "r");
99                                 if (f)
100                                 {
101                                         if (fscanf(f, "%x", &tmp) == 1)
102                                                 bpp = tmp;
103                                         fclose(f);
104                                 }
105                         }
106                         is_oled = 3;
107                 }
108         }
109 #endif
110 #ifdef SET_RIGHT_HALF_VFD_SKIN
111         fprintf(stdout,"SET RIGHT HALF VFD SKIN\n");
112         FILE *f = fopen("/proc/stb/lcd/right_half", "w");
113         fprintf(f,"skin");
114         fclose(f);
115 #endif
116         instance=this;
117
118         setSize(xres, yres, bpp);
119 }
120
121 void eDBoxLCD::setInverted(unsigned char inv)
122 {
123         inverted=inv;
124         update();
125 }
126
127 void eDBoxLCD::setLED(int value, int option)
128 {
129         switch(option)
130         {
131                 case LED_BRIGHTNESS:
132                         ioctl(lcdfd, LED_IOCTL_BRIGHTNESS_NORMAL, (unsigned char)value);
133                         break;
134                 case LED_DEEPSTANDBY:
135                         ioctl(lcdfd, LED_IOCTL_BRIGHTNESS_DEEPSTANDBY, (unsigned char)value);
136                         break;
137                 case LED_BLINKINGTIME:
138                         ioctl(lcdfd, LED_IOCTL_BLINKING_TIME, (unsigned char)value);
139                         break;
140         }
141 }
142
143 void eDBoxLCD::setLEDDefault(int normal, int deepstandby, int blinktime)
144 {
145         int value = (blinktime << 16) + (deepstandby << 8) + normal;
146         ioctl(lcdfd, LED_IOCTL_SET_DEFAULT, value);
147 }
148
149 int eDBoxLCD::setLCDContrast(int contrast)
150 {
151 #ifndef NO_LCD
152         int fp;
153         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
154         {
155                 eDebug("[LCD] can't open /dev/dbox/fp0");
156                 return(-1);
157         }
158
159         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
160         {
161                 eDebug("[LCD] can't set lcd contrast");
162         }
163         close(fp);
164 #endif
165         return(0);
166 }
167
168 int eDBoxLCD::setLCDBrightness(int brightness)
169 {
170 #ifndef NO_LCD
171         eDebug("setLCDBrightness %d", brightness);
172         FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
173         if (!f)
174                 f = fopen("/proc/stb/fp/oled_brightness", "w");
175         if (f)
176         {
177                 if (fprintf(f, "%d", brightness) == 0)
178                         eDebug("write /proc/stb/lcd/oled_brightness failed!! (%m)");
179                 fclose(f);
180         }
181         else
182         {
183                 int fp;
184                 if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
185                 {
186                         eDebug("[LCD] can't open /dev/dbox/fp0");
187                         return(-1);
188                 }
189
190                 if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
191                         eDebug("[LCD] can't set lcd brightness (%m)");
192                 close(fp);
193         }
194 #endif
195         return(0);
196 }
197
198 eDBoxLCD::~eDBoxLCD()
199 {
200         if (lcdfd>=0)
201         {
202                 close(lcdfd);
203                 lcdfd=-1;
204         }
205 }
206
207 eDBoxLCD *eDBoxLCD::getInstance()
208 {
209         return instance;
210 }
211
212 void eDBoxLCD::update()
213 {
214 #ifdef BUILD_VUPLUS /* ikseong  */
215         return ;
216 #endif
217         if (lcdfd >= 0)
218         {
219                 if (!is_oled || is_oled == 2)
220                 {
221                         unsigned char raw[132*8];
222                         int x, y, yy;
223                         for (y=0; y<8; y++)
224                         {
225                                 for (x=0; x<132; x++)
226                                 {
227                                         int pix=0;
228                                         for (yy=0; yy<8; yy++)
229                                         {
230                                                 pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
231                                         }
232                                         raw[y*132+x]=(pix^inverted);
233                                 }
234                         }
235                         write(lcdfd, raw, 132*8);
236                 }
237                 else if (is_oled == 3)
238                         write(lcdfd, _buffer, _stride * res.height());
239                 else
240                 {
241                         unsigned char raw[64*64];
242                         int x, y;
243                         memset(raw, 0, 64*64);
244                         for (y=0; y<64; y++)
245                         {
246                                 int pix=0;
247                                 for (x=0; x<128 / 2; x++)
248                                 {
249                                         pix = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
250                                         if (inverted)
251                                                 pix = 0xFF - pix;
252                                         raw[y*64+x] = pix;
253                                 }
254                         }
255                         write(lcdfd, raw, 64*64);
256                 }
257         }
258 }
259