A web server for monitoring and controlling a MakerBot Replicator over the USB host and ethernet.

Dependencies:   IAP NTPClient RTC mbed-rtos mbed Socket lwip-sys lwip BurstSPI

Fork of LPC1768_Mini-DK by Frank Vannieuwkerke

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI_TFT_ILI9320.h Source File

SPI_TFT_ILI9320.h

00001 /**************************************************************************************************
00002  *****                                                                                        *****
00003  *****  Name: SPI_TFT.h                                                                       *****
00004  *****  Ver.: 1.0                                                                             *****
00005  *****  Date: 04/01/2013                                                                      *****
00006  *****  Auth: Frank Vannieuwkerke                                                             *****
00007  *****        Erik Olieman                                                                    *****
00008  *****  Func: library for 240*320 pixel TFT with ILI9320 LCD Controller                       *****
00009  *****                                                                                        *****
00010  *****  Rewrite from Peter Drescher code - http://mbed.org/cookbook/SPI-driven-QVGA-TFT       *****
00011  *****                                                                                        *****
00012  **************************************************************************************************/
00013 
00014 #ifndef MBED_SPI_TFT_H
00015 #define MBED_SPI_TFT_H
00016 
00017 /* This library allows you to store a background image on the local flash memory of the microcontroller, 
00018 from any filesystem (such as SD cards). This allows very fast writing of this specific image, and it allows
00019 you to write text in a nice way over the image. However it does cost the last 5 flash sectors of the LPC1768.
00020 Generally that won't be a problem, if it is a problem, add #define NO_FLASH_BUFFER before including this file.
00021 */
00022 
00023 
00024 #include "GraphicsDisplay.h"
00025 #include "BurstSPI.h"
00026 #include "mbed.h"
00027 
00028 #ifndef NO_FLASH_BUFFER
00029     #include "IAP.h"
00030 #endif
00031 
00032 
00033 #define incx() x++, dxt += d2xt, t += dxt
00034 #define incy() y--, dyt += d2yt, t += dyt
00035 
00036 #define SPI_F_LO    10000000
00037 #define SPI_F_HI    48000000
00038 
00039 /* some RGB565 color definitions                                                 */
00040 #define Black           0x0000      /*   0,   0,   0 */
00041 #define Navy            0x000F      /*   0,   0, 128 */
00042 #define DarkGreen       0x03E0      /*   0, 128,   0 */
00043 #define DarkCyan        0x03EF      /*   0, 128, 128 */
00044 #define Maroon          0x7800      /* 128,   0,   0 */
00045 #define Purple          0x780F      /* 128,   0, 128 */
00046 #define Olive           0x7BE0      /* 128, 128,   0 */
00047 #define LightGrey       0xC618      /* 192, 192, 192 */
00048 #define DarkGrey        0x7BEF      /* 128, 128, 128 */
00049 #define Blue            0x001F      /*   0,   0, 255 */
00050 #define Green           0x07E0      /*   0, 255,   0 */
00051 #define Cyan            0x07FF      /*   0, 255, 255 */
00052 #define Red             0xF800      /* 255,   0,   0 */
00053 #define Magenta         0xF81F      /* 255,   0, 255 */
00054 #define Yellow          0xFFE0      /* 255, 255,   0 */
00055 #define White           0xFFFF      /* 255, 255, 255 */
00056 #define Orange          0xFD20      /* 255, 165,   0 */
00057 #define GreenYellow     0xAFE5      /* 173, 255,  47 */
00058 
00059 /** Class to use TFT displays with an ILI9320 controller using SPI mode
00060 */
00061  class SPI_TFT : public GraphicsDisplay {
00062  public:
00063 
00064   /** Create a SPI_TFT object connected to SPI and two pins
00065    *
00066    * @param mosi,miso,sclk SPI
00067    * @param cs pin connected to CS of display
00068    * @param reset pin connected to RESET of display
00069    *
00070    */
00071   SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name ="TFT");
00072 
00073   /** Write a value to the to a LCD register
00074    *
00075    * @param reg register to be written
00076    * @param val data to be written
00077    */
00078   void wr_reg (unsigned char reg, unsigned short val);
00079 
00080   /** Get the width of the screen in pixels
00081    *
00082    * @param
00083    * @returns width of screen in pixels
00084    *
00085    */
00086   virtual int width();
00087 
00088   /** Get the height of the screen in pixels
00089    *
00090    * @returns height of screen in pixels
00091    *
00092    */
00093   virtual int height();
00094 
00095   /** Draw a pixel at x,y with color
00096    *
00097    * @param x (horizontal position)
00098    * @param y (vertical position)
00099    * @param color (16 bit pixel color)
00100    */
00101   virtual void pixel(int x, int y,int colour);
00102 
00103   /** Set draw window region to whole screen
00104    *
00105    */
00106   void WindowMax (void);
00107 
00108   /** draw a 1 pixel line
00109    *
00110    * @param x0,y0 start point
00111    * @param x1,y1 stop point
00112    * @param color 16 bit color
00113    *
00114    */
00115   void line(int x0, int y0, int x1, int y1, int colour);
00116 
00117   /** draw a rect
00118    *
00119    * @param x0,y0 top left corner
00120    * @param w,h   width and height
00121    * @param color 16 bit color
00122    *                                                   *
00123    */
00124   void rect(int x0, int y0, int w, int h, int colour);
00125 
00126   /** draw a filled rect
00127    *
00128    * @param x0,y0 top left corner
00129    * @param w,h   width and height
00130    * @param color 16 bit color
00131    *
00132    */
00133   void fillrect(int x0, int y0, int w, int h, int colour);
00134 
00135   /** draw an ellipse - source : http://enchantia.com/graphapp/doc/tech/ellipses.html
00136    *
00137    * @param xc,yc center point
00138    * @param a,b semi-major axis and semi-minor axis
00139    * @param color 16 bit color
00140    *
00141    */
00142   void draw_ellipse(int xc, int yc, int a, int b, unsigned int color);
00143 
00144   /** draw a filled ellipse - source : http://enchantia.com/graphapp/doc/tech/ellipses.html
00145    *
00146    * @param xc,yc center point
00147    * @param a,b semi-major axis and semi-minor axis
00148    * @param color 16 bit color
00149    *
00150    */
00151   void fill_ellipse(int xc, int yc, int a, int b, unsigned int color);
00152 
00153   /** setup cursor position
00154    *
00155    * @param x x-position (top left)
00156    * @param y y-position
00157    */
00158   virtual void locate(int x, int y);
00159 
00160   /** Fill the screen with _background color
00161    *
00162    */
00163   virtual void cls (void);
00164 
00165   /** Read ILI9320 ID
00166    *
00167    * @returns LCD ID code
00168    *
00169    */
00170   unsigned short Read_ID(void);
00171 
00172   /** calculate the max number of char in a line
00173    *
00174    * @returns max columns
00175    * depends on actual font size
00176    *
00177    */
00178   virtual int columns(void);
00179 
00180   /** calculate the max number of rows
00181    *
00182    * @returns max rows
00183    * depends on actual font size
00184    *
00185    */
00186   virtual int rows(void);
00187 
00188   /** put a char on the screen
00189    *
00190    * @param value char to print
00191    * @returns printed char
00192    *
00193    */
00194   virtual int _putc(int value);
00195 
00196   /** draw a character on given position out of the active font to the TFT
00197    *
00198    * @param x x-position of char (top left)
00199    * @param y y-position
00200    * @param c char to print
00201    *
00202    */
00203   virtual void character(int x, int y, int c);
00204 
00205   /** paint a bitmap on the TFT
00206    *
00207    * @param x,y : upper left corner
00208    * @param w width of bitmap
00209    * @param h high of bitmap
00210    * @param *bitmap pointer to the bitmap data
00211    *
00212    *   bitmap format: 16 bit R5 G6 B5
00213    *
00214    *   use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
00215    *   use winhex to load this file and mark data stating at offset 0x46 to end
00216    *   use edit -> copy block -> C Source to export C array
00217    *   paste this array into your program
00218    *
00219    *   define the array as static const unsigned char to put it into flash memory
00220    *   cast the pointer to (unsigned char *) :
00221    *   tft.Bitmap(10,40,309,50,(unsigned char *)scala);
00222    */
00223   void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned char *Name_BMP);
00224 
00225 
00226    /** paint a BMP (16/24 bit) from filesytem on the TFT (slow)
00227    *
00228    * The 16-bit format is RGB-565. Using 16-bit is faster than 24-bit, however every program can
00229    * output 24 bit BMPs (including MS paint), while it is hard to get them in the correct 16-bit format.
00230    *
00231    * @param x,y : upper left corner
00232    * @param *Name_BMP location of the BMP file (for example "/sd/test.bmp")
00233    * @returns 1 if bmp file was found and stored
00234    * @returns 0 if file was not found
00235    * @returns -1 if file was not a bmp
00236    * @returns -2 if bmp file is not 16/24bit
00237    * @returns -3 if bmp file is wrong for screen
00238    * @returns -4 if buffer malloc goes wrong
00239    *
00240    */
00241 
00242   int Bitmap(unsigned int x, unsigned int y, const char *Name_BMP);
00243 
00244 
00245 
00246   /** select the font to use
00247    *
00248    * @param f pointer to font array
00249    *
00250    *   font array can be created with GLCD Font Creator from http://www.mikroe.com
00251    *   you have to add 4 parameter at the beginning of the font array to use:
00252    *   - the number of bytes / char
00253    *   - the vertial size in pixels
00254    *   - the horizontal size in pixels
00255    *   - the number of bytes per vertical line
00256    *   you also have to change the array to char[]
00257    *
00258    */
00259   void set_font(unsigned char* f);
00260 
00261   /** Set the orientation of the screen
00262    *  x,y: 0,0 is always top left
00263    *
00264    * @param o direction to use the screen (0-3) 90� Steps
00265    *
00266    */
00267   void set_orientation(unsigned int o);
00268 
00269 
00270   /** Modify the orientation of the screen
00271    *  x,y: 0,0 is always top left
00272    *
00273    * @param none
00274    *
00275    * ILI9320 limitation: The orientation can only be used with a window command (registers 0x50..0x53)
00276    * 
00277    * reg 03h (Entry Mode) : BGR = 1 - ORG = 1 - ID0, ID1 and AM are set according to the orientation variable.
00278    * IMPORTANT : when ORG = 1, the GRAM writing direction follows the orientation (ID0, ID1, AM bits)
00279    *             AND we need to use the window command (reg 50h..53h) to write to an area on the display
00280    *             because we cannot change reg 20h and 21h to set the GRAM address (they both remain at 00h).
00281    *             This means that the pixel routine does not work when ORG = 1.
00282    *             Routines relying on the pixel routine first need to set reg 03h = 0x1030
00283    *             (cls, circle and line do so) AND need to write the data according to the orientation variable.
00284    */
00285   void mod_orientation(void);
00286   
00287   #ifndef NO_FLASH_BUFFER
00288   /** Move an image to the background buffer
00289   * 
00290   * The image must fit exactly on the screen (240x320). This function takes quite some time, depending on source filesystem.
00291   *
00292   * @param *Name_BMP location of the BMP file (for example "/sd/test.bmp")
00293   * @returns 1 if bmp file was found and stored
00294   * @returns 0 if file was not found
00295   * @returns -1 if file was not a bmp
00296   * @returns -2 if bmp file is not 16/24bit
00297   * @returns -3 if bmp file is wrong for screen
00298   * @returns -4 if buffer malloc goes wrong
00299   */
00300   int fileToFlash(const char *Name_BMP);
00301   
00302   /** Use the image on the flash memory as background
00303   *
00304   * @param active - true to use the image, false to use static color
00305   */
00306   void backgroundImage(bool active);
00307   #endif
00308 
00309 
00310 
00311 
00312   
00313 // ------------------ PROTECTED PART ------------------
00314 protected:
00315 
00316   /** draw a horizontal line
00317    *
00318    * @param x0 horizontal start
00319    * @param x1 horizontal stop
00320    * @param y vertical position
00321    * @param color 16 bit color
00322    *
00323    */
00324   void hline(int x0, int x1, int y, int colour);
00325 
00326   /** draw a vertical line
00327    *
00328    * @param x horizontal position
00329    * @param y0 vertical start
00330    * @param y1 vertical stop
00331    * @param color 16 bit color
00332    */
00333   void vline(int y0, int y1, int x, int colour);
00334 
00335   /** Set draw window region
00336    *
00337    * @param x horizontal position
00338    * @param y vertical position
00339    * @param w window width in pixel
00340    * @param h window height in pixels
00341    */
00342   virtual void window (int x, int y, int w, int h);
00343 
00344   /** Init the ILI9320 controller
00345    *
00346    */
00347   void tft_reset();
00348 
00349    /** Write data to the LCD controller
00350    *
00351    * @param dat data written to LCD controller
00352    *
00353    */
00354   void wr_dat(unsigned short value);
00355 
00356    /** Start data sequence to the LCD controller
00357    *
00358    */
00359   void wr_dat_start(void);
00360 
00361   /** Write a command the LCD controller
00362    *
00363    * @param cmd: command to be written
00364    *
00365    */
00366   void wr_cmd(unsigned char value);
00367 
00368   /** Read data from the LCD controller
00369    *
00370    * @returns data from LCD controller
00371    *
00372    */
00373   unsigned short rd_dat(void);
00374 
00375   /** Read a LCD register
00376    *
00377    * @param reg register to be read
00378    * @returns value of the register
00379    */
00380   unsigned short rd_reg (unsigned char reg);
00381 
00382   /** Set the cursor position
00383    *
00384    * @param x (horizontal position)
00385    * @param y (vertical position)
00386    *
00387    * Can only be used when reg 03h = 0x1030 (see note in mod_orientation).
00388    *
00389    */
00390   void SetCursor( unsigned short Xpos, unsigned short Ypos );
00391   
00392     struct bitmapData {
00393         int return_code;
00394         int width, height;
00395         int bits, pad;
00396         int start_data;
00397         FILE *file;
00398     };
00399     
00400   /** Get bitmap info
00401    *
00402    * @param   *Name_BMP  Bitmap filename
00403    * @returns structure: return_code        1 if bmp file was found and stored
00404    *                                        0 if file was not found
00405    *                                        -1 if file was not a bmp
00406    *                                        -2 if bmp file is not 16/24bit
00407    *                                        -3 if bmp file is wrong for screen
00408    *                     width, height      Bitmap size
00409    *                     bits, pad          BPP, padding (multiple of 4 bytes)
00410    *                     start_data         Starting address of the byte where the bitmap image data (pixel array) can be found
00411    *                     *file              Bitmap filename
00412    */
00413     bitmapData getBitmapData(const char *Name_BMP);
00414 
00415   unsigned int orientation;
00416   unsigned int char_x;
00417   unsigned int char_y;
00418   bool backgroundimage;
00419   BurstSPI _spi;
00420   DigitalOut _cs;
00421   unsigned char* font;
00422   #ifndef NO_FLASH_BUFFER
00423   IAP     iap;
00424   int backgroundOrientation;
00425   #endif
00426 
00427 };
00428 
00429 #endif