Lukasz Uszko / Mbed OS DiffPressureMeter

Dependencies:   TFT_Touch_WaveShare

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI_TFT.h Source File

SPI_TFT.h

00001 /* mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller
00002  * Copyright (c) 2011 Peter Drescher - DC2PD
00003  *
00004  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00005  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00006  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00007  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00008  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00009  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00010  * THE SOFTWARE.
00011  */
00012 
00013 #ifndef MBED_SPI_TFT_H
00014 #define MBED_SPI_TFT_H
00015 
00016 
00017 #include "mbed.h"
00018 #include "GraphicsDisplay.h"
00019 
00020 #define RGB(r,g,b)  (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
00021 
00022 #define SPI_START   (0x70)              /* Start byte for SPI transfer        */
00023 #define SPI_RD      (0x01)              /* WR bit 1 within start              */
00024 #define SPI_WR      (0x00)              /* WR bit 0 within start              */
00025 #define SPI_DATA    (0x02)              /* RS bit 1 within start byte         */
00026 #define SPI_INDEX   (0x00)              /* RS bit 0 within start byte         */
00027 
00028 
00029 /* some RGB color definitions                                                 */
00030 #define Black           0x0000      /*   0,   0,   0 */
00031 #define Navy            0x000F      /*   0,   0, 128 */
00032 #define DarkGreen       0x03E0      /*   0, 128,   0 */
00033 #define DarkCyan        0x03EF      /*   0, 128, 128 */
00034 #define Maroon          0x7800      /* 128,   0,   0 */
00035 #define Purple          0x780F      /* 128,   0, 128 */
00036 #define Olive           0x7BE0      /* 128, 128,   0 */
00037 #define LightGrey       0xC618      /* 192, 192, 192 */
00038 #define DarkGrey        0x7BEF      /* 128, 128, 128 */
00039 #define Blue            0x001F      /*   0,   0, 255 */
00040 #define Green           0x07E0      /*   0, 255,   0 */
00041 #define Cyan            0x07FF      /*   0, 255, 255 */
00042 #define Red             0xF800      /* 255,   0,   0 */
00043 #define Magenta         0xF81F      /* 255,   0, 255 */
00044 #define Yellow          0xFFE0      /* 255, 255, 0   */
00045 #define White           0xFFFF      /* 255, 255, 255 */
00046 
00047 /** Display control class, based on GraphicsDisplay and TextDisplay
00048  *
00049  * Example:
00050  * @code
00051  * #include "stdio.h"
00052  * #include "mbed.h"
00053  * #include "SPI_TFT.h"
00054  * #include "string"
00055  * #include "Arial12x12.h"
00056  * #include "Arial24x23.h"
00057  * 
00058  *
00059  *
00060  * // the TFT is connected to SPI pin 5-7 
00061  * SPI_TFT TFT(p5, p6, p7, p8, p15,"TFT"); // mosi, miso, sclk, cs, reset
00062  * 
00063  * int main() {
00064  *     TFT.claim(stdout);      // send stdout to the TFT display 
00065  *     //TFT.claim(stderr);      // send stderr to the TFT display
00066  *
00067  *     TFT.background(Black);    // set background to black
00068  *     TFT.foreground(White);    // set chars to white
00069  *     TFT.cls();                // clear the screen
00070  *     TFT.set_font((unsigned char*) Arial12x12);  // select the font
00071  *     
00072  *     TFT.set_orientation(0);
00073  *     TFT.locate(0,0);
00074  *     printf("  Hello Mbed 0");
00075  *     TFT.set_font((unsigned char*) Arial24x23);  // select font 2
00076  *     TFT.locate(2,5);
00077  *     TFT.printf("Bigger Font");
00078  *  }
00079  * @endcode
00080  */
00081  class SPI_TFT : public GraphicsDisplay {
00082  public:
00083 
00084   /** Create a SPI_TFT object connected to SPI and two pins
00085    *
00086    * @param mosi,miso,sclk SPI
00087    * @param cs pin connected to CS of display
00088    * @param reset pin connected to RESET of display
00089    *
00090    */ 
00091   SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset,const char* name ="TFT");
00092     
00093   /** Get the width of the screen in pixel
00094    *
00095    * @param 
00096    * @returns width of screen in pixel
00097    *
00098    */    
00099   virtual int width();
00100 
00101   /** Get the height of the screen in pixel
00102    *
00103    * @returns height of screen in pixel 
00104    *
00105    */     
00106   virtual int height();
00107     
00108   /** Draw a pixel at x,y with color 
00109    *  
00110    * @param x horizontal position
00111    * @param y vertical position
00112    * @param color 16 bit pixel color
00113    */    
00114   virtual void pixel(int x, int y, int colour);
00115     
00116   /** draw a circle
00117    *
00118    * @param x0,y0 center
00119    * @param r radius
00120    * @param color 16 bit color                                                                 *
00121    *
00122    */    
00123   void circle(int x, int y, int r, int colour); 
00124   
00125   /** draw a filled circle
00126    *
00127    * @param x0,y0 center
00128    * @param r radius
00129    * @param color 16 bit color                                                                 *
00130    *
00131    * use circle with different radius,
00132    * can miss some pixel
00133    */    
00134   void fillcircle(int x, int y, int r, int colour); 
00135   
00136  
00137     
00138   /** draw a 1 pixel line
00139    *
00140    * @param x0,y0 start point
00141    * @param x1,y1 stop point
00142    * @param color 16 bit color
00143    *
00144    */    
00145   void line(int x0, int y0, int x1, int y1, int colour);
00146     
00147   /** draw a rect
00148    *
00149    * @param x0,y0 top left corner
00150    * @param x1,y1 down right corner
00151    * @param color 16 bit color
00152    *                                                   *
00153    */    
00154   void rect(int x0, int y0, int x1, int y1, int colour);
00155     
00156   /** draw a filled rect
00157    *
00158    * @param x0,y0 top left corner
00159    * @param x1,y1 down right corner
00160    * @param color 16 bit color
00161    *
00162    */    
00163   void fillrect(int x0, int y0, int x1, int y1, int colour);
00164     
00165   /** setup cursor position
00166    *
00167    * @param column 0 to max
00168    * @param row 0 to max 
00169    * max depend on font size
00170    */   
00171   void locate(int column, int row);
00172     
00173   /** Fill the screen with _backgroun color
00174    *
00175    */   
00176   virtual void cls (void);   
00177     
00178   /** calculate the max number of char in a line
00179    *
00180    * @returns max columns
00181    * depends on actual font size
00182    *
00183    */    
00184   int columns(void);
00185     
00186   /** calculate the max number of columns
00187    *
00188    * @returns max column
00189    * depends on actual font size
00190    *
00191    */   
00192   int rows(void);
00193     
00194   /** put a char on the screen
00195    *
00196    * @param value char to print
00197    * @returns printed char
00198    *
00199    */
00200   int _putc(int value);
00201     
00202   /** draw a character on given position out of the active font to the TFT
00203    *
00204    * @param col column
00205    * @param row row
00206    * @param c char to print
00207    *
00208    */    
00209   virtual void character(int col, int row, int c);
00210     
00211   /** paint a bitmap on the TFT 
00212    *
00213    * @param x,y : upper left corner 
00214    * @param w width of bitmap
00215    * @param h high of bitmap
00216    * @param *bitmap pointer to the bitmap data
00217    *
00218    *   bitmap format: 16 bit R5 G6 B5
00219    * 
00220    *   use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5            
00221    *   use winhex to load this file and mark data stating at offset 0x46 to end
00222    *   use edit -> copy block -> C Source to export C array
00223    *   paste this array into your program
00224    * 
00225    */    
00226   void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap);
00227     
00228   /** select the font to use
00229    *
00230    * @param f pointer to font array 
00231    *                                                                              
00232    *   font array can created with GLCD Font Creator from http://www.mikroe.com
00233    *   you have to add 4 parameter at the beginning of the font array to use: 
00234    *   - the number of byte / char
00235    *   - the vertial size in pixel
00236    *   - the horizontal size in pixel
00237    *   - the number of byte per vertical line
00238    *   you also have to change the array to char[]
00239    *
00240    */   
00241   void set_font(unsigned char* f);
00242    
00243   /** Set the orientation of the screen
00244    *  x,y: 0,0 is always top left 
00245    *
00246    * @param o direction to use the screen (0-3) 90&#65533; Steps  
00247    *
00248    */  
00249   void set_orientation(unsigned int o);
00250     
00251   SPI _spi;
00252   DigitalOut _cs;
00253   DigitalOut _reset;
00254   unsigned char* font;
00255    
00256 protected:
00257 
00258   /** draw a horizontal line
00259    *
00260    * @param x0 horizontal start
00261    * @param x1 horizontal stop
00262    * @param y vertical position
00263    * @param color 16 bit color                                               
00264    *
00265    */
00266   void hline(int x0, int x1, int y, int colour);
00267     
00268   /** draw a vertical line
00269    *
00270    * @param x horizontal position
00271    * @param y0 vertical start 
00272    * @param y1 vertical stop
00273    * @param color 16 bit color
00274    */
00275   void vline(int y0, int y1, int x, int colour);
00276     
00277   /** Set draw window region
00278    *
00279    * @param x horizontal position
00280    * @param y vertical position
00281    * @param w window width in pixel
00282    * @param h window height in pixels
00283    */    
00284   void window (unsigned int x, unsigned int y, unsigned int w, unsigned int h);
00285     
00286   /** Set draw window region to whole screen
00287    *
00288    */  
00289   void WindowMax (void);
00290     
00291   /** Init the HX8347D controller 
00292    *
00293    */    
00294   void tft_reset();
00295     
00296    /** Write data to the LCD controller
00297    *
00298    * @param dat data written to LCD controller
00299    * 
00300    */   
00301   void wr_dat(int value);
00302     
00303   /** Write a command the LCD controller 
00304    *
00305    * @param cmd: command to be written   
00306    *
00307    */   
00308   void wr_cmd(int value);
00309     
00310    /** Start data sequence to the LCD controller
00311    * 
00312    */   
00313   void wr_dat_start();
00314     
00315   /** Stop of data writing to the LCD controller
00316    *   
00317    */  
00318   void wr_dat_stop();
00319     
00320   /** write data to the LCD controller
00321    *
00322    * @param data to be written 
00323    *                                           *
00324    */    
00325   void wr_dat_only(unsigned short dat);
00326     
00327   /** Read data from the LCD controller
00328    *
00329    * @returns data from LCD controller
00330    *  
00331    */    
00332   unsigned short rd_dat(void);
00333     
00334   /** Write a value to the to a LCD register
00335    *
00336    * @param reg register to be written
00337    * @param val data to be written
00338    */   
00339   void wr_reg (unsigned char reg, unsigned short val);
00340     
00341   /** Read a LCD register
00342    *
00343    * @param reg register to be read
00344    * @returns value of the register 
00345    */    
00346   unsigned short rd_reg (unsigned char reg);
00347     
00348   unsigned int orientation;
00349   unsigned int char_x;
00350     
00351 };
00352 
00353 #endif