Skip to content

Commit

Permalink
first Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
beabel committed Nov 26, 2023
1 parent 75ce98f commit 2d1803f
Show file tree
Hide file tree
Showing 12 changed files with 1,309 additions and 0 deletions.
576 changes: 576 additions & 0 deletions AZ_TOUCH_BASIC/00_font.h

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions AZ_TOUCH_BASIC/00_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
_______. _______ .___________.___________. __ .__ __. _______ _______.
/ || ____|| | || | | \ | | / _____| / |
| (----`| |__ `---| |----`---| |----`| | | \| | | | __ | (----`
\ \ | __| | | | | | | | . ` | | | |_ | \ \
.----) | | |____ | | | | | | | |\ | | |__| | .----) |
|_______/ |_______| |__| |__| |__| |__| \__| \______| |_______/
*/
//select english or use german as standard
//#define ENGLISH
//########### TFT Color Settings
#define COLOR_BG ILI9341_BLACK
//###########
#define HOSTNAME "ESP-Hostname"
#define AP_NAME "ESP_WIFI_AP"
#define OTA_PW "ota_update"
#define TZONE "CET-1CEST,M3.5.0/03,M10.5.0/03" //https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
/*
_______.___________. ___ .__ __. _______ ___ .______ _______
/ | | / \ | \ | | | \ / \ | _ \ | \
| (----`---| |----` / ^ \ | \| | | .--. | / ^ \ | |_) | | .--. |
\ \ | | / /_\ \ | . ` | | | | | / /_\ \ | / | | | |
.----) | | | / _____ \ | |\ | | '--' | / _____ \ | |\ \----.| '--' |
|_______/ |__| /__/ \__\ |__| \__| |_______/ /__/ \__\ | _| `._____||_______/
_______ ______ .__ __. __.___________. ______ __ __ ___ .__ __. _______ _______
| \ / __ \ | \ | | (_ ) | / || | | | / \ | \ | | / _____|| ____|
| .--. | | | | | \| | |/`---| |----` | ,----'| |__| | / ^ \ | \| | | | __ | |__
| | | | | | | | . ` | | | | | | __ | / /_\ \ | . ` | | | |_ | | __|
| '--' | `--' | | |\ | | | | `----.| | | | / _____ \ | |\ | | |__| | | |____
|_______/ \______/ |__| \__| |__| \______||__| |__| /__/ \__\ |__| \__| \______| |_______|
*/
/*_______Pin definitions____*/
#define BEEPER 21 //
//touchscreen //
#define TOUCH_CS 14 //
#define TOUCH_IRQ 27 //
//display //
#define TFT_CS 5 //
#define TFT_DC 4 //
#define TFT_RST 22 //
#define TFT_LED 15 //
#define TFT_MOSI 23 //
#define TFT_CLK 18 //
#define TFT_MISO 19 //
/*______End of definitions__*/
//define names for fonts
#define FNT9 &AT_Standard9pt7b
#define FNT12 &AT_Bold12pt7b
#define LED_ON 0
//definitions for text output alignment
#define ALIGNLEFT 0
#define ALIGNCENTER 1
#define ALIGNRIGHT 2
//########### Global Variables
uint16_t minutes; //current number of minutes since midnight
uint8_t weekday; //current weekday
struct tm ti; //time structure with current time
boolean connected; //flag to signal active connection
uint32_t discon = 0; //tick-counter value to calculate disconnected time
uint32_t tick = 0; //last tick-counter value to trigger timed event
//########### Variables to hold configuration data
Preferences pref;
String ssid = ""; //ssid for WLAN connection
String pkey = ""; //passkey for WLAN connection
String ntp = "de.pool.ntp.org"; //NTP server url

//instance of display driver
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
//instance for touchscreen driver
XPT2046_Touchscreen touch(TOUCH_CS, TOUCH_IRQ);
//instance to start callbacks on touch events
TouchEvent tevent(touch);
54 changes: 54 additions & 0 deletions AZ_TOUCH_BASIC/01_lang.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

#ifdef ENGLISH/*_____________________ ENGLISH _______________________*/
//pref.h
#define LANG_loadPreferences "Load Preferences"
#define LANG_setPreferences "set pref: ssid= %i password = %i ntp = %i\n"
//tft.h
#define LANG_setBGLight "percent = %i LED = %i\n"
#define LANG_TextInBox "Token %s Length %i total %s Length %i Line %i\n"
#define LANG_OnTouchClick "Touch on %i, %i\n"
#define LANG_ShowOtaProgress "Progress %i%%"
//wlan.h
#define LANG_InitWifi "Connect WiFi"
#define LANG_WifiDisconnect "WIFI Disconnect"
#define LANG_ApDisconnect "AP disconnect"
#define LANG_SetWifiMode "Start WIFI Mode"
#define LANG_TryConnectWifi "Try to connect WIFI: "
#define LANG_WithPassword " with password: "
#define LANG_NoConnectionStartAp "No Connection! \nStart Access-Point."
#define LANG_ConnectionLost "Connection Lost !!!"
//ota.h
#define LANG_UpdateSketch "Updating Sketch"
#define LANG_Done "Done"
//test.h
#define LANG_Connected "connected"
#define LANG_NotConnected "Not connected"
#define LANG_ConnectAp "Please connect to AP SSID:"
#define LANG_ApIp "Call this IP:"
#else /*_____________________ GERMAN _______________________*/
//pref.h
#define LANG_loadPreferences "Lade Einstellungen"
#define LANG_setPreferences "eingestellt: ssid= %i passwort = %i ntp = %i\n"
//tft.h
#define LANG_setBGLight "prozent = %i LED = %i\n"
#define LANG_TextInBox "Token %s Länge %i gesamt %s Länge %i Linie %i\n"
#define LANG_OnTouchClick "Berührung bei %i, %i\n"
#define LANG_ShowOtaProgress "Fortschritt %i%%"
//wlan.h
#define LANG_InitWifi "Verbinde WLAN"
#define LANG_WifiDisconnect "WLAN trennen"
#define LANG_ApDisconnect "AP trennen"
#define LANG_SetWifiMode "Starte WLAN Modus"
#define LANG_TryConnectWifi "Versuche Wlan : "
#define LANG_WithPassword " zu verbinden mit diesem Passwort: "
#define LANG_NoConnectionStartAp "Keine Verbindung! \nStarte Access-Point."
#define LANG_ConnectionLost "Verbindung verloren !!!"
//ota.h
#define LANG_UpdateSketch "Update Sketch"
#define LANG_Done "fertig"
//test.h
#define LANG_Connected "verbunden"
#define LANG_NotConnected "nicht verbunden"
#define LANG_ConnectAp "Bitte verbinde dich mit AP SSID:"
#define LANG_ApIp "Rufe dann diese IP auf:"
#endif
9 changes: 9 additions & 0 deletions AZ_TOUCH_BASIC/02_pref.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
void pref_GetValues(){
Serial.println(LANG_loadPreferences);
pref.begin("esp_settings", false);
//get values from preferences
if (pref.isKey("ssid")) ssid = pref.getString("ssid");
if (pref.isKey("pkey")) pkey = pref.getString("pkey");
if (pref.isKey("ntp")) ntp = pref.getString("ntp");
Serial.printf(LANG_setPreferences,ssid,pkey,ntp);
}
153 changes: 153 additions & 0 deletions AZ_TOUCH_BASIC/03_tft.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//adjust the brightness of the background led
void tft_setBGLight(uint8_t prct) {
uint16_t ledb;
ledb = 255*prct/100;
if (ledb > 255) ledb = 255;
if (ledb <3) ledb = 3; //minimal brightness
if (LED_ON == 0) ledb = 255 - ledb;
Serial.printf(LANG_setBGLight,prct,ledb);
//set the LED
analogWrite(TFT_LED, ledb);
}
//called from the main loop to handle touch events
void tft_TouchLoop() {
tevent.pollTouchScreen();
}
//clear the whole display
void tft_DisplayClear() {
tft.fillScreen(COLOR_BG);
}
//encode text from unicode to display code for supplied font
//src and dst buffer needs to exist
void tft_encodeUnicode(const char* src, char* dst){
uint8_t i = 0, i1 = 0;
char c;
uint16_t m = strlen(src);
do {
c = src[i];
if (c==195) { //UTF8 characters german umlauts
i++;
switch (src[i]) {
case 164: c=130; break; //ä
case 182: c=131; break; //ö
case 188: c=132; break; //ü
case 159: c=133; break; //ß
case 132: c=127; break; //Ä
case 150: c=128; break; //Ö
case 156: c=129; break; //Ü
default: c=255;
}
} else if (c == 194) { //UTF8 char for degree symbol
i++;
if (src[i] == 176) c=134; else c=255;
}
if (c<135) dst[i1] = c;
i++;
i1++;
} while (i<m);
dst[i1] = 0;
}
//display a text in a rectangle on position x,y with the size w,h
//the text can bbe aligned left, center or right text size can be set to standard or big
//font color and background color can be selected as well as the number of lines
void tft_TextInBox(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const char* text, uint8_t align = ALIGNLEFT, boolean big = false, uint16_t fc = ILI9341_WHITE , uint16_t bg = ILI9341_BLACK, uint8_t lines = 1 ) {
char tmp[256];
char msg[50];
char * token;
int16_t xt,yt,h0,x0,sp;
uint16_t wt,ht,len;
uint8_t l;
//first the rectangle will be filled with the background color to erase old content
tft.fillRect(x,y,w,h,bg);
if (big) tft.setFont(FNT12); else tft.setFont(FNT9);
tft.setTextColor(fc);
//encoding for german umlauts
tft_encodeUnicode(text, tmp);
tft.getTextBounds("n",0,100,&xt,&yt,&wt,&ht);
sp =wt;
//Serial.printf("Space = %i\n",sp);
tft.getTextBounds(tmp,0,100,&xt,&yt,&wt,&ht);
//Serial.printf("Text %s Länge %i\n",text,wt);
h0 = 100 - yt;
x0 = x;
y = y + h0 +1;
if (wt<w) { //enough space in one line
if (align == ALIGNCENTER) x0 += (w-wt)/2;
if (align == ALIGNRIGHT) x0 += (w-wt);
//Serial.printf("x= %i, y= %i, ht= %i, text = %s\n",x0,y,ht,tmp);
tft.setCursor(x0, y);
tft.print(tmp);
} else { //if there is not enough space in one line we adjust the text by word wrap
//if only one line is allowed, the text will be cut off on a word boundary
l=lines;
token = strtok(tmp," ");

msg[0] = 0;
len = 0;
while ((token != NULL) && (l>0)) {
tft.getTextBounds(token,0,100,&xt,&yt,&wt,&ht);
Serial.printf(LANG_TextInBox,token,wt,msg,len,l);
if ((len + wt + sp) < w) {
if (len > 0) { strcat(msg," "); len +=sp; }
len = len + wt;
strcat(msg,token);
} else {
x0 = x;
if (align == ALIGNCENTER) x0 += (w-len)/2;
if (align == ALIGNRIGHT) x0 += (w-len);
tft.setCursor(x0, y);
tft.print(msg);
len = wt;
strcpy(msg,token);
y = y + ht +1;
l--;
}
Serial.println(token);
token = strtok(NULL, " ");

}
if ((len > 0) && (l>0)) {
x0 = x;
if (align == ALIGNCENTER) x0 += (w-len)/2;
if (align == ALIGNRIGHT) x0 += (w-len);
y = y + h0 +1;
tft.setCursor(x0, y);
tft.print(msg);
}
}
}
//register a callback for any touch event.
//we get position and type of the event
void tft_OnTouchClick(TS_Point p) {
char txt[50];
sprintf(txt,"Touch on %i, %i\n",p.x,p.y);
tft_TextInBox(0,220,320,20,txt,ALIGNLEFT,false,ILI9341_WHITE,COLOR_BG,1);
//later on real Project you must select pages or actions here
}

//show the progress of softwareupdate as a bar and a number
void tft_ShowOtaProgress(uint32_t prc) {
char tmp[20];
sprintf(tmp,LANG_ShowOtaProgress,prc);
tft_TextInBox(5, 50, 310, 30, tmp, ALIGNLEFT, true, ILI9341_GREEN, COLOR_BG,1);
if (prc == 0) {
tft.drawRect(5,80,310,20,ILI9341_BLUE);
} else {
tft.fillRect(5,80,310 * prc / 100,20,ILI9341_BLUE);
}
}
//##########################################
//prepare display
void tft_SetupDisplay() {
pinMode(TFT_LED,OUTPUT);
tft_setBGLight(80);
tft.begin();
tft.fillScreen(COLOR_BG);
tft.setRotation(3);
tft.setFont(FNT12);
touch.begin();
tevent.setResolution(tft.width(),tft.height());
tevent.setDrawMode(false);
//register callback function
tevent.registerOnTouchClick(tft_OnTouchClick);
}
53 changes: 53 additions & 0 deletions AZ_TOUCH_BASIC/04_wlan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//connect to the WLAN
boolean wlan_InitWiFi(String ssid, String pkey) {
Serial.println(LANG_InitWifi);
boolean connected = false;
//first disconnect
Serial.print(LANG_WifiDisconnect);
WiFi.disconnect();
Serial.print(LANG_ApDisconnect);
WiFi.softAPdisconnect(true);
Serial.print(LANG_SetWifiMode);
//satrt station mode
WiFi.setHostname(HOSTNAME); //define hostname
WiFi.mode(WIFI_STA);
Serial.print(LANG_TryConnectWifi);
Serial.println(ssid);
Serial.print(LANG_WithPassword);
Serial.println(pkey);
//if we have a config we try to connect
if (ssid != "") {
WiFi.begin(ssid.c_str(),pkey.c_str());
uint8_t cnt = 0;
while ((WiFi.status() != WL_CONNECTED) && (cnt<20)){
delay(500);
Serial.print(".");
cnt++;
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
connected = true;
}
}
//if we have no connection /no config or wrong config)
//we start an access point to allow configuration
if (!connected) {
Serial.println(LANG_NoConnectionStartAp);
WiFi.mode(WIFI_AP);
WiFi.softAP(AP_NAME,"",1);
}
return connected;
}

void wlan_DetectConnectionLost(){
//detect a disconnect
if (connected && (WiFi.status() != WL_CONNECTED)){
connected = false;
//remember tick count for automatic retry
discon = millis();
Serial.printf("==>INFO Variable: connected= %i discon = %i weekday = %i\n",connected,discon,weekday);
Serial.println(LANG_ConnectionLost);
}
}
10 changes: 10 additions & 0 deletions AZ_TOUCH_BASIC/05_ntp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
void ntp_GetTime(){
//setup real time clock
configTzTime(TZONE, ntp.c_str());
//fill timestarukture ti, minutes and weekday with now
getLocalTime(&ti);
minutes = ti.tm_hour * 60 + ti.tm_min;
weekday = ti.tm_wday;
Serial.printf("==>INFO NTP: %i:%i Day: %i\n",ti.tm_hour,ti.tm_min,ti.tm_wday);
Serial.printf("==>INFO Variable: minutes= %i weekday = %i\n",minutes,weekday);
}
Loading

0 comments on commit 2d1803f

Please sign in to comment.