GLCD and Touchpad Library

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

GLCD and Touchpad Library

Postby Tectu » Thu Jun 07, 2012 8:41 pm

Hello Folks,

After I got very very good support from you guys and ChibiOS itself, I'd like to give something back in return :)

I am currently working on a complete library for GLCDs and Touchpads. I developed it with a 3.2" 320x240 pixel TFT with a SSD1289 controller. The touchpad controller is a ADS7843.
It's the well known HY32 module from waveshare (16$ at ebay).
Sadly the GLCD library does only support direct GPIO 16 lines connection to the LCD yet, but SPI should be implementable easily, since you only need to change 3 __inline lowlevel functions.
The GLCD does support all four modes (portrait, landscape, portrait-inverted, landscape-inverted). Also, when you read the current touchpad coordinates using tpReadX() and tpReadY(), the coordinates you'll get are relative to the current screen zero-point (GLCD zero point is always in the upper left corner in each mode). This means, that you can simply do things like: lcdDrawPixel(tpReadX(), tpReadY(), Green); without needing to calculate touchpad-to-LCD offsets in each mode.

The touchpad does run in 12-bit mode. There's also a calibration mode which works great and very accurate (I can draw pixel-vise here). You do need to do a calibration every reset, because it dosen't store the calibrationvalues in the flash yet.


Besides the standard GLCD and touchpad features, I started to build a GUI on top of it.
The guiDrawButton() function draws a button at the given coordinates with the given size with the given button color, writes a string into the middle of it with the given font color, creates a thread which does set the variable to 1 if you touch inside the button area and set's it to 0 otherwise. This allows to make very easy user interfaces :)

For example, you can write in your main program:

Code: Select all

static msg_t Thread2(void *arg) {
    uint8_t startState = 0, stopState = 0;

    guiDrawButton(10, 10, 100, 100, "START", Black, Green, &startState);
    guiDrawButton(10, 110, 100, 200, "STOP", Black, Red, &stopState);

    while(TRUE) {
        if(startState)
            palSetPad(GPIOD, GPIOD_LED2);
        if(stopState)
            palClearPad(GPIOD, GPIOD_LED2);
    }   
}



Besides the GUI, I made a function which draws a 2D graphs. You just have to pass a two dimensional array, containing X and Y coordinates which get drawn as points, or connected with lines.

Here are two pictures:

Code used in the pictures:

Code: Select all

uint16_t coord[][2] = {{0,0}, {50,50}, {75,50}, {150,120}, {185,120}, {195,40},{220,40},{260, 0}};

graphDrawSystem(20, lcdGetWidth()-20, lcdGetHeight()-20, 20, Yellow);       // draw the yellow arrows with the grid
graphDrawNet(coord, sizeof(coord)/sizeof(coord[0]), 2, Red, Green);         // draw the points with green dots, radius 2 pixels


My library contains the following files:
    glcd.c / glcd.h
    fonts.c / fonts.h
    touchpad.c / touchpad.h
    gui.c / gui.h
    graph.c / graph.h
As you can see, everything is very bare, naked and very transparent. You can just leave all the other stuff and just take the glcd.c und glcd.h files when you don't need the touchpad, GUI and Graph drawing functions.

You can find the complete library here: https://github.com/Tectu/ChibiOS-LCD-Driver

Of course there is still much much much work to do, but I hope that someone can use this already. :)
The library does also lack of documentation, but i guess that 99% of the functions are very self explanatory.
Please just ask if you want to know anything more.
I am open for any kind of optimizations :)

~ Tectu
Last edited by Tectu on Fri Jun 08, 2012 9:45 pm, edited 6 times in total.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: GLCD and Touchpad Library

Postby Giovanni » Thu Jun 07, 2012 8:51 pm

Good work,

I have to verify the LCD on my ST board is the same, I wish to give it a try.

Giovanni

indigos
Posts: 14
Joined: Sat Jun 02, 2012 10:46 am

Re: GLCD and Touchpad Library

Postby indigos » Fri Jun 08, 2012 9:26 pm

Great work!!!

I have a H32D with SSD1289 (are you sure you have SSD1298?) and TSC2046 (TI evolution of ADS7846) taken by EgoChina for $19 shipment included.
The development test for this LCD Touch was in my todo list, thank you for remove it. I'm a newbie now with ARM and ChibiOS.
I see source code of rossum's microtouch and he use this method for touch: read the pressure and when it increase read X and Y values and make average values.
I don't know if is better, but I'd like test it.

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: GLCD and Touchpad Library

Postby Tectu » Fri Jun 08, 2012 9:49 pm

indigos wrote:Great work!!!

Thanks :)

indigos wrote:I have a H32D with SSD1289 (are you sure you have SSD1298?)

I do have SSD1289, that was a typo, thanks for the fix!


indigos wrote:read the pressure and when it increase read X and Y values and make average values.

I am not sure if I understand this correctly. Calling tpReadX() or tpReadY() in my library has the following effect:
    - make a dummy readout before each "real" readout (this reduces noise very very well!)
    - make 3 readouts
    - calculate average and return

In fact you can change the amount of meassurements before calculating the average in the touchpad.h file. It's the #define CONVERSIONS 3

Keep hacking!


~ Tectu

indigos
Posts: 14
Joined: Sat Jun 02, 2012 10:46 am

Re: GLCD and Touchpad Library

Postby indigos » Fri Jun 08, 2012 10:33 pm

Sorry I read now the datasheet of ADS7843, there isn't support for Pressure Measurement.
This is evolution of TI's touch controllers:
ADS7843 => ADS7846 => TSC2046
With ADS7846 you can read pressure. The datasheet report: this information is useful for understand if touch is pen or finger.

This is the project rossum's microtouch. He haven't touch controller, use ADC of microcontroller.
This is a little part of source code (src/hardware/Hardware.cpp), ReadZ read the pressure.

Code: Select all

// Get a single valid sample
u8 TouchSample(int* xx, int* yy)
{
    int z1,z2,x;
    ReadZ(&z1,&z2); // left edge

    // z2 = 0, z1 = 1
   // As pressure increases z2->z1 (1-z2/z1)*x
   // (z1-z2)*x/z1 measures resistance
   if (z2 < 8)
       return 0; // not touching

   x = ReadX();
   int t = (long)z2*x/z1;
   t += abs(512-x)>>3; // horrible hack to make it slightly more linear across x
   t -= 84;
   if (t <= 0)
      return 0;
   t += t;

   *yy = ReadY();
   *xx = x;
   if (t > 255)
      t = 255;
   return t; // Range to 0..255
}

TouchSample is called many times for average. rossum call it 5 times.
3 + 3, 5, 9 times don't matter, when the result value is good.
I think also 8 bit value is good.

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: GLCD and Touchpad Library

Postby Tectu » Fri Jun 08, 2012 10:36 pm

Ah, I see know what you mean, but I don't think that this is related, since the most people don't have this feature.
Thanks for the information anyways :)

Did you try out my library yet?


~ Tectu

indigos
Posts: 14
Joined: Sat Jun 02, 2012 10:46 am

Re: GLCD and Touchpad Library

Postby indigos » Fri Jun 08, 2012 11:00 pm

Tectu wrote:Did you try out my library yet?

Not yet, I will try it shortly. I hope tomorrow.

Sorry I need a little information from you.
Where is "dummy readout before each "real" readout"? I can't understand.
readX or readY are simple spi operations:
- chip select
- send data
- read data

tpReadRealY or tpReadRealX
call readX or readY CONVERSIONS times and make an average

tpReadX or tpReadY
call tpReadRealY or tpReadRealX and return correct value with calibration.

Maybe I don't see some part of your code.

EDIT
Tectu wrote:Ah, I see know what you mean, but I don't think that this is related, since the most people don't have this feature.
Thanks for the information anyways :)

In description of waveshare's LCD there is:
Touch Screen Controller: XPT2046 (compatible with ADS7843)
XPT2046 has pressure measurement.

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: GLCD and Touchpad Library

Postby Tectu » Fri Jun 08, 2012 11:14 pm

The dummy readout is a REAL readout, but the value it returns will be ignored. This is because the first readout does have much noise on it. That's in every library from every touchpadcontroller and in every datasheet ;-)

The library has the following read functions:
    - tpReadRealX() this does do a dumm read-out before each "real" read-out (touchpad.c line 62) and does CONVERSIONS number of conversions, calculates the average and returns that result.
    - tpReadX() this does call tpReadRealX() and just uses the calibration values to return a calibrated readout of the X coordinate.

You are supposed to ALWAYS just use tpReadX() and tpReadY(). The ReadReal functions are static and not accessible outside of the touchpad.c file.
The ReadReal functions are just "helper" functions for tpReadX() and tpReadY().
Also note, that tpReadX() and tpReadY() do work without calling tpCalibrate() before. The results just won't be very accurate.

I hope this helps somehow?


~ Tectu

indigos
Posts: 14
Joined: Sat Jun 02, 2012 10:46 am

Re: GLCD and Touchpad Library

Postby indigos » Sat Jun 09, 2012 7:47 am

Sorry my question was very very stupid. :oops: :oops:

Code: Select all

        for(i=0; i<CONVERSIONS; i++) {
                readX(); // dummy readout
                results += readX(); // real read
        }

I don't know because my brain ignored the row readX().
Sorry for lost time. :oops:

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: GLCD and Touchpad Library

Postby Tectu » Sat Jun 09, 2012 11:16 am

Hey, that's no problem at all!

First, I wrote in my first post that you shall ask, when you have ANY questions, second, there are no stupid questions :D


~ Tectu


Return to “LCD Driver and Graphic Framework”

Who is online

Users browsing this forum: No registered users and 34 guests