Michael Kuchnik / Mbed 2 deprecated uLCD_Multiscreen

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VirtualScreen.h Source File

VirtualScreen.h

00001 #ifndef VIRTUALSCREEN_H
00002 #define VIRTUALSCREEN_H
00003 #include "ScreenUpdater.h"
00004 #include "Command.h"
00005 #include "DrawPixel.h"
00006 #include "ClearScreen.h"
00007 #include "BackgroundColor.h"
00008 #include "Reset.h"
00009 #include "BaudRate.h"
00010 #include "Blit.h"
00011 #include "mbed.h"
00012 /**
00013  * Class represents a virtual screen of arbitrary size. To keep simplicity, screen data should be kept
00014  * in a pixel format where it can be read by physical devices.
00015  */
00016 template <class pixelType, class screenType>
00017 class VirtualScreen {
00018     private:
00019         std::vector<screenType*> screens;
00020         std::vector<ScreenUpdater*> updaters; 
00021         int rows;
00022         int columns;
00023         int screenSize;
00024     public:
00025         /**
00026          * Creates a virtual screen of size rows * columns. This screen is partitioned into different physical
00027          * screens. Writing pixels into this screen will transparently write into
00028          * these separate screens.
00029          *
00030          * @param screens A vector of physical screens to be used for drawing on.
00031          * @param rows The number of rows in the virtual screen.
00032          * @param columns The number of columns in the virtual screen. 
00033          */
00034         VirtualScreen<pixelType, screenType>(std::vector<screenType*> screens, int rows, int columns) {
00035             printf("Virtual Screen Booting.");
00036             this->screens = screens;
00037             for (typename std::vector<screenType*>::iterator scr_iter = screens.begin(); scr_iter != screens.end(); scr_iter++) {
00038                 ScreenUpdater *updater = new ScreenUpdater(*scr_iter);
00039                 updaters.push_back(updater);
00040             }    
00041             this->rows = rows;
00042             this->columns = columns;
00043             this->screenSize = 128;
00044         }
00045         
00046         ~VirtualScreen<pixelType, screenType>() {
00047             for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) {
00048                 delete (*updater_iter);
00049             }
00050         }
00051         
00052         /**
00053          * Writes a pixel value into the virtual screen at (row, column) index.
00054          *
00055          * @param x The row to write the pixel to.
00056          * @param y The column to write the pixel to.
00057          * @param value The value to write to the pixel.
00058          */
00059         void setPixel(int x, int y, pixelType value) {
00060             //printf("Set pixel on row %d, col %d, val %d\n", x, y, value);
00061             int screen_number = x / screenSize; 
00062             int x_pixel = x % screenSize; 
00063             //printf("Predicting screen %d, at col %d\n", screen_number, x_pixel);
00064             ScreenUpdater* updater = updaters.at(screen_number); 
00065             //printf("Updater made.\n"); 
00066             Command *command = new DrawPixel(x_pixel, y, value); 
00067            //printf("Adding command.\n");
00068             updater->addCommand(command); 
00069         }
00070         /**
00071          * Clears the screens of all attached screens.
00072          */
00073         void clearScreen() {
00074             for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) {
00075                 Command* command = new ClearScreen(); 
00076                 (*updater_iter)->addCommand(command);
00077             }
00078         }
00079         /**
00080          * Resets the screens of all attached screens.
00081          */
00082         void resetScreen() {
00083             for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) {
00084                 Command* command = new Reset(); 
00085                 (*updater_iter)->addCommand(command);
00086             }
00087         }
00088         /**
00089          * Sets the baud rate for all screens.
00090          *
00091          * @param rate The desired baud rate.
00092          */
00093         void setBaudRate(int rate) {
00094             for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) {
00095                 Command* command = new BaudRate(rate); 
00096                 (*updater_iter)->addCommand(command);
00097             }
00098         }
00099         /**
00100          * draw a block of pixels
00101          */
00102         void BLIT(int x, int y, int w, int h, int *colors) {
00103             for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) {
00104                 Command* command = new Blit(x,y,w,h,colors); 
00105                 (*updater_iter)->addCommand(command);
00106             }
00107         }
00108         /**
00109          * Sets the background color for all screens.
00110          *
00111          * @param color The color of the desired background.
00112          */
00113         void background_color(int color) {
00114             for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) {
00115                 Command* command = new BackgroundColor(color); 
00116                 (*updater_iter)->addCommand(command);
00117             }
00118         }
00119 };
00120 #endif