Virtual Screens on openchronos-ng

Hello everybody,

First of all excuse my poor blogging ability because honestly this is the first time ever I'm writing to something resembling a blog.

Now straight to the topic,
I just pushed a commit that introduces a very cool feature. Basically it introduces
virtual screens. What is a virtual screen?

A virtual screen is a pointer to a memory region created by lcd_screen_create(). You can pass that pointer to all of the display functions:

display_symbol()
display_char()
display_chars()
display_clear()

when you do that, the data is not actually written to the real LCD memory but to a dynamic memory region. If you want to directly write to the real screen, just set the screen parameter of any of the above functions to NULL.

You can also copy, at any time, the contents of a virtual screen into the real screen and vice-versa, using the lcd_screen_virtual_to_real() and lcd_screen_real_to_virtual()
functions respectively.

You may now ask, alright but why do we need this?
Suppose you are developing a module which uses the num button to switch between several "screens". An actual example is the clock module, where in the first screen it shows the time, day and month and in the second screen it shows the year. As I was implementing that functionality, I realized the code was getting too complex.
I started wondering if there was a more efficient way of doing it and here we are!

If you look to the clock module, where I'm already using this functionality, you can see that two virtual screens are created.
Each virtual screen saves both segment and blinking information.
So I actually display stuff in the two virtual screens (time, day and month in screen0 and year in screen1) without worrying which screen is actually being shown. At each point where the real screen actually needs to be refreshed, I just copy the contents of the "active" virtual screen into the real screen using lcd_screen_virtual_to_real().

Now some technical notes:
Each virtual screen actually takes 24bytes of memory. It might seem a lot but if you consider the size of the code that you would actually need to write to handle the cases where these functions are meant to be used, I'm pretty sure the size of the code you would need to write would be way bigger. Hence we are saving flash by using a little more RAM.

One final note for those not familiar with openchronos-ng:
Openchronos-ng doesn't have the "two independent menus" functionality anymore. We found that its implementation made the firmware bigger than it was worth. So at openchronos-ng we actually only have one menu where modules (applications) can use the entire screen.

Posted by miknix 2012-05-13 Labels: virtual screens