Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / drv / lcd_gfx_qt.cpp
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2006, 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Bernie Innocenti <bernie@codewiz.org>
34  *
35  * \brief Custom control for graphics LCD emulation (implementation)
36  */
37
38 #include "lcd_gfx_qt.h"
39 #include <emul/emul.h>
40 #include <cfg/debug.h>
41 #include <gfx/gfx.h> // CONFIG_BITMAP_FMT
42
43 #include <QtGui/QPainter>
44 #include <QtGui/QImage>
45 #include <QtGui/QSizePolicy>
46 #include <QtCore/QSize>
47
48 // Display colors
49 #define LCD_FG_COLOR 0x0, 0x0, 0x0
50 #define LCD_BG_COLOR 0xBB, 0xCC, 0xBB
51
52
53 EmulLCD::EmulLCD(QWidget *parent) :
54         QFrame(parent),
55         fg_color(LCD_FG_COLOR),
56         bg_brush(QColor(LCD_BG_COLOR))
57 {
58         // Optimized rendering: we repaint everything anyway
59         setAttribute(Qt::WA_NoSystemBackground);
60
61         // initialize bitmap
62         memset(raster, 0xAA, sizeof(raster));
63
64         // set widget frame
65         setFrameStyle(QFrame::Panel | QFrame::Sunken);
66         frame_width = frameWidth();
67
68         setMinimumSize(WIDTH + frame_width * 2, HEIGHT + frame_width * 2);
69
70         #if CONFIG_EMULLCD_SCALE
71                 QSizePolicy pol = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred, QSizePolicy::Frame);
72                 pol.setHeightForWidth(true);
73         #else
74                 QSizePolicy pol = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, QSizePolicy::Frame);
75         #endif
76         setSizePolicy(pol);
77 }
78
79
80 EmulLCD::~EmulLCD()
81 {
82         // nop
83 }
84
85 #if CONFIG_EMULLCD_SCALE
86 int EmulLCD::heightForWidth(int w) const
87 {
88                 int h;
89
90                 w -= frame_width * 2;
91                 h = (w * HEIGHT + WIDTH/2) / WIDTH;
92                 h += frame_width * 2;
93
94                 return h;
95 }
96 #endif // CONFIG_EMULLCD_SCALE
97
98 void EmulLCD::paintEvent(QPaintEvent * /*event*/)
99 {
100         QPainter p(this);
101         QImage img(raster, WIDTH, HEIGHT, QImage::Format_Mono);
102
103         #if CONFIG_EMULLCD_SCALE
104                 int w = width() - frame_width * 2;
105                 int h = height() - frame_width * 2;
106                 if ((w != WIDTH) || (h != HEIGHT))
107                 {
108                         p.scale((qreal)w / WIDTH, (qreal)h / HEIGHT);
109                         //p.setRenderHint(QPainter::SmoothPixmapTransform);
110                 }
111         #endif // CONFIG_EMULLCD_SCALE
112
113         p.setBackgroundMode(Qt::OpaqueMode);
114         p.setBackground(bg_brush);
115         p.setPen(fg_color);
116
117         p.drawImage(QPoint(frame_width, frame_width), img);
118 }
119
120 void EmulLCD::writeRaster(uint8_t *new_raster)
121 {
122 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
123
124         // Straight copy
125         //memcpy(raster, new_raster, sizeof(raster));
126
127         // Inverting copy
128         for (int i = 0; i < (int)sizeof(raster); ++i)
129                 raster[i] = ~new_raster[i];
130
131 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
132
133         // Rotation + inversion
134         for (int y = 0; y < HEIGHT; ++y)
135         {
136                 for (int xbyte = 0; xbyte < WIDTH/8; ++xbyte)
137                 {
138                         uint8_t v = 0;
139                         for (int xbit = 0; xbit < 8; ++xbit)
140                                 v |= (new_raster[(xbyte * 8 + xbit) + (y / 8) * WIDTH] & (1 << (y%8)) )
141                                         ? (1 << (7 - xbit)) : 0;
142
143                         raster[y * ((WIDTH + 7) / 8) + xbyte] = v;
144                 }
145         }
146 #else
147         #error Unsupported bitmap format
148 #endif
149
150         repaint();
151 }
152
153
154
155 #include <gfx/gfx.h>
156 #include <cfg/debug.h>
157
158 DECLARE_WALL(wall_before_raster, WALL_SIZE)
159 /**
160  * Raster buffer to draw into.
161  *
162  * Bits in the bitmap bytes have vertical orientation,
163  * as required by the LCD driver.
164  */
165 static uint8_t lcd_raster[RAST_SIZE(EmulLCD::WIDTH, EmulLCD::HEIGHT)];
166 DECLARE_WALL(wall_after_raster, WALL_SIZE)
167
168
169
170
171 /*extern "C"*/ void lcd_gfx_qt_init(Bitmap *lcd_bitmap)
172 {
173         //FIXME INIT_WALL(wall_before_raster);
174         //FIXME INIT_WALL(wall_after_raster);
175         gfx_bitmapInit(lcd_bitmap, lcd_raster, EmulLCD::WIDTH, EmulLCD::HEIGHT);
176         gfx_bitmapClear(lcd_bitmap);
177 }
178
179 /*extern "C"*/ void lcd_gfx_qt_blitBitmap(const Bitmap *bm)
180 {
181         //FIXME CHECK_WALL(wall_before_raster);
182         //FIXME CHECK_WALL(wall_after_raster);
183         emul->emulLCD->writeRaster(bm->raster);
184 }
185
186 #include "lcd_gfx_qt_moc.cpp"
187