hallo,
ich hätte da 2 Sourcecodes für die Grafik- und Font-Ausgabe, die man exemplarisch verwenden könnte, und wie ich sie auch selber bereits mit g++ auf meinen Arduinos verwende.
Zusätzlich hätte ich noch einen dritten Source-Code (UTFT UTouch) für Touchscreen-Funktionen mit Buttons.
ein ganz einfacher Test (eigentlich ein Mini-TFT-speed-test), der nur Basisfunktionen testet (speziell für die o.s. ILI9341_due-lib) ist dieser hier:
Code:
// HaWe TFT Brickbench
// ported to ILI9341_due library by HaWE
#include <SPI.h>
// ILI9341_due NEW lib by Marek Buriak http://marekburiak.github.io/ILI9341_due/
#include "ILI9341_due_config.h"
#include "ILI9341_due.h"
#include "SystemFont5x7.h"
//#include "Streaming.h"
// For the Adafruit shield, these are the default.
/*
#define TFT_RST 8
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
*/
#define tft_cs 50
#define tft_dc 49
#define tft_rst 0
ILI9341_due tft = ILI9341_due(tft_cs, tft_dc, tft_rst);
char textBuff[20];
// Color set
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
//#define BLUE 0x001F
#define BLUE 0x102E
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define DARKGREEN 0x03E0
#define WHITE 0xFFFF
uint16_t color;
uint16_t colorFONDO = BLACK;
#define TimerMS() millis()
unsigned long runtime[8];
void TFTprint(char sbuf[], int16_t x, int16_t y) {
tft.cursorToXY(x, y);
tft.print(sbuf);
}
inline void displayValues() {
char buf[120];
tft.fillScreen(BLACK); // clrscr()
sprintf (buf, "%3d %9ld int_Add", 0, runtime[0]); TFTprint(buf, 0,10);
sprintf (buf, "%3d %9ld int_Mult", 1, runtime[1]); TFTprint(buf, 0,20);
sprintf (buf, "%3d %9ld float_op", 2, runtime[2]); TFTprint(buf, 0,30);
sprintf (buf, "%3d %9ld randomize", 3, runtime[3]); TFTprint(buf, 0,40);
sprintf (buf, "%3d %9ld matrx_algb", 4, runtime[4]); TFTprint(buf, 0,50);
sprintf (buf, "%3d %9ld arr_sort", 5, runtime[5]); TFTprint(buf, 0,60);
sprintf (buf, "%3d %9ld TextOut", 6, runtime[6]); TFTprint(buf, 0,70);
sprintf (buf, "%3d %9ld Graphics", 7, runtime[7]); TFTprint(buf, 0,80);
}
int32_t test_TextOut(){
int y;
char buf[120];
for(y=0;y<20;++y) {
tft.fillScreen(BLACK); // clrscr()
sprintf (buf, "%3d %9d int_Add", y, 1000); TFTprint(buf, 0,10);
sprintf (buf, "%3d %9d int_Mult", 0, 1010); TFTprint(buf, 0,20);
sprintf (buf, "%3d %9d float_op", 0, 1020); TFTprint(buf, 0,30);
sprintf (buf, "%3d %9d randomize", 0, 1030); TFTprint(buf, 0,40);
sprintf (buf, "%3d %9d matrx_algb", 0, 1040); TFTprint(buf, 0,50);
sprintf (buf, "%3d %9d arr_sort", 0, 1050); TFTprint(buf, 0,60);
sprintf (buf, "%3d %9d displ_txt", 0, 1060); TFTprint(buf, 0,70);
sprintf (buf, "%3d %9d testing...", 0, 1070); TFTprint(buf, 0,80);
}
return y;
}
int32_t test_graphics(){
int y;
char buf[120];
for(y=0;y<100;++y) {
tft.fillScreen(BLACK);
sprintf (buf, "%3d", y); TFTprint(buf, 0,80); // outcomment for downwards compatibility
tft.drawCircle(50, 40, 10, WHITE);
tft.fillCircle(30, 24, 10, WHITE);
tft.drawLine(10, 10, 60, 60, WHITE);
tft.drawLine(50, 20, 90, 70, WHITE);
tft.drawRect(20, 20, 40, 40, WHITE);
tft.fillRect(65, 25, 20, 30, WHITE);
//tft.drawEllipse(70, 30, 15, 20); // original test
tft.drawCircle(70, 30, 15, WHITE); // alternatively, just if no drawEclipse is avaiable in the Arduino graph libs!
}
return y;
}
int test(){
unsigned long time0, x, y;
double s;
char buf[120];
int i;
float f;
// lcd display text / graphs
time0=TimerMS();
s=test_TextOut();
runtime[6]=TimerMS()-time0;
sprintf (buf, "%3d %9ld TextOut", 6, runtime[6]); Serial.println( buf);
TFTprint(buf, 0,70);
time0=TimerMS();
s=test_graphics();
runtime[7]=TimerMS()-time0;
sprintf (buf, "%3d %9ld Graphics", 7, runtime[7]); Serial.println( buf);
TFTprint(buf, 0,80);
Serial.println();
y = 0;
for (x = 0; x < 8; ++x) {
y += runtime[x];
}
displayValues();
sprintf (buf, "gesamt ms: %9ld ", y);
Serial.println( buf);
TFTprint(buf, 0,110);
x=50000000.0/y;
sprintf (buf, "benchmark: %9ld ", x);
Serial.println( buf);
TFTprint(buf, 0,120);
return 1;
}
void setup() {
Serial.begin(115200);
// Setup the LCD
tft.begin();
tft.setRotation(iliRotation270);
tft.fillScreen(colorFONDO);
tft.setFont(SystemFont5x7);
tft.setTextColor(WHITE);
Serial.println("tft started");
}
void loop() {
char buf[120];
test();
sprintf (buf, "Ende HaWe brickbench");
Serial.println( buf);
TFTprint(buf, 0, 140);
while(1);
}
der Test, der speziell alle Grafikfunktionen zeigt, wie sie oben im Video zusehen sind, ist dieser hier:
Code:
// UTFT Demo ported to ILI9341_due library by TFTLCDCyg
// Based on Demo 320x240 Serial of UTFT library
// UTFT-web: http://www.henningkarlsen.com/electronics
#include <SPI.h>
// ILI9341_due NEW lib by Marek Buriak http://marekburiak.github.io/ILI9341_due/
#include "ILI9341_due_config.h"
#include "ILI9341_due.h"
#include "SystemFont5x7.h"
//#include "Streaming.h"
// For the Adafruit shield, these are the default.
#define TFT_RST 8
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC, TFT_RST);
char textBuff[20];
// Color set
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
//#define BLUE 0x001F
#define BLUE 0x102E
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define DARKGREEN 0x03E0
#define WHITE 0xFFFF
uint16_t color;
uint16_t colorFONDO = BLACK;
void setup()
{
randomSeed(analogRead(0));
// TFT 2.2" SPI
Serial.begin(9600);
tft.begin();
tft.setRotation(iliRotation270);
tft.fillScreen(colorFONDO);
tft.setFont(SystemFont5x7);
}
void ILI9341duenodelay()
{
int buf[318];
int x, x2;
int y, y2;
int r;
tft.fillScreen(colorFONDO);
int timeinit = millis();
//ILI9341due NEW
tft.fillRect(0, 0, 320, 15, RED);
tft.setTextColor(WHITE, RED);
tft.printAlignedOffseted(F("* ILI9341_due UTFT 240x320 Demo *"), gTextAlignTopCenter, 0, 3);
tft.fillRect(0, 226, 320, 240, tft.color565(64, 64, 64));
tft.setTextColor(YELLOW, tft.color565(64, 64, 64));
tft.printAlignedOffseted("<http://electronics.henningkarlsen.com>", gTextAlignBottomCenter, 0, -3);
//ILI9341due NEW
tft.drawRect(0, 15, 320, 211, BLUE);
//ILI9341due NEW
// Draw crosshairs
tft.drawLine(159, 15, 159, 224, BLUE);
tft.drawLine(1, 119, 318, 119, BLUE);
for (int i = 9; i < 310; i += 10)
tft.drawLine(i, 117, i, 121, BLUE);
for (int i = 19; i < 220; i += 10)
tft.drawLine(157, i, 161, i, BLUE);
// Draw sin-, cos- and tan-lines
tft.setTextColor(CYAN, BLACK);
tft.printAt("Sin", 5, 17);
for (int i = 1; i < 318; i++)
{
tft.drawPixel(i, 119 + (sin(((i*1.13)*3.14) / 180) * 95), CYAN);
}
tft.setTextColor(RED, BLACK);
tft.printAt("Cos", 5, 29);
for (int i = 1; i < 318; i++)
{
tft.drawPixel(i, 119 + (cos(((i*1.13)*3.14) / 180) * 95), RED);
}
tft.setTextColor(YELLOW, BLACK);
tft.printAt("Tan", 5, 41);
for (int i = 1; i < 318; i++)
{
tft.drawPixel(i, 119 + (tan(((i*1.13)*3.14) / 180)), YELLOW);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
tft.drawLine(159, 16, 159, 224, BLUE);
tft.drawLine(1, 119, 318, 119, BLUE);
// Draw a moving sinewave
x = 1;
for (int i = 1; i < (318 * 20); i++)
{
x++;
if (x == 319)
x = 1;
if (i > 319)
{
if ((x == 159) || (buf[x - 1] == 119))
color = BLUE;
else
color = BLACK;
tft.drawPixel(x, buf[x - 1], color);
}
y = 119 + (sin(((i*1.1)*3.14) / 180)*(90 - (i / 100)));
tft.drawPixel(x, y, CYAN);
buf[x - 1] = y;
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
// Draw some filled rectangles
for (int i = 1; i < 6; i++)
{
switch (i)
{
case 1:
color = MAGENTA;
break;
case 2:
color = RED;
break;
case 3:
color = GREEN;
break;
case 4:
color = BLUE;
break;
case 5:
color = YELLOW;
break;
}
tft.fillRect(70 + (i * 20), 30 + (i * 20), 60, 60, color);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
// Draw some filled, rounded rectangles
for (int i = 1; i < 6; i++)
{
switch (i)
{
case 1:
color = MAGENTA;
break;
case 2:
color = RED;
break;
case 3:
color = GREEN;
break;
case 4:
color = BLUE;
break;
case 5:
color = YELLOW;
break;
}
tft.fillRoundRect(190 - (i * 20), 30 + (i * 20), 60, 60, 3, color);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
// Draw some filled circles
for (int i = 1; i < 6; i++)
{
switch (i)
{
case 1:
color = MAGENTA;
break;
case 2:
color = RED;
break;
case 3:
color = GREEN;
break;
case 4:
color = BLUE;
break;
case 5:
color = YELLOW;
break;
}
tft.fillCircle(100 + (i * 20), 60 + (i * 20), 30, color);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
// Draw some lines in a pattern
for (int i = 17; i < 222; i += 5)
{
tft.drawLine(1, i, (i*1.44) - 10, 224, RED);
}
for (int i = 222; i > 17; i -= 5)
{
tft.drawLine(318, i, (i*1.44) - 11, 17, RED);
}
for (int i = 222; i > 17; i -= 5)
{
tft.drawLine(1, i, 331 - (i*1.44), 17, CYAN);
}
for (int i = 17; i < 222; i += 5)
{
tft.drawLine(318, i, 330 - (i*1.44), 223, CYAN);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
// Draw some random circles
for (int i = 0; i < 100; i++)
{
color = tft.color565(random(255), random(255), random(255));
x = 32 + random(256);
y = 45 + random(146);
r = random(30);
tft.drawCircle(x, y, r, color);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
// Draw some random rectangles
for (int i = 0; i < 100; i++)
{
color = tft.color565(random(255), random(255), random(255));
x = random(316);
y = random(207);
x2 = random(316 - x);
y2 = random(207 - y);
tft.drawRect(2 + x, 16 + y, x2, y2, color);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
// Draw some random rounded rectangles
for (int i = 0; i < 100; i++)
{
color = tft.color565(random(255), random(255), random(255));
x = random(310);
y = random(201);
x2 = random(310 - x);
y2 = random(201 - y);
tft.drawRoundRect(2 + x, 16 + y, x2 + 6, y2 + 6, 3, color);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
for (int i = 0; i < 100; i++)
{
color = tft.color565(random(255), random(255), random(255));
x = 2 + random(316);
y = 16 + random(209);
x2 = 2 + random(316);
y2 = 16 + random(209);
tft.drawLine(x, y, x2, y2, color);
}
//delay(2000);
//ILI9341due NEW
tft.fillRect(1, 16, 318, 209, BLACK);
for (int i = 0; i < 10000; i++)
{
color = tft.color565(random(255), random(255), random(255));
tft.drawPixel(2 + random(316), 16 + random(209), color);
}
int timetest = millis() - timeinit;
//delay(2000);
//ILI9341due NEW
tft.fillRect(0, 0, 320, 240, BLUE);
tft.fillRoundRect(80, 70, 159, 99, 3, RED);
tft.setTextColor(WHITE, RED);
tft.printAt("That's it!", 130, 93);
tft.printAt("Restarting in a", 112, 119);
tft.printAt("few seconds...", 117, 132);
tft.setTextColor(WHITE, BLUE);
tft.printAt("Runtime: (msecs)", 112, 210);
sprintf(textBuff, "%d", timetest);
tft.printAt(textBuff, 146, 225);
}
void loop()
{
ILI9341duenodelay();
delay(5000);
}
UTFT Utouch:
Code:
// UTouch_ButtonTest (C)2010-2012 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how create and use buttons.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
#include <UTFT.h>
#include <UTouch.h>
// Declare which fonts we will be using
extern uint8_t BigFont[];
// Uncomment the next two lines for the Arduino 2009/UNO
//UTFT myGLCD(ITDB24D,19,18,17,16); // Remember to change the model parameter to suit your display module!
//UTouch myTouch(15,10,14,9,8);
// Uncomment the next two lines for the Arduino Mega
UTFT myGLCD(ITDB32S, 38,39,40,41); // Remember to change the model parameter to suit your display module!
UTouch myTouch(6,5,4,3,2);
int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";
/*************************
** Custom functions **
*************************/
void drawButtons()
{
// Draw the upper row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.printNumI(x+1, 27+(x*60), 27);
}
// Draw the center row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
if (x<4)
myGLCD.printNumI(x+6, 27+(x*60), 87);
}
myGLCD.print("0", 267, 87);
// Draw the lower row of buttons
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10, 130, 150, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 130, 150, 180);
myGLCD.print("Clear", 40, 147);
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (160, 130, 300, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (160, 130, 300, 180);
myGLCD.print("Enter", 190, 147);
myGLCD.setBackColor (0, 0, 0);
}
void updateStr(int val)
{
if (stCurrentLen<20)
{
stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, LEFT, 224);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
/*************************
** Required functions **
*************************/
void setup()
{
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(0, 0, 255);
drawButtons();
}
void loop()
{
while (true)
{
if (myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((y>=10) && (y<=60)) // Upper row
{
if ((x>=10) && (x<=60)) // Button: 1
{
waitForIt(10, 10, 60, 60);
updateStr('1');
}
if ((x>=70) && (x<=120)) // Button: 2
{
waitForIt(70, 10, 120, 60);
updateStr('2');
}
if ((x>=130) && (x<=180)) // Button: 3
{
waitForIt(130, 10, 180, 60);
updateStr('3');
}
if ((x>=190) && (x<=240)) // Button: 4
{
waitForIt(190, 10, 240, 60);
updateStr('4');
}
if ((x>=250) && (x<=300)) // Button: 5
{
waitForIt(250, 10, 300, 60);
updateStr('5');
}
}
if ((y>=70) && (y<=120)) // Center row
{
if ((x>=10) && (x<=60)) // Button: 6
{
waitForIt(10, 70, 60, 120);
updateStr('6');
}
if ((x>=70) && (x<=120)) // Button: 7
{
waitForIt(70, 70, 120, 120);
updateStr('7');
}
if ((x>=130) && (x<=180)) // Button: 8
{
waitForIt(130, 70, 180, 120);
updateStr('8');
}
if ((x>=190) && (x<=240)) // Button: 9
{
waitForIt(190, 70, 240, 120);
updateStr('9');
}
if ((x>=250) && (x<=300)) // Button: 0
{
waitForIt(250, 70, 300, 120);
updateStr('0');
}
}
if ((y>=130) && (y<=180)) // Upper row
{
if ((x>=10) && (x<=150)) // Button: Clear
{
waitForIt(10, 130, 150, 180);
stCurrent[0]='\0';
stCurrentLen=0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 224, 319, 239);
}
if ((x>=160) && (x<=300)) // Button: Enter
{
waitForIt(160, 130, 300, 180);
if (stCurrentLen>0)
{
for (x=0; x<stCurrentLen+1; x++)
{
stLast[x]=stCurrent[x];
}
stCurrent[0]='\0';
stCurrentLen=0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 208, 319, 239);
myGLCD.setColor(0, 255, 0);
myGLCD.print(stLast, LEFT, 208);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER EMPTY", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER EMPTY", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
}
}
}
}
Anm.:
teilw. benutzen die Libs eine unterschiedliche Funktions-Syntax, z.B. für Rechtecke:
ILI9341_due verwendet die Größenabgabe ab Startpunkt (x1, y1, width, height)
UTFT verwendet die diagonalen Exkpunkte (x1,y1, x2,y2)
Außerdem ist die Farb-Steuerung unterschiedlich
ILI9341_due verwendet die Farbe als Übergabeparameter,
UTFT verwendet stattdessen einen eigen Befehl SetColor().
- insgesamt aber eher unbedeutende Unterschiede, wenn man nicht gezwungen ist, immer wieder alle Codes hin und her konvertieren zu müssen.
Lesezeichen