So this has been dead for a while but I just wanted to update the thread. I have been working with a Moddable Two which is an esp32 that has an integrated ILI9341 driven TFT display and FT6206 driven Capacitive touchscreen. I found this thread as I was looking for a way to input the passcode for a wifi config program I am working on. It's nothing special. Just learning how to use this hardware with Arduino as opposed to the Moddable SDK. Thanks to your keyboard code with some modification I'm well on my way. I'm posting the updated code for anyone else who's trying something similar.
//
//Thanks to Adafruit forums member Asteroid for the original sketch!
//
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// This is calibration data for the raw touch data to the screen coordinates
#define IsWithin(x, a, b) ((x>=a)&&(x<=b))
#define TS_MINX 0
#define TS_MINY 0
#define TS_MAXX 320
#define TS_MAXY 240
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ts = Adafruit_FT6206(); // for the cap touchscreen
#define TFT_CS 15 // specific to the Moddable Two
#define TFT_DC 2 //specific to the Moddable Two
//Haven't had luck with hardware SPI on the Moddable Two
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, 13, 14, 0, 12); //specific to the Moddable Two
#if (defined(__AVR__))
#include <avr\pgmspace.h>
#else
#include <pgmspace.h>
#endif
const char Mobile_KB[3][13] PROGMEM = {
{0, 13, 10, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'},
{1, 12, 9, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'},
{3, 10, 7, 'Z', 'X', 'C', 'V', 'B', 'N', 'M'},
};
const char Mobile_NumKeys[3][13] PROGMEM = {
{0, 13, 10, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'},
{0, 13, 10, '-', '/', ':', ';', '(', ')', '
, '&', '@', '"'},
{5, 8, 5, '.', ',', '?', '!', '''}
};
const char Mobile_SymKeys[3][13] PROGMEM = {
{0, 13, 10, '[', ']', '{', '}', '#', '%', '^', '*', '+', '='},
{4, 9, 6, '_', '\', '|', '~', '<', '>'}, //4
{5, 8, 5, '.', ',', '?', '!', '''}
};
const char textLimit = 25;
char MyBuffer[textLimit];
void MakeKB_Button(const char type[][13])
{
tft.setTextSize(2);
tft.setTextColor(0xffff, 0xf000);
for (int y = 0; y < 3; y++)
{
int ShiftRight = 15 * pgm_read_byte(&(type[y][0]));
for (int x = 3; x < 13; x++)
{
if (x >= pgm_read_byte(&(type[y][1]))) break;
drawButton(15 + (30 * (x - 3)) + ShiftRight, 100 + (30 * y), 20, 25); // this will draw the button on the screen by so many pixels
tft.setCursor(20 + (30 * (x - 3)) + ShiftRight, 105 + (30 * y));
tft.print(char(pgm_read_byte(&(type[y][x]))));
}
}
//ShiftKey
drawButton(15, 160, 35, 25);
tft.setCursor(27, 168);
tft.print('^');
//Special Characters
drawButton(15, 190, 35, 25);
tft.setCursor(21, 195);
tft.print(F("SP"));
//BackSpace
drawButton(270, 160, 35, 25);
tft.setCursor(276, 165);
tft.print(F("BS"));
//Return
drawButton(270, 190, 35, 25);
tft.setCursor(276, 195);
tft.print(F("RT"));
//Spacebar
drawButton(60, 190, 200, 25);
tft.setCursor(105, 195);
tft.print(F("SPACE BAR"));
}
void drawButton(int x, int y, int w, int h)
{
// grey
tft.fillRoundRect(x - 3, y + 3, w, h, 3, 0x8888); //Button Shading
// white
tft.fillRoundRect(x, y, w, h, 3, 0xffff);// outter button color
//red
tft.fillRoundRect(x + 1, y + 1, w - 1 * 2, h - 1 * 2, 3, 0xf800); //inner button color
}
void GetKeyPress(char * textBuffer)
{
char key = 0;
static bool shift = false, special = false, back = false, lastSp = false, lastSh = false;
static char bufIndex = 0;
if (ts.touched()) // changed for cap
{
//ShiftKey
if (TouchButton(15, 160, 35, 25))
{
shift = !shift;
delay(200);
}
//Special Characters
if (TouchButton(15, 190, 35, 25))
{
special = !special;
delay(200);
}
if (special != lastSp || shift != lastSh)
{
if (special)
{
if (shift)
{
tft.fillScreen(ILI9341_BLUE);
MakeKB_Button(Mobile_SymKeys);
}
else
{
tft.fillScreen(ILI9341_BLUE);
MakeKB_Button(Mobile_NumKeys);
}
}
else
{
tft.fillScreen(ILI9341_BLUE);
MakeKB_Button(Mobile_KB);
tft.setTextColor(0xffff, 0xf800);
}
if (special)
tft.setTextColor(0x0FF0, 0xf800);
else
tft.setTextColor(0xFFFF, 0xf800);
tft.setCursor(21, 195);
tft.print(F("SP"));
if (shift)
tft.setTextColor(0x0FF0, 0xf800);
else
tft.setTextColor(0xffff, 0xf800);
tft.setCursor(27, 168);
tft.print('^');
lastSh = shift;
lastSp = special;
lastSh = shift;
}
for (int y = 0; y < 3; y++)
{
int ShiftRight;
if (special)
{
if (shift)
ShiftRight = 15 * pgm_read_byte(&(Mobile_SymKeys[y][0]));
else
ShiftRight = 15 * pgm_read_byte(&(Mobile_NumKeys[y][0]));
}
else
ShiftRight = 15 * pgm_read_byte(&(Mobile_KB[y][0]));
for (int x = 3; x < 13; x++)
{
if (x >= (special ? (shift ? pgm_read_byte(&(Mobile_SymKeys[y][1])) : pgm_read_byte(&(Mobile_NumKeys[y][1]))) : pgm_read_byte(&(Mobile_KB[y][1])) )) break;
if (TouchButton(15 + (30 * (x - 3)) + ShiftRight, 100 + (30 * y), 20, 25)) // this will draw the button on the screen by so many pixels
{
if (bufIndex < (textLimit - 1))
{
delay(200);
if (special)
{
if (shift)
textBuffer[bufIndex] = pgm_read_byte(&(Mobile_SymKeys[y][x]));
else
textBuffer[bufIndex] = pgm_read_byte(&(Mobile_NumKeys[y][x]));
}
else
textBuffer[bufIndex] = (pgm_read_byte(&(Mobile_KB[y][x])) + (shift ? 0 : ('a' - 'A')));
bufIndex++;
}
break;
}
}
}
//Spacebar
if (TouchButton(60, 190, 200, 25))
{
textBuffer[bufIndex++] = ' ';
delay(200);
}
//BackSpace
if (TouchButton(270, 160, 35, 25))
{
if ((bufIndex) > 0)
bufIndex--;
textBuffer[bufIndex] = 0;
tft.setTextColor(0, ILI9341_BLUE);
tft.setCursor(15, 80);
tft.print(F(" "));
delay(200);
}
//Return
if (TouchButton(270, 190, 35, 25))
{
Serial.println(textBuffer);
while (bufIndex > 0)
{
bufIndex--;
textBuffer[bufIndex] = 0;
}
tft.setTextColor(0, ILI9341_BLUE);
tft.setCursor(15, 80);
tft.print(F(" "));
}
}
tft.setTextColor(0xffff, 0xf800);
tft.setCursor(15, 80);
tft.print(textBuffer);
}
void setup(void)
{
Serial.begin(115200);
tft.begin();
if (!ts.begin())
Serial.println(F("Unable to start touchscreen."));
else
Serial.println(F("Touchscreen started."));
tft.fillScreen(ILI9341_BLUE);
// origin = left,top landscape (USB left upper)
tft.setRotation(2);
MakeKB_Button(Mobile_KB);
}
void loop()
{
// See if there's any touch data for us
GetKeyPress(MyBuffer);
}
byte TouchButton(int x, int y, int w, int h)
{
int X, Y;
// Retrieve a point
TS_Point p = ts.getPoint();
Y = map(p.x, TS_MINY, TS_MAXY, tft.height(), 0);
X = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
return (IsWithin(X, x, x + w) & IsWithin(Y, y, y + h));
}