Hallo zusammen,
kaum ist das eine Problem gelöst kommt das Nächste. Ich möchte auf meinem Touchscreen ein Einstellmenu einbinden, auf welchem man die Farben anpassen kann. Das ist grundsätzlich kein Problem, wenn da nicht das RGB565 Problem wäre. Die Farben werden als z.Bsp. 0xFFFF (Weiss) übergeben.
Ich kann auch eine eingabe von 255,255,255 in FFFF konvertieren. Jetzt müsste aber vor das FFFF noch das 0x. Und genau da happert es.
Folgendes habe ich:
Gesamter code:
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <Adafruit_TFTLCD.h> // Hardware-specific library
MCUFRIEND_kbv tft;
#include<ADXL345_WE.h> //Gyroskop Bibliothek
#include <TFT_eSPI.h> //wegen der Farbumrechnung, und weil im Speicher noch platz ist
#include <TouchScreen.h>
//#include"pitches.h"
#include"icons.h"
//Uhr
#include <Wire.h> //Wire Bibliothek einbinden
#include <RTClib.h> //Real Time Clock Bibliothek einbinden
//Temp Sensor
#include "DHT.h" //Temp/Feuchtigkeitssensor Bibliothek einbinden
//Uhr definieren
RTC_DS1307 rtc; //Uhr benennen
#define RST_PIN -1 //Reset Pin nicht vorhanden --> also -1
//Temperatursensor definieren
#define DHTPIN 31 //Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 //DHT 22
DHT dht(DHTPIN, DHTTYPE); //Sensor benennen
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
//3.5 Calibration
const int XP = 6, XM = A2, YP = A1, YM = 7; //320x480 ID=0x9486
const int TS_LEFT = 942, TS_RT = 183, TS_TOP = 967, TS_BOT = 187;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint p;
Adafruit_GFX_Button on_btn, off_btn, page1_btn, page2_btn, page3_btn, page4_btn, page5_btn, page6_btn;
Adafruit_GFX_Button ok_btn, cncl_btn, plus_btn, minus_btn;
Adafruit_GFX_Button menu_btn, info_btn, back_btn, next_btn;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
// Button calibration
int margin = 5;
int btnWidth = 100;
int btnHeight = 40;
int btnY = 200;
// Software variable
bool enable_nuit = false;
int parameter = 50, old_parameter = 50;
const int rotar = 1; //Display Rotation
float rad = 0.0174532925;
//Uhrzeitvariablen
String Datumget;
String Uhrget;
String Tagnow;
String Wochentag;
String Datumget_alt;
String Uhrget_alt;
String Tagnow_alt;
String Wochentag_alt;
byte ControllerUhr = 0;
//Temperaturvariablen
float h, h_alt, t, t_alt;
//Batterien
float Aufbaubatterie, Bordbatterie;
//Wasserstandvariablen
int Wasserstand;
int Liner = 250;
//Gauge Variablen
#define RED2RED 0
#define GREEN2GREEN 1
#define BLUE2BLUE 2
#define BLUE2RED 3
#define GREEN2RED 4
#define RED2GREEN 5
//Gyroskop definieren
#define ADXL345_I2CADDR 0x53 // 0x1D if SDO = HIGH
ADXL345_WE myAcc = ADXL345_WE(ADXL345_I2CADDR); //Name des Gyroskops
uint32_t runTime = -99999; // time for next update
//Ein paar Farben definieren
#define BLACK 0x0000
#define GREY 0x5555
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define DARKGREEN 0x05C0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define GOLD 0xDDC0
#define WHITE 0xFFFF
#define DARKGREY 0x10A2
String Voreground, Backbround, Fontcolor;
enum pageId {
MENU,
INFO,
BATTERIE,
WATER,
INFO1,
ALARM,
REALALARM,
SOLAR,
SOLAR1,
SOLAR2,
WETTER,
EINSTELL,
EINSTELLFARBE,
LEVEL
};
unsigned int currentPage = MENU, oldPage = -1;
//Tonausgabe
int tonPin = 45;
//Solarvariablen
float SolarAmps, SolarAmps2, SolarVolts, BatAmps, BatVolts;
int SolarWatts;
//Einstellungen
float abwlr = 1.5; //winkelabweichung zum Keilen
float abwhv = 1.5;
//wofür (von pos. vor loop)
bool down ;
//Umrechnung RGB in 565
#define RGB565(r, g, b) ((((r)& 0xF8) << 8) | (((g) & 0xFC) << 3) | (((b) & 0xF8) >> 3))
String farbname;
uint16_t farbe, Farb = "0x";
//##############################################
//#Setup
//##############################################
void setup(void)
{
Serial.begin(9600);
//init TFTTouch
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println(F("Calibrate for your Touch Panel"));
if (ID == 0xD3D3) ID = 0x9486; //for 3.5" TFT LCD Shield , 0x9341 for 2.8" TFT LCD Shield
tft.begin(ID);
tft.setRotation(rotar); //0-PORTRAIT 1-PAYSAGE 2-REVERSE PORTRAIT 3-REVERSE PAYSAGE
currentPage = MENU; // Mit dem Menu starten
Serial.println("Home Page");
//Uhr
#ifndef ESP8266 //Wenn ein ESP8266 angeschlossen ist
while (!Serial); // auf den Seriellen Port warten. Dieser wird für natives USB benötigt
#endif
if (! rtc.begin())
{ //Wenn rtc.begin fehlschlägt
Serial.println("Konnte die RTC nicht starten"); //Ausgabe auf den seriellen Monitor
Serial.flush(); //Seriellen Monitor leeren
abort(); //abbrechen
}
delay(1000); //1 Sekunde warten
if (! rtc.isrunning())
{ //Wenn RTC nicht läuft
Serial.println("RTC läuft nicht, also müssen wir die Zeit einstellen");
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //Daten von Systemzeit holen
// rtc.adjust(DateTime(2023, 6, 23, 18, 7, 30)); //Datum und Uhrzeit manuell eingeben (ohne anführende Nullen
}
// Wenn nur Zeit/Datum eingestellt werden muss
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //Daten von Systemzeit holen
// rtc.adjust(DateTime(2023, 6, 23, 18, 7, 30)); //Datum und Uhrzeit manuell eingeben (ohne anführende Nullen
getUhrzeit();
//Gyroskop abfragen
if (!myAcc.init()) {
Serial.println("ADXL345 not connected!");
}
//Korrekturfaktoren für das Gyroskop
myAcc.setCorrFactors(-262.0, 279.0, -270.0, 275.0, -291.0, 214.0);
//Gyroskop ausrichten
Serial.println("Position your ADXL345 flat and don't move it");
delay(2000);
myAcc.measureAngleOffsets();
Serial.println("....done");
//Gyroskop Abtastrate
myAcc.setDataRate(ADXL345_DATA_RATE_50);
Serial.print("Data rate: ");
Serial.print(myAcc.getDataRateAsString());
//Gyroskop Messbereich
myAcc.setRange(ADXL345_RANGE_2G);
Serial.print(" / g-Range: ");
Serial.println(myAcc.getRangeAsString());
Serial.println();
// Temperatursensor starten
dht.begin(); //DHT Sensor starten
//Wire starten
Wire.begin();
Wire.setClock(400000L);
//Wasserstand holen
getWasser();
pinMode(tonPin, OUTPUT);
}
//##############################################
//#Loop
//##############################################
void loop(void) {
switch (currentPage)
{
case MENU: //Menu page
if (currentPage != oldPage) drawMenuScreen();
page1_btn.press(down && page1_btn.contains(pixel_x, pixel_y));
page2_btn.press(down && page2_btn.contains(pixel_x, pixel_y));
page3_btn.press(down && page3_btn.contains(pixel_x, pixel_y));
page4_btn.press(down && page4_btn.contains(pixel_x, pixel_y));
page5_btn.press(down && page5_btn.contains(pixel_x, pixel_y));
page6_btn.press(down && page6_btn.contains(pixel_x, pixel_y));
if (page1_btn.justReleased())
page1_btn.drawButton();
if (pixel_x <= 484 and pixel_y <= 42 and pixel_x >= 452 and pixel_y >= 10)
currentPage = INFO;
if (pixel_x <= 446 and pixel_y <= 42 and pixel_x >= 414 and pixel_y >= 10)
currentPage = EINSTELL;
if (page1_btn.justPressed()) {
page1_btn.drawButton(true);
currentPage = LEVEL;
}
if (page2_btn.justPressed()) {
page2_btn.drawButton(true);
currentPage = BATTERIE;
}
if (page3_btn.justPressed()) {
page3_btn.drawButton(true);
currentPage = WATER;
}
if (page4_btn.justPressed()) {
page4_btn.drawButton(true);
currentPage = WETTER;
}
if (page5_btn.justPressed()) {
page5_btn.drawButton(true);
currentPage = SOLAR;
}
if (page6_btn.justPressed()) {
page6_btn.drawButton(true);
currentPage = ALARM;
}
break;
case WATER: //Wasserstand
if (currentPage != oldPage) drawWaterScreen();
hbp();
break;
case LEVEL: //Wasserstand
if (currentPage != oldPage) drawLevelScreen();
getLevel();
hbp();
break;
case SOLAR: //Solar 160W
if (currentPage != oldPage) drawSolarScreen();
hbp();
vbp(SOLAR1);
break;
case SOLAR1: //Solar 200 W
if (currentPage != oldPage) drawSolar1Screen();
hbp();
vbp(SOLAR2);
zbp(SOLAR);
break;
case SOLAR2: //Batterie
if (currentPage != oldPage) drawSolar2Screen();
hbp();
zbp(SOLAR1);
break;
case ALARM: //Wasserstand
if (currentPage != oldPage) drawAlarmScreen();
hbp();
break;
case WETTER:
if (currentPage != oldPage) drawWetterScreen();
getUhrzeit();
updateUhrzeit();
getTemperatur();
hbp();
break;
case INFO:
if (currentPage != oldPage) drawInfoScreen();
if (Touch_getXY()) {
currentPage = MENU;
}
break;
case EINSTELL:
if (currentPage != oldPage) drawEinstellScreen();
hbp();
vbp(EINSTELLFARBE);
break;
case EINSTELLFARBE:
if (currentPage != oldPage) drawEinstellFarbeScreen();
hbp();
zbp(EINSTELL);
break;
case BATTERIE:
if (currentPage != oldPage) drawBatterieScreen();
hbp();
break;
case REALALARM: //Alarmanlage
if (currentPage != oldPage) drawRealAlarmScreen();
// draw0Gauge(tft.width() / 2, tft.height() / 2, 75);
hbp();
break;
}
if (oldPage == currentPage) {
down = Touch_getXY();
} else {
down = false;
}
}
/************************************************************************************
Screens gestalten
************************************************************************************/
void drawMenuScreen() {
tft.fillScreen(BLACK);
tft.setTextColor(GREEN);
// Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "WoMo-Monitor", GREEN);
//tft.setFont(&FreeSmallFont);
tft.setTextSize(2);
tft.drawLine(0, 34, tft.width() * 0.6, 34, DARKGREEN); // Draws the red line
tft.setTextColor(WHITE, BLACK);//((255, 255, 255), (0,0,0));
tft.setCursor(0, 80);
tft.setTextColor(GREEN, BLACK);//((255, 255, 255), (0,0,0));
// Button
tft.drawRGBBitmap(448 - 16, 10, INFOICON, 32, 32);
tft.drawRGBBitmap(410 - 16, 10, SETT, 32, 32);
tft.setFont(NULL);
page3_btn.initButton(&tft, (tft.width() / 2) - (btnWidth) - (margin / 2), 130, 2 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "Wasser", 2);
page1_btn.initButton(&tft, (tft.width() / 2) + (btnWidth) + (margin / 2), 130, 2 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "Level", 2);
page2_btn.initButton(&tft, (tft.width() / 2) - (btnWidth) - (margin / 2), (130 + btnHeight + margin), 2 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "Batterie", 2);
page4_btn.initButton(&tft, (tft.width() / 2) + (btnWidth) + (margin / 2), (130 + btnHeight + margin), 2 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "Wetter", 2);
page5_btn.initButton(&tft, (tft.width() / 2) - (btnWidth) - (margin / 2), (130 + btnHeight * 2) + margin * 2, 2 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "Solar", 2);
page6_btn.initButton(&tft, (tft.width() / 2) + (btnWidth) + (margin / 2), (130 + btnHeight * 2) + margin * 2, 2 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "Alarm", 2);
page1_btn.drawButton(false);
page2_btn.drawButton(false);
page3_btn.drawButton(false);
page4_btn.drawButton(false);
page5_btn.drawButton(false);
page6_btn.drawButton(false);
//Button frame
tft.drawRoundRect(3, tft.height() / 2. - (1.5 * btnHeight + 2 * margin), tft.width() - 3, 4 * btnHeight + 5 * margin, 10, GREEN);
oldPage = currentPage;
}
void drawRealAlarmScreen()//momentan nicht benutzt
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
tft.setTextSize(3);
tft.setTextColor(WHITE, BLACK);
tft.setCursor(tft.width() / 2. - 9 * 3 * 3, 50);
tft.print("Du wolltest es so");
showHome();
oldPage = currentPage;
}
void drawBatterieScreen()
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "Ladezustand Batterien", CYAN);
tft.drawLine(0, 34, tft.width() * 0.6, 34, CYAN); // Draws the red line
tft.setFont(NULL);
//Aufbaubatterie
getAufbaubatterie();
getBordbatterie();
int radius = 70;
showmsgXY(48, 70, 1, &FreeSerif12pt7b, "Aufbaubatterie", CYAN);
tft.setCursor(80, 167);
tft.print(Aufbaubatterie / 100);
showmsgXY(140, 167, 1, &FreeSerif12pt7b, " V", CYAN);
ringMeter(Aufbaubatterie, 1180, 1480, 120 - radius, 160 - radius, radius, RED2GREEN);
//Bordbatterie
showmsgXY(298, 70, 1, &FreeSerif12pt7b, "Bordbatterie", CYAN);
tft.setCursor(320, 167);
tft.print(Bordbatterie / 100);
showmsgXY(380, 167, 1, &FreeSerif12pt7b, " V", CYAN);
radius = 70;
ringMeter(Bordbatterie, 1200, 1400, 360 - radius, 160 - radius, radius, RED2GREEN);
tft.setFont(NULL);
showHome();
oldPage = currentPage;
}
void drawSolarScreen()//Solar gesamt
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "Solarleistung gesamt", CYAN);
tft.drawLine(0, 34, tft.width() * 0.6, 34, CYAN); // Draws the red line
getSolarVolts();
getSolarAmps();
getSolarAmps2();
getSolarWatts();
int radius = 70;
//Volts
tft.setCursor(50, 167);
tft.print(SolarVolts / 100);
showmsgXY(110, 167, 1, &FreeSerif12pt7b, " V", CYAN);
ringMeter(SolarVolts, 1200, 3600, 120 - radius - 30, 160 - radius, radius, RED2GREEN);
//Amps
tft.setCursor(200, 167);
tft.print(SolarAmps / 100 + SolarAmps2 / 100);
showmsgXY(260, 167, 1, &FreeSerif12pt7b, " A", CYAN);
ringMeter(SolarAmps, 100, 3000, 240 - radius, 160 - radius, radius, RED2GREEN);
//Watts
tft.setCursor(362, 167);
tft.print(SolarWatts);
showmsgXY(398, 167, 1, &FreeSerif12pt7b, " W", CYAN);
ringMeter(SolarWatts, 1, 400, 360 - radius + 30, 160 - radius, radius, RED2GREEN);
Serial.println("Solar V,A,W");
Serial.println(SolarVolts);
Serial.println(SolarAmps);
Serial.println(SolarWatts);
tft.setFont(NULL);
showHome();
showVor();
oldPage = currentPage;
}
void drawSolar1Screen()
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "Solarleistung", CYAN);
tft.drawLine(0, 34, tft.width() * 0.6, 34, CYAN); // Draws the red line
tft.setFont(NULL);
//Aufbaubatterie
getSolarVolts();
getSolarAmps();
int radius = 35;
showmsgXY(72, 70, 1, &FreeSerif12pt7b, "Von Solar", CYAN);
tft.setCursor(80, 167);
tft.print(SolarVolts / 100);
showmsgXY(140, 167, 1, &FreeSerif12pt7b, " V", CYAN);
ringMeter(Aufbaubatterie, 1180, 1480, 120 - radius, 160 - radius - 40, radius, RED2GREEN);
//Bordbatterie
showmsgXY(298, 70, 1, &FreeSerif12pt7b, "Zur Batterie", CYAN);
tft.setCursor(320, 167);
tft.print(Bordbatterie / 100);
showmsgXY(380, 167, 1, &FreeSerif12pt7b, " V", CYAN);
ringMeter(Bordbatterie, 1200, 1400, 360 - radius, 160 - radius - 40, radius, RED2GREEN);
ringMeter(Aufbaubatterie, 1180, 1480, 120 - radius, 160 + radius, radius, RED2GREEN);
ringMeter(Aufbaubatterie, 1180, 1480, 360 - radius, 160 + radius, radius, RED2GREEN);
tft.setFont(NULL);
showHome();
showVor();
showZuruck();
oldPage = currentPage;
}
void drawSolar2Screen()
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "Solarleistung", CYAN);
tft.drawLine(0, 34, tft.width() * 0.6, 34, CYAN); // Draws the red line
tft.setFont(NULL);
//Aufbaubatterie
getSolarVolts();
getSolarAmps();
int radius = 35;
showmsgXY(72, 70, 1, &FreeSerif12pt7b, "Von Solar", CYAN);
tft.setCursor(80, 167);
tft.print(Aufbaubatterie / 100);
showmsgXY(140, 167, 1, &FreeSerif12pt7b, " V", CYAN);
ringMeter(Aufbaubatterie, 1180, 1480, 120 - radius, 160 - radius - 40, radius, RED2GREEN);
//Bordbatterie
showmsgXY(298, 70, 1, &FreeSerif12pt7b, "Zur Batterie", CYAN);
tft.setCursor(320, 167);
tft.print(Bordbatterie / 100);
showmsgXY(380, 167, 1, &FreeSerif12pt7b, " V", CYAN);
ringMeter(Bordbatterie, 1200, 1400, 360 - radius, 160 - radius - 40, radius, RED2GREEN);
ringMeter(Aufbaubatterie, 1180, 1480, 120 - radius, 160 + radius, radius, RED2GREEN);
ringMeter(Aufbaubatterie, 1180, 1480, 360 - radius, 160 + radius, radius, RED2GREEN);
tft.setFont(NULL);
showHome();
showZuruck();
oldPage = currentPage;
}
void drawWaterScreen()
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "Frischwasser", CYAN);
tft.drawLine(0, 34, tft.width() * 0.6, 34, CYAN); // Draws the red line
tft.setFont(NULL);
getWasser();
int radius = 100;
ringMeter(Wasserstand, 0, 100, 240 - radius, 160 - radius, radius, RED2GREEN);
if (Wasserstand > 99)
{
tft.setCursor(210, 152);
tft.setTextSize(2);
tft.print(Wasserstand);
tft.print(" %");
}
else
{
tft.setCursor(222, 152);
tft.setTextSize(2);
tft.print(Wasserstand);
tft.print(" %");
}
showHome();
oldPage = currentPage;
}
void drawAlarmScreen()
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "Frischwasser", CYAN);
tft.drawLine(0, 34, tft.width() * 0.6, 34, CYAN); // Draws the red line
tft.setFont(NULL);
getWasser();
int radius = 100;
ringMeter(Wasserstand, 0, 100, 240 - radius, 160 - radius, radius, RED2GREEN);
if (Wasserstand > 99)
{
tft.setCursor(210, 152);
tft.setTextSize(2);
tft.print(Wasserstand);
tft.print(" %");
}
else
{
tft.setCursor(222, 152);
tft.setTextSize(2);
tft.print(Wasserstand);
tft.print(" %");
}
showHome();
oldPage = currentPage;
}
void drawLevelScreen()
{
tft.setRotation(rotar);
tft.fillScreen(BLACK);
//Title
showmsgXY(20, 30, 1, &FreeSerif12pt7b, "Nivelierung", CYAN);
tft.drawLine(0, 34, tft.width() * 0.6, 34, CYAN); // Draws the red line
tft.setFont(NULL);
getLevel();
tft.drawRect(10, 100, 60, 20, WHITE);
tft.drawRect(10, 250, 60, 20, WHITE);
tft.drawRect(300, 100, 60, 20, WHITE);
tft.drawRect(300, 250, 60, 20, WHITE);
tft.drawLine(40, 120, 40, 175, WHITE);
tft.drawLine(40, 195, 40, 250, WHITE);
tft.drawCircle(40, 185, 10, WHITE);
tft.drawLine(330, 120, 330, 250, WHITE);
tft.drawLine(50, 185, 330, 185, WHITE);
showHome();
oldPage = currentPage;
}
void drawInfoScreen() {
tft.setRotation(rotar);
tft.fillScreen(BLACK);//(100, 155, 203)
tft.drawRoundRect(10, 50, tft.width() - 20, tft.height() - 60, 5, GREEN); //tft.fillRect (10, 10, 60, 36);
tft.setFont(NULL);
tft.setTextColor(WHITE);
tft.setCursor(25, 30);
tft.write("Informationen ");
tft.write(0x81);
tft.write("ber das System");
tft.setCursor(25, 80);
tft.write("Dieses System wurde in m");
tft.write(0x81);
tft.write("hevoller");
tft.setCursor(25, 105);
tft.write("Kleinstarbeit und mit viel Zeit");
tft.setCursor(25, 130);
tft.write("von Roland zusammengekl");
tft.write(0x94);
tft.write("ppelt.");
tft.setCursor(25, 155);
tft.write("Bei Fragen gerne melden.");
tft.setCursor(25, 205);
tft.write("Tel: +41 79 XXX XX XX");
tft.setCursor(25, 230);
tft.write("email: rxxxxxxxxx@gmail.com");
tft.setCursor(25, 280);
tft.write("Programmversion: 0.1");
tft.write(0xE0);
tft.setFont(NULL);
oldPage = currentPage;
}
void drawEinstellScreen() {
tft.setRotation(rotar);
tft.fillScreen(BLACK);//(100, 155, 203)
tft.drawRoundRect(10, 50, tft.width() - 20, tft.height() - 60, 5, GREEN); //tft.fillRect (10, 10, 60, 36);
tft.setFont(NULL);
tft.setTextColor(WHITE);
tft.setCursor(25, 30);
tft.write("Einstellungen");
tft.setFont(NULL);
showHome();
showVor();
oldPage = currentPage;
}
void drawEinstellFarbeScreen() {
tft.setRotation(rotar);
farbe = RGB565(255, 0, 0);
Serial.println(farbe, HEX);
farbe = (farbe, HEX);
farbe = Farb + farbe;
Serial.println(Farb);
Serial.println(Farb + farbe);
tft.fillScreen(farbe);//(100, 155, 203)
tft.drawRoundRect(10, 50, tft.width() - 20, tft.height() - 60, 5, GREEN); //tft.fillRect (10, 10, 60, 36);
tft.setFont(NULL);
tft.setTextColor(WHITE);
tft.setCursor(25, 30);
tft.write("Farbeinstellungen");
tft.setFont(NULL);
showHome();
showZuruck();
oldPage = currentPage;
}
void drawWetterScreen() {
tft.setRotation(rotar);
tft.fillScreen(BLACK);//(100, 155, 203)
showHome();
oldPage = currentPage;
tft.setTextColor(GREEN);
tft.setCursor(85, 250);
tft.write("Bitte etwas l");
tft.write(0x84);
tft.write("nger dr");
tft.write(0x81);
tft.write("cken");
tft.setFont(NULL);
}
/************************************************************************************
Zusätzliche Funktionen
*************************************************************************************/
//##############################################
//#Touchscreen auf berührung prüfen
//##############################################
bool Touch_getXY(void)
{
p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH);
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
if (tft.width() <= tft.height()) { //Portrait
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());
} else {
pixel_x = map(p.y, TS_TOP, TS_BOT, 0, tft.width());
pixel_y = map(p.x, TS_RT, TS_LEFT, 0, tft.height());
}
}
// Serial.println(pixel_x);
// Serial.println(pixel_y);
return pressed;
}
//##############################################
//#Uhrzeit auslesen
//##############################################
void getUhrzeit()
{
DateTime now = rtc.now(); //Daten auslesen
char buf1[] = " DDD"; //Wochentag in buf2[] schreiben
Tagnow = (now.toString(buf1)); //Wochentag als String übergeben an tagnow
//Überprüfung welcher Wochentag ist, und diesen dann ausgeschrieben an Display senden mit Zeilenumbruch
if (Tagnow == " Sam") Wochentag = "Samstag";
else if (Tagnow == " Son") Wochentag = "Sonntag";
else if (Tagnow == " Mon") Wochentag = "Montag";
else if (Tagnow == " Die") Wochentag = "Dienstag";
else if (Tagnow == " Mit") Wochentag = "Mittwoch";
else if (Tagnow == " Don") Wochentag = "Donnerstag";
else if (Tagnow == " Fre") Wochentag = "Freitag";
//Überprüfung fertig
char buf2[] = "DD.MM.YYYY";
Datumget = (now.toString(buf2));
char buf3[] = "hh:mm:ss";
Uhrget = (now.toString(buf3)); //Aktuelle Uhrzeit in oledreset schreiben
}
//##############################################
//#Uhrzeit aktualisieren
//##############################################
void updateUhrzeit()
{
if (Uhrget != Uhrget_alt or Datumget != Datumget_alt or Tagnow != Tagnow_alt) {
Serial.println("void UpdateUhrzeit");
tft.setTextColor(GREEN, BLACK);
tft.setTextSize(2);
tft.setCursor(50, 50);
tft.print("Heute ist:");
tft.setCursor(261, 50);
tft.print(Wochentag);
Serial.println(Wochentag);
Serial.println(Tagnow);
tft.setCursor(261, 75);
tft.print(Datumget);
Serial.println(Datumget);
tft.setCursor(261, 100);
tft.print(Uhrget);
Serial.println(Uhrget);
Uhrget_alt = Uhrget;
Datumget_alt = Datumget;
Tagnow_alt = Tagnow;
}
}
//##############################################
//#Temperatursensor auswerten
//##############################################
void getTemperatur()
{
// Temperaturmessung dht
h = dht.readHumidity(); //Feuchtigkeit messen
t = dht.readTemperature(); //Temperatur in Celsius messen
if (isnan(h) || isnan(t)) { //testen, ob eine Messung fehlschlug
Serial.println(F("DHT Sensor konnte nicht messen"));
return; //zurück
}
if (h != h_alt or t != t_alt) {
Serial.println("void getTemperatur");
tft.setTextColor(GREEN, BLACK);
tft.setTextSize(2);
tft.setCursor(50, 150);
tft.print("Temperatur: ");
tft.setCursor(261, 150);
tft.print(t, 1);
tft.write(0xF7);
tft.write("C");
tft.setCursor(50, 175);
tft.print("Luftfeuchtigkeit: ");
tft.setCursor(261, 175);
tft.print(h, 1);
tft.write("%");
h_alt = h;
t_alt = t;
}
}
//##############################################
//#Vorläufige Werte für diverse Sensoren
//##############################################
void getWasser()
{
randomSeed(millis());
Wasserstand = random(10, 100);
Serial.println(Wasserstand);
}
void getSolarVolts()
{
randomSeed(millis());
SolarVolts = random(1200, 3600);
}
void getSolarAmps()
{
randomSeed(millis());
SolarAmps = random(100, 1500);
}
void getSolarAmps2()
{
randomSeed(millis());
SolarAmps = random(100, 1500);
}
void getSolarWatts()
{
SolarWatts = SolarVolts / 100 * (SolarAmps / 100 + SolarAmps2 / 100);
}
/*void getBatVolts()
{
randomSeed(millis());
BatVolts = random(1200, 1500);
}
void getBatAmps()
{
randomSeed(millis());
BatAmps = random(100, 150);
}*/
void getAufbaubatterie()
{
randomSeed(millis());
Aufbaubatterie = random(1180, 1480);
Serial.println("Aufbaubatterie: " + String(Aufbaubatterie));
}
void getBordbatterie()
{
randomSeed(millis());
Bordbatterie = random(1200, 1400);
Serial.println("Bordbatterie: " + String(Bordbatterie));
}
//##############################################
//#Nachricht an position mit Schriftart etc.
//##############################################
void showmsgXY(int x, int y, int sz, const GFXfont * f, const char *msg, char *Farbe)
{
int16_t x1, y1;
uint16_t wid, ht;
tft.setFont(f);
tft.setCursor(x, y);
tft.setTextColor(Farbe);
tft.setTextSize(sz);
tft.print(msg);
}
//#######################################################
//Ringmeter
//#######################################################
int ringMeter(int value, int vmin, int vmax, int x, int y, int r, byte scheme)
{
// Minimum value of r is about 52 before value text intrudes on ring
// drawing the text first is an option
x += r; y += r; // Calculate coords of centre of ring
int w = r / 4; // Width of outer ring is 1/4 of radius
int angle = 150; // Half the sweep angle of meter (300 degrees)
int text_colour = 0; // To hold the text colour
int v = map(value, vmin, vmax, -angle, angle); // Map the value to an angle v
byte seg = 5; // Segments are 5 degrees wide = 60 segments for 300 degrees
byte inc = 10; // Draw segments every 5 degrees, increase to 10 for segmented ring
// Draw colour blocks every inc degrees
for (int i = -angle; i < angle; i += inc) {
// Choose colour from scheme
int colour = 0;
switch (scheme) {
case 0: colour = RED; break; // Fixed colour
case 1: colour = GREEN; break; // Fixed colour
case 2: colour = BLUE; break; // Fixed colour
case 3: colour = rainbow(map(i, -angle, angle, 0, 127)); break; // Full spectrum blue to red
case 4: colour = rainbow(map(i, -angle, angle, 63, 127)); break; // Green to red (high temperature etc)
case 5: colour = rainbow(map(i, -angle, angle, 127, 63)); break; // Red to green (low battery etc)
default: colour = BLUE; break; // Fixed colour
}
// Calculate pair of coordinates for segment start
float sx = cos((i - 90) * 0.0174532925);
float sy = sin((i - 90) * 0.0174532925);
uint16_t x0 = sx * (r - w) + x;
uint16_t y0 = sy * (r - w) + y;
uint16_t x1 = sx * r + x;
uint16_t y1 = sy * r + y;
// Calculate pair of coordinates for segment end
float sx2 = cos((i + seg - 90) * 0.0174532925);
float sy2 = sin((i + seg - 90) * 0.0174532925);
int x2 = sx2 * (r - w) + x;
int y2 = sy2 * (r - w) + y;
int x3 = sx2 * r + x;
int y3 = sy2 * r + y;
if (i < v) { // Fill in coloured segments with 2 triangles
tft.fillTriangle(x0, y0, x1, y1, x2, y2, colour);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, colour);
text_colour = colour; // Save the last colour drawn
}
else // Fill in blank segments
{
tft.fillTriangle(x0, y0, x1, y1, x2, y2, DARKGREY);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, DARKGREY);
}
}
}
// #########################################################################
// Return a 16 bit rainbow colour
// #########################################################################
unsigned int rainbow(byte value)
{
// Value is expected to be in range 0-127
// The value is converted to a spectrum colour from 0 = blue through to 127 = red
byte red = 0; // Red is the top 5 bits of a 16 bit colour value
byte green = 0;// Green is the middle 6 bits
byte blue = 0; // Blue is the bottom 5 bits
byte quadrant = value / 32;
if (quadrant == 0) {
blue = 31;
green = 2 * (value % 32);
red = 0;
}
if (quadrant == 1) {
blue = 31 - (value % 32);
green = 63;
red = 0;
}
if (quadrant == 2) {
blue = 0;
green = 63;
red = value % 32;
}
if (quadrant == 3) {
blue = 0;
green = 63 - 2 * (value % 32);
red = 31;
}
return (red << 11) + (green << 5) + blue;
}
//#######################################################
//Fest platzierte Buttons
//#######################################################
void showHome()//Home Button anzeigen
{
tft.drawRGBBitmap(20, tft.height() - 32, HOMEP, 32, 32);
}
void hbp() //home Button pressed
{
if (pixel_x <= 52 and pixel_y <= tft.height() and pixel_x >= 20 and pixel_y >= tft.height() - 32)
{
currentPage = MENU;
}
}
void showVor()//Vorwärts Button anzeigen
{
tft.drawBitmap((tft.width() / 2) + 5, (tft.height() - 32), VOR, 32, 32, GREEN);
pixel_x = 0;
pixel_y = 0;
}
void vbp(unsigned int Page) //Vorwärts Button gedrückt
{
if (pixel_x <= tft.width() + 37 and pixel_y <= tft.height() and pixel_x >= tft.width() / 2 + 5 and pixel_y >= tft.height() - 32)
{
currentPage = Page;
pixel_x = 0;
pixel_y = 0;
}
}
void showZuruck()//Zurück Button anzeigen
{
tft.drawBitmap((tft.width() / 2) - 37, (tft.height() - 32), ZURUCK, 32, 32, GREEN);
}
void zbp(unsigned int Page) //Zurück Button pressed
{
if (pixel_x <= tft.width() / 2 - 5 and pixel_y <= tft.height() and pixel_x >= tft.width() / 2 - 37 and pixel_y >= tft.height() - 32)
{
currentPage = Page;
pixel_x = 0;
pixel_y = 0;
}
}
//#######################################################
//Nivelierung
//#######################################################
void getLevel()
{
float x_alt, y_alt, z_alt;
tft.setTextSize(2);
tft.setFont(NULL);
tft.setTextColor(GREEN, BLACK);
xyzFloat raw = myAcc.getRawValues();
xyzFloat g = myAcc.getGValues();
xyzFloat angle = myAcc.getAngles();
xyzFloat corrAngles = myAcc.getCorrAngles();
/*if (corrAngles.x - x_alt > 0.5 or corrAngles.x - x_alt < -0.5 or corrAngles.y - y_alt > 0.5 or corrAngles.y - y_alt < -0.5 or corrAngles.z - z_alt > 0.5 or corrAngles.z - z_alt < -0.5)
{
tft.setCursor(0, 40);
tft.print("cAngle x = ");
tft.print(corrAngles.x, 1);
tft.println(" ");
tft.print("cAngle y = ");
tft.print(corrAngles.y, 1);
tft.println(" ");
tft.print("cAngle z = ");
tft.print(corrAngles.z, 1);
tft.println(" ");
tft.setCursor(10, 40);*/
x_alt = corrAngles.x;
y_alt = corrAngles.y;
z_alt = corrAngles.z;
tft.fillCircle(400, 185, 50, GREEN);
tft.drawCircle(400, 185, 10, BLACK);
tft.drawCircle(400, 185, 51, WHITE);
// tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
// }
//Variablen für Berechnungen definieren
int Achsabstand = 410;
int Radstand = 165;
int Diagonale = 442;
float links, rechts, vorne, hinten;
float pi = 3.14156;
//Links Keile
if (corrAngles.x > abwlr and corrAngles.y<abwhv and corrAngles.y> abwhv * -1)
{
if (corrAngles.x > 10 and corrAngles.y<abwhv and corrAngles.y> abwhv * -1)
{
corrAngles.x = 10;
}
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Links ");
links = (sin(corrAngles.x * rad)) * Radstand;
Serial.println(corrAngles.x);
Serial.println(links);
tft.print(links, 1);
tft.print(" cm ");
tft.fillRect(301, 101, 58, 18, RED);
tft.fillRect(301, 251, 58, 18, GREEN);
tft.fillRect(11, 101, 58, 18, RED);
tft.fillRect(11, 251, 58, 18, GREEN);
}
//Rechts Keile
if (corrAngles.x < abwlr * -1 and corrAngles.y<abwhv and corrAngles.y> abwhv * -1)
{
if (corrAngles.x < -10 and corrAngles.y<abwhv and corrAngles.y> abwhv * -1)
{
corrAngles.x = -10;
}
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Rechts ");
rechts = (sin(corrAngles.x * -1 * rad)) * Radstand;
Serial.println(corrAngles.x);
Serial.println(rechts);
tft.print(rechts, 1);
tft.print(" cm ");
tft.fillRect(301, 101, 58, 18, GREEN);
tft.fillRect(301, 251, 58, 18, RED);
tft.fillRect(11, 101, 58, 18, GREEN);
tft.fillRect(11, 251, 58, 18, RED);
}
//Vorne Keile
if (corrAngles.y < abwhv * -1 and corrAngles.x<abwlr and corrAngles.x> abwlr * -1)
{
if (corrAngles.y < - 10 and corrAngles.x<abwlr and corrAngles.x> abwlr * -1)
{
corrAngles.y = -10;
}
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Vorne ");
vorne = (sin(corrAngles.y * -1 * rad)) * Achsabstand;
Serial.println(corrAngles.y);
Serial.println(vorne);
tft.print(vorne, 1);
tft.print(" cm ");
tft.fillRect(301, 101, 58, 18, RED);
tft.fillRect(301, 251, 58, 18, RED);
tft.fillRect(11, 101, 58, 18, GREEN);
tft.fillRect(11, 251, 58, 18, GREEN);
}
//Hinten Keile
if (corrAngles.y > abwhv and corrAngles.x<abwlr and corrAngles.x> abwlr * -1)
{
if (corrAngles.y > 10 and corrAngles.x<abwlr and corrAngles.x> abwlr * -1)
{
corrAngles.y = 10;
}
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Hinten ");
hinten = (sin(corrAngles.y * rad)) * Achsabstand;
Serial.println(corrAngles.y);
Serial.println(hinten, 1);
tft.print(hinten, 1);
tft.print(" cm ");
tft.fillRect(301, 101, 58, 18, GREEN);
tft.fillRect(301, 251, 58, 18, GREEN);
tft.fillRect(11, 101, 58, 18, RED);
tft.fillRect(11, 251, 58, 18, RED);
}
//Hinten Links Keile
if (corrAngles.x > abwlr and corrAngles.y > abwhv)
{
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Hinten Links ca. ");
hinten = (sin(corrAngles.y * rad)) * Achsabstand;
links = (sin(corrAngles.x * rad)) * Radstand;
Serial.println(corrAngles.y);
Serial.println(hinten, 1);
if (hinten < links)tft.print(links, 1);
if (hinten > links)tft.print(hinten, 1);
tft.print(" cm ");
tft.fillRect(11, 101, 58, 18, RED);
tft.fillRect(301, 101, 58, 18, GREEN);
tft.fillRect(301, 251, 58, 18, GREEN);
tft.fillRect(11, 251, 58, 18, GREEN);
}
//Hinten Rechts Keile
if (corrAngles.x < abwlr * -1 and corrAngles.y > abwhv)
{
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Hinten Rechts ca. ");
hinten = (sin(corrAngles.y * rad)) * Achsabstand;
rechts = (sin(corrAngles.x * -1 * rad)) * Radstand;
Serial.println(corrAngles.y);
Serial.println(hinten, 1);
if (hinten < rechts)tft.print(links, 1);
if (hinten > rechts)tft.print(hinten, 1);
tft.print(" cm ");
tft.fillRect(11, 251, 58, 18, RED);
tft.fillRect(301, 101, 58, 18, GREEN);
tft.fillRect(301, 251, 58, 18, GREEN);
tft.fillRect(11, 101, 58, 18, GREEN);
}
//Vorne Links Keile
if (corrAngles.x > abwlr and corrAngles.y < abwhv * -1)
{
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Vorne Links ca. ");
vorne = (sin(corrAngles.y * -1 * rad)) * Achsabstand;
links = (sin(corrAngles.x * rad)) * Radstand;
Serial.println(corrAngles.y);
Serial.println(hinten, 1);
if (vorne < links)tft.print(links, 1);
if (vorne > links)tft.print(vorne, 1);
tft.print(" cm ");
tft.fillRect(301, 101, 58, 18, RED);
tft.fillRect(301, 251, 58, 18, GREEN);
tft.fillRect(11, 101, 58, 18, GREEN);
tft.fillRect(11, 251, 58, 18, GREEN);
}
//Vorne Rechts Keile
if (corrAngles.x < abwlr * -1 and corrAngles.y < abwhv * -1)
{
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.setCursor(70, 40);
tft.print("Vorne Rechts ca. ");
vorne = (sin(corrAngles.y * -1 * rad)) * Achsabstand;
rechts = (sin(corrAngles.x * -1 * rad)) * Radstand;
Serial.println(corrAngles.y);
Serial.println(hinten, 1);
if (vorne < rechts)tft.print(links, 1);
if (vorne > rechts)tft.print(hinten, 1);
tft.print(" cm ");
tft.fillRect(301, 251, 58, 18, RED);
tft.fillRect(301, 101, 58, 18, GREEN);
tft.fillRect(11, 101, 58, 18, GREEN);
tft.fillRect(11, 251, 58, 18, GREEN);
}
if ((corrAngles.x < abwlr and corrAngles.x > abwlr * -1) and (corrAngles.y < abwhv and corrAngles.y > abwhv * -1))
{
Serial.println("Juhuu");
Serial.println(corrAngles.x);
tft.setCursor(70, 40);
tft.fillCircle(400 + corrAngles.y * 4, 185 + corrAngles.x * 4, 5, RED);
tft.print(" ");
tft.fillRect(11, 101, 58, 18, GREEN);
tft.fillRect(11, 251, 58, 18, GREEN);
tft.fillRect(301, 101, 58, 18, GREEN);
tft.fillRect(301, 251, 58, 18, GREEN);
}
}
der Screen auf welchem die Funktion dann implementiert werden sollte:
void drawEinstellFarbeScreen() {
tft.setRotation(rotar);
farbe=RGB565(255,0,0);
Serial.println(farbe,HEX);
farbe=(farbe,HEX);
farbe=Farb+farbe;
Serial.println(Farb);
Serial.println(Farb+farbe);
tft.fillScreen(farbe);//(100, 155, 203)
tft.drawRoundRect(10, 50, tft.width() - 20, tft.height() - 60, 5, GREEN); //tft.fillRect (10, 10, 60, 36);
tft.setFont(NULL);
tft.setTextColor(WHITE);
tft.setCursor(25, 30);
tft.write("Farbeinstellungen");
tft.setFont(NULL);
showHome();
showZuruck();
oldPage = currentPage;
}
da kommt aber logischerweise nicht das raus was ich möchte.
Frage:
Wie kann ich eine Variable names Farbschema vom typ uint16_t generieren, und zwar aus den Zeichen 0x und der Variable farbe (welche farbe,HEX enthält)?