fix typo
[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         locked=0;
20         _buffer=new unsigned char[res.height()*res.width()];
21         memset(_buffer, 0, res.height()*res.width());
22         _stride=res.width();
23 }
24
25 eLCD::~eLCD()
26 {
27         delete [] _buffer;
28 }
29
30 int eLCD::lock()
31 {
32         if (locked)
33                 return -1;
34
35         locked=1;
36         return lcdfd;
37 }
38
39 void eLCD::unlock()
40 {
41         read( lcdfd, NULL, 0);
42         if ( errno == 9 )
43         {
44                 eDebug("reopen lcd");
45                 lcdfd=open("/dev/dbox/lcd0", O_RDWR);  // reopen device
46         }
47         else
48                 eDebug("do not reopen lcd.. errno = %d", errno);
49     
50         locked=0;
51 }
52
53 eDBoxLCD::eDBoxLCD(): eLCD(eSize(132, 64))
54 {
55 #ifndef NO_LCD
56         lcdfd = open("/dev/dbox/oled0", O_RDWR);
57         if (lcdfd < 0)
58         {
59                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
60                 is_oled = 0;
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         int fp;
107         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
108         {
109                 eDebug("[LCD] can't open /dev/dbox/fp0");
110                 return(-1);
111         }
112
113         if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness))
114         {
115                 eDebug("[LCD] can't set lcd brightness");
116         }
117         close(fp);
118         return(0);
119 }
120
121 eDBoxLCD::~eDBoxLCD()
122 {
123         if (lcdfd>0)
124         {
125                 close(lcdfd);
126                 lcdfd=0;
127         }
128 }
129
130 eDBoxLCD *eDBoxLCD::getInstance()
131 {
132         return instance;
133 }
134
135 void eDBoxLCD::update()
136 {
137         if (!is_oled)
138         {
139                 unsigned char raw[132*8];
140                 int x, y, yy;
141                 for (y=0; y<8; y++)
142                 {
143                         for (x=0; x<132; x++)
144                         {
145                                 int pix=0;
146                                 for (yy=0; yy<8; yy++)
147                                 {
148                                         pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
149                                 }
150                                 raw[y*132+x]=(pix^inverted);
151                         }
152                 }
153                 if (lcdfd>0)
154                         write(lcdfd, raw, 132*8);
155         } else
156         {
157                 unsigned char raw[64*64];
158                 int x, y;
159                 memset(raw, 0, 64*64);
160                 for (y=0; y<64; y++)
161                 {
162                         for (x=0; x<128 / 2; x++)
163                                 raw[y*64+x] = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
164                 }
165                 if (lcdfd > 0)
166                         write(lcdfd, raw, 64*64);
167         }
168 }
169