An example program using the RA8875 Display controller with the touch-screen option.

Dependencies:   RA8875

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"           // tested with v112
00003 #include "RA8875.h"         // tested with v102
00004 
00005 
00006 const char * PROG_NAME = "RA8875 Touch Colors";
00007 const char * BUILD_DATE = __DATE__ " " __TIME__;
00008 
00009 // Define this for 800x480 panel, undefine for 480x272
00010 #define BIG_SCREEN
00011 
00012 // Define this for Cap touch panel, undefine for resistive
00013 #define CAP_TOUCH
00014 
00015 #ifdef CAP_TOUCH
00016 RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, I2C:{SDA,SCL,/IRQ}, name
00017 #else
00018 RA8875 lcd(p5, p6, p7, p12, NC, "tft");             // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, name
00019 LocalFileSystem local("local");                     // access to calibration file for resistive touch.
00020 #endif
00021 
00022 #ifdef BIG_SCREEN
00023     #define LCD_W 800
00024     #define LCD_H 480
00025     #define LCD_C 8         // color - bits per pixel
00026     #define DEF_RADIUS 50   // default radius of the fingerprint
00027     #define BL_NORM 25      // Backlight Normal setting (0 to 255)
00028 #else
00029     #define LCD_W 480
00030     #define LCD_H 272
00031     #define LCD_C 8         // color - bits per pixel
00032     #define DEF_RADIUS 20   // default radius of the fingerprint
00033     #define BL_NORM 25      // Backlight Normal setting (0 to 255)
00034 #endif
00035 
00036 Serial pc(USBTX, USBRX);
00037 
00038 #define min(a,b) ((a<b)?a:b)
00039 #define max(a,b) ((a>b)?a:b)
00040 
00041 int GetScreenCapture(void)
00042 {
00043     char fqfn[50];
00044     int i = 0;
00045     
00046     pc.printf("Screen Capture... ");
00047     for (i=1; i< 100; i++) {
00048         snprintf(fqfn, sizeof(fqfn), "/local/Screen%02d.bmp", i);
00049         FILE * fh = fopen(fqfn, "rb");
00050         if (!fh) {
00051             lcd.PrintScreen(0,0,LCD_W,LCD_H,fqfn);
00052             pc.printf(" as /local/Screen%02d.bmp\r\n", i);
00053             return i;
00054         } else {
00055             fclose(fh);     // close this and try the next
00056         }
00057     }
00058     return 0;
00059 }
00060 
00061 const char * Bitstream(uint16_t value)
00062 {
00063     static char buffer[17];
00064     char * p = buffer;
00065     uint16_t mask = 0x8000;
00066     
00067     do {
00068         *p++ = (value & mask) ? '1' : '0';
00069         mask >>= 1;
00070     } while (mask);
00071     *p = '\0';
00072     return buffer;
00073 }
00074 
00075 
00076 // Calibrate the resistive touch screen, and store the data on the
00077 // local file system.
00078 //
00079 void CalibrateTS(void)
00080 {
00081     FILE * fh;
00082     tpMatrix_t matrix;
00083     RetCode_t r;
00084     Timer testperiod;
00085  
00086     r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
00087     if (r == noerror) {
00088         fh = fopen("/local/tpcal.cfg", "wb");
00089         if (fh) {
00090             fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
00091             fclose(fh);
00092             printf("  tp cal written.\r\n");
00093             lcd.cls();
00094         } else {
00095             printf("  couldn't open tpcal file.\r\n");
00096         }
00097     } else {
00098         printf("error return: %d\r\n", r);
00099     }
00100     lcd.cls();
00101 }
00102 
00103 // Try to load a previous resistive touch screen calibration from storage. If it
00104 // doesn't exist, activate the touch screen calibration process.
00105 //
00106 void InitTS(void)
00107 {
00108     FILE * fh;
00109     tpMatrix_t matrix;
00110 
00111     fh = fopen("/local/tpcal.cfg", "rb");
00112     if (fh) {
00113         fread(&matrix, sizeof(tpMatrix_t), 1, fh);
00114         fclose(fh);
00115         lcd.TouchPanelSetMatrix(&matrix);
00116         printf("  tp cal loaded.\r\n");
00117     } else {
00118         CalibrateTS();
00119     }
00120 }
00121 
00122 
00123 int main() {
00124     pc.baud(460800);    // I like a snappy terminal, so crank it up!
00125     pc.printf("\r\n%s Build %s\r\n", PROG_NAME, BUILD_DATE);
00126  
00127     pc.printf("Turning on display\r\n");
00128     lcd.init(LCD_W,LCD_H,LCD_C);
00129     lcd.TouchPanelInit();
00130     lcd.Backlight_u8(BL_NORM);
00131     
00132     #ifndef CAP_TOUCH
00133     InitTS();               // resistive touch calibration
00134     #endif
00135     lcd.cls();
00136 
00137     lcd.printf("%s\r\nBuild %s", PROG_NAME, BUILD_DATE);
00138     
00139     // +----------------------------------------------------+
00140     // | Prog Name            [Capture]     (x,y) (xxx,yyy) |
00141     // | Build Date           [Capture]      rgb (RR,GG,BB) |
00142     // | +------------------------------------------------+ | y =  50
00143     // | |                Sample Shown Here               | |
00144     // | +------------------------------------------------+ | y =  89
00145     // | Red 0 to 255 ------------------------------------+ | y = 100
00146     // | |                                                | |
00147     // | +------------------------------------------------+ | y = 139
00148     // | Grn 0 to 255 ------------------------------------+ | y = 150
00149     // | |                                                | |
00150     // | +------------------------------------------------+ | y = 189
00151     // | Blu 0 to 255 ------------------------------------+ | y = 200
00152     // | |                                                | |
00153     // | +------------------------------------------------+ | y = 239
00154     // +----------------------------------------------------+ y = 271
00155     //   10                                             w-10
00156     rect_t RGBList[] = {
00157         { 10,100, LCD_W-10,139 },    // R
00158         { 10,150, LCD_W-10,189 },    // G
00159         { 10,200, LCD_W-10,239 },    // B
00160         { 10, 50, LCD_W-10, 89 }     // This for the Sample
00161     };
00162     rect_t PrintScreenRect = { LCD_W/2-30, 5, LCD_W/2+30, 40 };
00163     lcd.fillrect(RGBList[0], Red);
00164     lcd.fillrect(RGBList[1], Green);
00165     lcd.fillrect(RGBList[2], Blue);
00166     lcd.fillrect(PrintScreenRect, Gray);
00167     lcd.foreground(Blue);
00168     lcd.background(Gray);
00169     lcd.puts(LCD_W/2-28, 15, "Capture");
00170     lcd.background(Black);
00171     color_t rgb = Black;
00172     uint8_t rgbVal[3] = { 0, 0, 0 };
00173     
00174     for (;;) {
00175         point_t p;
00176         
00177         if (lcd.TouchPanelReadable(&p)) {
00178             lcd.foreground(Blue);
00179             lcd.SetTextCursor(LCD_W-72, 0);
00180             lcd.printf("(%3d,%3d)", p.x, p.y);
00181             
00182             if (lcd.Intersect(PrintScreenRect, p)) {
00183                 GetScreenCapture();
00184             }
00185             for (int i=0; i<3; i++) {
00186                 if (lcd.Intersect(RGBList[i], p)) {
00187                     uint8_t mag = (255 * (float)(p.x - RGBList[i].p1.x)) / (float)(RGBList[i].p2.x - RGBList[i].p1.x);
00188                     rgbVal[i] = mag;
00189                     lcd.SetTextCursor(LCD_W-80, 16);
00190                     lcd.foreground(Blue);
00191                     lcd.printf("(%02X,%02X,%02X)", rgbVal[0], rgbVal[1], rgbVal[2]);
00192                     rgb = RGB(rgbVal[0], rgbVal[1], rgbVal[2]);
00193                     pc.printf("RGB: %04X %s\r\n", rgb, Bitstream(rgb));
00194                     lcd.fillrect(RGBList[3], rgb);
00195                     break;
00196                 }
00197             }
00198         }
00199     }
00200 }