4 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief Generic text LCD driver (impl.).
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
17 *#* Revision 1.2 2006/02/24 00:27:14 bernie
18 *#* Use new naming convention for list macros.
20 *#* Revision 1.1 2005/11/04 18:00:42 bernie
21 *#* Import into DevLib.
23 *#* Revision 1.11 2005/06/14 14:43:43 bernie
24 *#* Add DevLib headers.
26 *#* Revision 1.10 2005/06/06 17:41:57 batt
27 *#* Add lcd_layerSet function.
29 *#* Revision 1.9 2005/06/01 16:40:07 batt
30 *#* Remove debug string.
32 *#* Revision 1.8 2005/06/01 16:38:04 batt
33 *#* Adapt to changes in mware/list.h.
35 *#* Revision 1.7 2005/06/01 10:45:22 batt
36 *#* lcd_setAddr(): Bugfix boundary condition; Misc cleanup.
38 *#* Revision 1.6 2005/06/01 10:36:23 batt
39 *#* Layer: Rename member variables and Doxygenize.
41 *#* Revision 1.5 2005/05/27 11:05:58 batt
42 *#* Do not write on lcd if layer is hidden.
47 #include <drv/timer.h> // timer_delay()
48 #include <mware/formatwr.h> // _formatted_write()
49 #include <cfg/macros.h> // BV()
50 #include <cfg/debug.h>
52 #include <string.h> // strlen()
55 /*! Maximum number of layers. */
59 /*! Semaphore to arbitrate access to the display. */
60 static struct Semaphore lcd_semaphore;
61 #define LOCK_LCD sem_obtain(&lcd_semaphore)
62 #define UNLOCK_LCD sem_release(&lcd_semaphore)
63 #else /* !CONFIG_KERNEL */
64 #define LOCK_LCD do {} while (0)
65 #define UNLOCK_LCD do {} while (0)
66 #endif /* !CONFIG_KERNEL */
68 DECLARE_LIST_TYPE(Layer);
71 static Layer lcd_LayersPool[LCD_LAYERS];
72 static LIST_TYPE(Layer) lcd_Layers;
73 static LIST_TYPE(Layer) lcd_FreeLayers;
76 * Current cursor status.
78 * One of LCD_CMD_CURSOR_OFF, LCD_CMD_CURSOR_BLOCK or LCD_CMD_CURSOR_LINE.
80 static uint8_t lcd_CursorStatus;
82 /*! Current cursor position, encoded as a Cursor position and status. */
83 static lcdpos_t lcd_CursorAddr;
86 void lcd_setAddr(Layer *layer, lcdpos_t addr)
88 /* Sanity check: wrap around to display limits */
89 while (addr >= LCD_ROWS * LCD_COLS)
90 addr -= LCD_ROWS * LCD_COLS;
103 void lcd_unlock(void)
108 #endif /* CONFIG_KERNEL */
112 * Write one character to the display at the current
113 * cursor prosition, then move the cursor right. The
114 * cursor is wrapped to the next line when it moves
115 * beyond the end of the current line.
117 * \note Does _NOT_ lock the display semaphore.
119 static void lcd_putCharUnlocked(char c, Layer *layer)
122 lcdpos_t addr = layer->addr;
124 /* Store character in layer buffer */
125 layer->buf[addr] = c;
127 /* Move to next character */
128 if (++layer->addr >= LCD_COLS * LCD_ROWS)
131 /* Do not write on LCD if layer is hidden. */
132 if (layer->pri == LAYER_HIDDEN)
136 * Check if this location is obscured by
137 * other layers above us.
139 for (l2 = layer->pred; l2->pred; l2 = l2->pred)
143 /* DB(kprintf("layer %04x obs %04x at %d\n", l2, layer, addr);) */
148 /* Write character */
152 /* FIXME: should look for layers beneath! */
157 void lcd_putChar(char c, Layer *layer)
160 lcd_putCharUnlocked(c, layer);
164 void lcd_layerSet(Layer *layer, char c)
169 lcd_setAddr(layer, 0);
170 for (i = 0; i < LCD_COLS * LCD_ROWS; i++)
171 lcd_putCharUnlocked(c, layer);
176 void lcd_clear(Layer *layer)
178 lcd_layerSet(layer, 0);
182 void lcd_clearLine(Layer *layer, int y)
187 lcd_setAddr(layer, LCD_POS(0, y));
188 for (i = 0; i < LCD_COLS; i++)
189 lcd_putCharUnlocked(0, layer);
194 void lcd_moveCursor(lcdpos_t addr)
202 char lcd_setCursor(char mode)
204 static const char cursor_cmd[3] =
206 LCD_CMD_CURSOR_OFF, LCD_CMD_CURSOR_BLOCK, LCD_CMD_CURSOR_LINE
208 char oldmode = lcd_CursorStatus;
211 lcd_CursorStatus = mode;
212 lcd_setReg(cursor_cmd[(int)mode]);
214 lcd_moveCursor(lcd_CursorAddr);
221 int lcd_vprintf(Layer *layer, lcdpos_t addr, uint8_t mode, const char *format, va_list ap)
228 * Se il cursore era acceso, spegnilo durante
229 * l'output per evitare che salti alla posizione
232 if (lcd_CursorStatus)
233 lcd_setReg(LCD_CMD_CURSOR_OFF);
235 /* Spostamento del cursore */
236 lcd_setAddr(layer, addr);
238 if (mode & LCD_CENTER)
243 * NOTE: calculating the string lenght BEFORE it gets
244 * printf()-formatted. Real lenght may differ.
246 pad = (LCD_COLS - strlen(format)) / 2;
248 lcd_putCharUnlocked(' ', layer);
251 len = _formatted_write(format, (void (*)(char, void *))lcd_putCharUnlocked, layer, ap);
253 if (mode & (LCD_FILL | LCD_CENTER))
254 while (layer->addr % LCD_COLS)
255 lcd_putCharUnlocked(' ', layer);
258 * Riaccendi il cursore e riportalo alla
261 if (lcd_CursorStatus)
262 lcd_setCursor(lcd_CursorStatus);
270 int lcd_printf(Layer *layer, lcdpos_t addr, uint8_t mode, const char *format, ...)
275 va_start(ap, format);
276 len = lcd_vprintf(layer, addr, mode, format, ap);
284 * Internal function to move a layer between two positions.
286 * \note The layer must be *already* enqueued in some list.
287 * \note The display must be already locked!
289 static void lcd_enqueueLayer(Layer *layer, char pri)
293 /* Remove layer from whatever list it was in before */
299 * Search for the first layer whose priority
300 * is less or equal to the layer we are adding.
302 FOREACH_NODE(l2, &lcd_Layers)
307 INSERT_BEFORE(layer, l2);
310 Layer *lcd_newLayer(char pri)
316 if (ISLISTEMPTY(&lcd_FreeLayers))
323 layer = (Layer *)LIST_HEAD(&lcd_FreeLayers);
325 memset(layer->buf, 0, LCD_ROWS * LCD_COLS);
327 lcd_enqueueLayer(layer, pri);
334 * Redraw the display (internal).
336 * \note The display must be already locked.
338 static void lcd_refresh(void)
343 for (addr = 0; addr < LCD_ROWS * LCD_COLS; ++addr)
345 FOREACH_NODE(l, &lcd_Layers)
347 //kprintf("%d %x %p\n", addr, l->buf[0], l);
348 if (l->pri == LAYER_HIDDEN)
353 /* Refresh location */
354 lcd_putc(addr, l->buf[addr]);
359 /* Draw background */
367 * Rearrange layer depth and refresh display accordingly.
369 * \note Setting a priority of LAYER_HIDDEN makes the layer invisible.
371 void lcd_setLayerDepth(Layer *layer, char pri)
373 if (pri != layer->pri)
376 lcd_enqueueLayer(layer, pri);
377 /* Vile but simple */
383 void lcd_deleteLayer(Layer *layer)
387 /* We use lcd_refresh() instead. Much simpler than this mess, but slower. */
392 /* Repair damage on underlaying layers */
393 for (addr = 0; addr < LCD_ROWS * LCD_COLS; ++addr)
395 /* If location was covered by us */
396 if (layer->buf[addr])
398 /* ...and it wasn't covered by others above us... */
399 for (l2 = layer->pred; l2->pred; l2 = l2->pred)
401 /* can't just break here! */
404 /* ...scan underlaying layers to repair damage */
405 for (l2 = layer->succ; l2->succ; l2 = l2->succ)
408 /* Refresh character */
409 lcd_putc(addr, l2->buf[addr]);
411 /* No need to search on deeper layers */
421 // Remove layer from lcd_Layers list.
424 /* Put layer back into free list */
425 ADDHEAD(&lcd_FreeLayers, layer);
433 static void lcd_setDefLayer(Layer *layer)
435 lcd_DefLayer = layer;
438 #include <cfg/debug.h>
443 LIST_INIT(&lcd_Layers);
444 LIST_INIT(&lcd_FreeLayers);
445 for (i = 0; i < LCD_LAYERS; ++i)
446 ADDHEAD(&lcd_FreeLayers, &lcd_LayersPool[i]);
448 lcd_setDefLayer(lcd_newLayer(0));
460 for (i = 0; i < LCD_ROWS * LCD_COLS; ++i)
462 lcd_putCharUnlocked('0' + (i % 10), lcd_DefLayer);
466 #endif /* CONFIG_TEST */