#include <tinyFAT.h>
#include <UTFT.h>
#include <UTFT_tinyFAT.h>
#include <memorysaver.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <EtherCard.h>
#define REQUEST_RATE 5000 // milliseconds
// ethernet interface mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// remote website name
const char website[] PROGMEM = "google.com";
byte Ethernet::buffer[700];
UTFT tft(CTE32HR,38,39,40,41);
UTFT_tinyFAT myFiles(&tft);
extern uint8_t SmallRusFont[];
extern uint8_t BigRusFont[];
extern uint8_t SixteenSegment32x48[];
int PirPin1=2; // на каком пине первый датчик движения
int calibrationTime = 3; //время на каллибрацию датчиков движения
int TimeOnDisplay=10; //сколько секунд будет светится дисплей после обнаружения движения
boolean needrefresh=true;//требуется обновить рисунок квартиры
String st_time="",old_st_time="";
String st_dt="",old_st_dt="",st_dn="",old_st_dn="";
int tmp1time=0;
int ss_sd=53; //master spi для работы с карточкой
int ss_eth=49; //мастер spi для работы с ethernet
const uint8_t utf8_rus_charmap[] PROGMEM = {'A',128,'B',129,130,'E',131,132,133,134,135,'K',136,'M','H','O',137,'P','C','T',138,139,'X',140,141,
142,143,144,145,146,147,148,149,'a',150,151,152,153,'e',154,155,156,157,158,159,160,161,162,'o',163,'p','c',164,'y',165,'x',166,167,168,169,170,
171,172,173,174,175};
void printRus(UTFT rus,char *st, int x, int y, int deg){
int stl, i;
stl = strlen(st);
if (rus.orient==PORTRAIT){
if (x==RIGHT) x=(rus.disp_x_size+1)-(stl*rus.cfont.x_size);
if (x==CENTER) x=((rus.disp_x_size+1)-(stl*rus.cfont.x_size))/2;
} else {
if (x==RIGHT) x=(rus.disp_y_size+1)-(stl*rus.cfont.x_size);
if (x==CENTER) x=((rus.disp_y_size+1)-(stl*rus.cfont.x_size))/2;
}
uint8_t utf_high_byte = 0;
uint8_t ch, ch_pos = 0;
for (i = 0; i < stl; i++) {
ch = st[i];
if ( ch >= 128) {
if ( utf_high_byte == 0 && (ch ==0xD0 || ch == 0xD1)) {
utf_high_byte = ch;
continue;
} else {
if ( utf_high_byte == 0xD0) {
if (ch == 0x81) { //Ё
ch = 6;
} else {
if(ch <= 0x95) {
ch -= 0x90;
} else if( ch < 0xB6){
ch -= (0x90 - 1);
} else {
ch -= (0x90 - 2);
}}
ch = pgm_read_byte(utf8_rus_charmap + ch);
} else if (utf_high_byte == 0xD1) {
if (ch == 0x91) {//ё
ch = 39;
} else {
ch -= 0x80;
ch += 50;
}
ch = pgm_read_byte(utf8_rus_charmap + ch);
}
utf_high_byte = 0;
}
} else {
utf_high_byte = 0;
}
if (deg==0) {rus.printChar(ch, x + (ch_pos * (rus.cfont.x_size)), y);} else {
rus.rotateChar(ch, x, y, ch_pos, deg);
}
++ch_pos;
}
};
void setup(){
Serial.begin(57600);
Serial.println("\n[getDHCPandDNS]");
if (ether.begin(sizeof Ethernet::buffer, mymac,49) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("My IP: ", ether.myip);
// ether.printIp("Netmask: ", ether.mymask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
tft.InitLCD(LANDSCAPE);
file.initFAT();
tft.clrScr();
tft.setColor(VGA_BLACK);
tft.fillRect(0,0, tft.getDisplayXSize()-1, tft.getDisplayYSize()-1);
tft.setFont(BigRusFont);
tft.setColor(VGA_WHITE);
tft.setBackColor(VGA_TRANSPARENT);
digitalWrite(PirPin1, LOW);
//дадим датчику время на калибровку
printRus(tft,"Калибруем датчик движения...", 0, 0,0);
for(int i = 0; i < calibrationTime; i++){
delay(1000);
}
pinMode(PirPin1, INPUT);
tft.clrScr();
//tft.clrScr();
}
String To2Digs(String ss){
if (ss.length()==1){ss="0"+ss;};
return ss;
};
void OffOnLedMove(){
if (digitalRead(PirPin1) == HIGH) {
tmp1time=millis();
tft.lcdOn();
//printRus(tft,"Движение..", 0, 0,0);
} else {
if ((millis()-tmp1time)>TimeOnDisplay*1000){
tft.lcdOff();
//printRus(tft,"Всё тихо..", 0, 0,0);
};
};
};
void DateTimeDisplay(){
char hr_time[5],hr_dt[15],hr_dn[20];
tmElements_t tm;
if (RTC.read(tm)) {
st_time=To2Digs(String(tm.Hour))+":"+To2Digs(String(tm.Minute));
if (st_time!=old_st_time){
st_time.toCharArray(hr_time,st_time.length()+1);
tft.setColor(0,0,0);
tft.fillRect(0,0,160,50);
tft.setColor(255,255,255);
tft.setFont(SixteenSegment32x48);
printRus(tft,hr_time, 0, 0,0);
};
old_st_time=st_time;
st_dt=To2Digs(String(tm.Day))+"/"+To2Digs(String(tm.Month))+"/"+String(tmYearToCalendar(tm.Year));
if (st_dt!=old_st_dt){
st_dt.toCharArray(hr_dt,st_dt.length()+1);
tft.setColor(0,0,0);
tft.fillRect(0,50,160,70);
tft.setColor(255,255,255);
tft.setFont(BigRusFont);
printRus(tft,hr_dt, 0, 50,0);
};
old_st_dt=st_dt;
st_dn=String(weekday());
if (st_dn=="6"){st_dn="Понедельник";};
if (st_dn=="0"){st_dn="Вторник";};
if (st_dn=="1"){st_dn="Среда";};
if (st_dn=="2"){st_dn="Четверг";};
if (st_dn=="3"){st_dn="Пятница";};
if (st_dn=="4"){st_dn="Суббота";};
if (st_dn=="5"){st_dn="Воскресенье";};
st_dn.toCharArray(hr_dn,st_dn.length()+1);
if (st_dn!=old_st_dn){
tft.setColor(0,0,0);
tft.fillRect(0,70,190,90);
tft.setColor(255,255,255);
tft.setFont(BigRusFont);
printRus(tft,hr_dn, 0, 70,0);
};
old_st_dn=st_dn;
}
};
void HomeDisplay(){
if (needrefresh==true){
myFiles.loadBitmap(tft.getDisplayXSize()-344, tft.getDisplayYSize()-153, 344, 153, "myhome.raw");
needrefresh=false;
};
};
void loop(){
tft.clrScr();
//OffOnLedMove();
DateTimeDisplay();
HomeDisplay();
ether.packetLoop(ether.packetReceive());
needrefresh=true;
}