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.1 2005/11/04 18:00:42 bernie
18 *#* Import into DevLib.
20 *#* Revision 1.11 2005/06/14 14:43:43 bernie
21 *#* Add DevLib headers.
23 *#* Revision 1.10 2005/06/06 17:41:57 batt
24 *#* Add lcd_layerSet function.
26 *#* Revision 1.9 2005/06/01 16:40:07 batt
27 *#* Remove debug string.
29 *#* Revision 1.8 2005/06/01 16:38:04 batt
30 *#* Adapt to changes in mware/list.h.
32 *#* Revision 1.7 2005/06/01 10:45:22 batt
33 *#* lcd_setAddr(): Bugfix boundary condition; Misc cleanup.
35 *#* Revision 1.6 2005/06/01 10:36:23 batt
36 *#* Layer: Rename member variables and Doxygenize.
38 *#* Revision 1.5 2005/05/27 11:05:58 batt
39 *#* Do not write on lcd if layer is hidden.
44 #include <drv/timer.h> // timer_delay()
45 #include <mware/formatwr.h> // _formatted_write()
46 #include <cfg/macros.h> // BV()
47 #include <cfg/debug.h>
49 #include <string.h> // strlen()
52 /*! Maximum number of layers. */
56 /*! Semaphore to arbitrate access to the display. */
57 static struct Semaphore lcd_semaphore;
58 #define LOCK_LCD sem_obtain(&lcd_semaphore)
59 #define UNLOCK_LCD sem_release(&lcd_semaphore)
60 #else /* !CONFIG_KERNEL */
61 #define LOCK_LCD do {} while (0)
62 #define UNLOCK_LCD do {} while (0)
63 #endif /* !CONFIG_KERNEL */
65 DECLARE_LIST_TYPE(Layer);
68 static Layer lcd_LayersPool[LCD_LAYERS];
69 static LIST_TYPE(Layer) lcd_Layers;
70 static LIST_TYPE(Layer) lcd_FreeLayers;
73 * Current cursor status.
75 * One of LCD_CMD_CURSOR_OFF, LCD_CMD_CURSOR_BLOCK or LCD_CMD_CURSOR_LINE.
77 static uint8_t lcd_CursorStatus;
79 /*! Current cursor position, encoded as a Cursor position and status. */
80 static lcdpos_t lcd_CursorAddr;
83 void lcd_setAddr(Layer *layer, lcdpos_t addr)
85 /* Sanity check: wrap around to display limits */
86 while (addr >= LCD_ROWS * LCD_COLS)
87 addr -= LCD_ROWS * LCD_COLS;
100 void lcd_unlock(void)
105 #endif /* CONFIG_KERNEL */
109 * Write one character to the display at the current
110 * cursor prosition, then move the cursor right. The
111 * cursor is wrapped to the next line when it moves
112 * beyond the end of the current line.
114 * \note Does _NOT_ lock the display semaphore.
116 static void lcd_putCharUnlocked(char c, Layer *layer)
119 lcdpos_t addr = layer->addr;
121 /* Store character in layer buffer */
122 layer->buf[addr] = c;
124 /* Move to next character */
125 if (++layer->addr >= LCD_COLS * LCD_ROWS)
128 /* Do not write on LCD if layer is hidden. */
129 if (layer->pri == LAYER_HIDDEN)
133 * Check if this location is obscured by
134 * other layers above us.
136 for (l2 = layer->pred; l2->pred; l2 = l2->pred)
140 /* DB(kprintf("layer %04x obs %04x at %d\n", l2, layer, addr);) */
145 /* Write character */
149 /* FIXME: should look for layers beneath! */
154 void lcd_putChar(char c, Layer *layer)
157 lcd_putCharUnlocked(c, layer);
161 void lcd_layerSet(Layer *layer, char c)
166 lcd_setAddr(layer, 0);
167 for (i = 0; i < LCD_COLS * LCD_ROWS; i++)
168 lcd_putCharUnlocked(c, layer);
173 void lcd_clear(Layer *layer)
175 lcd_layerSet(layer, 0);
179 void lcd_clearLine(Layer *layer, int y)
184 lcd_setAddr(layer, LCD_POS(0, y));
185 for (i = 0; i < LCD_COLS; i++)
186 lcd_putCharUnlocked(0, layer);
191 void lcd_moveCursor(lcdpos_t addr)
199 char lcd_setCursor(char mode)
201 static const char cursor_cmd[3] =
203 LCD_CMD_CURSOR_OFF, LCD_CMD_CURSOR_BLOCK, LCD_CMD_CURSOR_LINE
205 char oldmode = lcd_CursorStatus;
208 lcd_CursorStatus = mode;
209 lcd_setReg(cursor_cmd[(int)mode]);
211 lcd_moveCursor(lcd_CursorAddr);
218 int lcd_vprintf(Layer *layer, lcdpos_t addr, uint8_t mode, const char *format, va_list ap)
225 * Se il cursore era acceso, spegnilo durante
226 * l'output per evitare che salti alla posizione
229 if (lcd_CursorStatus)
230 lcd_setReg(LCD_CMD_CURSOR_OFF);
232 /* Spostamento del cursore */
233 lcd_setAddr(layer, addr);
235 if (mode & LCD_CENTER)
240 * NOTE: calculating the string lenght BEFORE it gets
241 * printf()-formatted. Real lenght may differ.
243 pad = (LCD_COLS - strlen(format)) / 2;
245 lcd_putCharUnlocked(' ', layer);
248 len = _formatted_write(format, (void (*)(char, void *))lcd_putCharUnlocked, layer, ap);
250 if (mode & (LCD_FILL | LCD_CENTER))
251 while (layer->addr % LCD_COLS)
252 lcd_putCharUnlocked(' ', layer);
255 * Riaccendi il cursore e riportalo alla
258 if (lcd_CursorStatus)
259 lcd_setCursor(lcd_CursorStatus);
267 int lcd_printf(Layer *layer, lcdpos_t addr, uint8_t mode, const char *format, ...)
272 va_start(ap, format);
273 len = lcd_vprintf(layer, addr, mode, format, ap);
281 * Internal function to move a layer betweet two positions.
283 * \note The layer must be *already* enqueued in some list.
284 * \note The display must be already locked!
286 static void lcd_enqueueLayer(Layer *layer, char pri)
290 /* Remove layer from whatever list it was in before */
296 * Search for the first layer whose priority
297 * is less or equal to the layer we are adding.
299 FOREACHNODE(l2, &lcd_Layers)
304 INSERTBEFORE(layer, l2);
307 Layer *lcd_newLayer(char pri)
313 if (ISLISTEMPTY(&lcd_FreeLayers))
320 layer = (Layer *)LIST_HEAD(&lcd_FreeLayers);
322 memset(layer->buf, 0, LCD_ROWS * LCD_COLS);
324 lcd_enqueueLayer(layer, pri);
331 * Redraw the display (internal).
333 * \note The display must be already locked.
335 static void lcd_refresh(void)
340 for (addr = 0; addr < LCD_ROWS * LCD_COLS; ++addr)
342 FOREACHNODE(l, &lcd_Layers)
344 //kprintf("%d %x %p\n", addr, l->buf[0], l);
345 if (l->pri == LAYER_HIDDEN)
350 /* Refresh location */
351 lcd_putc(addr, l->buf[addr]);
356 /* Draw background */
364 * Rearrange layer depth and refresh display accordingly.
366 * \note Setting a priority of LAYER_HIDDEN makes the layer invisible.
368 void lcd_setLayerDepth(Layer *layer, char pri)
370 if (pri != layer->pri)
373 lcd_enqueueLayer(layer, pri);
374 /* Vile but simple */
380 void lcd_deleteLayer(Layer *layer)
384 /* We use lcd_refresh() instead. Much simpler than this mess, but slower. */
389 /* Repair damage on underlaying layers */
390 for (addr = 0; addr < LCD_ROWS * LCD_COLS; ++addr)
392 /* If location was covered by us */
393 if (layer->buf[addr])
395 /* ...and it wasn't covered by others above us... */
396 for (l2 = layer->pred; l2->pred; l2 = l2->pred)
398 /* can't just break here! */
401 /* ...scan underlaying layers to repair damage */
402 for (l2 = layer->succ; l2->succ; l2 = l2->succ)
405 /* Refresh character */
406 lcd_putc(addr, l2->buf[addr]);
408 /* No need to search on deeper layers */
418 // Remove layer from lcd_Layers list.
421 /* Put layer back into free list */
422 ADDHEAD(&lcd_FreeLayers, layer);
430 static void lcd_setDefLayer(Layer *layer)
432 lcd_DefLayer = layer;
435 #include <cfg/debug.h>
440 LIST_INIT(&lcd_Layers);
441 LIST_INIT(&lcd_FreeLayers);
442 for (i = 0; i < LCD_LAYERS; ++i)
443 ADDHEAD(&lcd_FreeLayers, &lcd_LayersPool[i]);
445 lcd_setDefLayer(lcd_newLayer(0));
457 for (i = 0; i < LCD_ROWS * LCD_COLS; ++i)
459 lcd_putCharUnlocked('0' + (i % 10), lcd_DefLayer);
463 #endif /* CONFIG_TEST */