MCUFRIEND_kbv Guide

Hi there,

Thanks for your time. I was just wondering if there is an official guide for the MCUFRIEND_kbv library, that lists each function and gives a quick example of implementation? This will help a lot when I am exploring capabilities for a TFT I recently bought. I can find online some common features and uses, but I am unsure of the more complicated or obscure functions. Thank you very much.

You always can see the source code and comments in it - the library is available on Github

There are a lot of source codes. I have tried some of them, but which ones define the functions?

All of them...
Just play around with it, you'll pick it up pretty quick. And if you need help, @david_prentice can help you as he wrote the library.

2 Likes

Function definitions usually contains in <library_name>.h file.

As far I know the author of MCUFRIEND_kbv library is one of members of the forum. I hope he answers you in more detail.

david_prentice

Thanks.

Simple answer is that I am lazy.
If someone would like to write a guide I would be grateful.

Otherwise I suggest that you simply run all of the examples.
If there is something that interests you, study the source code.
Or just quote the example sketch by name and ask for help.

MCUFRIEND_kbv is just a regular "Adafruit_GFX" style library.
So you can run most graphics programs.
e.g. by including MCUFRIEND_kbv.h, MCUFRIEND_kbv constructor and tft.begin(tft.readID())

Study any "Adafruit_GFX" tutorials.

The only extra "hardware" features are the ability to read registers and display memory.

There are two different approaches to providing library examples:

  1. many separate tiny sketches to illustrate each function.
  2. stuff as much into a few big example sketches.

"graphictest_kbv.ino" stuffs as much as it can into a Uno.
But this means it should show up any problems e.g. wrong colours, directions, readPixel errors, ...

I always advise you to start with "graphictest_kbv.ino"
You don't need to understand everything.
Just observe the graphics.

David.

2 Likes

So after exploring the different examples, I have tried to implement it in my own code. However, when I try to get the touch screen information, something weird happens.
With line 29 (TSPoint ts = touch.getPoint();) commented out, I can see the circle and the rectangle.

#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>

MCUFRIEND_kbv tft;
TouchScreen touch(8, A3, A2, 9, 300);

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GRAY    0x8410


void setup() {
  tft.reset();
  Serial.begin(9600);
  uint16_t ID = tft.readID();
  tft.setRotation(1);
  tft.begin(ID);
  tft.fillScreen(BLACK);
}

void loop() {
  //TSPoint ts = touch.getPoint();
  /*if (ts.z > touch.pressureThreshhold) {
    Serial.print("X = ");
    Serial.println(ts.x);
    Serial.print("Y = ");
    Serial.println(ts.y);
    Serial.println("");
  }*/
  tft.fillCircle(70, 40, 30, YELLOW);
  tft.drawRect(100, 200, 50, 20, MAGENTA);
  delay(500);
}

However, if I uncomment it to retrieve the touch screen information, I can no longer see the rectangle nor the circle. Why is this?

(Restored from edit)

Your question is not related to the MCUFRIEND library.

For touchscreen interactions you used

library. Try to solve your problem with it.

I disagree. The problem is related to any GFX style library that shares pins with a raw resistive TouchScreen.h panel.

@ philip_l_mega2560
Please restore your original question. e.g. click on the red pen icon in #10

From MCUFRIEND_kbv Button_simple.ino example

bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);      //because TFT control pins
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    }
    return pressed;
}

from Adafruit_TFTLCD tftpaint_shield.ino example

void loop()
{
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);

  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    /*

from MCUFRIEND_kbv Touch_shield_new.ino

    tp = ts.getPoint();   //tp.x, tp.y are ADC values

    // if sharing pins, you'll need to fix the directions of the touchscreen pins
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    // we have some minimum pressure we consider 'valid'
    // pressure of 0 means no pressing!

    if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {

1 Like

Oh, thank you. I didn't realise I had to restore the shared pins. It works now.

No problem. It is wise to leave your question(s) available for 24 hours or so.

The world goes to bed at different times!

If you do make a substantial "edit" please mention it. e.g. "I have restored original message"

David.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.