#include <LiquidCrystal_I2C.h>
#include <MicroBeaut.h>
#include "IMalloc.h"
#include "Queue.h"
class Recipe {
public:
String Name;
float CookingTime;
public:
Recipe(){Name=""; CookingTime=0;}
Recipe(String name, float cookingTime){Name=name; CookingTime=cookingTime;}
};
Recipe recipes[] = { Recipe("Menu-A", 2.0)
,Recipe("Menu-B", 3.0)
,Recipe("Menu-C", 4.0)
,Recipe("Menu-D", 5.0)
};
const int inputPin[] = {A2, A3, 13, 7};
MicroBeaut_Rising reInput[12];
MicroBeaut_Queue<Recipe,6> order;
const String menuList[4] = {"Order In Q.: ", "Next :", "CurrQ:", "TimeR:"};
const String unitList[4] = {"Q.", "", "", "sec."};
String buffer[4];
float remainingTime;
LiquidCrystal_I2C myLCD(0x27, 20, 4);
MicroBeaut_TimeSchedule tslcdDisplay;
const float updateRateLCD = 0.05; // 100 miliseconds
MicroBeaut_TimerOn tmrCookingTime;
String LCDAlignLeft(String stringValue, int Length = 20);
void Cooking();
void Order();
void setup() {
Serial.begin(115200);
myLCD.init();
myLCD.backlight();
tslcdDisplay.Setup(updateRateLCD, LCDDisplay);
Serial.println("Applied Microcontroller Programming");
Serial.println("By MicroBeaut");
for(int index = 0 ; index < 2 /*order.Capacity()*/ /*sizeof(recipes)/sizeof(Recipe)*/ ; index++) {
order.Enqueue(recipes[random(0,3)]);
}
for(int index = 0 ; index < sizeof(inputPin)/sizeof(int) ; index++) {
pinMode(inputPin[index], INPUT_PULLUP);
}
ReadQueue();
LCDDisplay();
delay(2000);
}
void loop() {
tslcdDisplay.Run();
Order();
ReadQueue();
Cokking();
}
void Order(){
for(int index = 0 ; index < sizeof(inputPin)/sizeof(int) ; index++) {
if (reInput[index].Rising(!digitalRead(inputPin[index]))) {
order.Enqueue(recipes[index]);
}
}
}
void Cokking() {
int count = order.Count();
if (count > 0 ) {
Recipe currOrder = order.Peek();
tmrCookingTime.SetTimeDelay(currOrder.CookingTime);
tmrCookingTime.TimerOn(true);
if (tmrCookingTime.Output()) {
tmrCookingTime.TimerOn(false);
order.Dequeue();
}
remainingTime = tmrCookingTime.GetTimeDelay() - tmrCookingTime.GetElapsedTime();
}
else {
remainingTime = 0.0;
}
}
void ReadQueue() {
Recipe currOrder;
Recipe nexOrder;
int count = order.Count();
if (count > 0) {
currOrder = order.Peek();
}
else {
currOrder.Name ="-";
}
if (count > 1) {
nexOrder = order[1];
}
else {
nexOrder.Name ="-";
}
buffer[0] = count + " " + order.Length();
buffer[1] = nexOrder.Name + (order.Count() > 1 ? " " + String(nexOrder.CookingTime,1) + "s" : "");
buffer[2] = currOrder.Name + (order.Count() > 0 ? " " + String(currOrder.CookingTime,1) + "s" : "");
buffer[3] = remainingTime;
}
void LCDDisplay() {
String strLCD = "";
for (int index = 0 ; index < 4 ; index++) {
myLCD.setCursor(0, index);
strLCD = menuList[index];
if (index == 0){
strLCD = LCDAlignLeft(strLCD, 14);
}
else {
strLCD = LCDAlignLeft(strLCD, 7);
}
strLCD = strLCD + buffer[index]; + " " + unitList[index];
strLCD = LCDAlignLeft(strLCD, 20);
myLCD.print(strLCD);
}
}
String LCDAlignLeft(String stringValue, int Length = 20) {
for (int index = 0 ; Length - stringValue.length() ; index++) {
stringValue += " ";
}
return stringValue;
}