#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#include "max7219func.h"
#include <SPI.h>
#include <TimeLib.h>
#define PIN_RECEIVER 12 // Signal Pin of IR receiver
#define MAX_MESG 20
IRrecv receiver(PIN_RECEIVER);
LiquidCrystal_I2C lcd(0x27, 16, 2);
utf8_t szMesg[MAX_MESG + 1];
void getTime(utf8_t *psz)
{
char dummyfont;
if(numFont == Font3x8) dummyfont = 1;
else if(numFont == Font4x8) dummyfont = 2;
else if(numFont == Font5x8) dummyfont = 3;
sprintf((char*)psz, "%02d%s%02d%c\x9a%02d\x9a", hours, seconds % 2 ? (numFont == Font5x8 ? "\x9c" : " ") : (numFont == Font5x8 ? "\x9b" : ":"), minutes, numFont == Font5x8 ? '\0' : ' ', seconds);
if(hours<10) *psz = dummyfont;
//if(numFont == Font5x8 ) psz[1 < 10 ? 4 : 5] = '\x0';
}
void checkIR() {
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
}
void utfStrConv(utf8_t *str)
{
uint8_t stlLen = strlen(str);
bool utfFound;
while(*str != '\0'){
switch (*str) {
case 0xC3: *str = *(str + 1) + 64; utfFound = true; break;
case 0xC5:
utfFound = true;
switch (*(str + 1)) {
case 0x90: case 0x91: *str = *(str + 1) + 5; break; // Ő ő
case 0xB0: case 0xB1: *str = *(str + 1) - 25; // Ű ű
}
default: utfFound = false;
}
str++;
if(utfFound)
for(utf8_t *tmp = str; *tmp != '\0'; tmp++)
*tmp = *(tmp + 1);
}
}
utf8_t helyseg[] = "Várpalota:";
utf8_t temp[] = "-21.6\x90\x43";
utf8_t hum[] = "Pára:";
utf8_t humDigs[] = "25%";
uint8_t intensity = 2;
void setup()
{
Wire.begin();
mx.begin();
lcd.init();
lcd.print("<press a button>");
Serial.begin(115200);
receiver.enableIRIn(); // Start the receiver
while (!Serial) // Wait for the serial connection to be establised.
delay(50);
Serial.println();
Serial.print(F("IRrecvDemo is now running and waiting for IR message on Pin "));
Serial.println(PIN_RECEIVER);
numFont = numFonts[numFontIndex];
utfStrConv(helyseg);
utfStrConv(hum);
mx.control(MD_MAX72XX::INTENSITY, intensity);
//printText("aaaaaai");delay(10000);
}
//char animStr[] = {'P','L','U'};
//enum animTypes { Pr, LR, UD };
uint8_t animType;
enum states { Idle, InitCycle, TimeStart, ShowTime,ShowOtherInfos} curState;
bool lock = false;
bool jmpNext = false;
uint32_t idleTime;
void loop() {
static enum states state = InitCycle;
static uint32_t showTimeFor = 2000, showValuesFor = 1000;
static uint8_t lastSec;
static byte anims[5];
byte tempPos;
//----------------------------------------------------------------
switch (state) {
case Idle:
yield(); // ESP?
if (elapsed(idleTime)){
if(curState == ShowTime) animEnd = true;
if(animEnd){
animEnd = false;
updown = lr = random(2);
if(lock && (curState == ShowTime || curState == ShowTempDigits || curState == ShowHumidityDigits)){ Serial.print(curState);
anims[curState - 3] = Pr;
curState = curState - 1;
}
if(curState == ShowHumidityDigits) state = curState = InitCycle;
else state = curState = curState + 1;
animType = random(3);
}
else state = curState;
idleTime = 15;
}
else {
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
if(jmpNext){
startTime[0] += idleTime;
animEnd = true;
jmpNext = false;
}
}
}
if(curState == ShowTime) state = ShowTime;
break;
case InitCycle:
//for(int i = 0; i < 5; i++){ anims[i] = random(3); Serial.print(animStr[anims[i]]); }
//Serial.println();
//anims[0] = UD;
//anims[1] = Pr;
//anims[2] = UD;
//anims[3] = Pr;
//anims[4] = UD;
//for(int i = 0; i < 5; i++)Serial.print(animStr[anims[i]]);
DS1307getTime();
setTime(hours, minutes, seconds, mday, monthL, yearL);
getTime(szMesg);//Serial.println((char *)szMesg);
animType = random(3);
//lr = LEFT;
curState = TimeStart;
case TimeStart:
switch (animType) {
case Pr: printText(szMesg); break;
case LR: TSLR(szMesg); break;
case UD: TSUD(szMesg);
}
state = Idle; idleTime = 15;
break;
case ShowTime: //Serial.println("showtime");delay(2000);
hours = hour(), minutes = minute(), seconds = second();
if (lastSec != seconds) {
lastSec = seconds;
getTime(szMesg);
if (numFont == Font5x8) {
printText(szMesg);
seconds <= 30 ? mx.setPoint(7, seconds == 0 ? 30 : 31 - seconds, true) : mx.setPoint(7, seconds == 31 ? 1 : seconds - 30, true);
if (seconds > 55)mx.setColumn(30, 4 << seconds % 5);
if (seconds > 25 && seconds < 31) mx.setColumn(1, 4 << (seconds < 30 ? seconds % 5 : 5));
} else
printText(szMesg);
}
state = Idle;
idleTime = showTimeFor;
break;
case ShowOtherInfos:
switch (animType) {
case Pr: printText(helyseg); break;
case LR: lr = true; TSLR(helyseg); break;
case UD: TSUD(helyseg);
}
state = Idle;
if(animEnd) idleTime = showValuesFor;
break;
case ShowTempDigits:
switch (anims[2]) {
case Pr: printText(temp); break;
case LR: TSLR(temp); break;
case UD: TSUD(temp);
}
state = Idle;
if(animEnd) idleTime = showValuesFor;
break;
case ShowHumidityText:
switch (anims[3]) {
case Pr: printText(hum); break;
case LR: TSLR(hum); break;
case UD: TSUD(hum);
}
state = Idle;
if(animEnd) idleTime = showValuesFor;
break;
case ShowHumidityDigits:
switch (anims[4]) {
case Pr: printText(humDigs); break;
case LR: TSLR(humDigs); break;
case UD: TSUD(humDigs);
}
state = Idle;
if(animEnd) idleTime = showValuesFor;
break;
}
}
void lcdPrint(const char* text)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("button pressed:");
lcd.setCursor(0, 1);
lcd.print(text);
lcd.print(" code: ");
lcd.print(receiver.decodedIRData.command);
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 162:
char time[2];
//lcdPrint("POWER");
DS1307getTime();
lcdPrint(itoa(seconds, time, 10));
break;
case 226:
lcdPrint("MENU");
lock = !lock;
break;
case 34:
lcdPrint("TEST");
jmpNext = true;
animState = 0;
mx.clear();
break;
case 2:
lcdPrint("PLUS");
break;
case 194:
lcdPrint("BACK");
break;
case 224:
lcdPrint("PREV.");
break;
case 168:
lcdPrint("PLAY");
break;
case 144:
lcdPrint("NEXT");
break;
case 104:
lcdPrint("num: 0");
break;
case 152:
lcdPrint("MINUS");
break;
case 176:
lcdPrint("key: C");
startTime[0] += idleTime;
animState = 0;
animEnd = true;
numFontIndex = (numFontIndex + 1) % 3;
numFont = numFonts[numFontIndex];
curState = TimeStart;
break;
case 48:
lcdPrint("num: 1");
break;
case 24:
lcdPrint("num: 2");
break;
case 122:
lcdPrint("num: 3");
break;
case 16:
lcdPrint("num: 4");
break;
case 56:
lcdPrint("num: 5");
break;
case 90:
lcdPrint("num: 6");
break;
case 66:
lcdPrint("num: 7");
break;
case 74:
lcdPrint("num: 8");
break;
case 82:
lcdPrint("num: 9");
break;
default:
lcd.clear();
lcd.print(receiver.decodedIRData.command);
lcd.print(" other button");
}
}