diff --git a/CYD/LED_Solution.cpp b/CYD/LED_Solution.cpp new file mode 100644 index 0000000..60b8d02 --- /dev/null +++ b/CYD/LED_Solution.cpp @@ -0,0 +1,24 @@ +#include + +//pins for the RGB LED +const int LED_R = 4; +const int LED_G = 16; +const int LED_B = 17; + +void setLedColor(bool redOn, bool greenOn, bool blueOn) +{ + // active-low LED: + digitalWrite(LED_R, redOn ? LOW : HIGH); + digitalWrite(LED_G, greenOn ? LOW : HIGH); + digitalWrite(LED_B, blueOn ? LOW : HIGH); +} + +void setupLed() +{ + pinMode(LED_R, OUTPUT); + pinMode(LED_G, OUTPUT); + pinMode(LED_B, OUTPUT); + + // alguses kõik välja + setLedColor(false, false, false); +} \ No newline at end of file diff --git a/CYD/TouchHandler.cpp b/CYD/TouchHandler.cpp new file mode 100644 index 0000000..99210d7 --- /dev/null +++ b/CYD/TouchHandler.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include "TouchHandler.h" + +#define TOUCH_IRQ 36 +#define TOUCH_MOSI 32 +#define TOUCH_MISO 39 +#define TOUCH_CLK 25 +#define TOUCH_CS 33 + +#define SCREEN_WIDTH 320 +#define SCREEN_HEIGHT 240 + +XPT2046_Touchscreen touchscreen(TOUCH_CS, TOUCH_IRQ); + +void initTouch() +{ + SPI.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI, TOUCH_CS); + touchscreen.begin(); + touchscreen.setRotation(1); +} + +bool getTouchPoint(uint16_t &x, uint16_t &y, uint16_t &z) +{ + if (!(touchscreen.tirqTouched() && touchscreen.touched())) + { + return false; + } + + TS_Point p = touchscreen.getPoint(); + + // CYD working example ranges + x = map(p.x, 200, 3700, 1, SCREEN_WIDTH); + y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT); + z = p.z; + + // lihtne sanity check + if (x > SCREEN_WIDTH || y > SCREEN_HEIGHT || z == 0) + { + return false; + } + + return true; +} \ No newline at end of file diff --git a/CYD/main.cpp b/CYD/main.cpp index e178e53..b8a7b51 100644 --- a/CYD/main.cpp +++ b/CYD/main.cpp @@ -1,5 +1,7 @@ #include #include "AnimationPlayer.h" +#include "LED_Solution.h" +#include "TouchHandler.h" void handleCommand(String cmd) { @@ -12,16 +14,19 @@ void handleCommand(String cmd) if (cmd == "caring") { switchFolder("/caring"); + setLedColor(true, false, false); Serial.println("Switched to /caring"); } else if (cmd == "surprised") { switchFolder("/surprised"); + setLedColor(false, false, true); Serial.println("Switched to /surprised"); } else if (cmd == "pride") { switchFolder("/pride"); + setLedColor(false, true, false); Serial.println("Switched to /pride"); } else @@ -33,13 +38,18 @@ void handleCommand(String cmd) void setup() { Serial.begin(115200); - used_to_be_setup(); + used_to_be_setup();//Edited setup from animationplayer. + initTouch(); + setupLed(); switchFolder("/pride"); + Serial.println("Ready for UART commands"); } +bool touchWasDown = false; + void loop() { if (Serial.available()){ String cmd = Serial.readStringUntil('\n'); @@ -47,4 +57,19 @@ void loop() { } updateAnimationPlayer(); + + uint16_t x, y, z; + bool touchNow = getTouchPoint(x, y, z); + + if (touchNow && !touchWasDown) + { + Serial.print("X = "); + Serial.print(x); + Serial.print(" | Y = "); + Serial.print(y); + Serial.print(" | Z = "); + Serial.println(z); + } + + touchWasDown = touchNow; } \ No newline at end of file