Merge branch 'bug_514_new_proc_oled_brightness_path'
[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(eSize size): res(size)
18 {
19         lcdfd = -1;
20         locked=0;
21         _buffer=new unsigned char[res.height()*res.width()];
22         memset(_buffer, 0, res.height()*res.width());
23         _stride=res.width();
24 }
25
26 eLCD::~eLCD()
27 {
28         delete [] _buffer;
29 }
30
31 int eLCD::lock()
32 {
33         if (locked)
34                 return -1;
35
36         locked=1;
37         return lcdfd;
38 }
39
40 void eLCD::unlock()
41 {
42         locked=0;
43 }
44
45 eDBoxLCD::eDBoxLCD(): eLCD(eSize(132, 64))
46 {
47         is_oled = 0;
48 #ifndef NO_LCD
49         lcdfd = open("/dev/dbox/oled0", O_RDWR);
50         if (lcdfd < 0)
51         {
52                 FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
53                 if (!f)
54                         f = fopen("/proc/stb/fp/oled_brightness", "w");
55                 if (f)
56                 {
57                         is_oled = 2;
58                         fclose(f);
59                 }
60                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
61         } else
62         {
63                 eDebug("found OLED display!");
64                 is_oled = 1;
65         }
66 #else
67         lcdfd = -1;
68 #endif
69         instance=this;
70
71         if (lcdfd<0)
72                 eDebug("couldn't open LCD - load lcd.o!");
73         else
74         {
75                 int i=LCD_MODE_BIN;
76                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
77                 inverted=0;
78         }
79 }
80
81 void eDBoxLCD::setInverted(unsigned char inv)
82 {
83         inverted=inv;
84         update();       
85 }
86
87 int eDBoxLCD::setLCDContrast(int contrast)
88 {
89         int fp;
90         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
91         {
92                 eDebug("[LCD] can't open /dev/dbox/fp0");
93                 return(-1);
94         }
95
96         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
97         {
98                 eDebug("[LCD] can't set lcd contrast");
99         }
100         close(fp);
101         return(0);
102 }
103
104 int eDBoxLCD::setLCDBrightness(int brightness)
105 {
106         eDebug("setLCDBrightness %d", brightness);
107         FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
108         if (!f)
109                 f = fopen("/proc/stb/fp/oled_brightness", "w");
110         if (f)
111         {
112                 if (fprintf(f, "%d", brightness) == 0)
113                         eDebug("write /proc/stb/lcd/oled_brightness failed!! (%m)");
114                 fclose(f);
115         }
116         else
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(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
126                         eDebug("[LCD] can't set lcd brightness (%m)");
127                 close(fp);
128         }
129         return(0);
130 }
131
132 eDBoxLCD::~eDBoxLCD()
133 {
134         if (lcdfd>=0)
135         {
136                 close(lcdfd);
137                 lcdfd=-1;
138         }
139 }
140
141 eDBoxLCD *eDBoxLCD::getInstance()
142 {
143         return instance;
144 }
145
146 void eDBoxLCD::update()
147 {
148         if (!is_oled || is_oled == 2)
149         {
150                 unsigned char raw[132*8];
151                 int x, y, yy;
152                 for (y=0; y<8; y++)
153                 {
154                         for (x=0; x<132; x++)
155                         {
156                                 int pix=0;
157                                 for (yy=0; yy<8; yy++)
158                                 {
159                                         pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
160                                 }
161                                 raw[y*132+x]=(pix^inverted);
162                         }
163                 }
164                 if (lcdfd >= 0)
165                         write(lcdfd, raw, 132*8);
166         } else
167         {
168                 unsigned char raw[64*64];
169                 int x, y;
170                 memset(raw, 0, 64*64);
171                 for (y=0; y<64; y++)
172                 {
173                         int pix=0;
174                         for (x=0; x<128 / 2; x++)
175                         {
176                                 pix = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
177                                 if (inverted)
178                                         pix = 0xFF - pix;
179                                 raw[y*64+x] = pix;
180                         }
181                 }
182                 if (lcdfd >= 0)
183                         write(lcdfd, raw, 64*64);
184         }
185 }
186