Demo program for the Oled display

Dependencies:   mbed-src

Committer:
star297
Date:
Mon Jan 12 21:10:53 2015 +0000
Revision:
3:1fb597da3fdf
Parent:
2:1eac3ea8c6e8
tidy code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:94bf5ec759da 1 #include "mbed.h"
star297 0:94bf5ec759da 2 #include "OLED32028P1T.h"
star297 0:94bf5ec759da 3
star297 1:793dbd6d6498 4
star297 1:793dbd6d6498 5 OLED32028P1T oled(PTE0,PTE1,PTA12); // Oled Display tx, rx, rs
star297 2:1eac3ea8c6e8 6 InterruptIn dcfSignalIn(PTC0); // conection of output NON-inverting DCF module
star297 1:793dbd6d6498 7 DigitalOut SignalLED(LED3); // indicates DCF signal on Mbed
star297 1:793dbd6d6498 8
star297 1:793dbd6d6498 9 Timer T1,T2,T3;
star297 1:793dbd6d6498 10 Ticker Ticker50ms; // DCF IRQ timer
star297 1:793dbd6d6498 11
star297 1:793dbd6d6498 12 #define clkx 110 //this point represent the x center of the clock where maths is done
star297 1:793dbd6d6498 13 #define clky 138 //this point represent the y center of the clock where maths is done
star297 1:793dbd6d6498 14
star297 1:793dbd6d6498 15 char timebuf[40];
star297 1:793dbd6d6498 16 char datebuf[40];
star297 1:793dbd6d6498 17 char paritycheck,paritym,parityu,paritydmy;
star297 1:793dbd6d6498 18 char testu,testm,testdmy,summertime;
star297 1:793dbd6d6498 19 char min,minh,minl,hourh,hourl,day,dayh,dayl,monthh,monthl,yearh,yearl;
star297 1:793dbd6d6498 20 char weekDayName[7][4] = {"Sun","Mon","Tue","Wed","Thu", "Fri","Sat"};
star297 1:793dbd6d6498 21 char monthName[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
star297 1:793dbd6d6498 22 char MAX_DAY[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Max days in a month for a non-leap year
star297 1:793dbd6d6498 23 char statusText[6][20] = {"Waiting Synchronise"," Wait for 21st bit ","Reading Signal data"," Checking Parity "," Last Minute Okay "," Signal error "};
star297 1:793dbd6d6498 24 char leapSymbol[2][10] = {" ", "Leap Year"};
star297 0:94bf5ec759da 25
star297 3:1fb597da3fdf 26 int hour,minute,second,dayofweek,dayofmonth,month,year,nextsec;
star297 1:793dbd6d6498 27 int RTCsecond,RTCminute,RTChour,RTCdayofweek,RTCdayofmonth,RTCmonth,RTCyear;
star297 1:793dbd6d6498 28 int xbuffer,ybuffer,IRQ;
star297 1:793dbd6d6498 29 int xs,ys,xm,ym,xh,yh,x,y;
star297 1:793dbd6d6498 30 int handHour=60,handMin=75,handSec=78; //RTC clock hand sizes
star297 1:793dbd6d6498 31 int numposx=105,numposy=135; // RTC clock number screen posistion
star297 1:793dbd6d6498 32
star297 1:793dbd6d6498 33 int start,sync,SignalStatus,nosignal,laststart,loop,interrupt_counter,dcf_sec,error_count;
star297 1:793dbd6d6498 34 int tz,rtcset,rtcset_times;
star297 1:793dbd6d6498 35 int r=255,g=255,b=255;
star297 1:793dbd6d6498 36 int draw_graph,dcf_good,signal_error,dcf_error,display;
star297 1:793dbd6d6498 37 int p_minute,p_hour,p_dayofweek,p_dayofmonth,p_month,p_year;
star297 1:793dbd6d6498 38 int dcf_array[61];
star297 1:793dbd6d6498 39
star297 1:793dbd6d6498 40 float angleH,angleM,angleS;
star297 1:793dbd6d6498 41 float fsecond,fminute;
star297 1:793dbd6d6498 42
star297 1:793dbd6d6498 43 typedef unsigned char byte;
star297 1:793dbd6d6498 44
star297 1:793dbd6d6498 45 struct tm t;
star297 1:793dbd6d6498 46
star297 1:793dbd6d6498 47 void RTCsetclock(),RTCset(),RTCread(),RTCclock(),RTCclockdraw(),RTCclockupdate();
star297 1:793dbd6d6498 48 void dcfISR(),dcfIRQ(),checkSIGNAL(),signal(),DCFbitprint();
star297 1:793dbd6d6498 49 void DCFclock(),DCFloop(),DCFshowClock(),DCFtestparity();
star297 1:793dbd6d6498 50 void DCFcheck(),bitprint(),DCFdrawgraph();
star297 1:793dbd6d6498 51 void DCFbitmapdraw(),DCFparitycalc(),DCFbitstatus(),DCFbitstatus2();
star297 1:793dbd6d6498 52 void DCFsetTime();
star297 0:94bf5ec759da 53
star297 1:793dbd6d6498 54 struct SignalStatus {
star297 1:793dbd6d6498 55 bool is_leap; // Leap year flag (NOT actually transmitted but calculated)
star297 1:793dbd6d6498 56 bool DCFsample50; // dcf sample at 50mS into the start of the second, seocnd marker pulse
star297 1:793dbd6d6498 57 bool DCFsample150; // dcf sample at 150mS into the start of the second, bit=1 if high, 0 if low
star297 1:793dbd6d6498 58 bool DCFsample300; // dcf sample at 300mS into the start of the second, error if high
star297 1:793dbd6d6498 59 bool DCFsample500; // dcf sample at 500mS into the start of the second, error if high
star297 1:793dbd6d6498 60 bool DCFsample600; // dcf sample at 600mS into the start of the second, error if high
star297 1:793dbd6d6498 61 unsigned char frame; // Received MSF framing code 01111110
star297 1:793dbd6d6498 62 int second; // MSF-DCF second (NOT actually transmitted but calculated)
star297 1:793dbd6d6498 63 } dcf_status;
star297 1:793dbd6d6498 64
star297 1:793dbd6d6498 65
star297 1:793dbd6d6498 66 // Return the maximum day in month for a given month & year
star297 1:793dbd6d6498 67 byte maxDay(byte year, byte month)
star297 1:793dbd6d6498 68 {
star297 1:793dbd6d6498 69 byte lastday, leap;
star297 1:793dbd6d6498 70 leap = year%4 == 0 && year%100 !=0 || year%400 == 0;
star297 1:793dbd6d6498 71 lastday = MAX_DAY[month - 1];
star297 1:793dbd6d6498 72 dcf_status.is_leap = leap > 0 ? 1 : 0;
star297 1:793dbd6d6498 73 if ((leap > 0) && (month == 2))
star297 1:793dbd6d6498 74 lastday++;
star297 1:793dbd6d6498 75 return lastday;
star297 1:793dbd6d6498 76 }
star297 0:94bf5ec759da 77
star297 1:793dbd6d6498 78 int main() {
star297 1:793dbd6d6498 79
star297 1:793dbd6d6498 80 SignalLED=1;
star297 1:793dbd6d6498 81 oled.init();
star297 1:793dbd6d6498 82 oled.clear();
star297 1:793dbd6d6498 83 oled.setTextBackgroundType(TEXT_OPAQUE);
star297 1:793dbd6d6498 84 oled.setFontSize(FONT12X16);oled.setFontColor(oled.toRGB(255,255,0));
star297 1:793dbd6d6498 85 oled.drawText(2,0,(FONT12X16),"--drawText vs. printf--",oled.toRGB(255,0,255));
star297 1:793dbd6d6498 86 oled.drawText(2,22,(FONT5X7),"Use drawText as much as possible, it's nearly twice",oled.toRGB(255,255,255));
star297 1:793dbd6d6498 87 oled.drawText(2,23,(FONT5X7),"as fast as printf. Make sure the serial stream is",oled.toRGB(255,255,255));
star297 1:793dbd6d6498 88 oled.drawText(2,24,(FONT5X7),"NOT",oled.toRGB(255,0,0));
star297 1:793dbd6d6498 89 oled.drawText(6,24,(FONT5X7),"interrupted when printing.",oled.toRGB(255,255,255));
star297 1:793dbd6d6498 90 if (time(NULL) < 1396310400) {set_time(1396310400);}
star297 1:793dbd6d6498 91 T3.start();
star297 0:94bf5ec759da 92
star297 1:793dbd6d6498 93 while(T3<10){
star297 1:793dbd6d6498 94 oled.setFontColor(oled.toRGB(0,255,255));
star297 1:793dbd6d6498 95 oled.locate (12,18);
star297 1:793dbd6d6498 96 oled.printf("Next page in %2.1f seconds ",10 - T3.read());
star297 1:793dbd6d6498 97
star297 1:793dbd6d6498 98 time_t seconds = time(NULL);
star297 1:793dbd6d6498 99 strftime(timebuf, 32, "RTC %I:%M:%S %p", localtime(&seconds));
star297 1:793dbd6d6498 100 strftime(datebuf, 32, "%a %d %b %Y ", localtime(&seconds));
star297 1:793dbd6d6498 101
star297 1:793dbd6d6498 102 T1.start();
star297 1:793dbd6d6498 103 oled.drawText(2,2,(FONT12X16),timebuf,oled.toRGB(255,255,0));
star297 1:793dbd6d6498 104 oled.drawText(2,3,(FONT12X16),datebuf,oled.toRGB(255,255,0));
star297 1:793dbd6d6498 105 T1.stop();
star297 1:793dbd6d6498 106
star297 1:793dbd6d6498 107 T2.start();
star297 1:793dbd6d6498 108 oled.setFontColor(oled.toRGB(0,255,0));
star297 1:793dbd6d6498 109 oled.setFontSize(FONT12X16);
star297 1:793dbd6d6498 110 oled.locate (2,6);
star297 1:793dbd6d6498 111 oled.printf("%s",timebuf);
star297 1:793dbd6d6498 112 oled.locate (2,7);
star297 1:793dbd6d6498 113 oled.printf("%s",datebuf);
star297 1:793dbd6d6498 114 T2.stop();
star297 0:94bf5ec759da 115
star297 1:793dbd6d6498 116 oled.setFontColor(oled.toRGB(255,255,255));
star297 1:793dbd6d6498 117 oled.setFontSize(FONT8X12);
star297 1:793dbd6d6498 118 oled.locate (4,6);
star297 1:793dbd6d6498 119 oled.printf("drawText time %3.2f mS",T1.read()*1000);T1.reset();
star297 1:793dbd6d6498 120 oled.locate (4,11);
star297 2:1eac3ea8c6e8 121 oled.printf("printf time %3.2f mS",T2.read()*1000);T2.reset();
star297 2:1eac3ea8c6e8 122 oled.locate (4,12);
star297 2:1eac3ea8c6e8 123 oled.printf("%d",seconds);
star297 1:793dbd6d6498 124 }
star297 1:793dbd6d6498 125 T3.reset();
star297 1:793dbd6d6498 126 while(1){
star297 1:793dbd6d6498 127 IRQ=0;
star297 1:793dbd6d6498 128 SignalLED=1;
star297 1:793dbd6d6498 129 RTCclock();
star297 1:793dbd6d6498 130 }
star297 1:793dbd6d6498 131
star297 1:793dbd6d6498 132 }
star297 1:793dbd6d6498 133
star297 1:793dbd6d6498 134 void RTCclock()
star297 1:793dbd6d6498 135 {
star297 1:793dbd6d6498 136 while(IRQ==0){
star297 1:793dbd6d6498 137 oled.disableTouch();oled.clear();
star297 1:793dbd6d6498 138 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:793dbd6d6498 139 RTCclockdraw();
star297 1:793dbd6d6498 140 oled.drawTextButton(1, 200, 210,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "DCF Clock");
star297 1:793dbd6d6498 141 oled.drawTextButton(1, 252, 185,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "Set RTC");
star297 1:793dbd6d6498 142 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 2:1eac3ea8c6e8 143 oled.drawText(0,0,(FONT12X16),"RTC Clock",oled.toRGB(0,255,0));
star297 2:1eac3ea8c6e8 144 oled.drawText(38,7,(FONT5X7),"Touch position",oled.toRGB(255,0,0));
star297 2:1eac3ea8c6e8 145 oled.enableTouch();oled.resetTouchArea();
star297 2:1eac3ea8c6e8 146 T1.start();
star297 1:793dbd6d6498 147
star297 1:793dbd6d6498 148 while(IRQ==0){
star297 1:793dbd6d6498 149 oled.setFontSize(FONT12X16);oled.locate(0,1);
star297 1:793dbd6d6498 150 oled.setFontColor(oled.toRGB(255,255,255));
star297 1:793dbd6d6498 151 time_t seconds = time(NULL);
star297 2:1eac3ea8c6e8 152 strftime(timebuf, 32, "%I:%M:%S.", localtime(&seconds));
star297 1:793dbd6d6498 153 strftime(datebuf, 32, "%a %d %b %Y ", localtime(&seconds));
star297 2:1eac3ea8c6e8 154 oled.drawText(11,0,(FONT12X16),timebuf,oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 155 oled.drawText(11,1,(FONT12X16),datebuf,oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 156
star297 2:1eac3ea8c6e8 157 if (seconds != nextsec){T1.reset();RTCclockupdate();nextsec=seconds;}
star297 2:1eac3ea8c6e8 158 oled.locate(20,0);
star297 2:1eac3ea8c6e8 159 oled.printf("%02.f ",T1.read()*100);
star297 2:1eac3ea8c6e8 160 oled.getTouch(&xbuffer,&ybuffer);
star297 1:793dbd6d6498 161
star297 2:1eac3ea8c6e8 162 oled.setFontSize(FONT5X7);oled.locate(40,9); // this indicates touch position
star297 2:1eac3ea8c6e8 163 oled.printf("X %03d Y %03d ",xbuffer,ybuffer); // can be removed
star297 1:793dbd6d6498 164
star297 1:793dbd6d6498 165 if((xbuffer>195 && xbuffer<300) && (ybuffer>205 && ybuffer<220)){DCFclock();}
star297 1:793dbd6d6498 166 if((xbuffer>240 && xbuffer<300) && (ybuffer>182 && ybuffer<200)){RTCsetclock();}
star297 1:793dbd6d6498 167 }
star297 1:793dbd6d6498 168 }
star297 1:793dbd6d6498 169 }
star297 1:793dbd6d6498 170
star297 1:793dbd6d6498 171 void RTCclockdraw()
star297 1:793dbd6d6498 172 {
star297 1:793dbd6d6498 173 (x)=0;
star297 1:793dbd6d6498 174 while ((x)<60){
star297 1:793dbd6d6498 175 x++;angleS=(x)*6; // clock minute markers in angle form
star297 1:793dbd6d6498 176 xs=(sin((angleS*3.14)/180)) * 90; // X component of markers
star297 1:793dbd6d6498 177 ys=(cos((angleS*3.14)/180)) * 90; // Y component of markers
star297 1:793dbd6d6498 178 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(255,255,255));
star297 1:793dbd6d6498 179 }
star297 1:793dbd6d6498 180 (x)=0;
star297 1:793dbd6d6498 181 while ((x)<13){
star297 1:793dbd6d6498 182 x++;angleS=(x)*30; // clock hour markers in angle form
star297 1:793dbd6d6498 183 xs=(sin((angleS*3.14)/180)) * 90; // X component of markers
star297 1:793dbd6d6498 184 ys=(cos((angleS*3.14)/180)) * 90; // Y component of markers
star297 1:793dbd6d6498 185 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(0,255,255));
star297 1:793dbd6d6498 186 if (x==1) oled.drawTextGraphic(numposx +(xs+8),numposy + (ys+8), FONT5X7, "5", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 187 if (x==2) oled.drawTextGraphic(numposx +(xs+10),numposy + (ys+4), FONT5X7, "4", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 188 if (x==3) oled.drawTextGraphic(numposx +(xs+10),numposy + (ys), FONT5X7, "3", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 189 if (x==4) oled.drawTextGraphic(numposx +(xs+10),numposy + (ys-4), FONT5X7, "2", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 190 if (x==5) oled.drawTextGraphic(numposx +(xs+8),numposy + (ys-8), FONT5X7, "1", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 191 if (x==6) oled.drawTextGraphic(numposx +(xs),numposy + (ys-8), FONT5X7, "12", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 192 if (x==7) oled.drawTextGraphic(numposx +(xs-4),numposy + (ys-8), FONT5X7, "11", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 193 if (x==8) oled.drawTextGraphic(numposx +(xs-10),numposy + (ys-4), FONT5X7, "10", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 194 if (x==9) oled.drawTextGraphic(numposx +(xs-4),numposy + (ys), FONT5X7, "9", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 195 if (x==10) oled.drawTextGraphic(numposx +(xs-4),numposy + (ys+4), FONT5X7, "8", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 196 if (x==11) oled.drawTextGraphic(numposx +(xs),numposy + (ys+8), FONT5X7, "7", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 197 if (x==12) oled.drawTextGraphic(numposx +(xs+3),numposy + (ys+8), FONT5X7, "6", 1, 1, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 198 }
star297 1:793dbd6d6498 199 oled.setPenSize(0);
star297 1:793dbd6d6498 200 oled.drawCircle(clkx,clky,80,oled.toRGB(0,0,0));
star297 1:793dbd6d6498 201 oled.setPenSize(1);
star297 1:793dbd6d6498 202 oled.drawCircle(clkx,clky,80,oled.toRGB(255,255,255));
star297 1:793dbd6d6498 203 oled.drawCircle(clkx,clky,90,oled.toRGB(255,255,255));
star297 1:793dbd6d6498 204 (x)=0;
star297 1:793dbd6d6498 205 }
star297 1:793dbd6d6498 206
star297 1:793dbd6d6498 207 void RTCsetclock()
star297 1:793dbd6d6498 208 {
star297 1:793dbd6d6498 209 oled.disableTouch();oled.clear();
star297 1:793dbd6d6498 210 oled.drawText(0,0,(FONT12X16)," --Manual set RTC Time--",oled.toRGB(0,255,0));
star297 1:793dbd6d6498 211 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:793dbd6d6498 212 oled.drawTextButton(1, 265, 200,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, " Back ");
star297 1:793dbd6d6498 213 oled.drawTextButton(1, 10, 200,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Reset RTC ");
star297 1:793dbd6d6498 214 oled.drawTextButton(1, 120, 200,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Set RTC ");
star297 1:793dbd6d6498 215 oled.drawTextButton(1, 16, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+");
star297 1:793dbd6d6498 216 oled.drawTextButton(1, 16, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-");
star297 1:793dbd6d6498 217 oled.drawTextButton(1, 52, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+");
star297 1:793dbd6d6498 218 oled.drawTextButton(1, 52, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-");
star297 1:793dbd6d6498 219 oled.drawTextButton(1, 88, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+");
star297 2:1eac3ea8c6e8 220 oled.drawTextButton(1, 88, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-");
star297 1:793dbd6d6498 221 oled.drawTextButton(1, 174, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "+");
star297 1:793dbd6d6498 222 oled.drawTextButton(1, 174, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "-");
star297 1:793dbd6d6498 223 oled.drawTextButton(1, 208, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " + ");
star297 1:793dbd6d6498 224 oled.drawTextButton(1, 208, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " - ");
star297 1:793dbd6d6498 225 oled.drawTextButton(1, 270, 115,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " + ");
star297 1:793dbd6d6498 226 oled.drawTextButton(1, 270, 140,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " - ");
star297 1:793dbd6d6498 227 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 2:1eac3ea8c6e8 228 oled.setFontSize(FONT8X12);oled.locate (6,14);
star297 2:1eac3ea8c6e8 229 oled.setFontColor(oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 230 time_t seconds = time(NULL);
star297 2:1eac3ea8c6e8 231 oled.printf("time_t = %d seconds",seconds);
star297 1:793dbd6d6498 232 RTCread();
star297 1:793dbd6d6498 233 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear;
star297 1:793dbd6d6498 234
star297 2:1eac3ea8c6e8 235 while(IRQ==0){
star297 1:793dbd6d6498 236 oled.enableTouch();
star297 1:793dbd6d6498 237 time_t seconds = time(NULL);
star297 2:1eac3ea8c6e8 238 strftime(timebuf, 26, "%H:%M:%S", localtime(&seconds));
star297 2:1eac3ea8c6e8 239 strftime(datebuf, 26, "%a %d %b' %Y", localtime(&seconds));
star297 2:1eac3ea8c6e8 240 oled.drawText(1,2,(FONT12X16),timebuf,oled.toRGB(255,255,0));
star297 2:1eac3ea8c6e8 241 oled.drawText(10,2,(FONT12X16),datebuf,oled.toRGB(255,255,0));
star297 2:1eac3ea8c6e8 242 oled.setFontSize(FONT8X12);oled.locate (6,5);
star297 2:1eac3ea8c6e8 243 oled.setFontColor(oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 244 oled.printf("time_t = %d seconds",seconds);
star297 1:793dbd6d6498 245
star297 1:793dbd6d6498 246 oled.getTouch(&xbuffer,&ybuffer);
star297 2:1eac3ea8c6e8 247 if((xbuffer>240 && xbuffer<290) && (ybuffer>190 && ybuffer<210)){IRQ=1;}
star297 2:1eac3ea8c6e8 248 if((xbuffer>20 && xbuffer<109) && (ybuffer>190 && ybuffer<210)){
star297 2:1eac3ea8c6e8 249 set_time(1420070400);time_t seconds = time(NULL);RTCread();
star297 1:793dbd6d6498 250 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear;
star297 2:1eac3ea8c6e8 251 oled.setFontSize(FONT8X12);oled.locate (6,14);
star297 2:1eac3ea8c6e8 252 oled.setFontColor(oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 253 oled.printf("time_t = %d seconds",seconds);
star297 2:1eac3ea8c6e8 254 }
star297 2:1eac3ea8c6e8 255 if((xbuffer>120 && xbuffer<195) && (ybuffer>195 && ybuffer<210)){RTCset();time_t seconds = time(NULL);RTCread();
star297 2:1eac3ea8c6e8 256 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear;
star297 2:1eac3ea8c6e8 257 oled.setFontSize(FONT8X12);oled.locate (6,14);
star297 2:1eac3ea8c6e8 258 oled.setFontColor(oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 259 oled.printf("time_t = %d seconds",seconds);
star297 1:793dbd6d6498 260 }
star297 1:793dbd6d6498 261
star297 1:793dbd6d6498 262 if((xbuffer>25 && xbuffer<40) && (ybuffer>124 && ybuffer<140)){hour++;}
star297 1:793dbd6d6498 263 if((xbuffer>25 && xbuffer<40) && (ybuffer>146 && ybuffer<160)){hour--;}
star297 1:793dbd6d6498 264 if((xbuffer>58 && xbuffer<74) && (ybuffer>124 && ybuffer<140)){minute++;}
star297 1:793dbd6d6498 265 if((xbuffer>58 && xbuffer<74) && (ybuffer>146 && ybuffer<160)){minute--;}
star297 1:793dbd6d6498 266 if((xbuffer>90 && xbuffer<105) && (ybuffer>124 && ybuffer<140)){second++;}
star297 1:793dbd6d6498 267 if((xbuffer>90 && xbuffer<105) && (ybuffer>146 && ybuffer<160)){second--;}
star297 1:793dbd6d6498 268 if((xbuffer>170 && xbuffer<186) && (ybuffer>124 && ybuffer<140)){dayofmonth++;}
star297 1:793dbd6d6498 269 if((xbuffer>170 && xbuffer<186) && (ybuffer>146 && ybuffer<160)){dayofmonth--;}
star297 1:793dbd6d6498 270 if((xbuffer>200 && xbuffer<230) && (ybuffer>124 && ybuffer<140)){month++;}
star297 1:793dbd6d6498 271 if((xbuffer>200 && xbuffer<230) && (ybuffer>146 && ybuffer<160)){month--;}
star297 1:793dbd6d6498 272 if((xbuffer>258 && xbuffer<288) && (ybuffer>124 && ybuffer<140)){year++;}
star297 1:793dbd6d6498 273 if((xbuffer>258 && xbuffer<288) && (ybuffer>146 && ybuffer<160)){year--;}
star297 1:793dbd6d6498 274 oled.disableTouch();
star297 1:793dbd6d6498 275
star297 1:793dbd6d6498 276 if (second >= 60) {second = 0;}
star297 1:793dbd6d6498 277 if (second < 0) {second = 59;}
star297 1:793dbd6d6498 278 if (minute >= 60) {minute = 0;}
star297 1:793dbd6d6498 279 if (minute < 0) {minute = 59;}
star297 1:793dbd6d6498 280 if (hour >= 24) {hour = 0;}
star297 1:793dbd6d6498 281 if (hour < 0) {hour = 23;}
star297 1:793dbd6d6498 282 if (dayofweek > 6){dayofweek = 0;}
star297 1:793dbd6d6498 283 if (dayofweek < 0){dayofweek = 6;}
star297 1:793dbd6d6498 284 if (month > 12) {month = 1;}
star297 1:793dbd6d6498 285 if (month < 1) {month = 12;}
star297 1:793dbd6d6498 286 if (dayofmonth <1) {dayofmonth = maxDay(year, month);}
star297 1:793dbd6d6498 287 if (dayofmonth > maxDay(year, month)) {dayofmonth = 1;}
star297 1:793dbd6d6498 288 if (year > 99) {year = 1;}
star297 1:793dbd6d6498 289 if (year < 0) {year = 99;}
star297 1:793dbd6d6498 290
star297 1:793dbd6d6498 291
star297 1:793dbd6d6498 292 oled.setFontSize(FONT12X16);oled.locate(1,6);
star297 2:1eac3ea8c6e8 293 oled.setFontColor(oled.toRGB(128,255,255));
star297 1:793dbd6d6498 294 oled.printf("%2d:%02d:%02d ",hour,minute,second);
star297 2:1eac3ea8c6e8 295 oled.printf("%s %02d %s' 20%02d",weekDayName[dayofweek],dayofmonth,monthName[month-1],year);
star297 1:793dbd6d6498 296 }
star297 0:94bf5ec759da 297
star297 1:793dbd6d6498 298 }
star297 1:793dbd6d6498 299 void RTCclockupdate()
star297 1:793dbd6d6498 300 {
star297 1:793dbd6d6498 301 RTCread();
star297 1:793dbd6d6498 302 fsecond = (RTCsecond/12);
star297 1:793dbd6d6498 303 fminute = (RTCminute/12);
star297 1:793dbd6d6498 304 if (x==1){ //Erase all hands
star297 1:793dbd6d6498 305 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(0,0,0)); // Erase Second's hand
star297 1:793dbd6d6498 306 oled.drawLine(clkx,clky,clkx+xm,clky-ym,oled.toRGB(0,0,0)); // Erase Minute's hand
star297 1:793dbd6d6498 307 oled.drawLine(clkx,clky,clkx+xh,clky-yh,oled.toRGB(0,0,0)); // Erase Hour's hand
star297 1:793dbd6d6498 308 }
star297 1:793dbd6d6498 309 //Calculations to get the second point of the clock hands. (first point is always the center of the clock)
star297 1:793dbd6d6498 310 angleS=(RTCsecond*6); //seconds in angle form, 360 degrees divided by 60 seconds = 6, x6 to get current angle
star297 1:793dbd6d6498 311 xs=(sin((angleS*3.14)/180)) * handSec; //get X component of the second's hand
star297 1:793dbd6d6498 312 ys=(cos((angleS*3.14)/180)) * handSec; //get Y component of the second's hand
star297 1:793dbd6d6498 313 angleM=((RTCminute*6) + fsecond); //minutes in angle form, 360 degrees divided by 60 minutes = 6, add seconds fraction, x6 to get current angle
star297 1:793dbd6d6498 314 xm=(sin((angleM*3.14)/180)) * handMin; //get X component of the minutes's hand
star297 1:793dbd6d6498 315 ym=(cos((angleM*3.14)/180)) * handMin; //get Y component of the minutes's hand
star297 1:793dbd6d6498 316 angleH=(((RTChour*5) + (fminute))*6); //hours in angle form, 360 degrees multiply hours by 5 hours = 60, add minute fraction, x6 to get angle
star297 1:793dbd6d6498 317 xh=(sin((angleH*3.14)/180)) * handHour; //get X component of the hours's hand
star297 1:793dbd6d6498 318 yh=(cos((angleH*3.14)/180)) * handHour; //get Y component of the hours's hand
star297 1:793dbd6d6498 319 x=1;
star297 1:793dbd6d6498 320 //Draw current time hands
star297 1:793dbd6d6498 321 oled.drawLine(clkx,clky,clkx+xm,clky-ym,oled.toRGB(255,255,255));
star297 1:793dbd6d6498 322 oled.drawLine(clkx,clky,clkx+xh,clky-yh,oled.toRGB(0,255,255));
star297 1:793dbd6d6498 323 oled.drawLine(clkx,clky,clkx+xs,clky-ys,oled.toRGB(255,255,0));
star297 1:793dbd6d6498 324 oled.setPenSize(0);
star297 1:793dbd6d6498 325 oled.drawCircle(clkx,clky,5,oled.toRGB(255,255,255)); // Draw the center of the second's hand
star297 1:793dbd6d6498 326 }
star297 1:793dbd6d6498 327
star297 1:793dbd6d6498 328 void RTCread()
star297 1:793dbd6d6498 329 {
star297 1:793dbd6d6498 330 time_t seconds = time(NULL);
star297 1:793dbd6d6498 331 char buffer[80];
star297 1:793dbd6d6498 332 strftime(buffer, 2,"%S", localtime(&seconds));
star297 1:793dbd6d6498 333 RTCsecond = atoi(buffer);
star297 1:793dbd6d6498 334 strftime(buffer, 2,"%M", localtime(&seconds));
star297 1:793dbd6d6498 335 RTCminute = atoi(buffer);
star297 1:793dbd6d6498 336 strftime(buffer, 2,"%H", localtime(&seconds));
star297 1:793dbd6d6498 337 RTChour = atoi(buffer);
star297 1:793dbd6d6498 338 strftime(buffer, 2,"%d", localtime(&seconds));
star297 1:793dbd6d6498 339 RTCdayofmonth = atoi(buffer);
star297 1:793dbd6d6498 340 strftime(buffer, 2,"%w", localtime(&seconds));
star297 1:793dbd6d6498 341 RTCdayofweek = atoi(buffer);
star297 1:793dbd6d6498 342 strftime(buffer, 2,"%m", localtime(&seconds));
star297 1:793dbd6d6498 343 RTCmonth = atoi(buffer);
star297 1:793dbd6d6498 344 strftime(buffer, 2,"%y", localtime(&seconds));
star297 1:793dbd6d6498 345 RTCyear = atoi(buffer);
star297 1:793dbd6d6498 346 }
star297 1:793dbd6d6498 347
star297 1:793dbd6d6498 348 void RTCset()
star297 1:793dbd6d6498 349 {
star297 1:793dbd6d6498 350 t.tm_sec = (second); // 0-59
star297 1:793dbd6d6498 351 t.tm_min = (minute); // 0-59
star297 1:793dbd6d6498 352 t.tm_hour = (hour); // 0-23
star297 1:793dbd6d6498 353 t.tm_mday = (dayofmonth); // 1-31
star297 1:793dbd6d6498 354 t.tm_mon = (month-1); // 0-11 DCF "0" = Jan, -1 added for Mbed RCT clock format
star297 1:793dbd6d6498 355 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
star297 3:1fb597da3fdf 356 set_time(mktime(&t)-3600); // set RTC clock to DCF -1 hour for GMT time zone
star297 3:1fb597da3fdf 357 rtcset=1;
star297 1:793dbd6d6498 358 }
star297 1:793dbd6d6498 359
star297 1:793dbd6d6498 360 // **************************** DCF clock **********************************************
star297 1:793dbd6d6498 361
star297 1:793dbd6d6498 362 void DCFclock()
star297 1:793dbd6d6498 363 {
star297 2:1eac3ea8c6e8 364 oled.clear();
star297 2:1eac3ea8c6e8 365 oled.drawText(0,1,(FONT12X16),("--- DCF77 Atomic Clock ---"),oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 366 oled.drawText(3,3,(FONT12X16),("Checking Signal..."),oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 367 T1.reset();T1.start();
star297 2:1eac3ea8c6e8 368 while(dcfSignalIn == 1 && T1.read()<.5){}
star297 2:1eac3ea8c6e8 369 while(dcfSignalIn == 0 && T1.read()<.5){}
star297 2:1eac3ea8c6e8 370 T1.stop();
star297 2:1eac3ea8c6e8 371 if (T1.read()>.5){
star297 2:1eac3ea8c6e8 372 T1.stop();
star297 2:1eac3ea8c6e8 373 oled.drawText(0,5,(FONT12X16),(" !! No signal detected !! "),oled.toRGB(255,0,0));
star297 2:1eac3ea8c6e8 374 oled.drawText(2,9,(FONT8X12),("Connect Non inverting signal of DCF"),oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 375 oled.drawText(2,10,(FONT8X12),("receiver module to specified Pin."),oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 376 oled.drawText(5,12,(FONT8X12),("Running Demo mode."),oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 377 wait(5);
star297 2:1eac3ea8c6e8 378 }
star297 2:1eac3ea8c6e8 379 wait(1);
star297 1:793dbd6d6498 380 oled.clear();DCFbitmapdraw();RTCread();
star297 1:793dbd6d6498 381 second=RTCsecond;minute=RTCminute;hour=RTChour;dayofweek=RTCdayofweek;dayofmonth=RTCdayofmonth;month=RTCmonth;year=RTCyear;
star297 1:793dbd6d6498 382 if(tz==0) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (GMT)",oled.toRGB(255,0,0));}
star297 1:793dbd6d6498 383 if (tz==1) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (CET)",oled.toRGB(255,0,0));}
star297 1:793dbd6d6498 384 if (tz==2) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (EET)",oled.toRGB(255,0,0));}
star297 1:793dbd6d6498 385 if (tz==3) {oled.drawText(0,0,(FONT8X12)," RTC Clock Time (MSK)",oled.toRGB(255,0,0));}
star297 1:793dbd6d6498 386 oled.drawText(22,5,(FONT8X12)," DCF Time (CET)",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 387 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:793dbd6d6498 388 oled.drawTextButton(1, 270, 140,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, " Back ");
star297 1:793dbd6d6498 389 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:793dbd6d6498 390 draw_graph=0;DCFdrawgraph();
star297 1:793dbd6d6498 391 oled.drawLine(155,190,169,190, oled.toRGB(225, 255, 255));
star297 1:793dbd6d6498 392 oled.drawLine(169,190,169,203, oled.toRGB(225, 255, 255));
star297 1:793dbd6d6498 393 oled.drawLine(170,203,287,203, oled.toRGB(255,255,255));
star297 1:793dbd6d6498 394 oled.drawLine(287,190,287,203, oled.toRGB(225, 255, 255));
star297 1:793dbd6d6498 395 oled.drawLine(288,190,305,190, oled.toRGB(225, 255, 255));
star297 1:793dbd6d6498 396 oled.drawText(0,26,(FONT5X7)," 0 start min hour Dt dy mon year 60",oled.toRGB(255,0,0));
star297 1:793dbd6d6498 397 oled.setTouchArea(240,140,290,160);
star297 3:1fb597da3fdf 398 start=0;sync=1;x=0;y=5;r=0;SignalStatus=0;laststart=2;loop=0;
star297 2:1eac3ea8c6e8 399 interrupt_counter = 0;dcf_sec=0;error_count=0,display=0;signal_error=0;rtcset_times=0;
star297 2:1eac3ea8c6e8 400 hour = 0;minute = 0,second = 0,dayofweek = 6;dayofmonth = 1;month = 1;year = 0;
star297 1:793dbd6d6498 401
star297 2:1eac3ea8c6e8 402 T1.start();
star297 2:1eac3ea8c6e8 403 while(dcfSignalIn == 1 && T1.read()<.5){}
star297 2:1eac3ea8c6e8 404 while(dcfSignalIn == 0 && T1.read()<.5){}
star297 2:1eac3ea8c6e8 405 T1.stop();
star297 2:1eac3ea8c6e8 406 dcfSignalIn.rise(dcfIRQ); //Trigger dcfISR Ticker on rising edge of DCF pulse
star297 2:1eac3ea8c6e8 407 interrupt_counter = 0;sync=0;
star297 2:1eac3ea8c6e8 408
star297 1:793dbd6d6498 409 Ticker50ms.attach(& dcfISR, .05);
star297 1:793dbd6d6498 410
star297 1:793dbd6d6498 411 while (IRQ==0) {
star297 1:793dbd6d6498 412 if (loop==0) {
star297 1:793dbd6d6498 413 DCFloop(); // Continuously get dcf time and Display data every second interrupted by the Ticker every 50ms
star297 1:793dbd6d6498 414 loop=1;}
star297 1:793dbd6d6498 415 }
star297 1:793dbd6d6498 416 oled.clear();
star297 1:793dbd6d6498 417 }
star297 1:793dbd6d6498 418
star297 1:793dbd6d6498 419 void DCFloop()
star297 1:793dbd6d6498 420 {
star297 1:793dbd6d6498 421 if (interrupt_counter == 0) {
star297 1:793dbd6d6498 422 if (start == 0) {dcf_sec=0;}
star297 1:793dbd6d6498 423 if (dcf_sec == 0){dcf_array[58] = 0;}
star297 3:1fb597da3fdf 424 if (dcf_good==1 && dcf_sec==2) {RTCset();}
star297 1:793dbd6d6498 425 }
star297 1:793dbd6d6498 426
star297 1:793dbd6d6498 427 if (interrupt_counter == 1) {
star297 1:793dbd6d6498 428 oled.setFontSize(FONT12X16);
star297 1:793dbd6d6498 429 oled.locate(15,5);oled.setFontColor(oled.toRGB(255,255,255));
star297 1:793dbd6d6498 430 oled.printf("%02d:%02d:%02d", hour, minute, second);
star297 1:793dbd6d6498 431 oled.locate(3,13);oled.setFontColor(oled.toRGB(255,255,255));
star297 1:793dbd6d6498 432 oled.setFontSize(FONT12X16);oled.printf("%02d",dcf_sec);
star297 1:793dbd6d6498 433 }
star297 1:793dbd6d6498 434
star297 1:793dbd6d6498 435 if (interrupt_counter==2){
star297 1:793dbd6d6498 436 if (rtcset==0) {
star297 1:793dbd6d6498 437 oled.drawText(33,2,(FONT5X7),"RTC -- un-set -- ",oled.toRGB(255,0,0));}
star297 1:793dbd6d6498 438 if (rtcset==1) {
star297 1:793dbd6d6498 439 oled.drawText(33,2,(FONT5X7),"RTC *** set *** ",oled.toRGB(0,255,0));}
star297 1:793dbd6d6498 440 oled.setFontSize(FONT12X16);oled.locate(0,1);
star297 1:793dbd6d6498 441 oled.setFontColor(oled.toRGB(255,255,255));
star297 1:793dbd6d6498 442 RTCread();
star297 1:793dbd6d6498 443 oled.printf("%2d:%02d:%02d",RTChour,RTCminute,RTCsecond);
star297 1:793dbd6d6498 444 }
star297 1:793dbd6d6498 445
star297 1:793dbd6d6498 446 if (interrupt_counter == 4) {
star297 1:793dbd6d6498 447 if (dcf_sec > 59) {dcf_sec = 0;}
star297 1:793dbd6d6498 448 if (dcf_sec>20 && signal_error==1) {DCFbitmapdraw();DCFdrawgraph();start=0;signal_error=0;SignalStatus=5;}
star297 1:793dbd6d6498 449 if (dcf_sec == 57) {oled.drawText(36,7,(FONT8X12)," ",oled.toRGB(0,0,0));}
star297 1:793dbd6d6498 450 if (start == 0 && signal_error==0) {SignalStatus=0;}
star297 1:793dbd6d6498 451 }
star297 1:793dbd6d6498 452
star297 1:793dbd6d6498 453 if (interrupt_counter == 6 && dcf_sec > 0){
star297 1:793dbd6d6498 454 if (dcf_sec==1){DCFdrawgraph();SignalStatus = 1;}
star297 1:793dbd6d6498 455 oled.setPenSize(1); oled.drawRectangle(11, 229, (dcf_sec*5), 8, oled.toRGB(0,255,0));draw_graph=0;
star297 1:793dbd6d6498 456 if (signal_error==1) {DCFdrawgraph();signal_error=0;SignalStatus=5;}
star297 1:793dbd6d6498 457 }
star297 1:793dbd6d6498 458
star297 1:793dbd6d6498 459 if (interrupt_counter == 7){
star297 1:793dbd6d6498 460 DCFbitstatus();
star297 1:793dbd6d6498 461 if (dcf_sec == 21){SignalStatus=2;r = !r;}
star297 1:793dbd6d6498 462 if (dcf_sec > 20 && dcf_sec < 59 ) {DCFbitprint();}
star297 1:793dbd6d6498 463 }
star297 1:793dbd6d6498 464
star297 1:793dbd6d6498 465 if (interrupt_counter == 8){
star297 1:793dbd6d6498 466 if (start==1){dcf_array[dcf_sec]=dcf_status.DCFsample150;}
star297 1:793dbd6d6498 467 oled.setFontSize(FONT12X16);oled.locate(0,2);
star297 1:793dbd6d6498 468 oled.setFontColor(oled.toRGB(255,255,255));
star297 1:793dbd6d6498 469 oled.printf("%s %02d %s' 20%02d",weekDayName[RTCdayofweek],RTCdayofmonth,monthName[RTCmonth-1],RTCyear);
star297 1:793dbd6d6498 470 }
star297 1:793dbd6d6498 471
star297 1:793dbd6d6498 472 if (interrupt_counter == 10) {
star297 1:793dbd6d6498 473 oled.setFontSize(FONT8X12);oled.locate(23,8);oled.setFontColor(oled.toRGB(255,255,255));
star297 1:793dbd6d6498 474 oled.printf("%s %02d %s' 20%02d", weekDayName[dayofweek], dayofmonth, monthName[month-1], year);
star297 1:793dbd6d6498 475 if (summertime) oled.drawText(23,9,(FONT8X12),"Summer Time",oled.toRGB(255,128,0));
star297 1:793dbd6d6498 476 else oled.drawText(23,9,(FONT8X12),"Winter Time",oled.toRGB(0,255,255));
star297 1:793dbd6d6498 477 oled.drawText(23,10,(FONT8X12),leapSymbol[dcf_status.is_leap],oled.toRGB(0,255,160));
star297 1:793dbd6d6498 478 }
star297 1:793dbd6d6498 479
star297 1:793dbd6d6498 480 if (interrupt_counter == 11 & second == 1) {
star297 1:793dbd6d6498 481 oled.setFontSize(FONT5X7);oled.locate(34,5);
star297 1:793dbd6d6498 482 oled.setFontColor(oled.toRGB(255,255,255));
star297 2:1eac3ea8c6e8 483 oled.printf("RTC set x %d", rtcset_times);
star297 1:793dbd6d6498 484 }
star297 1:793dbd6d6498 485
star297 1:793dbd6d6498 486 if (interrupt_counter == 12){
star297 1:793dbd6d6498 487 if (SignalStatus==0) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(255,255,255));}
star297 1:793dbd6d6498 488 if (SignalStatus==1) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(255,255,0));}
star297 1:793dbd6d6498 489 if (SignalStatus==2) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(0,255,255));}
star297 1:793dbd6d6498 490 if (SignalStatus==3) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(0,0,255));}
star297 1:793dbd6d6498 491 if (SignalStatus==4) {oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(0,255,0));}
star297 1:793dbd6d6498 492 if (SignalStatus==5) {
star297 1:793dbd6d6498 493 oled.drawText(26,20,(FONT5X7),statusText[SignalStatus],oled.toRGB(255,0,0));
star297 1:793dbd6d6498 494 oled.drawText(36,7,(FONT8X12)," ",oled.toRGB(0,0,0));
star297 1:793dbd6d6498 495 start=0;SignalStatus=0;sync=0;
star297 1:793dbd6d6498 496 }
star297 1:793dbd6d6498 497 }
star297 1:793dbd6d6498 498
star297 1:793dbd6d6498 499 if (interrupt_counter==13){
star297 2:1eac3ea8c6e8 500 if (start==0 && laststart != start) {oled.drawText(26,18,(FONT5X7),("No Sync "),oled.toRGB(255,0,0));laststart=start;}
star297 2:1eac3ea8c6e8 501 if (start==1 && laststart != start) {oled.drawText(26,18,(FONT5X7),("In Sync - running"),oled.toRGB(0,255,0));laststart=start;}
star297 1:793dbd6d6498 502 }
star297 0:94bf5ec759da 503
star297 1:793dbd6d6498 504 if (interrupt_counter>13){
star297 1:793dbd6d6498 505 oled.getTouch(&xbuffer,&ybuffer);
star297 1:793dbd6d6498 506 if (display==0){
star297 1:793dbd6d6498 507 if((xbuffer>2 && xbuffer<318) && (ybuffer>2 && ybuffer<238)){
star297 1:793dbd6d6498 508 oled.displayControl(0x01,0x01);display=1;
star297 1:793dbd6d6498 509 oled.resetTouchArea();}}
star297 1:793dbd6d6498 510 if((xbuffer>240 && xbuffer<290) && (ybuffer>140 && ybuffer<160)){
star297 2:1eac3ea8c6e8 511 dcfSignalIn.rise(NULL);Ticker50ms.detach();IRQ=1;}
star297 1:793dbd6d6498 512 }
star297 1:793dbd6d6498 513
star297 1:793dbd6d6498 514 if (interrupt_counter == 19){DCFbitstatus();}
star297 1:793dbd6d6498 515
star297 1:793dbd6d6498 516 }
star297 1:793dbd6d6498 517
star297 1:793dbd6d6498 518 void DCFtestparity()
star297 1:793dbd6d6498 519 {
star297 1:793dbd6d6498 520 rtcset=0;DCFparitycalc();
star297 1:793dbd6d6498 521 paritycheck= testu or testm or testdmy;
star297 1:793dbd6d6498 522 if (p_year>99) paritycheck=1;
star297 1:793dbd6d6498 523 if (p_month>12) paritycheck=1;
star297 1:793dbd6d6498 524 if (p_dayofmonth>31) paritycheck=1;
star297 1:793dbd6d6498 525 if (p_hour>23) paritycheck=1;
star297 1:793dbd6d6498 526 if (p_minute>59) paritycheck=1;
star297 1:793dbd6d6498 527 if (paritycheck) {dcf_good=0;SignalStatus = 5;} // bad parity
star297 1:793dbd6d6498 528 else {dcf_good=1;SignalStatus = 4;} // good parity
star297 1:793dbd6d6498 529 }
star297 1:793dbd6d6498 530
star297 1:793dbd6d6498 531 void DCFsetTime()
star297 1:793dbd6d6498 532 {
star297 1:793dbd6d6498 533 second = dcf_sec;
star297 1:793dbd6d6498 534 minute=p_minute;
star297 1:793dbd6d6498 535 hour=p_hour;
star297 1:793dbd6d6498 536 dayofweek=p_dayofweek;
star297 1:793dbd6d6498 537 dayofmonth=p_dayofmonth;
star297 1:793dbd6d6498 538 month=p_month;
star297 1:793dbd6d6498 539 year=p_year;
star297 1:793dbd6d6498 540 oled.drawText(36,7,(FONT8X12),"*",oled.toRGB(0,255,0));
star297 2:1eac3ea8c6e8 541 rtcset_times++;
star297 1:793dbd6d6498 542 }
star297 1:793dbd6d6498 543 void DCFbitprint()
star297 1:793dbd6d6498 544 {
star297 1:793dbd6d6498 545 if (r==1) {oled.setFontColor(oled.toRGB(0,0,255));} // alternate bit draw colour
star297 1:793dbd6d6498 546 else {oled.setFontColor(oled.toRGB(255,255,255));}
star297 1:793dbd6d6498 547 oled.setFontSize(FONT8X12);
star297 1:793dbd6d6498 548 if (dcf_sec==21){x=5;y=7;}
star297 1:793dbd6d6498 549 if (dcf_sec==28){x=5;y=15;oled.locate(5,15);oled.printf(" ");}
star297 1:793dbd6d6498 550 if (dcf_sec==29){x=7;y=7;}
star297 1:793dbd6d6498 551 if (dcf_sec==35){x=7;y=15;}
star297 1:793dbd6d6498 552 if (dcf_sec==36){x=9;y=7;}
star297 1:793dbd6d6498 553 if (dcf_sec==42){x=11;y=7;}
star297 1:793dbd6d6498 554 if (dcf_sec==45){x=13;y=7;}
star297 1:793dbd6d6498 555 if (dcf_sec==50){x=15;y=7;}
star297 1:793dbd6d6498 556 oled.locate(x,y);
star297 1:793dbd6d6498 557 oled.printf("%d",dcf_status.DCFsample150);
star297 1:793dbd6d6498 558 y++;
star297 1:793dbd6d6498 559 }
star297 1:793dbd6d6498 560
star297 1:793dbd6d6498 561 void DCFbitmapdraw()
star297 1:793dbd6d6498 562 {
star297 1:793dbd6d6498 563 x=2,y=6;
star297 1:793dbd6d6498 564 oled.drawText(x-2,y,(FONT8X12)," Dcf M H D d M Y",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 565 oled.drawText(x,y+1,(FONT8X12)," 1 i o a a o e",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 566 oled.drawText(x,y+2,(FONT8X12)," 2 n u t y n a",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 567 oled.drawText(x,y+3,(FONT8X12)," 4 u r e t r",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 568 oled.drawText(x,y+4,(FONT8X12)," 8 t h ",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 569 oled.drawText(x,y+5,(FONT8X12),"10 e ",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 570 oled.drawText(x,y+6,(FONT8X12),"20 ",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 571 oled.drawText(x,y+7,(FONT8X12),"40 ",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 572 oled.drawText(x,y+8,(FONT8X12),"80 ",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 573 oled.drawText(x,y+9,(FONT8X12)," P arity ",oled.toRGB(255,255,0));
star297 1:793dbd6d6498 574 oled.drawLine(8,83,135,83, oled.toRGB(0, 255, 0));
star297 1:793dbd6d6498 575 oled.drawLine(35,70,35,195, oled.toRGB(0, 255, 0));
star297 1:793dbd6d6498 576 }
star297 1:793dbd6d6498 577 void DCFdrawgraph()
star297 1:793dbd6d6498 578 {
star297 1:793dbd6d6498 579 if (draw_graph==0){
star297 1:793dbd6d6498 580 oled.setPenSize(0);
star297 1:793dbd6d6498 581 oled.drawRectangle(10,228,300,8,oled.toRGB(0,0,0));
star297 1:793dbd6d6498 582 oled.setPenSize(1);
star297 1:793dbd6d6498 583 oled.drawRectangle(10,227,302,10,oled.toRGB(255,255,255));
star297 1:793dbd6d6498 584 oled.drawLine(10,220,10,237,oled.toRGB(255,255,255));
star297 1:793dbd6d6498 585 oled.drawLine(312,220,312,237,oled.toRGB(255,255,255));
star297 1:793dbd6d6498 586 oled.drawLine(111,220,111,237,oled.toRGB(255,255,255)); //start
star297 1:793dbd6d6498 587 oled.drawLine(156,220,156,237,oled.toRGB(225,255,255)); //min
star297 1:793dbd6d6498 588 oled.drawLine(186,220,186,237,oled.toRGB(255,255,225)); //hour
star297 1:793dbd6d6498 589 oled.drawLine(216,220,216,237,oled.toRGB(225,255,225)); //date
star297 1:793dbd6d6498 590 oled.drawLine(231,220,231,237, oled.toRGB(225, 255, 225));//day
star297 1:793dbd6d6498 591 oled.drawLine(256,220,256,237, oled.toRGB(225, 255, 225));//month
star297 1:793dbd6d6498 592 oled.drawLine(301,220,301,237, oled.toRGB(225, 255, 225));//year
star297 1:793dbd6d6498 593 draw_graph=1;
star297 1:793dbd6d6498 594 }
star297 1:793dbd6d6498 595 }
star297 1:793dbd6d6498 596 void DCFbitstatus()
star297 1:793dbd6d6498 597 {
star297 1:793dbd6d6498 598 oled.locate(29,22);
star297 1:793dbd6d6498 599 oled.setFontSize(FONT5X7);
star297 1:793dbd6d6498 600 oled.setFontColor(oled.toRGB(255,0,0));
star297 1:793dbd6d6498 601 oled.printf("%d %d %d %d %d",dcf_status.DCFsample50,dcf_status.DCFsample150,dcf_status.DCFsample300,dcf_status.DCFsample500,dcf_status.DCFsample600);
star297 1:793dbd6d6498 602 oled.setPenSize(0);
star297 1:793dbd6d6498 603 if (!dcf_status.DCFsample50) {oled.drawRectangle(170,190,11,12, oled.toRGB(0,0,0));}
star297 1:793dbd6d6498 604 else {oled.drawRectangle(170,190,11,12, oled.toRGB(255,255,255));}
star297 1:793dbd6d6498 605 if (!dcf_status.DCFsample150) {oled.drawRectangle(182,190,11,12, oled.toRGB(0,0,0));}
star297 1:793dbd6d6498 606 else {oled.drawRectangle(182,190,11,12, oled.toRGB(255,255,255));}
star297 1:793dbd6d6498 607 if (!dcf_status.DCFsample300) {oled.drawRectangle(194,190,11,12, oled.toRGB(0,0,0));}
star297 1:793dbd6d6498 608 else {oled.drawRectangle(194,190,11,12, oled.toRGB(255,255,255));}
star297 1:793dbd6d6498 609 if (!dcf_status.DCFsample500) {oled.drawRectangle(206,190,23,12, oled.toRGB(0,0,0));}
star297 1:793dbd6d6498 610 else {oled.drawRectangle(206,190,23,12, oled.toRGB(255,255,255));}
star297 1:793dbd6d6498 611 if (!dcf_status.DCFsample600) {oled.drawRectangle(230,190,56,12, oled.toRGB(0,0,0));}
star297 1:793dbd6d6498 612 else {oled.drawRectangle(230,190,56,12, oled.toRGB(0,0,255));}
star297 1:793dbd6d6498 613 }
star297 1:793dbd6d6498 614
star297 1:793dbd6d6498 615 void DCFparitycalc()
star297 1:793dbd6d6498 616 {
star297 1:793dbd6d6498 617 //calculate summer/winter time----------------------------------------------------------------------
star297 1:793dbd6d6498 618 summertime = dcf_array[17] & 1;
star297 1:793dbd6d6498 619 //calculate hour--------------------------------------------------------------------------------------
star297 1:793dbd6d6498 620 hourh = dcf_array[34] * 20 + dcf_array[33] * 10;
star297 1:793dbd6d6498 621 hourl = dcf_array[32] * 8 + dcf_array[31] * 4 + dcf_array[30] * 2 + dcf_array[29] * 1;
star297 1:793dbd6d6498 622 p_hour = hourh + hourl;
star297 1:793dbd6d6498 623 //calculate minutes------------------------------------------------------------------------------------
star297 1:793dbd6d6498 624 minl = dcf_array[24] * 8 + dcf_array[23] * 4 + dcf_array[22] * 2 + dcf_array[21] * 1;
star297 1:793dbd6d6498 625 minh = dcf_array[27] * 40 + dcf_array[26] * 20 +dcf_array[25] * 10;
star297 1:793dbd6d6498 626 p_minute = minh + minl;
star297 1:793dbd6d6498 627 //calculate day of week--------------------------------------------------------------------------------
star297 1:793dbd6d6498 628 p_dayofweek = dcf_array[44] * 4 +dcf_array[43] * 2 + dcf_array[42] * 1;
star297 1:793dbd6d6498 629 //calculate day----------------------------------------------------------------------------------------
star297 1:793dbd6d6498 630 dayl = dcf_array[39] * 8 + dcf_array[38] * 4 + dcf_array[37] * 2 + dcf_array[36] * 1;
star297 1:793dbd6d6498 631 dayh = dcf_array[41] * 20 + dcf_array[40] * 10;
star297 1:793dbd6d6498 632 p_dayofmonth=dayh+dayl;
star297 1:793dbd6d6498 633 //calculate month--------------------------------------------------------------------------------------
star297 1:793dbd6d6498 634 monthh = dcf_array[49] * 10;
star297 1:793dbd6d6498 635 monthl = dcf_array[48] * 8 + dcf_array[47] * 4 + dcf_array[46] * 2 + dcf_array[45] * 1;
star297 1:793dbd6d6498 636 p_month = monthh +monthl;
star297 1:793dbd6d6498 637 //calculate year---------------------------------------------------------------------------------------
star297 1:793dbd6d6498 638 yearh = dcf_array[57] * 80 + dcf_array[56] * 40 + dcf_array[55] * 20 + dcf_array[54] * 10;
star297 1:793dbd6d6498 639 yearl = dcf_array[53] * 8 +dcf_array[52] * 4 + dcf_array[51] * 2 + dcf_array[50] * 1;
star297 1:793dbd6d6498 640 p_year = yearh+yearl;
star297 1:793dbd6d6498 641 //calculate parity
star297 1:793dbd6d6498 642 paritym = dcf_array[21] + dcf_array[22] + dcf_array[23] + dcf_array[24] + dcf_array[25] + dcf_array[26] +dcf_array[27] +dcf_array [28];
star297 1:793dbd6d6498 643 parityu =dcf_array[29] + dcf_array[30] + dcf_array[31] + dcf_array[32] + dcf_array[33] + dcf_array[34] + dcf_array[35];
star297 1:793dbd6d6498 644 paritydmy =dcf_array[36] + dcf_array [37] + dcf_array [38] + dcf_array [39] + dcf_array[40] + dcf_array[41] + dcf_array [42] + dcf_array [43] + dcf_array[44] + dcf_array [45] + dcf_array[46] + dcf_array [47] + dcf_array[48] + dcf_array[49] + dcf_array[50] + dcf_array[51] + dcf_array [52] + dcf_array[53] + dcf_array[54] + dcf_array[55] + dcf_array[56] + dcf_array[57] + dcf_array[58];
star297 1:793dbd6d6498 645 //test parity------------------------------
star297 1:793dbd6d6498 646 testu=parityu & 1;
star297 1:793dbd6d6498 647 testm=paritym & 1;
star297 1:793dbd6d6498 648 testdmy=paritydmy & 1;
star297 1:793dbd6d6498 649 }
star297 1:793dbd6d6498 650
star297 1:793dbd6d6498 651 void dcfIRQ(void)
star297 1:793dbd6d6498 652 {
star297 3:1fb597da3fdf 653 if (sync==0){ // align Ticker with DCF 59th bit start signal every minute
star297 1:793dbd6d6498 654 interrupt_counter = 0;sync=1;dcf_sec=0;error_count=0;
star297 1:793dbd6d6498 655 Ticker50ms.attach(& dcfISR, .05);
star297 1:793dbd6d6498 656 }
star297 1:793dbd6d6498 657 }
star297 1:793dbd6d6498 658
star297 1:793dbd6d6498 659 void dcfISR() //This is the interrupt service routine (ISR) that is called every 50ms
star297 1:793dbd6d6498 660 {
star297 1:793dbd6d6498 661 interrupt_counter++;loop=0;
star297 1:793dbd6d6498 662
star297 2:1eac3ea8c6e8 663 SignalLED = !dcfSignalIn; // Show dcfSignal state on LED
star297 1:793dbd6d6498 664
star297 1:793dbd6d6498 665 if (interrupt_counter == 20) { // 50mS x 20 = 1000mS = 1 Second
star297 1:793dbd6d6498 666 interrupt_counter = 0;second++;dcf_sec++;
star297 1:793dbd6d6498 667 if (start==0) dcf_sec=0;
star297 1:793dbd6d6498 668 }
star297 1:793dbd6d6498 669 if (interrupt_counter == 0){
star297 1:793dbd6d6498 670 if (dcf_sec==58) {SignalStatus = 3;}
star297 1:793dbd6d6498 671 if (dcf_sec==59) {DCFtestparity();}
star297 1:793dbd6d6498 672 if (dcf_good==1 && dcf_sec==1) {DCFsetTime();}
star297 1:793dbd6d6498 673 }
star297 1:793dbd6d6498 674 if (second >= 60) {++minute;second -=60;}
star297 1:793dbd6d6498 675 if (minute >= 60) {++hour;minute-=60;}
star297 1:793dbd6d6498 676 if (hour >= 24) {hour -=24;++dayofweek;++dayofmonth;}
star297 1:793dbd6d6498 677 if (dayofweek > 6) dayofweek = 0;
star297 1:793dbd6d6498 678 if (dayofmonth > maxDay(year, month)) {dayofmonth = 1;month++;}
star297 1:793dbd6d6498 679 if (month > 12) {month = 1;year++;}
star297 1:793dbd6d6498 680 if (year > 99) year = 1;
star297 1:793dbd6d6498 681
star297 1:793dbd6d6498 682 switch (interrupt_counter) {
star297 1:793dbd6d6498 683 case 1: { // 50mS after start of second pulse
star297 2:1eac3ea8c6e8 684 dcf_status.DCFsample50 = (dcfSignalIn);
star297 1:793dbd6d6498 685 break;}
star297 1:793dbd6d6498 686 case 3: { // 150mS after start of second pulse (bit "1" dcf Data)
star297 2:1eac3ea8c6e8 687 dcf_status.DCFsample150 = (dcfSignalIn);
star297 1:793dbd6d6498 688 break;}
star297 1:793dbd6d6498 689 case 6: { // 300mS after start of second (signal error if true)
star297 2:1eac3ea8c6e8 690 dcf_status.DCFsample300 = (dcfSignalIn);
star297 2:1eac3ea8c6e8 691 if (dcfSignalIn) {dcf_error = 1;}
star297 1:793dbd6d6498 692 break;}
star297 1:793dbd6d6498 693 case 10: { // 500mS after start of second (signal error if true)
star297 2:1eac3ea8c6e8 694 dcf_status.DCFsample500 = (dcfSignalIn);
star297 2:1eac3ea8c6e8 695 if (dcfSignalIn) {dcf_error = 1;}
star297 1:793dbd6d6498 696 break;}
star297 1:793dbd6d6498 697 case 12: { // 600mS after start of second (signal error if true)
star297 2:1eac3ea8c6e8 698 dcf_status.DCFsample600 = (dcfSignalIn);
star297 2:1eac3ea8c6e8 699 if (dcfSignalIn) {dcf_error = 1;}
star297 1:793dbd6d6498 700 break;}
star297 1:793dbd6d6498 701 }
star297 1:793dbd6d6498 702 if (dcf_status.DCFsample50==0 && dcf_sec<58){sync=0;}
star297 1:793dbd6d6498 703
star297 1:793dbd6d6498 704 if (interrupt_counter==1){
star297 2:1eac3ea8c6e8 705 if (dcfSignalIn){nosignal=1;}
star297 1:793dbd6d6498 706 else nosignal++;
star297 1:793dbd6d6498 707 if (nosignal>5){nosignal=2; SignalStatus = 5;signal_error=1;}
star297 1:793dbd6d6498 708 }
star297 0:94bf5ec759da 709
star297 1:793dbd6d6498 710 if (interrupt_counter==15){
star297 1:793dbd6d6498 711 if (!dcf_status.DCFsample150 && !dcf_status.DCFsample50) {sync=0;start=1;}
star297 1:793dbd6d6498 712 }
star297 1:793dbd6d6498 713 if (interrupt_counter==18){
star297 1:793dbd6d6498 714 if (dcf_error==1) {error_count++;dcf_error=0;}
star297 1:793dbd6d6498 715 if (error_count > 3 && sync==1) {error_count = 0;signal_error=1;}
star297 1:793dbd6d6498 716 }
star297 0:94bf5ec759da 717
star297 1:793dbd6d6498 718 } // End of ISR
star297 1:793dbd6d6498 719