tirsdag den 25. oktober 2011

LCD Library

I thought I might add a few comments about the LCD library. The library is based around the idea that you want to update each pixel at most once each time the display changes. You might just draw everything you need to draw on top of each other, but let me tell you, that is quite slow, very visible and not so pretty. Been there, done that.

At the core of the library is a short routine that uses one DMA channel for moving pixel data from a memory buffer to SSP (the generalized peripheral used for SPI). The buffer only contains enough data for a fraction of the display. There is simply not enough RAM on the processor for the entire display. Think about it: 320 across times 240 down times two bytes of data, what's that? That's right, 150 kilobytes, and how much is on the processor? 64 kilobytes, not enough.

Actually there are two buffers, one buffer is updated with pixel data by the processor while the DMA peripheral moves data from the other buffer to the SSP peripheral and (as a SPI bit stream) to the LCD controller. This double-buffering scheme helps keep the SPI bit stream flowing, which is very necessary to make the graphics snappy. Of course, since the buffers are small, the buffer swapping must take place several times in order to refresh the entire display.

So, what does the core routine draw and how? Well, the core routine draws by calling functions supplied by any client of the library (that would be you, if you like) through an abstraction known as a view structure (much like in the Android platform). I intend to keep the view structure simple. It looks something like this now:
typedef struct View {
  scalar_t left, top, width, height;  
  struct View * parent;
  struct View * * children;
  size_t numChildren;
  void (*draw)(view_t * view);
  void (*click)(view_t * view);
} view_t;
I consider replacing draw and click with a single function which then will handle all notifications and requests. I probably will, since it saves view memory.

The view coordinates, left and right, are relative to any parent of the view, or to the screen in case the view has no parent. The coordinates are given in pixels. The dimensions, width and height are also given in pixels. The root view does not have a parent, for all other views the parent/children constitute a one-to-many relation that must be consistent. A view does not need to have children in which case children and numChildren are 0. Thinking about it, I might drop numChildren and zero-terminate the list instead, which makes sense, and conserves even more view memory. I was also considering replacing both with a call to the notification/request function, but I am reluctant to sacrifice simplicity for space savings in this case.

The member "draw" points to a function which is supposed to draw the view. This pointer may be NULL in which case the view is not drawn. Children of the view are still drawn (if they supply a draw function). The drawing primitives currently available to a view (and this is highly subject to change) are:
void lcd_drawPixel(point_t p, color_t c);
void lcd_fillRect(rect_t r, color_t color);
void lcd_drawBitmap(rect_t bounds, color_t * p);
Coordinates for these primitives are relative to the view origin.

The library keeps track of which rectangular part of a view is being redrawn. Any attempts to draw beyond the rectangle is futile, but in order to optimize drawing speed, a view may determine the clipping rectangle by calling:
rect_t lcd_getClip(void);
To have a view displayed, either set it as the root view or place in the root view hierarchy (or tree if you prefer):
void lcd_setRootView(view_t * v);
Then invalidate it:
void lcd_invalidateView(view_t * v);
Currently invalidation and redraw takes place immediately, I plan to decouple the redraw from the invalidation.


Ingen kommentarer:

Send en kommentar