#include "Adafruit_GFX.h" // Include core graphics library #include "Adafruit_ST7735.h" // Include Adafruit_ST7735 library to drive the display #include "Arduino.h" #include "HardwareSerial.h" #include "ExEzButton.h" #include "Status.h" #include "RotaryControler.h" #include "EchoLotSetup.h" #include "ThreePositionSwitch.h" #include "fonts/titillium_web_regular16pt7b.h" // Add a custom font #include "fonts/titillium_web_regular12pt7b.h" // Add a custom font #include "fonts/titillium_web_regular8pt7b.h" // Add a custom font #include "images/SFToolsLogo.h" #include "images/thermometer_32.h" #include "images/gear.h" #include "images/zzz.h" #include "images/ruler.h" #include "images/air.h" #include "images/water.h" #include "images/water_s.h" static const int trigPin = A0; static const int echoPin = A1; static const int RotEnc_Switch_Pin = A2; static const int RotEnc_Clk_Pin = A3; static const int RotEnc_Dta_Pin = A4; static const int Pos3Sw_Pin1 = 3; static const int Pos3Sw_Pin2 = 4; static const int Pos3Sw_Pin3 = 5; static const int Measure_Pin = 7; #define LED -1 // to +5v #define SCK 13//A1 with SCK on 13 and SDA on 11 it is 10x faster!!! #define SDA 11//A2 #define MISO -1 #define MOSI SDA #define CS 10 #define DC 9 #define RST 8 Status actualStatus; Status originStatus; String oldStatus = ""; String lastDistance = ""; // Create display: Adafruit_ST7735 tft = Adafruit_ST7735(CS, DC, RST); RotaryControler RotaryControler(RotEnc_Dta_Pin, RotEnc_Clk_Pin, RotEnc_Switch_Pin); ExEzButton MeasureBtn(Measure_Pin, false, 2000); ThreePositionSwitch ThreePositionSwitch(Pos3Sw_Pin1, Pos3Sw_Pin2, Pos3Sw_Pin3); //Display Display; void setup() { SetActualStatus(INITIALIZATION); // Display.init(); Serial.begin(115200); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(RotEnc_Switch_Pin, INPUT); pinMode(RotEnc_Dta_Pin, INPUT); pinMode(RotEnc_Clk_Pin, INPUT); pinMode(Measure_Pin, INPUT); MeasureBtn.setDebounceTime(50); RotaryControler.setDebounceTime(50); ThreePositionSwitch.setDebounceTime(50); // Display setup: // Use this initializer if you're using a 1.8" TFT tft.initR(INITR_BLACKTAB); // Initialize a ST7735S chip, black tab tft.fillScreen(ST7735_BLACK); // Fill screen with black tft.setRotation(1); // Set orientation of the display. Values are from 0 to 3. If not declared, orientation would be 0, // // which is portrait mode. // // tft.setTextWrap(false); // By default, long lines of text are set to automatically “wrap� back to the leftmost column. // // To override this behavior (so text will run off the right side of the display - useful for // // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored // // with setTextWrap(true). // tft.setCursor(0, 20); // Set position (x,y) tft.setFont(&titillium_web_regular16pt7b); tft.setTextSize(1); // Set text size. Goes from 0 (the smallest) to 20 (very big) tft.setTextColor(ST7735_WHITE); tft.println("AaBbCc012"); tft.setCursor(0, 60); // Set position (x,y) tft.setFont(&titillium_web_regular12pt7b); tft.setTextSize(1); // Set text size. Goes from 0 (the smallest) to 20 (very big) tft.setTextColor(ST7735_WHITE); tft.println("AaBbCc0129"); tft.setCursor(0, 80); // Set position (x,y) tft.setFont(&titillium_web_regular8pt7b); tft.setTextSize(1); // Set text size. Goes from 0 (the smallest) to 20 (very big) tft.setTextColor(ST7735_WHITE); tft.println("AaBbCc0129"); tft.drawBitmap(5, 100, epd_bitmap_gear, 24, 24, ST7735_YELLOW); tft.drawBitmap(40, 100, epd_bitmap_Zzz_24, 24, 24, ST7735_CYAN); tft.drawBitmap(75, 100, epd_bitmap_Measure_25, 25, 25, ST7735_RED); tft.fillTriangle(75, 98, 80, 102, 75, 107, ST7735_RED); tft.fillTriangle(82, 98, 87, 102, 82, 107, ST7735_RED); tft.fillTriangle(89, 98, 94, 102, 89, 107, ST7735_RED); tft.fillTriangle(96, 98, 101, 102, 96, 107, ST7735_RED); // tft.drawBitmap(110, 100, epd_bitmap_air, 24, 24, ST7735_CYAN); tft.drawBitmap(110, 100, epd_bitmap_water_s, 24, 24, ST7735_BLUE); // // tft.drawBitmap(0, 80, SFTools_Logo, 128, 34, ST7735_GREEN); // // tft.drawBitmap(1, 160 - 32, thermometer_icon_32, 32, 32, 0xE71C); // // tft.drawBitmap(33, 160 - 32, thermometer_icon_32, 32, 32, ST7735_YELLOW); // tft.drawRect(48, 137, 2, 6, ST7735_YELLOW); // // tft.drawBitmap(110, 96, thermometer_icon_32, 32, 32, ST7735_RED); // tft.drawRect(125, 101, 2, 10, ST7735_RED); // // delay(3000); // // tft.fillScreen(ST7735_BLACK); // Fill screen with black // Display.display(); } void loop() { MeasureBtn.loop(); RotaryControler.loop(); switch (actualStatus) { case INITIALIZATION: SetActualStatus(IDLE); break; case CONFIGURATION: break; case IDLE: if (RotaryControler.isSwitchLongPressed()) { Serial.println("RotaryEnc Long SwitchPressed"); RotaryControler.resetPosition(); } if (RotaryControler.isSwitchPressed()) { Serial.println("RotaryEnc SwitchPressed"); } else if (MeasureBtn.isPressing()) { SetActualStatus(MEASURING); } ThreePositionSwitch.loop(); int pin = ThreePositionSwitch.getPosition(); Serial.print("3-Pos-Schalter Position: "); Serial.println(pin); delay(200); break; case MEASURING: if (MeasureBtn.isPressing()) { digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(20); digitalWrite(trigPin, LOW); long duration = pulseIn(echoPin, HIGH); long distance = duration * 0.03432 / 2; Serial.print("Distance:"); Serial.println(distance); char buf[50]; sprintf(buf, "%lu cm", distance); if (!lastDistance.equals(buf)) { // tft.fillRect(0, 20, 130, 50, ST7735_BLACK); // tft.setCursor(0, 50); // Set position (x,y) // tft.setFont(&titillium_web_regular20pt7b); // tft.setTextSize(0); // Set text size. Goes from 0 (the smallest) to 20 (very big) // tft.setTextColor(ST7735_CYAN); // tft.println(buf); lastDistance = buf; } delay(200); } else { // tft.fillScreen(ST7735_BLACK); SetActualStatus(IDLE); } break; default: break; } } void SetActualStatus(Status newStatus) { if (actualStatus != newStatus) { actualStatus = newStatus; // Display.setRefreshScreen(); } }