Allow user to connect multiple screen.

Dependencies:   mbed-rtos mbed

Committer:
Ratchapong
Date:
Wed Mar 11 05:00:37 2015 +0000
Revision:
0:052d0f82433e
Working

Who changed what in which revision?

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