diff --git a/src/SmartDial.cpp b/src/SmartDial.cpp new file mode 100644 index 0000000..86983ca --- /dev/null +++ b/src/SmartDial.cpp @@ -0,0 +1,58 @@ +#include "SmartDial.hpp" + +SmartDial::SmartDial(int pin) +{ + m_pin = pin; +} +// Returns a value between 0 and 6 (inclusive) +int SmartDial::getValue() +{ + int v = analogRead(m_pin); + int mapped = map(v, 30, 1024, 0, 6); + return mapped; +} + +// Returns whether the value is currently increasing (1 to 3), decreasing (-1 to -3) or stable (0). +int SmartDial::smoothEdit(int *out_value, int t_min, int t_max) +{ + bool edit = false; + if (millis() >= next_step_time) + { + next_step_time = millis() + m_interval; + edit = true; + } + + int value = getValue(); + m_interval = 500; + if (value < 3) + { + if (edit && *out_value - 1 >= t_min) + (*out_value)--; + int speedSetting = (value + 3) - 6; // -3, -2, -1 + m_interval = getDelayFromSpeed(speedSetting); + return speedSetting; + } + if (value > 3) + { + if (edit && *out_value + 1 <= t_max) + (*out_value)++; + int speedSetting = (value - 3); // 3, 2, 1 + m_interval = getDelayFromSpeed(speedSetting); + return speedSetting; + } + return 0; +} + +int SmartDial::getDelayFromSpeed(int speedSetting) +{ + switch (abs(speedSetting)) + { + case 1: + return 1000; + case 2: + return 500; + case 3: + return 100; + } + return 0; +} \ No newline at end of file diff --git a/src/SmartDial.hpp b/src/SmartDial.hpp new file mode 100644 index 0000000..f8588c9 --- /dev/null +++ b/src/SmartDial.hpp @@ -0,0 +1,17 @@ +#pragma once +#include + +class SmartDial +{ +public: + SmartDial(int pin); + // Returns a value between 0 and 6 (inclusive) + int getValue(); + int smoothEdit(int *out_value, int t_min, int t_max); + +private: + int m_pin; + unsigned long m_interval = 500; + unsigned long next_step_time = 0; + int getDelayFromSpeed(int speedSetting); +}; \ No newline at end of file diff --git a/src/SmartDisplay.cpp b/src/SmartDisplay.cpp new file mode 100644 index 0000000..773bfbf --- /dev/null +++ b/src/SmartDisplay.cpp @@ -0,0 +1,37 @@ +#include "SmartDisplay.hpp" + +SmartDisplay::SmartDisplay(Adafruit_SSD1306 *display) +{ + m_display = display; +} + +void SmartDisplay::printTeaConfigScreen(int steepingSeconds, int editDir) +{ + m_display->clearDisplay(); + m_display->setTextColor(WHITE); + m_display->setTextSize(1); + m_display->setCursor(0, 0); + + m_display->println("Premium Earl Grey"); + m_display->println(" "); + + m_display->setTextSize(2); + m_display->print(steepingSeconds); + m_display->print(" "); + + if (editDir > 0) + { + for (int i = 0; i < editDir; i++) + { + m_display->print("+"); + } + } + if (editDir < 0) + { + for (int i = editDir; i < 0; i++) + { + m_display->print("-"); + } + } + m_display->display(); +} \ No newline at end of file diff --git a/src/SmartDisplay.hpp b/src/SmartDisplay.hpp new file mode 100644 index 0000000..7400ac4 --- /dev/null +++ b/src/SmartDisplay.hpp @@ -0,0 +1,13 @@ +#pragma once +#include +#include + +class SmartDisplay { + public: + SmartDisplay(Adafruit_SSD1306 *display); + void printTeaConfigScreen(int steepingSeconds, int editDir); + void printAddNewTeaScreen(); + void printSteepingInProgressScreen(); + private: + Adafruit_SSD1306 *m_display; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7f42cfe..148b235 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,12 +7,15 @@ #include "TeaTimer.hpp" #include "ContinuousServo.hpp" +#include "SmartDial.hpp" +#include "SmartDisplay.hpp" #define WIRE Wire -#define DISPLAY_PIN 4 // D2 (SDA) -#define SERVO_PIN 0 // D3 -#define TOUCH_BTN_PIN 16 // D0 +#define DISPLAY_PIN 4 // D2 (SDA) +#define SERVO_PIN 0 // D3 +#define TOUCH_BTN_PIN 16 // D0 +#define POTENTIOMETER_PIN A0 // A0 // RFID #define RST_PIN 2 // D4 @@ -25,20 +28,23 @@ auto display = Adafruit_SSD1306(128, 32, &WIRE); ContinuousServo contServo(SERVO_PIN); TeaTimer teaTimer; +SmartDial smartDial(POTENTIOMETER_PIN); +SmartDisplay smartDisplay(&display); + +enum State +{ + WAIT_FOR_RFID_SCAN, + DISPLAY_SCANNED_TEA_CONFIG, + ADD_NEW_TEA_CONFIG, + STEEPING_IN_PROGRESS, +}; +State state = State::DISPLAY_SCANNED_TEA_CONFIG; void serial_dump_byte_array(byte *buffer, byte bufferSize); +void oled_dump_byte_array(byte *buffer, byte bufferSize); void printMsg(const String &s); void printMsg(unsigned long l); -void oled_dump_byte_array(byte *buffer, byte bufferSize) -{ - for (byte i = 0; i < bufferSize; i++) - { - display.print(buffer[i] < 0x10 ? " 0" : " "); - display.print(buffer[i], HEX); - } -} - void setup() { Serial.begin(9600); @@ -100,8 +106,32 @@ bool isBtnPressed() return digitalRead(TOUCH_BTN_PIN) == HIGH; } +int steepingSeconds = 60; void loop() { + + switch (state) + { + case WAIT_FOR_RFID_SCAN: + { + break; + } + case DISPLAY_SCANNED_TEA_CONFIG: + { + int dir = smartDial.smoothEdit(&steepingSeconds, 0, 120); + smartDisplay.printTeaConfigScreen(steepingSeconds, dir); + break; + } + case ADD_NEW_TEA_CONFIG: + { + break; + } + case STEEPING_IN_PROGRESS: + { + break; + } + }; + // Look for new cards if (!mfrc522.PICC_IsNewCardPresent()) return; @@ -120,9 +150,9 @@ void loop() display.setTextSize(1); display.setCursor(0, 10); display.println("Card UID: "); - oled_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); - display.println(); - display.display(); + // oled_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); + // display.println(); + // display.display(); // PICC type, e.g. "MIFARE 1K0" Serial.print(F("PICC type: ")); @@ -169,4 +199,12 @@ void serial_dump_byte_array(byte *buffer, byte bufferSize) Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } +} +void oled_dump_byte_array(byte *buffer, byte bufferSize) +{ + for (byte i = 0; i < bufferSize; i++) + { + display.print(buffer[i] < 0x10 ? " 0" : " "); + display.print(buffer[i], HEX); + } } \ No newline at end of file