mirror of
https://github.com/zzzzDev4/IAS-Better-Tea.git
synced 2025-04-21 07:31:20 +02:00
155 lines
No EOL
3.5 KiB
C++
155 lines
No EOL
3.5 KiB
C++
#include <Arduino.h>
|
|
#include <SPI.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#include <MFRC522.h>
|
|
|
|
#include "TeaTimer.hpp"
|
|
#include "ContinuousServo.hpp"
|
|
|
|
#define WIRE Wire
|
|
|
|
#define DISPLAY_PIN 4 // D2 (SDA)
|
|
#define SERVO_PIN 0 // D3
|
|
#define TOUCH_BTN_PIN 16 // D0
|
|
|
|
// RFID
|
|
#define RST_PIN 2 // D4
|
|
uint8_t scanDelay = 5; // 5 second delay between reads
|
|
#define SS_PIN 15 // D8
|
|
MFRC522 mfrc522(SS_PIN, RST_PIN); // create RFID instance
|
|
MFRC522::MIFARE_Key key;
|
|
|
|
auto display = Adafruit_SSD1306(128, 32, &WIRE);
|
|
|
|
ContinuousServo contServo(SERVO_PIN);
|
|
TeaTimer teaTimer;
|
|
|
|
void serial_dump_byte_array(byte *buffer, byte bufferSize);
|
|
void printMsg(const String &s);
|
|
void printMsg(unsigned long l);
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
|
|
Serial.println("Initializing SPI...");
|
|
SPI.begin();
|
|
|
|
Serial.println("Initializing RFID...");
|
|
mfrc522.PCD_Init();
|
|
Serial.print(F("Ver: 0x"));
|
|
byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
|
Serial.println(readReg, HEX);
|
|
|
|
Serial.println("Initializing OLED...");
|
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32
|
|
printMsg("Tea is Great!");
|
|
delay(3000);
|
|
|
|
// Prepare the key (used both as key A and key B)
|
|
for (byte i = 0; i < 6; i++)
|
|
{
|
|
key.keyByte[i] = 0xFF;
|
|
}
|
|
|
|
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate basic recognition."));
|
|
Serial.print(F("Using key (for A and B):"));
|
|
serial_dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
|
|
Serial.println();
|
|
|
|
// teaTimer.init(&contServo);
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
pinMode(TOUCH_BTN_PIN, INPUT);
|
|
}
|
|
|
|
void printMsg(const String &s)
|
|
{
|
|
display.clearDisplay();
|
|
display.setTextColor(WHITE);
|
|
display.setTextSize(1);
|
|
display.setCursor(0, 10);
|
|
display.println(s);
|
|
display.display();
|
|
}
|
|
void printMsg(unsigned long l)
|
|
{
|
|
display.clearDisplay();
|
|
display.setTextColor(WHITE);
|
|
display.setTextSize(1);
|
|
display.setCursor(0, 10);
|
|
display.println(l);
|
|
display.display();
|
|
}
|
|
|
|
bool isBtnPressed()
|
|
{
|
|
if (millis() < 10000)
|
|
return false;
|
|
return digitalRead(TOUCH_BTN_PIN) == HIGH;
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
// Look for new cards
|
|
if (!mfrc522.PICC_IsNewCardPresent())
|
|
return;
|
|
|
|
// Select one of the cards
|
|
if (!mfrc522.PICC_ReadCardSerial())
|
|
return;
|
|
|
|
// Show some details of the PICC (that is: the tag/card)
|
|
// UID is a unique four hex pairs, e.g. E6 67 2A 12
|
|
Serial.print(F("Card UID:"));
|
|
serial_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
|
|
display.println("Card UID: ");
|
|
display.println();
|
|
// PICC type, e.g. "MIFARE 1K0"
|
|
Serial.print(F("PICC type: "));
|
|
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
|
|
Serial.println(mfrc522.PICC_GetTypeName(piccType));
|
|
|
|
// Halt PICC
|
|
mfrc522.PICC_HaltA();
|
|
|
|
Serial.print("Delay scanning...");
|
|
delay(scanDelay * 1000); // Turn seconds into milliseconds
|
|
Serial.println(" ...Ready to scan again!");
|
|
|
|
/*
|
|
teaTimer.tick();
|
|
|
|
if (isBtnPressed() && !teaTimer.isSteeping())
|
|
{
|
|
teaTimer.beginSteeping(5000);
|
|
}
|
|
if (teaTimer.isSteeping())
|
|
{
|
|
auto r = teaTimer.remainingSteepTimeSeconds();
|
|
printMsg(r);
|
|
}
|
|
else
|
|
{
|
|
printMsg("Default");
|
|
}*/
|
|
}
|
|
|
|
void toggle_led()
|
|
{
|
|
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
|
|
}
|
|
|
|
/**
|
|
* Helper routine to dump a byte array as hex values to Serial.
|
|
*/
|
|
void serial_dump_byte_array(byte *buffer, byte bufferSize)
|
|
{
|
|
for (byte i = 0; i < bufferSize; i++)
|
|
{
|
|
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
|
|
Serial.print(buffer[i], HEX);
|
|
}
|
|
} |