Allows users to seamlessly write to 2 or 3 uLCD screens as if they were one large screen.
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed
VirtualScreen.h@0:15002a72309b, 2015-03-11 (annotated)
- Committer:
- Mkuchnik3
- Date:
- Wed Mar 11 21:33:18 2015 +0000
- Revision:
- 0:15002a72309b
First Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mkuchnik3 | 0:15002a72309b | 1 | #ifndef VIRTUALSCREEN_H |
Mkuchnik3 | 0:15002a72309b | 2 | #define VIRTUALSCREEN_H |
Mkuchnik3 | 0:15002a72309b | 3 | #include "ScreenUpdater.h" |
Mkuchnik3 | 0:15002a72309b | 4 | #include "Command.h" |
Mkuchnik3 | 0:15002a72309b | 5 | #include "DrawPixel.h" |
Mkuchnik3 | 0:15002a72309b | 6 | #include "ClearScreen.h" |
Mkuchnik3 | 0:15002a72309b | 7 | #include "BackgroundColor.h" |
Mkuchnik3 | 0:15002a72309b | 8 | #include "Reset.h" |
Mkuchnik3 | 0:15002a72309b | 9 | #include "BaudRate.h" |
Mkuchnik3 | 0:15002a72309b | 10 | #include "Blit.h" |
Mkuchnik3 | 0:15002a72309b | 11 | #include "mbed.h" |
Mkuchnik3 | 0:15002a72309b | 12 | /** |
Mkuchnik3 | 0:15002a72309b | 13 | * Class represents a virtual screen of arbitrary size. To keep simplicity, screen data should be kept |
Mkuchnik3 | 0:15002a72309b | 14 | * in a pixel format where it can be read by physical devices. |
Mkuchnik3 | 0:15002a72309b | 15 | */ |
Mkuchnik3 | 0:15002a72309b | 16 | template <class pixelType, class screenType> |
Mkuchnik3 | 0:15002a72309b | 17 | class VirtualScreen { |
Mkuchnik3 | 0:15002a72309b | 18 | private: |
Mkuchnik3 | 0:15002a72309b | 19 | std::vector<screenType*> screens; |
Mkuchnik3 | 0:15002a72309b | 20 | std::vector<ScreenUpdater*> updaters; |
Mkuchnik3 | 0:15002a72309b | 21 | int rows; |
Mkuchnik3 | 0:15002a72309b | 22 | int columns; |
Mkuchnik3 | 0:15002a72309b | 23 | int screenSize; |
Mkuchnik3 | 0:15002a72309b | 24 | public: |
Mkuchnik3 | 0:15002a72309b | 25 | /** |
Mkuchnik3 | 0:15002a72309b | 26 | * Creates a virtual screen of size rows * columns. This screen is partitioned into different physical |
Mkuchnik3 | 0:15002a72309b | 27 | * screens. Writing pixels into this screen will transparently write into |
Mkuchnik3 | 0:15002a72309b | 28 | * these separate screens. |
Mkuchnik3 | 0:15002a72309b | 29 | * |
Mkuchnik3 | 0:15002a72309b | 30 | * @param screens A vector of physical screens to be used for drawing on. |
Mkuchnik3 | 0:15002a72309b | 31 | * @param rows The number of rows in the virtual screen. |
Mkuchnik3 | 0:15002a72309b | 32 | * @param columns The number of columns in the virtual screen. |
Mkuchnik3 | 0:15002a72309b | 33 | */ |
Mkuchnik3 | 0:15002a72309b | 34 | VirtualScreen<pixelType, screenType>(std::vector<screenType*> screens, int rows, int columns) { |
Mkuchnik3 | 0:15002a72309b | 35 | printf("Virtual Screen Booting."); |
Mkuchnik3 | 0:15002a72309b | 36 | this->screens = screens; |
Mkuchnik3 | 0:15002a72309b | 37 | for (typename std::vector<screenType*>::iterator scr_iter = screens.begin(); scr_iter != screens.end(); scr_iter++) { |
Mkuchnik3 | 0:15002a72309b | 38 | ScreenUpdater *updater = new ScreenUpdater(*scr_iter); |
Mkuchnik3 | 0:15002a72309b | 39 | updaters.push_back(updater); |
Mkuchnik3 | 0:15002a72309b | 40 | } |
Mkuchnik3 | 0:15002a72309b | 41 | this->rows = rows; |
Mkuchnik3 | 0:15002a72309b | 42 | this->columns = columns; |
Mkuchnik3 | 0:15002a72309b | 43 | this->screenSize = 128; |
Mkuchnik3 | 0:15002a72309b | 44 | } |
Mkuchnik3 | 0:15002a72309b | 45 | |
Mkuchnik3 | 0:15002a72309b | 46 | ~VirtualScreen<pixelType, screenType>() { |
Mkuchnik3 | 0:15002a72309b | 47 | for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) { |
Mkuchnik3 | 0:15002a72309b | 48 | delete (*updater_iter); |
Mkuchnik3 | 0:15002a72309b | 49 | } |
Mkuchnik3 | 0:15002a72309b | 50 | } |
Mkuchnik3 | 0:15002a72309b | 51 | |
Mkuchnik3 | 0:15002a72309b | 52 | /** |
Mkuchnik3 | 0:15002a72309b | 53 | * Writes a pixel value into the virtual screen at (row, column) index. |
Mkuchnik3 | 0:15002a72309b | 54 | * |
Mkuchnik3 | 0:15002a72309b | 55 | * @param x The row to write the pixel to. |
Mkuchnik3 | 0:15002a72309b | 56 | * @param y The column to write the pixel to. |
Mkuchnik3 | 0:15002a72309b | 57 | * @param value The value to write to the pixel. |
Mkuchnik3 | 0:15002a72309b | 58 | */ |
Mkuchnik3 | 0:15002a72309b | 59 | void setPixel(int x, int y, pixelType value) { |
Mkuchnik3 | 0:15002a72309b | 60 | //printf("Set pixel on row %d, col %d, val %d\n", x, y, value); |
Mkuchnik3 | 0:15002a72309b | 61 | int screen_number = x / screenSize; |
Mkuchnik3 | 0:15002a72309b | 62 | int x_pixel = x % screenSize; |
Mkuchnik3 | 0:15002a72309b | 63 | //printf("Predicting screen %d, at col %d\n", screen_number, x_pixel); |
Mkuchnik3 | 0:15002a72309b | 64 | ScreenUpdater* updater = updaters.at(screen_number); |
Mkuchnik3 | 0:15002a72309b | 65 | //printf("Updater made.\n"); |
Mkuchnik3 | 0:15002a72309b | 66 | Command *command = new DrawPixel(x_pixel, y, value); |
Mkuchnik3 | 0:15002a72309b | 67 | //printf("Adding command.\n"); |
Mkuchnik3 | 0:15002a72309b | 68 | updater->addCommand(command); |
Mkuchnik3 | 0:15002a72309b | 69 | } |
Mkuchnik3 | 0:15002a72309b | 70 | /** |
Mkuchnik3 | 0:15002a72309b | 71 | * Clears the screens of all attached screens. |
Mkuchnik3 | 0:15002a72309b | 72 | */ |
Mkuchnik3 | 0:15002a72309b | 73 | void clearScreen() { |
Mkuchnik3 | 0:15002a72309b | 74 | for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) { |
Mkuchnik3 | 0:15002a72309b | 75 | Command* command = new ClearScreen(); |
Mkuchnik3 | 0:15002a72309b | 76 | (*updater_iter)->addCommand(command); |
Mkuchnik3 | 0:15002a72309b | 77 | } |
Mkuchnik3 | 0:15002a72309b | 78 | } |
Mkuchnik3 | 0:15002a72309b | 79 | /** |
Mkuchnik3 | 0:15002a72309b | 80 | * Resets the screens of all attached screens. |
Mkuchnik3 | 0:15002a72309b | 81 | */ |
Mkuchnik3 | 0:15002a72309b | 82 | void resetScreen() { |
Mkuchnik3 | 0:15002a72309b | 83 | for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) { |
Mkuchnik3 | 0:15002a72309b | 84 | Command* command = new Reset(); |
Mkuchnik3 | 0:15002a72309b | 85 | (*updater_iter)->addCommand(command); |
Mkuchnik3 | 0:15002a72309b | 86 | } |
Mkuchnik3 | 0:15002a72309b | 87 | } |
Mkuchnik3 | 0:15002a72309b | 88 | /** |
Mkuchnik3 | 0:15002a72309b | 89 | * Sets the baud rate for all screens. |
Mkuchnik3 | 0:15002a72309b | 90 | * |
Mkuchnik3 | 0:15002a72309b | 91 | * @param rate The desired baud rate. |
Mkuchnik3 | 0:15002a72309b | 92 | */ |
Mkuchnik3 | 0:15002a72309b | 93 | void setBaudRate(int rate) { |
Mkuchnik3 | 0:15002a72309b | 94 | for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) { |
Mkuchnik3 | 0:15002a72309b | 95 | Command* command = new BaudRate(rate); |
Mkuchnik3 | 0:15002a72309b | 96 | (*updater_iter)->addCommand(command); |
Mkuchnik3 | 0:15002a72309b | 97 | } |
Mkuchnik3 | 0:15002a72309b | 98 | } |
Mkuchnik3 | 0:15002a72309b | 99 | /** |
Mkuchnik3 | 0:15002a72309b | 100 | * draw a block of pixels |
Mkuchnik3 | 0:15002a72309b | 101 | */ |
Mkuchnik3 | 0:15002a72309b | 102 | void BLIT(int x, int y, int w, int h, int *colors) { |
Mkuchnik3 | 0:15002a72309b | 103 | for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) { |
Mkuchnik3 | 0:15002a72309b | 104 | Command* command = new Blit(x,y,w,h,colors); |
Mkuchnik3 | 0:15002a72309b | 105 | (*updater_iter)->addCommand(command); |
Mkuchnik3 | 0:15002a72309b | 106 | } |
Mkuchnik3 | 0:15002a72309b | 107 | } |
Mkuchnik3 | 0:15002a72309b | 108 | /** |
Mkuchnik3 | 0:15002a72309b | 109 | * Sets the background color for all screens. |
Mkuchnik3 | 0:15002a72309b | 110 | * |
Mkuchnik3 | 0:15002a72309b | 111 | * @param color The color of the desired background. |
Mkuchnik3 | 0:15002a72309b | 112 | */ |
Mkuchnik3 | 0:15002a72309b | 113 | void background_color(int color) { |
Mkuchnik3 | 0:15002a72309b | 114 | for (typename std::vector<ScreenUpdater*>::iterator updater_iter = updaters.begin(); updater_iter != updaters.end(); updater_iter++) { |
Mkuchnik3 | 0:15002a72309b | 115 | Command* command = new BackgroundColor(color); |
Mkuchnik3 | 0:15002a72309b | 116 | (*updater_iter)->addCommand(command); |
Mkuchnik3 | 0:15002a72309b | 117 | } |
Mkuchnik3 | 0:15002a72309b | 118 | } |
Mkuchnik3 | 0:15002a72309b | 119 | }; |
Mkuchnik3 | 0:15002a72309b | 120 | #endif |