8 years, 6 months ago.

Touch Screen Calibration values

Can you tell me in a simple way how to obtain the calibrated touch screen values (after calibration) in either a string, character or integer form to store them in another location, SD card or IAP to reload after a reset. Other than the local filesystem.

Question relating to:

2 Answers

8 years, 6 months ago.

In mine, for the Freescale FRDM-k54F, I chose to store to the SDcard. A very straight forward translation, but SDfileSystem.h and FATFileSystem.h had to be added.

#include "mbed.h"           // tested with v92
#include "RA8875.h"         // tested with v80
#include "SDFileSystem.h" 
#include "FATFileSystem.h" 


SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");

RA8875 lcd(PTD2, PTD3, PTD1, PTD0, NC, "tft");    // FRDM-K64F   MOSI, MISO, SCK, /ChipSelect, /reset, name

void CalibrateTS(void)
{
    FILE * fh;
    tpMatrix_t matrix;
    RetCode_t r;

    r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
    INFO("  ret: %d", r);
    if (r == noerror) {
        fh = fopen("/sd/tpcal.cfg", "wb");
        if (fh) {
            fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
            fclose(fh);
            INFO("  tp cal written.");
        } else {
            WARN("  couldn't open tpcal file.");
        }
    } else {
        ERR("error return: %d", r);
    }
}


void InitTS(void)
{
    FILE * fh;
    tpMatrix_t matrix;

    fh = fopen("/sd/tpcal.cfg", "rb");
    if (fh) {
        fread(&matrix, sizeof(tpMatrix_t), 1, fh);
        fclose(fh);
        lcd.TouchPanelSetMatrix(&matrix);
        INFO("  tp cal loaded.");
    } else {
        CalibrateTS();
    }
}



Accepted Answer

Thanks' Michael, this now gives three ways to store the Calibration values, however at some point I will try to extract the values into readable characters so these can be 'manually' entered at start up. Once calibration has been done these values should remain pretty static for each individual display and would save code space and the need to store them off the MCU. The IAP MCU Flash storage method should also work on the K64F, I use Erik's API here:

https://developer.mbed.org/users/Sissors/code/FreescaleIAP/

Using the K64F will take up a 4k chunk of Flash, but other project information can be stored here too.

posted by Paul Staron 19 Oct 2015
8 years, 6 months ago.

Hi Paul,

The mPaint example shows how to perform the calibration and in that process "matrix" is filled in with that data. The example shows storing to the local file system, but if you have another storage path, you can use that. You can see in the fwrite that it writes the "size" of the matrix, just as a binary blob. If the fopen had a path to an sdcard, or if you used APIs to write (and restore) from eerom, that should work just fine.

Here's the two relevant functions:

void CalibrateTS(void)
{
    FILE * fh;
    tpMatrix_t matrix;
    RetCode_t r;
 
    r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
    INFO("  ret: %d", r);
    if (r == noerror) {
        fh = fopen("/local/tpcal.cfg", "wb");
        if (fh) {
            fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
            fclose(fh);
            INFO("  tp cal written.");
        } else {
            WARN("  couldn't open tpcal file.");
        }
    } else {
        ERR("error return: %d", r);
    }
}
 
 
void InitTS(void)
{
    FILE * fh;
    tpMatrix_t matrix;
 
    fh = fopen("/local/tpcal.cfg", "rb");
    if (fh) {
        fread(&matrix, sizeof(tpMatrix_t), 1, fh);
        fclose(fh);
        lcd.TouchPanelSetMatrix(&matrix);
        INFO("  tp cal loaded.");
    } else {
        CalibrateTS();
    }
}

Got there in the end. In case anyone needs it, below works but may not be the most efficient way.

Touch Calibration using Freescale IAP to store values in MCU Flash

#include "mbed.h"
#include "RA8875.h"
#include "FreescaleIAP.h"

RA8875 lcd(PTD2, PTD3, PTD1, PTD4, NC, "tft");   // MOSI-SDI, MISO-SDO, SCK, ChipSelect, reset, name    Teensy3.1
//RA8875 lcd(D11, D12, D13, D10, NC, "tft");     // MOSI-SDI, MISO-SDO, SCK, ChipSelect, reset, name    FRDM boards

DigitalOut led(LED1);

void CalibrateTS(void)
{
    tpMatrix_t matrix;
    RetCode_t r; 
    r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
    if (r == noerror) {        
        int address = flash_size() - SECTOR_SIZE;                   // Point to start of last sector in MCU Flash    
        erase_sector(address);                                      // Erase last sector, can't over-write data in MCU Flash memory.
        program_flash(address, (char*)&matrix, sizeof(tpMatrix_t)); // Write Touch cal (28) characters in last sector 
    }
} 
 
void InitTS(void)
{  
    tpMatrix_t matrix;
    int address = flash_size() - SECTOR_SIZE;    
    int *check = (int*)address;   
    if (check[0] != -1){                            // Check if blank(erased after reprogramming) or valid cal data written                 
        char* dataname = (char*)address;            // Point to start of last sector of MCU Flash
        memcpy(&matrix,dataname,sizeof(tpMatrix_t));// Copy cal data to the matrix container                  
        lcd.TouchPanelSetMatrix(&matrix);           // Set cal data    
    } 
    else{
        CalibrateTS();  // Blank MCU Flash, run Calibrate function. 
        }
}
 
int main() { 

    //lcd.init();
    lcd.init(800,480);   
    lcd.TouchPanelInit();    
    InitTS();
    lcd.cls();
    lcd.background(0,0,0);
    lcd.foreground(255,255,255);
    lcd.SetTextCursor(40, 20);
    lcd.printf("Touch screen to test ");
    
    while(1){  
        
        point_t p;
        TouchCode_t touchcode = lcd.TouchPanelReadable(&p);      
        if (touchcode != no_touch) {
            led=1;              
            lcd.SetTextCursor(40, 60);
            lcd.printf("(x - %3d  y - %3d) ",p.x,p.y);
        }
        led=0;              
    }
    
}
posted by Paul Staron 16 Oct 2015

Hi Paul. Glad you got there. I haven't used the IAP interface, but what you have found is that you don't need to know what's inside the "matrix" blob of data, you just have to save and restore it using available APIs. Of course, digging deeper into the source you can see what makes up that set of data.

posted by David Smart 18 Oct 2015