4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

added SmartDial for handling numerical inputs, basic state system and SmartDisplay utils

This commit is contained in:
zzzz 2024-05-01 21:14:40 +02:00
parent c48d941334
commit a4b7c00abd
5 changed files with 178 additions and 15 deletions

58
src/SmartDial.cpp Normal file
View file

@ -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;
}

17
src/SmartDial.hpp Normal file
View file

@ -0,0 +1,17 @@
#pragma once
#include <Arduino.h>
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);
};

37
src/SmartDisplay.cpp Normal file
View file

@ -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();
}

13
src/SmartDisplay.hpp Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
class SmartDisplay {
public:
SmartDisplay(Adafruit_SSD1306 *display);
void printTeaConfigScreen(int steepingSeconds, int editDir);
void printAddNewTeaScreen();
void printSteepingInProgressScreen();
private:
Adafruit_SSD1306 *m_display;
};

View file

@ -7,12 +7,15 @@
#include "TeaTimer.hpp" #include "TeaTimer.hpp"
#include "ContinuousServo.hpp" #include "ContinuousServo.hpp"
#include "SmartDial.hpp"
#include "SmartDisplay.hpp"
#define WIRE Wire #define WIRE Wire
#define DISPLAY_PIN 4 // D2 (SDA) #define DISPLAY_PIN 4 // D2 (SDA)
#define SERVO_PIN 0 // D3 #define SERVO_PIN 0 // D3
#define TOUCH_BTN_PIN 16 // D0 #define TOUCH_BTN_PIN 16 // D0
#define POTENTIOMETER_PIN A0 // A0
// RFID // RFID
#define RST_PIN 2 // D4 #define RST_PIN 2 // D4
@ -25,20 +28,23 @@ auto display = Adafruit_SSD1306(128, 32, &WIRE);
ContinuousServo contServo(SERVO_PIN); ContinuousServo contServo(SERVO_PIN);
TeaTimer teaTimer; 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 serial_dump_byte_array(byte *buffer, byte bufferSize);
void oled_dump_byte_array(byte *buffer, byte bufferSize);
void printMsg(const String &s); void printMsg(const String &s);
void printMsg(unsigned long l); 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() void setup()
{ {
Serial.begin(9600); Serial.begin(9600);
@ -100,8 +106,32 @@ bool isBtnPressed()
return digitalRead(TOUCH_BTN_PIN) == HIGH; return digitalRead(TOUCH_BTN_PIN) == HIGH;
} }
int steepingSeconds = 60;
void loop() 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 // Look for new cards
if (!mfrc522.PICC_IsNewCardPresent()) if (!mfrc522.PICC_IsNewCardPresent())
return; return;
@ -120,9 +150,9 @@ void loop()
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0, 10); display.setCursor(0, 10);
display.println("Card UID: "); display.println("Card UID: ");
oled_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); // oled_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
display.println(); // display.println();
display.display(); // display.display();
// PICC type, e.g. "MIFARE 1K0" // PICC type, e.g. "MIFARE 1K0"
Serial.print(F("PICC type: ")); Serial.print(F("PICC type: "));
@ -170,3 +200,11 @@ void serial_dump_byte_array(byte *buffer, byte bufferSize)
Serial.print(buffer[i], HEX); 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);
}
}