From 7b8f98cb3c94b7fa9237fa6d34fff5a96049cd8e Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Mon, 1 Apr 2024 16:49:44 +0200 Subject: [PATCH] Fully functioning screen code --- .../Screen-code-final/.theia/launch.json | 7 ++ .../Screen-code-final/Screen-code-final.ino | 115 ++++++++++++++++++ .../Screen-code-final/displayText.cpp | 106 ++++++++++++++++ .../Screen-code-final/displayText.h | 19 +++ .../Screen-code-final/headerFile.h | 22 ++++ .../Screen-code-full/Screen-code.ino | 0 6 files changed, 269 insertions(+) create mode 100644 arduino/Screen code/Screen-code-final/.theia/launch.json create mode 100644 arduino/Screen code/Screen-code-final/Screen-code-final.ino create mode 100644 arduino/Screen code/Screen-code-final/displayText.cpp create mode 100644 arduino/Screen code/Screen-code-final/displayText.h create mode 100644 arduino/Screen code/Screen-code-final/headerFile.h delete mode 100644 arduino/Screen code/Screen-code-full/Screen-code.ino diff --git a/arduino/Screen code/Screen-code-final/.theia/launch.json b/arduino/Screen code/Screen-code-final/.theia/launch.json new file mode 100644 index 0000000..8b8a0d4 --- /dev/null +++ b/arduino/Screen code/Screen-code-final/.theia/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + "version": "0.2.0", + "configurations": [ + ] +} diff --git a/arduino/Screen code/Screen-code-final/Screen-code-final.ino b/arduino/Screen code/Screen-code-final/Screen-code-final.ino new file mode 100644 index 0000000..0f965ba --- /dev/null +++ b/arduino/Screen code/Screen-code-final/Screen-code-final.ino @@ -0,0 +1,115 @@ +#include "headerFile.h" + +int i = 0; +int questionID = 1; +char* Question[] = { + "How clean are the toilets?", + "How clean is the study area?", + "What do you think of the temperature in the study area?", + "How crowded would you say the study area is?", + "Is there enough help available?" +}; + +char* Answer[] = { + "clean, normal, disgusting", + "clean, normal, disgusting", + "hot, perfect, cold", + "not at all, its fine, really crowded", + "no, decently, yes" +}; + + Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST, MISO); + DisplayText displayText(tft); + +void setup() { + //buttonpins + pinMode(16, INPUT_PULLDOWN); + pinMode(17, INPUT_PULLDOWN); + pinMode(18, INPUT_PULLDOWN); + Serial.begin(9600); + + tft.begin(); // Initialize the display + tft.setRotation(3); // Set the rotation to horizontal + tft.fillScreen(ST7796S_BLACK); + displayText.writeText("Loading...", 3, 0, 200, 0, true, true); + websocketSetup(); +} + + +void loop() { + webSocket.loop(); + screenButtonHandler(); +} + +void websocketSetup(){ + WiFiMulti.addAP("iotroam", "vbK9gbDBIB"); + WiFiMulti.addAP("ObsidianAmstelveen", "drijversstraatmaastricht"); + + while(WiFiMulti.run() != WL_CONNECTED) { + delay(100); + } + + // server address, port and URL + webSocket.begin("145.92.8.114", 80, "/ws"); + // try ever 500 again if connection has failed + webSocket.setReconnectInterval(500); +} + +void screenButtonHandler(){ + + int redButton = digitalRead(16); + int whiteButton = digitalRead(17); + int greenButton = digitalRead(18); + + if (initialized) { + initialized = false; + tft.fillScreen(ST7796S_BLACK); + displayText.writeText(Question[i], 3, 0, 0, 0, true, false); + displayText.writeText(Answer[i], 3, 0, 200, 0, true, true); + } + + if (redButton == HIGH){ + sendData(questionID, "0"); + } + if (whiteButton == HIGH){ + sendData(questionID, "1"); + } + if (greenButton == HIGH){ + sendData(questionID, "2"); + } + + + + if (redButton || whiteButton || greenButton) { + i++; + questionID++; + if (questionID == 6){ + questionID = 1; + } + if (i == 5) { + i = 0; + } + tft.fillScreen(ST7796S_BLACK); + displayText.writeText(Question[i], 3, 0, 0, 0, true, false); + displayText.writeText(Answer[i], 3, 0, 200, 0, true, true); + } + +} + +void sendData(int question, String answer){ + webSocket.sendTXT("{\"node\": \"" + String(WiFi.macAddress()) + "\", \"Response\":\"" + String(answer) + "\",\"QuestionID\":\"" + String(question) + "\"}"); +} + +void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { + const uint8_t* src = (const uint8_t*) mem; + USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); + for(uint32_t i = 0; i < len; i++) { + if(i % cols == 0) { + USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); + } + USE_SERIAL.printf("%02X ", *src); + src++; + } + USE_SERIAL.printf("\n"); +} + diff --git a/arduino/Screen code/Screen-code-final/displayText.cpp b/arduino/Screen code/Screen-code-final/displayText.cpp new file mode 100644 index 0000000..88dfd70 --- /dev/null +++ b/arduino/Screen code/Screen-code-final/displayText.cpp @@ -0,0 +1,106 @@ +#include "displayText.h" +#include "Arduino.h" + +//constructor +DisplayText::DisplayText(Adafruit_ST7796S_kbv& tftDisplay) : tft(tftDisplay) { + tft.setCursor(0,0); + tft.fillScreen(ST7796S_BLACK); +} +//display text public function +void DisplayText::writeText(char* text, int size, int posX, int posY, int screenTime, bool center, bool bottom) { + if (center) { + posX = centerText(text); + } + // if (bottom) { + // posY = bottomText(text); + // } + tft.setCursor(posX, posY); + tft.setTextSize(size); + printWordsFull(text, bottom); + delay(screenTime); +} + +//to center the text when enabled in the public function +int DisplayText::centerText(char* text) { + int16_t x1, y1; + uint16_t w, h; + tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); + int x = (tft.width() - w) / 2; + return x; +} + +// //to display the text at the bottom when enabled in the public function +// int DisplayText::bottomText(char* text) { +// int16_t x1, y1; +// uint16_t w, h; +// tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); +// int y = (tft.height() - h); +// return y; +// } + +//attempt to write the text out in full (wip) +void DisplayText::printWordsFull(char* text, bool bottom) { + const int screenWidth = 480; // replace with your TFT display width + const int lineHeight = 30; // replace with your text line height + //the double copy is needed so it doesnt alter the original text + char* newtext1 = strdup(text); // Create a copy of the string for the first strtok_r + char* newtext2 = strdup(text); // Create a second copy for the second strtok_r + char* saveptr1, *saveptr2; + char* word = strtok_r(newtext1, " ", &saveptr1); + char line[100] = ""; + int lineCount = 0; + int lineWidth = 0; + + // Calculate total number of lines + int totalLines = 0; + char* tempWord = strtok_r(newtext2, " ", &saveptr2); + while (tempWord != NULL) { + int16_t x1, y1; + uint16_t w, h; + tft.getTextBounds(tempWord, 0, 0, &x1, &y1, &w, &h); + if (lineWidth + w > screenWidth && strlen(line) > 0) { + //if the line width is greater than the screen width, then we need to add a new line + totalLines++; + lineWidth = w; + //otherwise we just add the width of the word to the line width + } else { + lineWidth += w; + } + tempWord = strtok_r(NULL, " ", &saveptr2); + } + totalLines++; // Add one for the last line + + // Reset variables for actual printing + strcpy(line, ""); + lineWidth = 0; + + while (word != NULL) { + int16_t x1, y1; + uint16_t w, h; + tft.getTextBounds(word, 0, 0, &x1, &y1, &w, &h); + + if (lineWidth + w > screenWidth && strlen(line) > 0) { + int y = bottom ? tft.height() - lineHeight * (totalLines - lineCount) : lineHeight * lineCount; + tft.setCursor(0, y); + tft.println(line); + lineCount++; + strcpy(line, word); + strcat(line, " "); + lineWidth = w; + } else { + strcat(line, word); + strcat(line, " "); + lineWidth += w; + } + + word = strtok_r(NULL, " ", &saveptr1); + } + + // print the last line + int y = bottom ? tft.height() - lineHeight * (totalLines - lineCount) : lineHeight * lineCount; + tft.setCursor(0, y); + tft.println(line); + + free(newtext1); // Free the memory allocated by strdup + free(newtext2); // Free the memory allocated by strdup +} \ No newline at end of file diff --git a/arduino/Screen code/Screen-code-final/displayText.h b/arduino/Screen code/Screen-code-final/displayText.h new file mode 100644 index 0000000..9888068 --- /dev/null +++ b/arduino/Screen code/Screen-code-final/displayText.h @@ -0,0 +1,19 @@ +#ifndef DISPLAYTEXT_H +#define DISPLAYTEXT_H + +#include "Adafruit_GFX.h" +#include "Adafruit_ST7796S_kbv.h" + +class DisplayText { + private: + Adafruit_ST7796S_kbv& tft; + int centerText(char* text); + // int bottomText(char* text); + void printWordsFull(char* text, bool bottom); + + public: + DisplayText(Adafruit_ST7796S_kbv& tftDisplay); + void writeText(char* text, int size, int posX, int posY, int screenTime, bool center, bool bottom); +}; + +#endif \ No newline at end of file diff --git a/arduino/Screen code/Screen-code-final/headerFile.h b/arduino/Screen code/Screen-code-final/headerFile.h new file mode 100644 index 0000000..4f1152e --- /dev/null +++ b/arduino/Screen code/Screen-code-final/headerFile.h @@ -0,0 +1,22 @@ +//screen stuff +#include +#include "Adafruit_GFX.h" +#include "Adafruit_ST7796S_kbv.h" +#include "displayText.h" + +#define TFT_CS 14 +#define TFT_DC 13 +#define TFT_RST 12 +#define MOSI 11 +#define SCK 10 +#define MISO 9 + +//websocket stuff +#include +#include +#include +#define USE_SERIAL Serial + +WiFiMulti WiFiMulti; +WebSocketsClient webSocket; +bool initialized = true; \ No newline at end of file diff --git a/arduino/Screen code/Screen-code-full/Screen-code.ino b/arduino/Screen code/Screen-code-full/Screen-code.ino deleted file mode 100644 index e69de29..0000000