From b4e402c60189f659596c4aa710112fb12ae32fa2 Mon Sep 17 00:00:00 2001 From: zzzz Date: Sat, 11 May 2024 20:01:41 +0200 Subject: [PATCH] Added SmartButtons for +- 10s config --- platformio.ini | 1 + src/SmartButton.cpp | 27 +++++++++++++++++++++++++ src/SmartButton.hpp | 11 ++++++++++ src/main.cpp | 49 +++++++++++++++++++++++++++++++++------------ src/main.hpp | 1 + 5 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 src/SmartButton.cpp create mode 100644 src/SmartButton.hpp diff --git a/platformio.ini b/platformio.ini index d0cf268..dd0649b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,3 +16,4 @@ lib_deps = adafruit/Adafruit SSD1306@^2.5.9 arduino-libraries/Arduino_JSON@^0.2.0 adafruit/Adafruit PN532@^1.3.3 + fastled/FastLED@^3.6.0 diff --git a/src/SmartButton.cpp b/src/SmartButton.cpp new file mode 100644 index 0000000..e98dd9f --- /dev/null +++ b/src/SmartButton.cpp @@ -0,0 +1,27 @@ +#include "SmartButton.hpp" +#include + +SmartButton::SmartButton(int pin) +{ + m_pin = pin; +} + +void SmartButton::init() +{ + pinMode(m_pin, INPUT); +} + +bool SmartButton::isJustPressed() +{ + if (digitalRead(m_pin) == HIGH) + { + m_isDown = false; + return false; + } + if (digitalRead(m_pin) == LOW && !m_isDown) + { + m_isDown = true; + return true; + } + return false; +} \ No newline at end of file diff --git a/src/SmartButton.hpp b/src/SmartButton.hpp new file mode 100644 index 0000000..4691ab7 --- /dev/null +++ b/src/SmartButton.hpp @@ -0,0 +1,11 @@ +#pragma once + +class SmartButton { + public: + SmartButton(int pin); + void init(); + bool isJustPressed(); + private: + bool m_isDown; + int m_pin; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ff84d06..f6d23e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,14 @@ #include "main.hpp" #include "secrets.hpp" #include "song.h" +#include #define WIRE Wire #define SERVO_PIN 14 // D5 #define POTENTIOMETER_PIN A0 // A0 #define BUZZER_PIN 12 // D6 +#define LEDSTRIPE_PIN 15 // D8 // buttons #define TACTILE_BTN_LEFT 0 // D3 @@ -22,6 +24,10 @@ Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // RFID with I2C #define DISPLAY_PIN 4 // D2 (SDA) auto display = Adafruit_SSD1306(128, 32, &WIRE); +// FastLED +#define PIXELCOUNT 20 +CRGB leds[PIXELCOUNT]; // Define the array of leds + // util classes ContinuousServo contServo(SERVO_PIN); TeaTimer teaTimer(&contServo); @@ -30,6 +36,9 @@ SmartDial smartDial(POTENTIOMETER_PIN); SmartDisplay smartDisplay(&display); TeaNetworking teaNet; +SmartButton buttonLeft(TACTILE_BTN_LEFT); +SmartButton buttonRight(TACTILE_BTN_RIGHT); + // networking String shuttleServer = "https://ias-tea-axum.shuttleapp.rs"; @@ -49,6 +58,24 @@ void setup() { Serial.begin(9600); + // FastLED setup + FastLED.addLeds(leds, PIXELCOUNT); + FastLED.clear(true); + FastLED.show(); + delay(2000); + + leds[0].setRGB(2, 0, 0); + FastLED.show(); + delay(1000); + + leds[10].setRGB(0, 2, 0); + FastLED.show(); + delay(1000); + + leds[19].setRGB(0, 0, 2); + FastLED.show(); + delay(1000); + teaNet.init(ssid, password); smartRFID.init(); @@ -62,11 +89,10 @@ void setup() pinMode(LED_BUILTIN, OUTPUT); pinMode(TOUCH_BTN_PIN, INPUT); - pinMode(TACTILE_BTN_LEFT, INPUT); - pinMode(TACTILE_BTN_RIGHT, INPUT); + buttonLeft.init(); + buttonRight.init(); } -int steepingSeconds = 60; void loop() { @@ -96,7 +122,7 @@ void loop() { smartDisplay.printWaitForRFIDScreen(); - playSong(); + // playSong(); // this blocks execution until a tag has been detected String uid = smartRFID.readTag(); @@ -121,7 +147,7 @@ void loop() result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + uid); } - currentTea = TeaData(result["teaname"], result["rfidcode"], result["watertemp"], result["steepingSeconds"]); + currentTea = TeaData(result["teaname"], result["rfidcode"], result["watertemp"], result["steepingseconds"]); state = State::DISPLAY_SCANNED_TEA_CONFIG; @@ -131,18 +157,15 @@ void loop() { int dir = smartDial.smoothEdit(¤tTea.m_steepingSeconds, 0, 6000); - if (digitalRead(TACTILE_BTN_LEFT) == LOW) + if (buttonLeft.isJustPressed()) { - printMsg("BUTTON GREEN"); + currentTea.m_steepingSeconds -= 10; } - else if (digitalRead(TACTILE_BTN_RIGHT) == LOW) + if (buttonRight.isJustPressed()) { - printMsg("BUTTON RED"); - } - else - { - smartDisplay.printTeaConfigScreen(currentTea.m_teaName, currentTea.m_steepingSeconds, dir); + currentTea.m_steepingSeconds += 10; } + smartDisplay.printTeaConfigScreen(currentTea.m_teaName, currentTea.m_steepingSeconds, dir); // change state if start pressed if (isBtnPressed()) diff --git a/src/main.hpp b/src/main.hpp index d4080c9..7cb8915 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -16,6 +16,7 @@ #include "SmartDisplay.hpp" #include "TeaNetworking.hpp" #include "TeaData.hpp" +#include "SmartButton.hpp" void playSong(); bool toggle_led(void *);