Network calendar
Update time:2018-04-13 Views:4975
Preface
Fireduino network calendar is an example which can through the WiFi network,and through NTP protocol access to network time,and the use of RTC running clock and calendar display,the calendar can be displayed the Gregorian calendar and week since 1583,and can be read calendar through the button.
Operation process
After power on the TFT initialization and WIFI initialization,WIFI connection and prompt on the TFT.
Through the network of NTP protocol access network time,and write to the RTC,if the NTP get failure,will be on the right side of the week display character"-_-||" said synchronization failure,and can eliminate the state after the next successful synchronization.
Enter the "clock" interface,on the TFT display dial,week,date and time.
Short press Mode key synchronization NTP network time.
After Long press Mode key to switch cycle the main display interface,mainly has the "clock" interface,"this month calendar" interface and "browse calendar" interface.
"this month calendar" interface display year,month,and this month calendar (including weeks) as well as highlighting the location of today,button response without switch interface.
"browse calendar" interface can be browse the calendar through the key.Short press MODE switch,short press the UP and DOWN the corresponding value,long press rapidly UP and DOWN the corresponding value.
Hardware connection
Fireduino
Fireduino as the main MCU,for driving TFT,RTC,KEY etc.to provide the logic operation environment.
TFT
TFT for the display of equipment,mainly to provide a display of the dial,the time character and calendar display.
RTC
RTC is onboard RTC independent units,used to hold and to obtain real time, using for HYM8563,mount in I2C1 interface,Fireduino provides specialized RTC library can be used directly.
Button
The button is used to modify the switching display interface,synchronization NTP time and browse calendar.
Software logical
Interface
Network calendar is mainly divided into the following interface
// status of display#define MODE_DISPLAY_TIME 0 //Display dial,week,date and time#define MODE_DISPLAY_CAL 1 //Display this month calendar#define MODE_BROWSE_CAL 2 //Browse calendar
Event
Event triggered by the button,to provide the basis for the event processing
#define EVENT_MODE 1 //Mode key raises the event#define EVENT_UP 2 // UP event #define EVENT_DOWN 3 // DOWN event#define EVENT_MODE_CHANG 4 // Mode change time#define EVENT_MODE_CLICK 5 //Mode key press event
Button processing
This example uses 3 buttons,the use of pin 5,6,7,each 20ms scan a button state,to support the long press and short press.
按键扫描
每20ms扫描一次。
void key_sacn(void){unsigned char pin_level = 0;if(digitalRead(5) == HIGH){ pin_level |= KEY_MODE;}if(digitalRead(6) == HIGH){ pin_level |= KEY_UP;}if(digitalRead(7) == HIGH){ pin_level |= KEY_DOWN;} key_trg = pin_level&(pin_level^key_count); key_count = pin_level;}
按键处理
根据状态,生成事件
void key_proc(void){if(key_trg & KEY_UP){ event_key = EVENT_UP ;} if(key_count & KEY_UP){ up_count++;if(up_count > 100){ up_count = 80 + up_count_i;if(up_count_i++ >100){ up_count_i = 100;} event_key = EVENT_UP;}}else{ up_count = 0; up_count_i = 0;} }
Display processing
According to different modes,display different interface.
MODE_DISPALY_TIME display the clock dial and week,date and time.
MODE_DISPALY_CAL display this month calendar,year,month,week,and highlight the date position.
MODE_BROWSE_CAL display the calendar can be browsed,the year and month can be modified,the modified bit will be the corresponding flicker.
Event processing
Event processing after the button processing,according to the time for different processing.
void event_proc(void){if(event_key){switch(event_key){case EVENT_MODE :.....break;case EVENT_UP :..... break;case MODIFY_HOUR :..... break;case EVENT_DOWN :..... break;case EVENT_MODE_CHANG :..... break;} event_key = 0;}}
Drawing processing
void clear_all(void); //Clear the entire screenvoid clear_dial(void); //Clear dialvoid clear_time_pointer(void); //Clear table pointervoid clear_time_txt(unsigned char flash_bit); //Clear clock interface display textvoid draw_dial(void); //Draw the dialvoid draw_time_pointer(RTCTime *time); //Draw table pointervoid draw_time_txt(RTCTime *time); //Draw the clock interface display textvoid draw_cal(RTCTime *time); //Draw today's calendarvoid draw_browse_cal(RTCTime *time); //Draw the calendar can be browsed
Arduino program structure
setup
// initialize the serial port Serial.begin(115200); // initialize the TFT Screen delay(1); TFTscreen.begin(); Serial.println("TFTscreen init success"); delay(1); // clear the screen with a pretty color TFTscreen.background(0, 0, 0); TFTscreen.setTextSize(1); TFTscreen.stroke(255,255,255); // attempt to connect to Wifi network: while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: ");Serial.println(ssid); TFTscreen.text("Connecting ",28,70);TFTscreen.text(" ... ",28,90);// Connect to network.status = WiFi.begin(ssid, pass); } Serial.println("Connected to wifi"); printWifiStatus(); TFTscreen.fillRect(0,70,128,40,0); // initialize UDP Serial.println("\nStarting connection to server..."); Udp.begin(localPort); // Init Wire and RTC will use I2C Interface Wire1.begin(); getNtpTimeSuccess = 0; // Get NTP time ntpTime(); // if failed ,note user if (!getNtpTimeSuccess) { TFTscreen.stroke(255,0,0); TFTscreen.textSize(1); TFTscreen.text("get NTP Time",28,70); TFTscreen.text("failed",46,80); delay(3000); TFTscreen.stroke(0,0,0); TFTscreen.text("get NTP Time",28,70); TFTscreen.text("failed",46,80); TFTscreen.stroke(255,255,255); TFTscreen.textSize(2); } else RTC.setTime(&time); // display the dial draw_dial();
loop
if(millis() > curr_ms){ curr_ms = millis() + 250;// displaytime(); display_proc();}//20ms 检测一次按键状态if(millis() > curr_ms_key){ curr_ms_key = millis() + 20; key_sacn(); key_proc(); event_proc(); }
Example program
#include <TFT.h> // Arduino LCD library#include "RTC.h"#include "WiFi.h"#include <WiFiUdp.h>#include "Wire.h" // define for calculate time#define SECONDS_IN_DAY 86400#define START_YEAR 1900#define TIME_ZONE +8 // status of display#define MODE_DISPLAY_TIME 0#define MODE_DISPLAY_CAL 1#define MODE_BROWSE_CAL 2//#define MODE_DGET_NTP_TIME_FAILD 3 // event of key#define EVENT_MODE 1#define EVENT_UP 2#define EVENT_DOWN 3#define EVENT_MODE_CHANG 4#define EVENT_MODE_CLICK 5 // Key flag#define KEY_MODE 0x01#define KEY_UP 0x02#define KEY_DOWN 0x04 #define UP_YEAR 0#define DOWN_YEAR 1#define UP_MON 2#define DOWN_MON 3 // count ms of key pressunsigned int curr_ms;unsigned int curr_ms_key; // current statusunsigned char curr_mode;unsigned char event_key; // counts of key pressunsigned char mode_count;unsigned char up_count;unsigned char down_count; unsigned char mode_count_i;unsigned char up_count_i;unsigned char down_count_i; // flag of keysunsigned char key_trg;unsigned char key_count; unsigned char modif_bit = 0; // for display the monthstatic int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};String mon_list[] = {" ","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; // WIFI informationint status = WL_IDLE_STATUS;char ssid[] = "Fireduino"; // your network SSID (name)char pass[] = "12345678"; // your network password // Get NTP time statusunsigned char getNtpTimeSuccess; unsigned int localPort = 2390; // local port to listen for UDP packets IPAddress timeServer(128, 138, 140, 44); // time-nw.nist.gov NTP server const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets // A UDP instance to let us send and receive packets over UDPWiFiUDP Udp; RTCTime time;RTCTime browseTime;String cal_char[6][7];String browse_cal_char[6][7]; TFT TFTscreen = TFT(); // function for WIFIvoid printWifiStatus(); // function for NTP Timeunsigned long sendNTPpacket(IPAddress& address); RTCTime * timeStamp2time(unsigned int timeStamp ,RTCTime *time); void ntpTime(void); // function for displayunsigned char Lcd_TimeX(unsigned char circle_x,unsigned char Length,unsigned char Angle);unsigned char Lcd_TimeY(unsigned char circle_y,unsigned char Length,unsigned char Angle);void clear_all(void);void clear_dial(void);void clear_time_pointer(void);void clear_time_txt(unsigned char flash_bit);void draw_dial(void);void draw_time_pointer(RTCTime *time);void draw_time_txt(RTCTime *time);void draw_cal(RTCTime *time);void draw_browse_cal(RTCTime *time); // function of key eventvoid key_sacn(void);void key_proc(void);void event_proc(void);void display_proc(void); void setup() { // initialize the serial port Serial.begin(115200); // initialize the TFT Screen delay(1); TFTscreen.begin(); Serial.println("TFTscreen init success"); delay(1); // clear the screen with a pretty color TFTscreen.background(0, 0, 0); TFTscreen.setTextSize(1); TFTscreen.stroke(255,255,255); // attempt to connect to Wifi network: while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: ");Serial.println(ssid); TFTscreen.text("Connecting ",28,70);TFTscreen.text(" ... ",28,90);// Connect to network.status = WiFi.begin(ssid, pass); } Serial.println("Connected to wifi"); printWifiStatus(); TFTscreen.fillRect(0,70,128,40,0); // initialize UDP Serial.println("\nStarting connection to server..."); Udp.begin(localPort); // Init Wire and RTC will use I2C Interface Wire1.begin(); getNtpTimeSuccess = 0; // Get NTP time ntpTime(); // if failed ,note user if (!getNtpTimeSuccess) { TFTscreen.stroke(255,0,0); TFTscreen.textSize(1); TFTscreen.text("get NTP Time",28,70); TFTscreen.text("failed",46,80); delay(3000); TFTscreen.stroke(0,0,0); TFTscreen.text