This library can be used to control a low-cost Adafruit 358 TFT display. It has basic functionality but is a starting point for others trying to control this type of display using the FRDM-K64F.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_358.cpp Source File

Adafruit_358.cpp

00001 /* Display library for Adafruit 358 TFT display written for the FRDM-K64F
00002  * 
00003  * Copyright (c) 2014 Brian Mazzeo
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  *
00006  * This 1.8" display has 128x160 color pixels
00007  */
00008 
00009 #include "mbed.h"
00010 #include "Adafruit_358.h"
00011 
00012 
00013 //Constructor
00014 Adafruit_358::Adafruit_358(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName RESET, PinName DC, const char *name)
00015     :  GraphicsDisplay(name), _spi(MOSI, MISO, SCLK), _cs(CS), _reset(RESET), _dc(DC) 
00016 {   
00017     char_x = 0;
00018     char_y = 0;
00019     screen_reset();
00020 }
00021 
00022 int Adafruit_358::width()
00023 {
00024     return 128;    
00025 }
00026 
00027 int Adafruit_358::height()
00028 {
00029     return 160;    
00030 }
00031 
00032 
00033 void Adafruit_358::wr_cmd(unsigned char cmd)
00034 {
00035     _dc = 0;
00036     _cs = 0;
00037     _spi.write(cmd);
00038     _dc = 1;
00039 };
00040     
00041 
00042 void Adafruit_358::screen_reset()
00043 {
00044     _spi.format(8,3);
00045     _spi.frequency(10000000);
00046     _cs = 1;
00047     _dc = 1;
00048     
00049     _reset = 1;                // Reset the display
00050     wait_us(500);
00051     _reset = 0;
00052     wait_us(500);
00053     _reset = 1;
00054     
00055     wait_ms(5);
00056     
00057     wr_cmd(ST7735_SWRESET);           // SW reset
00058     wait_ms(150);
00059     
00060     wr_cmd(ST7735_SLPOUT);           // Sleep off
00061     wait_ms(255);
00062        
00063     wr_cmd(ST7735_FRMCTR1);         // Frame rate control
00064     _spi.write(0x01);                   
00065     _spi.write(0x2C);                   
00066     _spi.write(0x2D);                 
00067     
00068     wr_cmd(ST7735_FRMCTR2);
00069     _spi.write(0x01);
00070     _spi.write(0x2C);
00071     _spi.write(0x2D);
00072     
00073     wr_cmd(ST7735_FRMCTR3);
00074     _spi.write(0x01);
00075     _spi.write(0x2C);
00076     _spi.write(0x2D);
00077     _spi.write(0x01);
00078     _spi.write(0x2C);
00079     _spi.write(0x2D);
00080 
00081     wr_cmd(ST7735_INVCTR);
00082     _spi.write(0x07);
00083     
00084     wr_cmd(ST7735_PWCTR1);
00085     _spi.write(0xA2);
00086     _spi.write(0x02);
00087     _spi.write(0x84);
00088     
00089     wr_cmd(ST7735_PWCTR2);
00090     _spi.write(0xC5);
00091     
00092     wr_cmd(ST7735_PWCTR3);
00093     _spi.write(0x0A);
00094     _spi.write(0x00);
00095     
00096     wr_cmd(ST7735_PWCTR4);
00097     _spi.write(0x8A);
00098     _spi.write(0x2A);
00099     
00100     wr_cmd(ST7735_PWCTR5);
00101     _spi.write(0x8A);
00102     _spi.write(0xEE);
00103     
00104     wr_cmd(ST7735_VMCTR1);
00105     _spi.write(0x0E);
00106     
00107     wr_cmd(ST7735_INVOFF);
00108     
00109     wr_cmd(ST7735_MADCTL);          // Memory access control (directions)
00110     _spi.write(0xC8);                   // row addr/col adddr, bottom to top refresh
00111     
00112     wr_cmd(ST7735_COLMOD);          // Color mode
00113     _spi.write(0x05);                   // 16 bit color
00114     
00115     wr_cmd(ST7735_CASET);           // Column address set
00116     _spi.write(0x00);               
00117     _spi.write(0x02);
00118     _spi.write(0x00);
00119     _spi.write(0x7F+0x02);
00120     
00121     wr_cmd(ST7735_RASET);           // Row address set
00122     _spi.write(0x00);
00123     _spi.write(0x01);
00124     _spi.write(0x00);
00125     _spi.write(0x9F+0x01);
00126     
00127     wr_cmd(ST7735_NORON);           // Normal display on    
00128     wait_ms(10);
00129     
00130     wr_cmd(ST7735_DISPON);          // Main screen turn on
00131     wait_ms(100);
00132 }
00133 
00134 void Adafruit_358::pixel(int x, int y, int color)
00135 {
00136     wr_cmd(0x2A);
00137     _spi.write(x >> 8);
00138     _spi.write(x);
00139     _cs = 1;
00140     wr_cmd(0x2B);
00141     _spi.write(y >> 8);
00142     _spi.write(y);
00143     _cs = 1;
00144     wr_cmd(0x2C);
00145     _spi.write(color >> 8);
00146     _spi.write(color & 0xff);
00147     _cs = 1;    
00148 }
00149 
00150 void Adafruit_358::WindowMax (void)
00151 {
00152     window (0, 0, width(),  height());
00153 }
00154 
00155 void Adafruit_358::cls() {
00156     fillrect(0, 0, width(), height(), _background);
00157 }
00158 
00159 
00160 void Adafruit_358::set_font(unsigned char* f)
00161 {
00162     font = f;
00163 }
00164 
00165 void Adafruit_358::fillrect(int x0, int y0, int x1, int y1, uint16_t color)
00166 {
00167     for (int x_coord = x0; x_coord <= x1; x_coord++) {
00168         for (int y_coord = y0; y_coord <= y1; y_coord++) {
00169             pixel(x_coord, y_coord, color);
00170             }
00171         }
00172 }