Add more functions to manage ini file.
[bertos.git] / bertos / mware / blanker.c
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 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2001 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Display Blanker (implementation).
34  *
35  *
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39 #include "blanker.h"
40 #include "hw/hw_blanker.h"
41
42 #include <drv/kbd.h>
43 #include <drv/timer.h>
44
45 /* Time without input events before starting blanker */
46 #define BLK_BLANKTIMEOUT        (15 * 1000)     /* ms */
47
48
49 #warning FIXME:Revise me!
50
51 /** Keyboard event handler to listen for key presses in blanker. */
52 static KbdHandler blk_KbdHandler;
53
54 /** Time since last key event. */
55 static ticks_t blk_lastevent;
56
57 /** Display blanking function is enabled. */
58 static bool blk_enabled;
59
60 /** Display blanker is engaged right now. */
61 static bool blk_active;
62
63
64 static bool blk_on(void)
65 {
66         if (!blk_active)
67         {
68                 blk_active = true;
69                 BLK_LCDOFF;
70         }
71         return true;
72 }
73
74
75 static void blk_off(void)
76 {
77         if (blk_active)
78         {
79                 blk_active = false;
80                 BLK_LCDON;
81         }
82 }
83
84
85 void blk_retrigger(void)
86 {
87         blk_lastevent = timer_clock();
88         blk_off();
89 }
90
91 #if 0
92 /**
93  * Matrix-like screen saver effect
94  */
95 static void blk_hack(void)
96 {
97         static signed char blk_colstart[CONFIG_LCD_COLS];
98         UBYTE row, col;
99
100
101         if (rand()%3 == 0)
102         {
103                 /* Modify one column */
104                 col = rand() % CONFIG_LCD_COLS;
105                 blk_colstart[col] += rand() % 12 - 5;
106         }
107
108         for (col = 0; col < CONFIG_LCD_COLS; ++col)
109         {
110                 if (blk_colstart[col] > 0)
111                 {
112                         --blk_colstart[col];
113
114                         /* Scroll down */
115                         for(row = CONFIG_LCD_ROWS-1; row; --row)
116                         {
117                                 lcd_SetAddr(blk_layer, LCD_POS(col,row));
118                                 lcd_PutChar(blk_layer->Buf[LCD_POS(col,row-1)], blk_layer);
119                         }
120
121                         /* Add new kanji */
122                         lcd_SetAddr(blk_layer, LCD_POS(col,0));
123                         lcd_PutChar((char)(rand() % 127 + 128), blk_layer);
124                 }
125                 else if (blk_colstart[col] < 0)
126                 {
127                         ++blk_colstart[col];
128
129                         /* Clear tail */
130                         for(row = 0; row < CONFIG_LCD_ROWS; ++row)
131                         {
132                                 if (blk_layer->Buf[LCD_POS(col,row)] != ' ')
133                                 {
134                                         lcd_SetAddr(blk_layer, LCD_POS(col,row));
135                                         lcd_PutChar(' ', blk_layer);
136                                         break;
137                                 }
138                         }
139                 }
140         }
141 }
142 #endif
143
144
145 static keymask_t blk_handlerFunc(keymask_t key)
146 {
147         /* key used to turn off blanker */
148         static keymask_t offkey;
149
150         ticks_t now = timer_clock();
151
152         /* If key pressed */
153         if (key != 0)
154         {
155                 blk_lastevent = now;
156                 if (blk_active)
157                 {
158                         blk_off();
159
160                         /* remember and eat key event */
161                         offkey = key;
162                         key = 0;
163                 }
164                 else if (key == offkey)
165                 {
166                         /* keep eating the key until released */
167                         key = 0;
168                 }
169
170                 /* pass key through */
171                 return key;
172         }
173
174         /* reset off key */
175         offkey = 0;
176
177         /* Blank timeout reached? */
178         if (now - blk_lastevent > ms_to_ticks(BLK_BLANKTIMEOUT))
179         {
180                 /* Enable blanker unless already done */
181                 if (!blk_active && !blk_on())
182                         return 0;
183
184 #if 0
185                 /* Do some nice visual effect */
186                 blk_hack();
187 #endif /* _DEBUG */
188         }
189
190         return 0;
191 }
192
193
194 void blk_enable(void)
195 {
196         if (!blk_enabled)
197         {
198                 blk_active = false;
199                 blk_lastevent = timer_clock();
200
201                 /* Add display blanker handler */
202                 blk_KbdHandler.hook = blk_handlerFunc;
203                 blk_KbdHandler.pri = 100; /* high priority */
204                 blk_KbdHandler.flags = KHF_RAWKEYS;
205                 kbd_addHandler(&blk_KbdHandler);
206
207                 blk_enabled = true;
208         }
209 }
210
211
212 void blk_disable(void)
213 {
214         if (blk_enabled)
215         {
216                 kbd_remHandler(&blk_KbdHandler);
217                 blk_off();
218                 blk_enabled = false;
219         }
220 }
221