#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#include "max7219func.h"
#include <SPI.h>
#include "ds1307.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 timeToString()
{
char dummyfont;
if (numFont == Font3x8) dummyfont = 1;
else if (numFont == Font4x8) dummyfont = 2;
else if (numFont == Font5x8) dummyfont = 3;
sprintf((char*)szMesg, "%02d%s%02d%c\x9a%02d\x9a", hours, seconds % 2 ? (numFont == Font5x8 ? "\x9c" : " ") : (numFont == Font5x8 ? "\x9b" : ":"), minutes, numFont == Font5x8 ? '\0' : ' ', seconds);
if (hours < 10) *szMesg = 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);
}
}
void getLocation(){
sprintf((char *)szMesg, "Várpalota:");
utfStrConv(szMesg);
}
void getTemp(){
sprintf((char *)szMesg, "-21.6\x90\x43");
}
void getHumidityText(){
sprintf((char *)szMesg, "Pára");
utfStrConv(szMesg);
}
void getHumidity(){
sprintf((char *)szMesg, "25%%");
}
uint8_t intensity = 2;
uint8_t numkeyval;
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];
mx.control(MD_MAX72XX::INTENSITY, intensity);
//printText("aaaaaai");delay(10000);
}
//char animStr[] = {'P','L','U'};
enum animTypes { Pr, LR, UD };
uint8_t animType, lastSec;
enum states { Start, TimeStart, ShowTime, ShowTempLoc, ShowTemp, ShowHumText, ShowHum } state = Start;
bool lock = false;
bool jmpNext = false;
uint32_t idleTime;
uint32_t showTimeFor = 5000, showValuesFor = 500;
uint8_t timeSetState;
bool setT;
void loop() {
static byte anims[5];
//----------------------------------------------------------------
yield(); // ESP?
if (elapsed(idleTime)) {
if (animEnd) {
animEnd = false;
updown = lr = random(2);
animType = random(3);
if (lock && (state == ShowTime || state == ShowTemp || state == ShowHum)) {
animType = Pr;
state = state - 1;
}
if (state == ShowHum) state = TimeStart;
else state = state + 1;
switch (state) {
case TimeStart:
DS1307getTime();
timeToString();//Serial.println((char *)szMesg);
break;
case ShowTempLoc:
lr = true;
getLocation(); break;
case ShowTemp: getTemp(); break;
case ShowHumText: getHumidityText(); break;
case ShowHum: getHumidity();
}
}
else {
if (state == ShowTime) {
seconds = second();
if (lastSec != seconds) {
hours = hour(), minutes = minute();
lastSec = seconds;
timeToString();
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);
}
if(elapsed(showTimeFor,1)) animEnd = true;
else animEnd = false;
idleTime = 15;
} else {
switch (animType) {
case Pr: printText(szMesg); break;
case LR: TSLR(szMesg); break;
case UD: TSUD(szMesg);
}
if (animEnd && state != TimeStart) idleTime = showValuesFor;
else idleTime = 15;
}
}
}
else {
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
}
// if (curState == ShowTime) state = ShowTime;
}
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 setTime(){
DS1307setTime();
DS1307getTime();
timeToString();
idleTime = 0;
animState = 0;
animEnd = true;
lastSec = 0;
startTime[1] = millis();
state = TimeStart;
//printText(szMesg);
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 162:
lcdPrint("POWER");
break;
case 226:
lcdPrint("MENU");
lock = !lock;
break;
case 34:
lcdPrint("TEST");
idleTime = 0;
animState = 0;
animEnd = true;
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 152:
lcdPrint("MINUS");
break;
case 176:
lcdPrint("key: C");
idleTime = 0;
animState = 0;
animEnd = true;
numFontIndex = (numFontIndex + 1) % 3;
numFont = numFonts[numFontIndex];
lastSec = 0;
startTime[1] = millis();
state = TimeStart;
break;
case 104: // 0
setT = !setT;
startTime[1] = 0;
break;
case 48: // 1
hours = (hours + 1) % 24;
break;
case 24: // 2
minutes = (minutes + 1) % 60;
break;
case 122: // 3
seconds = (seconds + 1) % 60;
break;
case 16: // 4
hours = hours == 0 ? 23 : hours - 1;
break;
case 56: // 5
minutes = minutes == 0 ? 59 : minutes - 1;
break;
case 90: // 6
seconds = seconds == 0 ? 59 : seconds - 1;
break;
case 66: // 7
break;
case 74: // 8
break;
case 82: // 9
break;
default:
lcd.clear();
lcd.print(receiver.decodedIRData.command);
lcd.print(" other button");
}
if(setT)setTime();
}