Skip to content

Commit

Permalink
Implement #200, #201, #202
Browse files Browse the repository at this point in the history
 * Add RTC and NTP blocks
 * Add WiFi localIP() block
 * Add BLE device address block
  • Loading branch information
pablosun committed Jul 18, 2018
1 parent 8f25cee commit 8bf6947
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 21 deletions.
34 changes: 23 additions & 11 deletions edit/blocks/linkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ Blockly.Blocks['linkit_ble_periphral'] = {
}
};

Blockly.Blocks['linkit_ble_get_address'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.LINKIT_SET_BLE_PERIPHRAL_HELPURL);
this.setColour(Blockly.Blocks.linkit.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.LINKIT_GET_BLE_ADDRESS);
this.setOutput(true, 'String');
this.setTooltip(Blockly.Msg.LINKIT_SET_BLE_PERIPHRAL_TOOLTIP);
}
};


/*
Blockly.Blocks['linkit_ble_periphral'] = {
init: function() {
Expand Down Expand Up @@ -246,17 +258,6 @@ Blockly.Blocks['linkit_ble_wait_until_ready'] = {
}
};

Blockly.Blocks['linkit_ble_get_address'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.LINKIT_SET_BLE_HELPURL);
this.setColour(Blockly.Blocks.linkit.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.LINKIT_SET_BLE_GET_ADDRESS_TITLE);
this.setOutput(true, 'String');
this.setTooltip(Blockly.Msg.LINKIT_SET_BLE_TOOLTIP);
}
};

Blockly.Blocks['linkit_ble_ready'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.LINKIT_SET_BLE_HELPURL);
Expand Down Expand Up @@ -774,6 +775,17 @@ Blockly.Blocks['linkit_wifi_ready'] = {
}
};

Blockly.Blocks['linkit_wifi_ip_address'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.LINKIT_SET_WIFI_HELPURL);
this.setColour(Blockly.Blocks.linkit.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.LINKIT_GET_WIFI_IP_TITLE);
this.setOutput(true, 'String');
this.setTooltip(Blockly.Msg.LINKIT_SET_WIFI_TOOLTIP);
}
};

Blockly.Blocks['linkit_wifi'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.LINKIT_SET_WIFI_HELPURL);
Expand Down
109 changes: 109 additions & 0 deletions edit/blocks/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,112 @@ Blockly.Blocks['millis'] = {
this.setTooltip(Blockly.Msg.TIMES_MILLIS_TOOLTIP);
}
};

Blockly.Blocks['ntp_get_datetime'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.TIMES_MILLIS_HELPURL);
this.setColour(Blockly.Blocks.times.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.TIMES_DATETIME_FROM_WIFI_APPENDTEXT);

// This block should return yyyy-mm-ddThh:mm:ss (based on ISO8601 but removes fractions)
this.setOutput(true, "String");
this.setTooltip(Blockly.Msg.TIMES_DATETIME_FROM_WIFI_TOOLTIP);

}
};

Blockly.Blocks['rtc_set_time_from_string'] = {
init: function() {
// This block should parse the input string yyyy-mm-ddThh:mm:ss+08 (ISO8601)
// and call LRTC.set() accordingly
this.setHelpUrl(Blockly.Msg.TIMES_DATETIME_RTC_HELPURL);
this.setColour(Blockly.Blocks.times.HUE);
this.appendValueInput("TIME_STRING")
.setCheck(["String", "Text"])
.appendField(Blockly.Msg.TIMES_DATETIME_SET_RTC_STRING_APPENDTEXT);

this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip(Blockly.Msg.TIMES_DATETIME_SET_RTC_STRING_TOOLTIP);

}
};

Blockly.Blocks['rtc_get_time'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.TIMES_DATETIME_RTC_HELPURL);
this.setColour(Blockly.Blocks.times.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.TIMES_DATETIME_GET_RTC);

// This block should return yyyy-mm-ddThh:mm:ss (based on ISO8601 but removes fractions)
this.setOutput(true, "String");
this.setTooltip(Blockly.Msg.TIMES_DATETIME_GET_RTC_TOOLTIP);
}
};

Blockly.Blocks['rtc_get_time_field'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.TIMES_DATETIME_RTC_HELPURL);
this.setColour(Blockly.Blocks.times.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.TIMES_DATETIME_GET_RTC_UNIT)
.appendField(new Blockly.FieldDropdown([
[Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_YEAR, '0'],
[Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_MONTH, '1'],
[Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_DAY, '2'],
[Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_HOUR, '3'],
[Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_MIN, '4'],
[Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_SEC, '5'],
]), 'FIELD');

this.appendValueInput("TIME_STRING")
.setCheck(["String"]);

// This block should parsed number
this.setInputsInline(true);
this.setOutput(true, "Number");
this.setTooltip(Blockly.Msg.TIMES_DATETIME_GET_RTC_UNIT_TOOLTIP);
}
};

Blockly.Blocks['rtc_set_time_from_number'] = {
init: function() {
// This block takes numerical values
// and call LRTC.set() accordingly
this.setHelpUrl(Blockly.Msg.TIMES_DATETIME_RTC_HELPURL);
this.setColour(Blockly.Blocks.times.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.TIMES_DATETIME_SET_RTC_NUMBER_APPENDTEXT);

this.appendValueInput("YEAR")
.setCheck(["Number"])
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_YEAR);
this.appendValueInput("MONTH")
.setCheck(["Number"])
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_MONTH);
this.appendValueInput("DAY")
.setCheck(["Number"])
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_DAY);
this.appendValueInput("HOUR")
.setCheck(["Number"])
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_HOUR);
this.appendValueInput("MIN")
.setCheck(["Number"])
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_MIN);
this.appendValueInput("SEC")
.setCheck(["Number"])
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.TIMES_DATETIME_RTC_APPENDTEXT_SEC);

this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip(Blockly.Msg.TIMES_DATETIME_SET_RTC_NUMBER_TOOLTIP);
}
};
23 changes: 13 additions & 10 deletions edit/generators/linkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ Blockly.Arduino.linkit_ble_periphral = function() {
return code;
};

Blockly.Arduino.linkit_ble_get_address = function() {
Blockly.Arduino.definitions_['define_linkit_ble_include'] = '#include <LBLE.h>';
Blockly.Arduino.setups_['define_linkit_ble_setup'] = 'LBLE.begin();';

var code = "LBLE.getDeviceAddress().toString()"
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/* origin:
Blockly.Arduino.linkit_ble_periphral = function() {
Expand Down Expand Up @@ -248,16 +256,6 @@ Blockly.Arduino.linkit_ble_wait_until_ready = function() {
return code;
};

Blockly.Arduino.linkit_ble_get_address = function() {

Blockly.Arduino.definitions_['define_linkit_ble_include'] = '#include <LBLE.h>';

Blockly.Arduino.setups_['define_linkit_ble_setup'] = 'LBLE.begin();';

var code = "LBLE.getDeviceAddress()";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.linkit_ble_ready = function() {

Blockly.Arduino.definitions_['define_linkit_ble_include'] = '#include <LBLE.h>';
Expand Down Expand Up @@ -493,6 +491,11 @@ Blockly.Arduino.linkit_wifi_ready_advanced = function() {
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.linkit_wifi_ip_address = function() {
var code = "WiFi.localIP().toString()";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.linkit_wifi_ready = function() {
var code = "(WiFi.begin(_lwifi_ssid, _lwifi_pass) == WL_CONNECTED)";
return [code, Blockly.Arduino.ORDER_ATOMIC];
Expand Down
159 changes: 159 additions & 0 deletions edit/generators/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,162 @@ Blockly.Arduino['micros'] = function() {
var code = 'micros()';
return [code,Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino['rtc_get_time'] = function() {
Blockly.Arduino.definitions_['define_rtc'] = '#include <LRTC.h>';
Blockly.Arduino.setups_['setup_rtc'] = 'LRTC.begin();';
Blockly.Arduino.definitions_['define_rtc_get_time'] = `
String get_time_from_RTC() {
// get time from the RTC module
LRTC.get();
// format to time string
static char buffer[] = "YYYY-MM-DDTHH:MM:SS+08";
sprintf(buffer, "%04ld-%02ld-%02ldT%02ld:%02ld:%02ld+08",
LRTC.year(),
LRTC.month(),
LRTC.day(),
LRTC.hour(),
LRTC.minute(),
LRTC.second());
return String(buffer);
}\n`

var code = 'get_time_from_RTC()';
return [code,Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino['rtc_set_time_from_number'] = function() {
Blockly.Arduino.definitions_['define_rtc'] = '#include <LRTC.h>';
Blockly.Arduino.setups_['setup_rtc'] = 'LRTC.begin();';

var year = Blockly.Arduino.valueToCode(this, 'YEAR', Blockly.Arduino.ORDER_ATOMIC) || '0'
var month = Blockly.Arduino.valueToCode(this, 'MONTH', Blockly.Arduino.ORDER_ATOMIC) || '0'
var day = Blockly.Arduino.valueToCode(this, 'DAY', Blockly.Arduino.ORDER_ATOMIC) || '0'
var hour = Blockly.Arduino.valueToCode(this, 'HOUR', Blockly.Arduino.ORDER_ATOMIC) || '0'
var min = Blockly.Arduino.valueToCode(this, 'MIN', Blockly.Arduino.ORDER_ATOMIC) || '0'
var sec = Blockly.Arduino.valueToCode(this, 'SEC', Blockly.Arduino.ORDER_ATOMIC) || '0'

var code = `LRTC.set(${year}, ${month}, ${day}, ${hour}, ${min}, ${sec});\n`
return code;
};

Blockly.Arduino['rtc_set_time_from_string'] = function() {
Blockly.Arduino.definitions_['define_rtc'] = '#include <LRTC.h>';
Blockly.Arduino.setups_['setup_rtc'] = 'LRTC.begin();';
Blockly.Arduino.definitions_['define_rtc_set_rtc_from_time_string'] = `
void set_rtc_from_time_string(const String& time_str) {
// field_index [0,1,2,3,4,5] = [Year,Month,Day,Hour,Minute,Sec]
int fields[6] = {0};
sscanf(time_str.c_str(), "%d-%d-%dT%d:%d:%d+08",
&fields[0], &fields[1], &fields[2],
&fields[3], &fields[4], &fields[5]);
LRTC.set(fields[0], fields[1], fields[2],
fields[3], fields[4], fields[5]);
}\n`

var time_str = Blockly.Arduino.valueToCode(this, 'TIME_STRING', Blockly.Arduino.ORDER_ATOMIC) || '"1900-00-00T00:00:00+08"'
var code = `set_rtc_from_time_string(${time_str});\n`
return code;
};

Blockly.Arduino['rtc_get_time_field'] = function() {
Blockly.Arduino.definitions_['define_rtc'] = '#include <LRTC.h>';
Blockly.Arduino.setups_['setup_rtc'] = 'LRTC.begin();';
Blockly.Arduino.definitions_['define_rtc_get_field_from_time_string'] = `
int get_field_from_time_string(const String& time_str, int field_index) {
// field_index [0,1,2,3,4,5] = [Year,Month,Day,Hour,Minute,Sec]
int fields[6] = {0};
sscanf(time_str.c_str(), "%d-%d-%dT%d:%d:%d+08",
&fields[0], &fields[1], &fields[2],
&fields[3], &fields[4], &fields[5]);
if(field_index < 0 || field_index > 5) {
return 0;
} else {
return fields[field_index];
}
}\n`

var time_str = Blockly.Arduino.valueToCode(this, 'TIME_STRING', Blockly.Arduino.ORDER_ATOMIC) || '"1900-00-00T00:00:00+08"'
var field_index = this.getFieldValue('FIELD')
var code = `get_field_from_time_string(${time_str}, ${field_index})`
return [code,Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino['ntp_get_datetime'] = function() {
Blockly.Arduino.definitions_['define_udp'] = '#include <WiFiUdp.h>';
Blockly.Arduino.definitions_['define_ctime'] = '#include <ctime>';
Blockly.Arduino.definitions_['define_ntp_get_datetime'] = `
const char *NTP_server = "time.stdtime.gov.tw";
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
static byte packetBuffer[NTP_PACKET_SIZE] = {0}; //buffer to hold incoming and outgoing packets
const unsigned int localPort = 2390; // local port to listen for UDP packets
static WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
String getNetworkTime() {
Udp.begin(localPort);
sendNTPpacket(NTP_server); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
const unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
const unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
const unsigned long secsSince1900 = highWord << 16 | lowWord;
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
const unsigned long epoch = secsSince1900 - seventyYears;
// Taiwan is UTC+8 = 8 * 60 * 60 seconds
const time_t taiwan_time = epoch + (8 * 60 * 60);
// const tm* pTime = gmtime(&taiwan_time);
static char time_text[] = "YYYY-MM-DDTHH:MM:SS+08";
strftime(time_text, sizeof(time_text), "%Y-%m-%dT%H:%M:%S+08", gmtime(&taiwan_time));
return String((const char*)time_text);
}
return String("Connection error");
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(const char* host) {
//Serial.println("1");
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
//Serial.println("2");
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
//Serial.println("3");
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(host, 123); //NTP requests are to port 123
//Serial.println("4");
Udp.write(packetBuffer, NTP_PACKET_SIZE);
//Serial.println("5");
Udp.endPacket();
//Serial.println("6");
return 0;
}
`
var code = `getNetworkTime()`;
return [code,Blockly.Arduino.ORDER_ATOMIC];
}
Loading

0 comments on commit 8bf6947

Please sign in to comment.