4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30 * All Rights Reserved.
33 * \brief Generic text LCD driver (impl.).
35 * \author Bernie Innocenti <bernie@codewiz.org>
36 * \author Stefano Fedrigo <aleph@develer.com>
42 #include <cfg/macros.h> // BV()
43 #include <cfg/debug.h>
45 #include <drv/timer.h> // timer_delay()
47 #include <mware/formatwr.h> // _formatted_write()
48 #include <struct/list.h> // LIST_EMPTY()
50 #include <string.h> // strlen()
53 /** Maximum number of layers. */
58 /** Semaphore to arbitrate access to the display. */
59 static struct Semaphore lcd_semaphore;
60 #define LOCK_LCD sem_obtain(&lcd_semaphore)
61 #define UNLOCK_LCD sem_release(&lcd_semaphore)
62 #else /* !CONFIG_KERN */
63 #define LOCK_LCD do {} while (0)
64 #define UNLOCK_LCD do {} while (0)
65 #endif /* !CONFIG_KERN */
67 DECLARE_LIST_TYPE(Layer);
70 static Layer lcd_LayersPool[LCD_LAYERS];
71 static LIST_TYPE(Layer) lcd_Layers;
72 static LIST_TYPE(Layer) lcd_FreeLayers;
75 * Current cursor status.
77 * One of LCD_CMD_CURSOR_OFF, LCD_CMD_CURSOR_BLOCK or LCD_CMD_CURSOR_LINE.
79 static uint8_t lcd_CursorStatus;
81 /** Current cursor position, encoded as a Cursor position and status. */
82 static lcdpos_t lcd_CursorAddr;
85 void lcd_setAddr(Layer *layer, lcdpos_t addr)
87 /* Sanity check: wrap around to display limits */
88 while (addr >= CONFIG_LCD_ROWS * CONFIG_LCD_COLS)
89 addr -= CONFIG_LCD_ROWS * CONFIG_LCD_COLS;
102 void lcd_unlock(void)
107 #endif /* CONFIG_KERN */
111 * Write one character to the display at the current
112 * cursor prosition, then move the cursor right. The
113 * cursor is wrapped to the next line when it moves
114 * beyond the end of the current line.
116 * \note Does _NOT_ lock the display semaphore.
118 static void lcd_putCharUnlocked(char c, Layer *layer)
121 lcdpos_t addr = layer->addr;
123 /* Store character in layer buffer */
124 layer->buf[addr] = c;
126 /* Move to next character */
127 if (++layer->addr >= CONFIG_LCD_COLS * CONFIG_LCD_ROWS)
130 /* Do not write on LCD if layer is hidden. */
131 if (layer->pri == LAYER_HIDDEN)
135 * Check if this location is obscured by
136 * other layers above us.
138 for (l2 = layer->pred; l2->pred; l2 = l2->pred)
142 /* DB(kprintf("layer %04x obs %04x at %d\n", l2, layer, addr);) */
147 /* Write character */
151 /* FIXME: should look for layers beneath! */
156 void lcd_putChar(char c, Layer *layer)
159 lcd_putCharUnlocked(c, layer);
163 void lcd_layerSet(Layer *layer, char c)
168 lcd_setAddr(layer, 0);
169 for (i = 0; i < CONFIG_LCD_COLS * CONFIG_LCD_ROWS; i++)
170 lcd_putCharUnlocked(c, layer);
175 void lcd_clear(Layer *layer)
177 lcd_layerSet(layer, 0);
181 void lcd_clearLine(Layer *layer, int y)
186 lcd_setAddr(layer, LCD_POS(0, y));
187 for (i = 0; i < CONFIG_LCD_COLS; i++)
188 lcd_putCharUnlocked(0, layer);
193 void lcd_moveCursor(lcdpos_t addr)
201 char lcd_setCursor(char mode)
203 static const char cursor_cmd[3] =
205 LCD_CMD_CURSOR_OFF, LCD_CMD_CURSOR_BLOCK, LCD_CMD_CURSOR_LINE
207 char oldmode = lcd_CursorStatus;
210 lcd_CursorStatus = mode;
211 lcd_setReg(cursor_cmd[(int)mode]);
213 lcd_moveCursor(lcd_CursorAddr);
220 int lcd_vprintf(Layer *layer, lcdpos_t addr, uint8_t mode, const char *format, va_list ap)
227 * Se il cursore era acceso, spegnilo durante
228 * l'output per evitare che salti alla posizione
231 if (lcd_CursorStatus)
232 lcd_setReg(LCD_CMD_CURSOR_OFF);
234 /* Spostamento del cursore */
235 lcd_setAddr(layer, addr);
237 if (mode & LCD_CENTER)
242 * NOTE: calculating the string lenght BEFORE it gets
243 * printf()-formatted. Real lenght may differ.
245 pad = (CONFIG_LCD_COLS - strlen(format)) / 2;
247 lcd_putCharUnlocked(' ', layer);
250 len = _formatted_write(format, (void (*)(char, void *))lcd_putCharUnlocked, layer, ap);
252 if (mode & (LCD_FILL | LCD_CENTER))
253 while (layer->addr % CONFIG_LCD_COLS)
254 lcd_putCharUnlocked(' ', layer);
257 * Riaccendi il cursore e riportalo alla
260 if (lcd_CursorStatus)
261 lcd_setCursor(lcd_CursorStatus);
269 int lcd_printf(Layer *layer, lcdpos_t addr, uint8_t mode, const char *format, ...)
274 va_start(ap, format);
275 len = lcd_vprintf(layer, addr, mode, format, ap);
283 * Internal function to move a layer between two positions.
285 * \note The layer must be *already* enqueued in some list.
286 * \note The display must be already locked!
288 static void lcd_enqueueLayer(Layer *layer, char pri)
292 /* Remove layer from whatever list it was in before */
298 * Search for the first layer whose priority
299 * is less or equal to the layer we are adding.
301 FOREACH_NODE(l2, &lcd_Layers)
306 INSERT_BEFORE(layer, l2);
309 Layer *lcd_newLayer(char pri)
315 if (LIST_EMPTY(&lcd_FreeLayers))
322 layer = (Layer *)LIST_HEAD(&lcd_FreeLayers);
324 memset(layer->buf, 0, CONFIG_LCD_ROWS * CONFIG_LCD_COLS);
326 lcd_enqueueLayer(layer, pri);
333 * Redraw the display (internal).
335 * \note The display must be already locked.
337 static void lcd_refresh(void)
342 for (addr = 0; addr < CONFIG_LCD_ROWS * CONFIG_LCD_COLS; ++addr)
344 FOREACH_NODE(l, &lcd_Layers)
346 //kprintf("%d %x %p\n", addr, l->buf[0], l);
347 if (l->pri == LAYER_HIDDEN)
352 /* Refresh location */
353 lcd_putc(addr, l->buf[addr]);
358 /* Draw background */
366 * Rearrange layer depth and refresh display accordingly.
368 * \note Setting a priority of LAYER_HIDDEN makes the layer invisible.
370 void lcd_setLayerDepth(Layer *layer, char pri)
372 if (pri != layer->pri)
375 lcd_enqueueLayer(layer, pri);
376 /* Vile but simple */
382 void lcd_deleteLayer(Layer *layer)
386 /* We use lcd_refresh() instead. Much simpler than this mess, but slower. */
391 /* Repair damage on underlaying layers */
392 for (addr = 0; addr < CONFIG_LCD_ROWS * CONFIG_LCD_COLS; ++addr)
394 /* If location was covered by us */
395 if (layer->buf[addr])
397 /* ...and it wasn't covered by others above us... */
398 for (l2 = layer->pred; l2->pred; l2 = l2->pred)
400 /* can't just break here! */
403 /* ...scan underlaying layers to repair damage */
404 for (l2 = layer->succ; l2->succ; l2 = l2->succ)
407 /* Refresh character */
408 lcd_putc(addr, l2->buf[addr]);
410 /* No need to search on deeper layers */
420 // Remove layer from lcd_Layers list.
423 /* Put layer back into free list */
424 ADDHEAD(&lcd_FreeLayers, layer);
432 static void lcd_setDefLayer(Layer *layer)
434 lcd_DefLayer = layer;
437 #include <cfg/debug.h>
441 sem_init(&lcd_semaphore);
446 LIST_INIT(&lcd_Layers);
447 LIST_INIT(&lcd_FreeLayers);
448 for (i = 0; i < LCD_LAYERS; ++i)
449 ADDHEAD(&lcd_FreeLayers, &lcd_LayersPool[i]);
451 lcd_setDefLayer(lcd_newLayer(0));