5fe1c3ad0926770050a1cb850542b2863ed01f55
[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 #ifndef NO_LCD
48         lcdfd = open("/dev/dbox/oled0", O_RDWR);
49         if (lcdfd < 0)
50         {
51                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
52                 is_oled = 0;
53         } else
54         {
55                 eDebug("found OLED display!");
56                 is_oled = 1;
57         }
58 #else
59         lcdfd = -1;
60 #endif
61         instance=this;
62
63         if (lcdfd<0)
64                 eDebug("couldn't open LCD - load lcd.o!");
65         else
66         {
67                 int i=LCD_MODE_BIN;
68                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
69                 inverted=0;
70         }
71 }
72
73 void eDBoxLCD::setInverted(unsigned char inv)
74 {
75         inverted=inv;
76         update();       
77 }
78
79 int eDBoxLCD::setLCDContrast(int contrast)
80 {
81         int fp;
82         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
83         {
84                 eDebug("[LCD] can't open /dev/dbox/fp0");
85                 return(-1);
86         }
87
88         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
89         {
90                 eDebug("[LCD] can't set lcd contrast");
91         }
92         close(fp);
93         return(0);
94 }
95
96 int eDBoxLCD::setLCDBrightness(int brightness)
97 {
98         eDebug("setLCDBrightness %d", brightness);
99         FILE *f=fopen("/proc/stb/fp/oled_brightness", "w");
100         if (f)
101         {
102                 if (fprintf(f, "%d", brightness) == 0)
103                         eDebug("write /proc/stb/fp/oled_brightness failed!! (%m)");
104                 fclose(f);
105         }
106         else
107         {
108                 int fp;
109                 if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
110                 {
111                         eDebug("[LCD] can't open /dev/dbox/fp0");
112                         return(-1);
113                 }
114
115                 if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
116                         eDebug("[LCD] can't set lcd brightness (%m)");
117                 close(fp);
118         }
119         return(0);
120 }
121
122 eDBoxLCD::~eDBoxLCD()
123 {
124         if (lcdfd>=0)
125         {
126                 close(lcdfd);
127                 lcdfd=-1;
128         }
129 }
130
131 eDBoxLCD *eDBoxLCD::getInstance()
132 {
133         return instance;
134 }
135
136 void eDBoxLCD::update()
137 {
138         if (!is_oled)
139         {
140                 unsigned char raw[132*8];
141                 int x, y, yy;
142                 for (y=0; y<8; y++)
143                 {
144                         for (x=0; x<132; x++)
145                         {
146                                 int pix=0;
147                                 for (yy=0; yy<8; yy++)
148                                 {
149                                         pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
150                                 }
151                                 raw[y*132+x]=(pix^inverted);
152                         }
153                 }
154                 if (lcdfd >= 0)
155                         write(lcdfd, raw, 132*8);
156         } else
157         {
158                 unsigned char raw[64*64];
159                 int x, y;
160                 memset(raw, 0, 64*64);
161                 for (y=0; y<64; y++)
162                 {
163                         for (x=0; x<128 / 2; x++)
164                                 raw[y*64+x] = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
165                 }
166                 if (lcdfd >= 0)
167                         write(lcdfd, raw, 64*64);
168         }
169 }
170