@@ -61,3 +61,162 @@ Blockly.Arduino['micros'] = function() {
61
61
var code = 'micros()' ;
62
62
return [ code , Blockly . Arduino . ORDER_ATOMIC ] ;
63
63
} ;
64
+
65
+ Blockly . Arduino [ 'rtc_get_time' ] = function ( ) {
66
+ Blockly . Arduino . definitions_ [ 'define_rtc' ] = '#include <LRTC.h>' ;
67
+ Blockly . Arduino . setups_ [ 'setup_rtc' ] = 'LRTC.begin();' ;
68
+ Blockly . Arduino . definitions_ [ 'define_rtc_get_time' ] = `
69
+ String get_time_from_RTC() {
70
+ // get time from the RTC module
71
+ LRTC.get();
72
+
73
+ // format to time string
74
+ static char buffer[] = "YYYY-MM-DDTHH:MM:SS+08";
75
+ sprintf(buffer, "%04ld-%02ld-%02ldT%02ld:%02ld:%02ld+08",
76
+ LRTC.year(),
77
+ LRTC.month(),
78
+ LRTC.day(),
79
+ LRTC.hour(),
80
+ LRTC.minute(),
81
+ LRTC.second());
82
+
83
+ return String(buffer);
84
+ }\n`
85
+
86
+ var code = 'get_time_from_RTC()' ;
87
+ return [ code , Blockly . Arduino . ORDER_ATOMIC ] ;
88
+ } ;
89
+
90
+ Blockly . Arduino [ 'rtc_set_time_from_number' ] = function ( ) {
91
+ Blockly . Arduino . definitions_ [ 'define_rtc' ] = '#include <LRTC.h>' ;
92
+ Blockly . Arduino . setups_ [ 'setup_rtc' ] = 'LRTC.begin();' ;
93
+
94
+ var year = Blockly . Arduino . valueToCode ( this , 'YEAR' , Blockly . Arduino . ORDER_ATOMIC ) || '0'
95
+ var month = Blockly . Arduino . valueToCode ( this , 'MONTH' , Blockly . Arduino . ORDER_ATOMIC ) || '0'
96
+ var day = Blockly . Arduino . valueToCode ( this , 'DAY' , Blockly . Arduino . ORDER_ATOMIC ) || '0'
97
+ var hour = Blockly . Arduino . valueToCode ( this , 'HOUR' , Blockly . Arduino . ORDER_ATOMIC ) || '0'
98
+ var min = Blockly . Arduino . valueToCode ( this , 'MIN' , Blockly . Arduino . ORDER_ATOMIC ) || '0'
99
+ var sec = Blockly . Arduino . valueToCode ( this , 'SEC' , Blockly . Arduino . ORDER_ATOMIC ) || '0'
100
+
101
+ var code = `LRTC.set(${ year } , ${ month } , ${ day } , ${ hour } , ${ min } , ${ sec } );\n`
102
+ return code ;
103
+ } ;
104
+
105
+ Blockly . Arduino [ 'rtc_set_time_from_string' ] = function ( ) {
106
+ Blockly . Arduino . definitions_ [ 'define_rtc' ] = '#include <LRTC.h>' ;
107
+ Blockly . Arduino . setups_ [ 'setup_rtc' ] = 'LRTC.begin();' ;
108
+ Blockly . Arduino . definitions_ [ 'define_rtc_set_rtc_from_time_string' ] = `
109
+ void set_rtc_from_time_string(const String& time_str) {
110
+ // field_index [0,1,2,3,4,5] = [Year,Month,Day,Hour,Minute,Sec]
111
+ int fields[6] = {0};
112
+ sscanf(time_str.c_str(), "%d-%d-%dT%d:%d:%d+08",
113
+ &fields[0], &fields[1], &fields[2],
114
+ &fields[3], &fields[4], &fields[5]);
115
+ LRTC.set(fields[0], fields[1], fields[2],
116
+ fields[3], fields[4], fields[5]);
117
+ }\n`
118
+
119
+ var time_str = Blockly . Arduino . valueToCode ( this , 'TIME_STRING' , Blockly . Arduino . ORDER_ATOMIC ) || '"1900-00-00T00:00:00+08"'
120
+ var code = `set_rtc_from_time_string(${ time_str } );\n`
121
+ return code ;
122
+ } ;
123
+
124
+ Blockly . Arduino [ 'rtc_get_time_field' ] = function ( ) {
125
+ Blockly . Arduino . definitions_ [ 'define_rtc' ] = '#include <LRTC.h>' ;
126
+ Blockly . Arduino . setups_ [ 'setup_rtc' ] = 'LRTC.begin();' ;
127
+ Blockly . Arduino . definitions_ [ 'define_rtc_get_field_from_time_string' ] = `
128
+ int get_field_from_time_string(const String& time_str, int field_index) {
129
+ // field_index [0,1,2,3,4,5] = [Year,Month,Day,Hour,Minute,Sec]
130
+ int fields[6] = {0};
131
+ sscanf(time_str.c_str(), "%d-%d-%dT%d:%d:%d+08",
132
+ &fields[0], &fields[1], &fields[2],
133
+ &fields[3], &fields[4], &fields[5]);
134
+ if(field_index < 0 || field_index > 5) {
135
+ return 0;
136
+ } else {
137
+ return fields[field_index];
138
+ }
139
+ }\n`
140
+
141
+ var time_str = Blockly . Arduino . valueToCode ( this , 'TIME_STRING' , Blockly . Arduino . ORDER_ATOMIC ) || '"1900-00-00T00:00:00+08"'
142
+ var field_index = this . getFieldValue ( 'FIELD' )
143
+ var code = `get_field_from_time_string(${ time_str } , ${ field_index } )`
144
+ return [ code , Blockly . Arduino . ORDER_ATOMIC ] ;
145
+ } ;
146
+
147
+ Blockly . Arduino [ 'ntp_get_datetime' ] = function ( ) {
148
+ Blockly . Arduino . definitions_ [ 'define_udp' ] = '#include <WiFiUdp.h>' ;
149
+ Blockly . Arduino . definitions_ [ 'define_ctime' ] = '#include <ctime>' ;
150
+ Blockly . Arduino . definitions_ [ 'define_ntp_get_datetime' ] = `
151
+ const char *NTP_server = "time.stdtime.gov.tw";
152
+ const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
153
+ static byte packetBuffer[NTP_PACKET_SIZE] = {0}; //buffer to hold incoming and outgoing packets
154
+ const unsigned int localPort = 2390; // local port to listen for UDP packets
155
+ static WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
156
+
157
+ String getNetworkTime() {
158
+ Udp.begin(localPort);
159
+ sendNTPpacket(NTP_server); // send an NTP packet to a time server
160
+ // wait to see if a reply is available
161
+ delay(1000);
162
+ if (Udp.parsePacket()) {
163
+ // We've received a packet, read the data from it
164
+ Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
165
+
166
+ //the timestamp starts at byte 40 of the received packet and is four bytes,
167
+ // or two words, long. First, esxtract the two words:
168
+ const unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
169
+ const unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
170
+ // combine the four bytes (two words) into a long integer
171
+ // this is NTP time (seconds since Jan 1 1900):
172
+ const unsigned long secsSince1900 = highWord << 16 | lowWord;
173
+ // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
174
+ const unsigned long seventyYears = 2208988800UL;
175
+ // subtract seventy years:
176
+ const unsigned long epoch = secsSince1900 - seventyYears;
177
+ // Taiwan is UTC+8 = 8 * 60 * 60 seconds
178
+ const time_t taiwan_time = epoch + (8 * 60 * 60);
179
+ // const tm* pTime = gmtime(&taiwan_time);
180
+ static char time_text[] = "YYYY-MM-DDTHH:MM:SS+08";
181
+ strftime(time_text, sizeof(time_text), "%Y-%m-%dT%H:%M:%S+08", gmtime(&taiwan_time));
182
+ return String((const char*)time_text);
183
+ }
184
+
185
+ return String("Connection error");
186
+ }
187
+
188
+ // send an NTP request to the time server at the given address
189
+ unsigned long sendNTPpacket(const char* host) {
190
+ //Serial.println("1");
191
+ // set all bytes in the buffer to 0
192
+ memset(packetBuffer, 0, NTP_PACKET_SIZE);
193
+ // Initialize values needed to form NTP request
194
+ // (see URL above for details on the packets)
195
+ //Serial.println("2");
196
+ packetBuffer[0] = 0b11100011; // LI, Version, Mode
197
+ packetBuffer[1] = 0; // Stratum, or type of clock
198
+ packetBuffer[2] = 6; // Polling Interval
199
+ packetBuffer[3] = 0xEC; // Peer Clock Precision
200
+ // 8 bytes of zero for Root Delay & Root Dispersion
201
+ packetBuffer[12] = 49;
202
+ packetBuffer[13] = 0x4E;
203
+ packetBuffer[14] = 49;
204
+ packetBuffer[15] = 52;
205
+
206
+ //Serial.println("3");
207
+
208
+ // all NTP fields have been given values, now
209
+ // you can send a packet requesting a timestamp:
210
+ Udp.beginPacket(host, 123); //NTP requests are to port 123
211
+ //Serial.println("4");
212
+ Udp.write(packetBuffer, NTP_PACKET_SIZE);
213
+ //Serial.println("5");
214
+ Udp.endPacket();
215
+ //Serial.println("6");
216
+
217
+ return 0;
218
+ }
219
+ `
220
+ var code = `getNetworkTime()` ;
221
+ return [ code , Blockly . Arduino . ORDER_ATOMIC ] ;
222
+ }
0 commit comments