This commit is contained in:
ocogeclub 2021-11-27 21:43:53 +09:00
parent 4680afcd95
commit 1255e84661
2 changed files with 213 additions and 190 deletions

View File

@ -184,8 +184,8 @@ Blockly.Msg["UGJ_DECTOHEX_TOOLTIP"] = "10進数を16進数に変換します。"
Blockly.Msg["UGJ_CANVAS_INIT_TITLE"] = "キャンバスを表示";
Blockly.Msg["UGJ_CANVAS_INIT_TOOLTIP"] = "キャンバスを表示し、使用できるようにします。";
Blockly.Msg["UGJ_FACEAPI_TITLE"] = "TensorFlowによる顔検出 %1 ランドマークを検出 %2 %3";
Blockly.Msg["UGJ_FACEAPI_TOOLTIP"] = "TensorFlow とFaceAPI をロードし、顔検出をできるようにします。";
// Blockly.Msg["UGJ_FACEAPI_TITLE"] = "TensorFlowによる顔検出 %1 ランドマークを検出 %2 %3";
// Blockly.Msg["UGJ_FACEAPI_TOOLTIP"] = "TensorFlow とFaceAPI をロードし、顔検出をできるようにします。";
Blockly.Msg["UGJ_SLEEP_TITLE"] = "%1 秒待つ";
Blockly.Msg["UGJ_SLEEP_TOOLTIP"] = "指定した秒数だけ処理を中断します。";

View File

@ -135,8 +135,8 @@ Blockly.JavaScript['ugj_controls_forEach'] = function (block) {
var variable_item = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('item'), Blockly.Variables.NAME_TYPE);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
`for (let i_index in ${value_list}) {`,
`${variable_item} = ${value_list}[i_index];`,
`for (let _index in ${value_list}) {`,
`${variable_item} = ${value_list}[_index];`,
statements_do,
`}`,
''
@ -321,9 +321,9 @@ Blockly.Blocks['ugj_gpio_open'] = {
};
Blockly.JavaScript['ugj_gpio_open'] = function (block) {
Blockly.JavaScript.provideFunction_(
'require_gpio', [`const pi = require('@ocogeclub/` + elutil.gpio_backend + `');`]
'require_gpio', [`const _pi = require('@ocogeclub/` + elutil.gpio_backend + `');`]
);
var code = `await pi.gpio_open();\n`; //
var code = `await _pi.gpio_open();\n`; //
return code;
};
Blockly.Python['ugj_gpio_open'] = function (block) {
@ -352,7 +352,7 @@ Blockly.Blocks['ugj_gpio_close'] = {
}
};
Blockly.JavaScript['ugj_gpio_close'] = function (block) {
var code = 'await pi.gpio_close();\n';
var code = 'await _pi.gpio_close();\n';
return code;
};
Blockly.Python['ugj_gpio_close'] = function (block) {
@ -387,7 +387,7 @@ Blockly.Blocks['ugj_gpio_set_output'] = {
};
Blockly.JavaScript['ugj_gpio_set_output'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.gpio_set_output(${value_gpio});\n`;
var code = `await _pi.gpio_set_output(${value_gpio});\n`;
return code;
};
Blockly.Python['ugj_gpio_set_output'] = function (block) {
@ -442,7 +442,7 @@ Blockly.Blocks['ugj_gpio_set_input'] = {
Blockly.JavaScript['ugj_gpio_set_input'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var dropdown_lflag = block.getFieldValue('lflag');
var code = `await pi.gpio_set_input(${value_gpio}, ${dropdown_lflag});\n`;
var code = `await _pi.gpio_set_input(${value_gpio}, ${dropdown_lflag});\n`;
return code;
};
Blockly.Python['ugj_gpio_set_input'] = function (block) {
@ -478,7 +478,7 @@ Blockly.Blocks['ugj_gpio_read'] = {
};
Blockly.JavaScript['ugj_gpio_read'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.gpio_read(${value_gpio})`;
var code = `await _pi.gpio_read(${value_gpio})`;
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.Python['ugj_gpio_read'] = function (block) {
@ -529,7 +529,7 @@ Blockly.Blocks['ugj_gpio_write'] = {
Blockly.JavaScript['ugj_gpio_write'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var dropdown_level = block.getFieldValue('level');
var code = `await pi.gpio_write(${value_gpio}, ${dropdown_level});\n`;
var code = `await _pi.gpio_write(${value_gpio}, ${dropdown_level});\n`;
return code;
};
Blockly.Python['ugj_gpio_write'] = function (block) {
@ -572,7 +572,7 @@ Blockly.Blocks['ugj_servo'] = {
Blockly.JavaScript['ugj_servo'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var value_pulsewidth = Blockly.JavaScript.valueToCode(block, 'pulsewidth', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.servo(${value_gpio}, ${value_pulsewidth});\n`;
var code = `await _pi.servo(${value_gpio}, ${value_pulsewidth});\n`;
return code;
};
Blockly.Python['ugj_servo'] = function (block) {
@ -621,7 +621,7 @@ Blockly.JavaScript['ugj_pwm'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var value_pwm_frequency = Blockly.JavaScript.valueToCode(block, 'pwm_frequency', Blockly.JavaScript.ORDER_ATOMIC);
var value_pwm_duty_cycle = Blockly.JavaScript.valueToCode(block, 'pwm_duty_cycle', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.pwm(${value_gpio}, ${value_pwm_frequency}, ${value_pwm_duty_cycle});\n`;
var code = `await _pi.pwm(${value_gpio}, ${value_pwm_frequency}, ${value_pwm_duty_cycle});\n`;
return code;
};
Blockly.Python['ugj_pwm'] = function (block) {
@ -678,9 +678,9 @@ Blockly.JavaScript['ugj_serial_open'] = function (block) {
// var value_tty = Blockly.JavaScript.valueToCode(block, 'tty', Blockly.JavaScript.ORDER_ATOMIC);
var dropdown_baud = block.getFieldValue('baud');
Blockly.JavaScript.provideFunction_(
'require_gpio', [`const pi = require('@ocogeclub/` + elutil.gpio_backend + `');`]
'require_gpio', [`const _pi = require('@ocogeclub/` + elutil.gpio_backend + `');`]
);
var code = `await pi.serial_open('/dev/serial0', ${dropdown_baud});\n`;
var code = `await _pi.serial_open('/dev/serial0', ${dropdown_baud});\n`;
return code;
};
Blockly.Python['ugj_serial_open'] = function (block) {
@ -711,7 +711,7 @@ Blockly.Blocks['ugj_serial_close'] = {
}
};
Blockly.JavaScript['ugj_serial_close'] = function (block) {
var code = 'pi.serial_close();\n';
var code = 'await _pi.serial_close();\n';
return code;
};
Blockly.Python['ugj_serial_close'] = function (block) {
@ -746,7 +746,7 @@ Blockly.Blocks['ugj_serial_write'] = {
};
Blockly.JavaScript['ugj_serial_write'] = function (block) {
var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.serial_write(${value_data});\n`;
var code = `await _pi.serial_write(${value_data});\n`;
return code;
};
Blockly.Python['ugj_serial_write'] = function (block) {
@ -781,7 +781,7 @@ Blockly.Blocks['ugj_serial_read'] = {
};
Blockly.JavaScript['ugj_serial_read'] = function (block) {
var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.serial_read(${value_count})`;
var code = `await _pi.serial_read(${value_count})`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.Python['ugj_serial_read'] = function (block) {
@ -818,9 +818,9 @@ Blockly.Blocks['ugj_i2c_open'] = {
Blockly.JavaScript['ugj_i2c_open'] = function (block) {
var value_i2c_address = Blockly.JavaScript.valueToCode(block, 'i2c_address', Blockly.JavaScript.ORDER_ATOMIC);
Blockly.JavaScript.provideFunction_(
'require_gpio', [`const pi = require('@ocogeclub/` + elutil.gpio_backend + `');`]
'require_gpio', [`const _pi = require('@ocogeclub/` + elutil.gpio_backend + `');`]
);
var code = `console.log('i2c_hand='+pi.i2c_open(${elutil.i2c_bus}, ${value_i2c_address}));\n`;
var code = `await _pi.i2c_open(${elutil.i2c_bus}, ${value_i2c_address});\n`;
return code;
};
Blockly.Python['ugj_i2c_open'] = function (block) {
@ -850,7 +850,7 @@ Blockly.Blocks['ugj_i2c_close'] = {
}
};
Blockly.JavaScript['ugj_i2c_close'] = function (block) {
var code = `await pi.i2c_close();\n`;
var code = `await _pi.i2c_close();\n`;
return code;
};
Blockly.Python['ugj_i2c_close'] = function (block) {
@ -891,7 +891,7 @@ Blockly.Blocks['ugj_i2c_write_byte_data'] = {
Blockly.JavaScript['ugj_i2c_write_byte_data'] = function (block) {
var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC);
var value_byte_val = Blockly.JavaScript.valueToCode(block, 'byte_val', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.i2c_write_byte_data(${value_reg}, ${value_byte_val});\n`;
var code = `await _pi.i2c_write_byte_data(${value_reg}, ${value_byte_val});\n`;
return code;
};
Blockly.Python['ugj_i2c_write_byte_data'] = function (block) {
@ -927,7 +927,7 @@ Blockly.Blocks['ugj_i2c_read_byte_data'] = {
};
Blockly.JavaScript['ugj_i2c_read_byte_data'] = function (block) {
var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.i2c_read_byte_data(${value_reg})\n`;
var code = `await _pi.i2c_read_byte_data(${value_reg})\n`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.Python['ugj_i2c_read_byte_data'] = function (block) {
@ -966,7 +966,7 @@ Blockly.Blocks['ugj_i2c_write_i2c_block_data'] = {
Blockly.JavaScript['ugj_i2c_write_i2c_block_data'] = function (block) {
var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC);
var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.i2c_write_i2c_block_data (${value_reg}, ${value_data});`;
var code = `await _pi.i2c_write_i2c_block_data (${value_reg}, ${value_data});`;
return code;
};
Blockly.Python['ugj_i2c_write_i2c_block_data'] = function (block) {
@ -1003,7 +1003,7 @@ Blockly.Blocks['ugj_i2c_read_device'] = {
};
Blockly.JavaScript['ugj_i2c_read_device'] = function (block) {
var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.i2c_read_device(${value_count})`;
var code = `await _pi.i2c_read_device(${value_count})`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.Python['ugj_i2c_read_device'] = function (block) {
@ -1038,7 +1038,7 @@ Blockly.Blocks['ugj_i2c_write_device'] = {
};
Blockly.JavaScript['ugj_i2c_write_device'] = function (block) {
var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC);
var code = `await pi.i2c_write_device(${value_data})\n`;
var code = `await _pi.i2c_write_device(${value_data})\n`;
return code;
};
Blockly.Python['ugj_i2c_write_device'] = function (block) {
@ -1094,19 +1094,19 @@ Blockly.JavaScript['ugj_bme280'] = function (block) {
var variable_hum = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('hum'), Blockly.Variables.NAME_TYPE);
var variable_pres = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('pres'), Blockly.Variables.NAME_TYPE);
Blockly.JavaScript.provideFunction_(
'require_bme280', [`const bme280 = require('@ocogeclub/bme280');`]
'require_bme280', [`const _bme280 = require('@ocogeclub/bme280');`]
);
var code = [
`const options = {`,
` i2cBusNo: ${elutil.i2c_bus},`,
` i2cAddress: ${value_address}`,
`};`,
`await bme280.init(options);`,
`let thp = await bme280.readSensorData();`,
`${variable_temp} = Math.round(thp.temperature_C * 10) / 10;`,
`${variable_hum} = Math.round(thp.humidity * 10) / 10;`,
`${variable_pres} = Math.round(thp.pressure_hPa);`,
`await bme280.cancel();`,
`await _bme280.init(options);`,
`let _thp = await _bme280.readSensorData();`,
`${variable_temp} = Math.round(_thp.temperature_C * 10) / 10;`,
`${variable_hum} = Math.round(_thp.humidity * 10) / 10;`,
`${variable_pres} = Math.round(_thp.pressure_hPa);`,
`await _bme280.cancel();`,
``
].join('\n');
return code;
@ -1159,9 +1159,9 @@ Blockly.Blocks['ugj_gesture_init'] = {
Blockly.JavaScript['ugj_gesture_init'] = function (block) {
var value_i2c_addr = Blockly.JavaScript.valueToCode(block, 'i2c_addr', Blockly.JavaScript.ORDER_ATOMIC);
Blockly.JavaScript.provideFunction_(
'require_paj7620', [`const paj7620 = require('@ocogeclub/paj7620');`]
'require_paj7620', [`const _paj7620 = require('@ocogeclub/paj7620');`]
);
var code = `await paj7620.init(${elutil.i2c_bus}, ${value_i2c_addr});
var code = `await _paj7620.init(${elutil.i2c_bus}, ${value_i2c_addr});
`;
return code;
};
@ -1190,7 +1190,7 @@ Blockly.Blocks['ugj_gesture_read'] = {
}
};
Blockly.JavaScript['ugj_gesture_read'] = function (block) {
var code = 'await paj7620.return_gesture()';
var code = 'await _paj7620.return_gesture()';
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.Python['ugj_gesture_read'] = function (block) {
@ -1218,7 +1218,7 @@ Blockly.Blocks['ugj_gesture_stop'] = {
}
};
Blockly.JavaScript['ugj_gesture_stop'] = function (block) {
var code = 'await paj7620.stop();\n';
var code = 'await _paj7620.stop();\n';
return code;
};
Blockly.Python['ugj_gesture_stop'] = function (block) {
@ -1366,8 +1366,30 @@ Blockly.Python['ugj_grideye_stop'] = function (block) {
var with_landmark;
var ugjFaceapiDefinition = {
"type": "ugj_faceapi",
"message0": "%{BKY_UGJ_FACEAPI_TITLE}",
"message0": "TensorFlowによる顔検出を秒に %1 回実行: %2 ランドマークを検出 %3 %4",
"args0": [
{
"type": "field_dropdown",
"name": "interval_sec",
"options": [
[
"1",
"1000"
],
[
"2",
"500"
],
[
"5",
"200"
],
[
"10",
"100"
]
]
},
{
"type": "field_checkbox",
"name": "with_landmark",
@ -1381,8 +1403,7 @@ var ugjFaceapiDefinition = {
"name": "do"
}
],
"inputsInline": true,
"tooltip": "%{BKY_UGJ_FACEAPI_TOOLTIP}",
"tooltip": "TensorFlow とFaceAPI をロードし、顔検出をできるようにします。",
"helpUrl": "",
"style": "multimedia_blocks"
};
@ -1392,11 +1413,13 @@ Blockly.Blocks['ugj_faceapi'] = {
}
};
Blockly.JavaScript['ugj_faceapi'] = function (block) {
var dropdown_interval_sec = block.getFieldValue('interval_sec');
with_landmark = block.getFieldValue('with_landmark') == 'TRUE';
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
`require('@tensorflow/tfjs-node');`,
`const faceapi = require('@vladmandic/face-api/dist/face-api.node.js');`,
`const _faceapi = require('@vladmandic/face-api/dist/face-api.node.js');`,
`const _interval_sec = ${dropdown_interval_sec};`,
statements_do,
''
].join('\n');
@ -1416,10 +1439,10 @@ Blockly.Blocks['ugj_face_init'] = {
};
Blockly.JavaScript['ugj_face_init'] = function (block) {
var code = [
"const videoEl = document.getElementById('subdisplay');",
`const displaySize = { width: videoEl.width, height: videoEl.height };`,
"const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: displaySize });",
"videoEl.srcObject = stream;",
"const _videoEl = document.getElementById('subdisplay');",
`const _displaySize = { width: videoEl.width, height: videoEl.height };`,
"const _stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: _displaySize });",
"_videoEl.srcObject = _stream;",
""
].join('\n');
return code;
@ -1438,12 +1461,12 @@ Blockly.Blocks['ugj_face_display'] = {
};
Blockly.JavaScript['ugj_face_display'] = function (block) {
var code = [
"videoEl.style.display = 'inline-block';",
`const overlay = document.createElement('canvas');`,
`overlay.setAttribute('width', videoEl.width);`,
`overlay.setAttribute('height', videoEl.height);`,
`overlay.className = 'subdisplay';`,
`document.getElementById('display_area').appendChild(overlay);`,
"_videoEl.style.display = 'inline-block';",
`const _overlay = document.createElement('canvas');`,
`_overlay.setAttribute('width', _videoEl.width);`,
`_overlay.setAttribute('height', _videoEl.height);`,
`_overlay.className = 'subdisplay';`,
`document.getElementById('display_area').appendChild(_overlay);`,
""
].join('\n');
return code;
@ -1472,41 +1495,41 @@ Blockly.JavaScript['ugj_face_detect'] = function (block) {
var variable_w = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('w'), Blockly.Variables.NAME_TYPE);
var variable_h = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('h'), Blockly.Variables.NAME_TYPE);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code_model = `await faceapi.nets.tinyFaceDetector.load('${ugj_const.library_path}models/');`;
if (with_landmark) { code_model += `\nawait faceapi.nets.faceLandmark68TinyNet.load('${ugj_const.library_path}models/');`; }
var code_detect_face = " let result = await faceapi.detectSingleFace(videoEl, options)"
var code_model = `await _faceapi.nets.tinyFaceDetector.load('${ugj_const.library_path}models/');`;
if (with_landmark) { code_model += `\nawait _faceapi.nets.faceLandmark68TinyNet.load('${ugj_const.library_path}models/');`; }
var code_detect_face = " let _result = await _faceapi.detectSingleFace(_videoEl, _options)"
if (with_landmark) { code_detect_face += `.withFaceLandmarks(true);` }
else { code_detect_face += `;` }
var code_rect;
if (with_landmark) {
code_rect = [
` ${variable_x} = Math.round(result.detection.box.x);`,
` ${variable_y} = Math.round(result.detection.box.y);`,
` ${variable_w} = Math.round(result.detection.box.width);`,
` ${variable_h} = Math.round(result.detection.box.height);`,
` ${variable_x} = Math.round(_result.detection.box.x);`,
` ${variable_y} = Math.round(_result.detection.box.y);`,
` ${variable_w} = Math.round(_result.detection.box.width);`,
` ${variable_h} = Math.round(_result.detection.box.height);`,
].join('\n');
} else {
code_rect = [
` ${variable_x} = Math.round(result.box.x);`,
` ${variable_y} = Math.round(result.box.y);`,
` ${variable_w} = Math.round(result.box.width);`,
` ${variable_h} = Math.round(result.box.height);`,
` ${variable_x} = Math.round(_result.box.x);`,
` ${variable_y} = Math.round(_result.box.y);`,
` ${variable_w} = Math.round(_result.box.width);`,
` ${variable_h} = Math.round(_result.box.height);`,
].join('\n');
}
var code = [
code_model,
"const options = new faceapi.TinyFaceDetectorOptions({ inputSize: 128, scoreThreshold : 0.3 });",
`const onPlay = async () => {`,
// ` const detectInterval = setInterval(async () => {`,
"const _options = new _faceapi.TinyFaceDetectorOptions({ inputSize: 128, scoreThreshold : 0.3 });",
// `const _onPlay = async () => {`,
` const _detectInterval = setInterval(async () => {`,
code_detect_face,
" if (result) {",
" if (_result) {",
code_rect,
statements_do,
" }",
// ` }, 500);`,
" setTimeout(() => onPlay())",
"}",
"videoEl.onplay = onPlay;",
` }, _interval_sec);`,
// " setTimeout(() => onPlay())",
// "}",
// "_videoEl.onplay = _onPlay;",
""
].join('\n');
return code;
@ -1525,11 +1548,11 @@ Blockly.Blocks['ugj_face_drawrect'] = {
}
};
Blockly.JavaScript['ugj_face_drawrect'] = function (block) {
var code_draw = ` faceapi.draw.drawDetections(overlay, resizedDetections);`;
var code_draw = ` _faceapi.draw.drawDetections(_overlay, _resizedDetections);`;
if (with_landmark) { code_draw += `\n faceapi.draw.drawFaceLandmarks(overlay, resizedDetections);`; }
var code = [
` faceapi.matchDimensions(overlay, displaySize);`,
` const resizedDetections = faceapi.resizeResults(result, displaySize);`,
` _faceapi.matchDimensions(_overlay, _displaySize);`,
` const _resizedDetections = _faceapi.resizeResults(_result, _displaySize);`,
code_draw,
""
].join('\n');
@ -1599,9 +1622,9 @@ Blockly.Blocks['ugj_canvas_init'] = {
};
Blockly.JavaScript['ugj_canvas_init'] = function (block) {
var code = [
`let canvas = document.getElementById('gcanvas');`,
`canvas.style.display = 'inline-block';`,
"let ctx = canvas.getContext('2d');",
`let _canvas = document.getElementById('gcanvas');`,
`_canvas.style.display = 'inline-block';`,
"let _ctx = _canvas.getContext('2d');",
''
].join('\n');
@ -1649,9 +1672,9 @@ Blockly.Blocks['ugj_canvas_loadimg'] = {
Blockly.JavaScript['ugj_canvas_loadimg'] = function (block) {
var value_imgfilename = Blockly.JavaScript.valueToCode(block, 'imgfilename', Blockly.JavaScript.ORDER_ATOMIC);
var code = [
`let img = new Image();`,
`img.src = '${ugj_const.doc_root}' + ${value_imgfilename};`,
`img.onload = () => ctx.drawImage(img,0,0);`,
`let _img = new Image();`,
`_img.src = '${ugj_const.doc_root}' + ${value_imgfilename};`,
`_img.onload = () => _ctx.drawImage(img,0,0);`,
// `ugj_canvasImg('${ugj_const.doc_root}' + ${value_imgfilename});`,
''
].join('\n');
@ -1689,7 +1712,7 @@ Blockly.JavaScript['ugj_canvas_clearrect'] = function (block) {
var value_y = Blockly.JavaScript.valueToCode(block, 'y', Blockly.JavaScript.ORDER_ATOMIC);
var value_w = Blockly.JavaScript.valueToCode(block, 'w', Blockly.JavaScript.ORDER_ATOMIC);
var value_h = Blockly.JavaScript.valueToCode(block, 'h', Blockly.JavaScript.ORDER_ATOMIC);
var code = `ctx.clearRect(${value_x},${value_y}, ${value_w}, ${value_h});\n`;
var code = `_ctx.clearRect(${value_x},${value_y}, ${value_w}, ${value_h});\n`;
return code;
};
@ -1710,7 +1733,7 @@ Blockly.Blocks['ugj_canvas_width'] = {
}
};
Blockly.JavaScript['ugj_canvas_width'] = function (block) {
var code = 'canvas.width';
var code = '_canvas.width';
return [code, Blockly.JavaScript.ORDER_NONE];
};
/*********************** */
@ -1729,7 +1752,7 @@ Blockly.Blocks['ugj_canvas_height'] = {
}
};
Blockly.JavaScript['ugj_canvas_height'] = function (block) {
var code = 'canvas.height';
var code = '_canvas.height';
return [code, Blockly.JavaScript.ORDER_NONE];
};
@ -1768,11 +1791,11 @@ Blockly.JavaScript['ugj_canvas_drawcircle'] = function (block) {
var value_r = Blockly.JavaScript.valueToCode(block, 'r', Blockly.JavaScript.ORDER_ATOMIC);
var value_color = Blockly.JavaScript.valueToCode(block, 'color', Blockly.JavaScript.ORDER_ATOMIC);
var code = [
'ctx.beginPath();',
`ctx.arc(${value_x}, ${value_y}, ${value_r}, 0, Math.PI*2);`,
`ctx.fillStyle = ${value_color};`,
'ctx.fill();',
'ctx.closePath();',
'_ctx.beginPath();',
`_ctx.arc(${value_x}, ${value_y}, ${value_r}, 0, Math.PI*2);`,
`_ctx.fillStyle = ${value_color};`,
'_ctx.fill();',
'_ctx.closePath();',
''
].join('\n');
return code;
@ -1815,11 +1838,11 @@ Blockly.JavaScript['ugj_canvas_drawrect'] = function (block) {
var value_h = Blockly.JavaScript.valueToCode(block, 'h', Blockly.JavaScript.ORDER_ATOMIC);
var value_color = Blockly.JavaScript.valueToCode(block, 'color', Blockly.JavaScript.ORDER_ATOMIC);
var code = [
`ctx.beginPath();`,
`ctx.rect(${value_x}, ${value_y}, ${value_w}, ${value_h});`,
`ctx.fillStyle = ${value_color};`,
`ctx.fill();`,
`ctx.closePath();`,
`_ctx.beginPath();`,
`_ctx.rect(${value_x}, ${value_y}, ${value_w}, ${value_h});`,
`_ctx.fillStyle = ${value_color};`,
`_ctx.fill();`,
`_ctx.closePath();`,
''
].join('\n');
return code;
@ -1914,7 +1937,7 @@ Blockly.JavaScript['ugj_draw_grideyedata'] = function (block) {
var colour_color_low = block.getFieldValue('color_low');
var value_temp_low = Blockly.JavaScript.valueToCode(block, 'temp_low', Blockly.JavaScript.ORDER_ATOMIC);
var functionName = Blockly.JavaScript.provideFunction_(
'mapVal',
'_mapVal',
['const ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = (val, inMin, inMax, outMin, outMax) => {',
`return (val - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;`,
'}'
@ -2132,8 +2155,8 @@ Blockly.JavaScript['ugj_event_key'] = function (block) {
var dropdown_updown = block.getFieldValue('updown');
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
`document.addEventListener('${dropdown_updown}', async (e) => {`,
` ${variable_key} = e.key;`,
`document.addEventListener('${dropdown_updown}', async (_e) => {`,
` ${variable_key} = _e.key;`,
statements_do,
`}, false);`,
''
@ -2201,14 +2224,14 @@ Blockly.JavaScript['ugj_socket'] = function (block) {
var variable_data = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('data'), Blockly.Variables.NAME_TYPE);
var statements_data = Blockly.JavaScript.statementToCode(block, 'data');
Blockly.JavaScript.provideFunction_(
'require_net', [`const net = require('net');`]
'require_net', [`const _net = require('net');`]
);
var code = [
`var client = net.connect(${value_port}, ${value_host});`,
`client.on('connect', async ()=>{`,
`var _client = _net.connect(${value_port}, ${value_host});`,
`_client.on('connect', async ()=>{`,
statements_connect,
`}).on('data', async data=>{`,
`${variable_data} = data.toString('utf-8', 0, data.length);`,
`}).on('data', async _data=>{`,
`${variable_data} = _data.toString('utf-8', 0, _data.length);`,
statements_data,
`}).on('close', ()=>{`,
`console.log('Connection closed.');`,
@ -2238,7 +2261,7 @@ Blockly.Blocks['ugj_socket_write'] = {
Blockly.JavaScript['ugj_socket_write'] = function (block) {
var value_cmd = Blockly.JavaScript.valueToCode(block, 'cmd', Blockly.JavaScript.ORDER_ATOMIC);
var code = [
`client.write(${value_cmd});`,
`_client.write(${value_cmd});`,
''
].join('\n');
return code;
@ -2272,16 +2295,16 @@ Blockly.JavaScript['ugj_network_httpserver'] = function (block) {
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var value_response = Blockly.JavaScript.valueToCode(block, 'response', Blockly.JavaScript.ORDER_ATOMIC);
Blockly.JavaScript.provideFunction_(
'require_http', [`const http = require('http');`]
'require_http', [`const _http = require('http');`]
);
var code = [
`let req, res;`,
`http.createServer(async (req, res) => {`,
`res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });`,
`${variable_url} = req.url;`,
`let _req, _res;`,
`_http.createServer(async (_req, _res) => {`,
`_res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });`,
`${variable_url} = _req.url;`,
statements_do,
// `res.write('<!DOCTYPE html><html lang="ja"><head><meta charset="UTF-8"></head><body>');`,
`res.end(${value_response});`,
// `_res.write('<!DOCTYPE html><html lang="ja"><head><meta charset="UTF-8"></head><body>');`,
`_res.end(${value_response});`,
`}).listen(3000);`,
''
].join('\n');
@ -2310,7 +2333,7 @@ Blockly.Blocks['ugj_network_axios_geturl'] = {
Blockly.JavaScript['ugj_network_axios_geturl'] = function (block) {
var value_url = Blockly.JavaScript.valueToCode(block, 'url', Blockly.JavaScript.ORDER_ATOMIC);
var functionName = Blockly.JavaScript.provideFunction_(
'getUrl',
'_getUrl',
[
`const axios = require('axios');`,
'const ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = async url => {',
@ -2359,28 +2382,28 @@ Blockly.JavaScript['ugj_network_sendmail'] = function (block) {
var value_subject = Blockly.JavaScript.valueToCode(block, 'subject', Blockly.JavaScript.ORDER_ATOMIC);
var value_text = Blockly.JavaScript.valueToCode(block, 'text', Blockly.JavaScript.ORDER_ATOMIC);
Blockly.JavaScript.provideFunction_(
'require_sendmail', [`const nodemailer = require('nodemailer');`]
'require_sendmail', [`const _nodemailer = require('nodemailer');`]
);
var code = [
`let smtp = nodemailer.createTransport({`,
`let _smtp = _nodemailer.createTransport({`,
`host: '192.168.0.201',`,
`port: 25`,
`});`,
`let message = {`,
`let _message = {`,
`from: 'no-reply@oc.x0.to',`,
`to: ${value_to},`,
`subject: ${value_subject},`,
`text: ${value_text}`,
`};`,
`try{`,
`smtp.sendMail(message, function(error, info){`,
`if(error){`,
`alert('送信エラー:' + error.message);`,
`_smtp.sendMail(_message, function(_error, _info){`,
`if(_error){`,
`alert('送信エラー:' + _error.message);`,
`return;`,
`}`,
`console.log('send successfully');`,
`});`,
`} catch(e) {alert('Error: ',e);}`,
`} catch(_e) {alert('Error: ',_e);}`,
''
].join('\n');
return code;
@ -2423,17 +2446,17 @@ Blockly.Blocks['ugj_webchat'] = {
};
Blockly.JavaScript['ugj_webchat'] = function (block) {
var code = [
`const blackboard = document.getElementById('blackboard');`,
"blackboard.style.display = 'inline-block';",
`const inputForm = document.getElementById('inputForm');`,
"inputForm.style.display = 'inline-block';",
`const inputBox = document.getElementById('inputBox');`,
"inputBox.focus();",
"const remoteVideo = document.getElementById('maindisplay');",
`remoteVideo.style.display = 'inline-block';`,
"const localVideo = document.getElementById('subdisplay');",
`localVideo.style.display = 'inline-block';`,
`var localStream;`,
`const _blackboard = document.getElementById('blackboard');`,
"_blackboard.style.display = 'inline-block';",
`const _inputForm = document.getElementById('inputForm');`,
"_inputForm.style.display = 'inline-block';",
`const _inputBox = document.getElementById('inputBox');`,
"_inputBox.focus();",
"const _remoteVideo = document.getElementById('maindisplay');",
`_remoteVideo.style.display = 'inline-block';`,
"const _localVideo = document.getElementById('subdisplay');",
`_localVideo.style.display = 'inline-block';`,
`var _localStream;`,
''
].join('\n');
return code;
@ -2454,11 +2477,11 @@ Blockly.Blocks['ugj_getusermedia'] = {
Blockly.JavaScript['ugj_getusermedia'] = function (block) {
var code = [
`navigator.mediaDevices.getUserMedia({video: true, audio: false})`,
' .then(stream => {',
' localVideo.srcObject = stream;',
' localStream = stream;',
' }).catch( error => {',
' console.error(\'mediaDevice.getUserMedia() error:\', error);',
' .then(_stream => {',
' _localVideo.srcObject = _stream;',
' _localStream = _stream;',
' }).catch( _error => {',
' console.error(\'mediaDevice.getUserMedia() error:\', _error);',
' return;',
' });',
''
@ -2489,24 +2512,24 @@ Blockly.JavaScript['ugj_skyway_newpeer'] = function (block) {
// var text_name = block.getFieldValue('NAME');
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
`const peer = new Peer(${variable_my_id}, {`,
`const _peer = new Peer(${variable_my_id}, {`,
" key: window.__SKYWAY_KEY__,",
" debug: 3",
"});",
"peer.on('open', () => {",
"_peer.on('open', () => {",
// ' ugj_blackboardWrite(`SkyWay: "${peer.id}" OK`)',
`${variable_my_id} = peer.id;`,
"inputForm.addEventListener('submit', onMakeCall);",
`${variable_my_id} = _peer.id;`,
"_inputForm.addEventListener('submit', _onMakeCall);",
statements_do,
"});",
"peer.on('error', err => alert(err.message));",
"const onMakeCall = e => {",
" e.preventDefault();",
" const call = peer.call(inputBox.value, localStream);",
" setupCallEventHandlers(call);",
" const connect = peer.connect(inputBox.value);",
" setupConnectEventHandlers(connect);",
" inputBox.value = '';",
"_peer.on('error', _err => alert(_err.message));",
"const _onMakeCall = _e => {",
" _e.preventDefault();",
" const _call = _peer.call(_inputBox.value, _localStream);",
" setupCallEventHandlers(_call);",
" const _connect = peer.connect(_inputBox.value);",
" setupConnectEventHandlers(_connect);",
" _inputBox.value = '';",
"}",
''
].join('\n');
@ -2527,12 +2550,12 @@ Blockly.Blocks['ugj_skyway_called'] = {
};
Blockly.JavaScript['ugj_skyway_called'] = function (block) {
var code = [
"peer.on('call', call => {",
" call.answer(localStream);",
" setupCallEventHandlers(call);",
"_peer.on('call', _call => {",
" _call.answer(_localStream);",
" _setupCallEventHandlers(_call);",
"});",
"peer.on('connection', connect => {",
" setupConnectEventHandlers(connect);",
"_peer.on('connection', _connect => {",
" _setupConnectEventHandlers(_connect);",
"});",
''
].join('\n');
@ -2558,10 +2581,10 @@ Blockly.JavaScript['ugj_skyway_events'] = function (block) {
var variable_remote_id = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('remote_id'), Blockly.Variables.NAME_TYPE);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
"const setupCallEventHandlers = call => call.on('stream', stream => remoteVideo.srcObject = stream);",
"const setupConnectEventHandlers = connect => {",
" inputForm.removeEventListener('submit', onMakeCall);",
` ${variable_remote_id} = connect.remoteId;`,
"const _setupCallEventHandlers = _call => _call.on('stream', _stream => _remoteVideo.srcObject = _stream);",
"const _setupConnectEventHandlers = _connect => {",
" _inputForm.removeEventListener('submit', _onMakeCall);",
` ${variable_remote_id} = _connect.remoteId;`,
statements_do,
"}",
''
@ -2586,8 +2609,8 @@ Blockly.Blocks['ugj_skyway_eventopen'] = {
Blockly.JavaScript['ugj_skyway_eventopen'] = function (block) {
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
" connect.on('open', () => {",
" inputForm.addEventListener('submit', onSendMsg)",
" _connect.on('open', () => {",
" _inputForm.addEventListener('submit', _onSendMsg)",
statements_do,
" });",
''
@ -2615,8 +2638,8 @@ Blockly.JavaScript['ugj_skyway_eventdata'] = function (block) {
var variable_data = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('data'), Blockly.Variables.NAME_TYPE);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
" connect.on('data', data => {",
` ${variable_data} = data;`,
" _connect.on('data', _data => {",
` ${variable_data} = _data;`,
statements_do,
" });",
''
@ -2644,10 +2667,10 @@ Blockly.JavaScript['ugj_skyway_eventsend'] = function (block) {
var variable_data = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('data'), Blockly.Variables.NAME_TYPE);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
" const onSendMsg = () => {",
` ${variable_data} = inputBox.value;`,
` connect.send(${variable_data});`,
" inputBox.value = '';",
" const _onSendMsg = () => {",
` ${variable_data} = _inputBox.value;`,
` _connect.send(${variable_data});`,
" _inputBox.value = '';",
statements_do,
" }",
''
@ -2683,9 +2706,9 @@ Blockly.JavaScript['ugj_file_readsync'] = function (block) {
var dropdown_encoding = block.getFieldValue('encoding');
let filepath = ugj_const.doc_root + value_filename.replace(/\'/g, '');
Blockly.JavaScript.provideFunction_(
'require_fs', [`const fs = require('fs');`]
'require_fs', [`const _fs = require('fs');`]
);
var code = `fs.readFileSync('${filepath}', '${dropdown_encoding}')`;
var code = `_fs.readFileSync('${filepath}', '${dropdown_encoding}')`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
@ -2787,22 +2810,22 @@ Blockly.JavaScript['ugj_event_answer'] = function (block) {
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
`ugj_fukidashi(${value_question}, 0);`,
`inputForm = document.getElementById('inputForm');`,
`inputBox = document.getElementById('inputBox');`,
"inputForm.style.display = 'inline-block'",
"inputBox.focus();",
"const inputFunc = async () => { ",
" if (inputBox.value.length > 0) {",
` ${variable_answer} = inputBox.value;`,
' inputForm.style.display = "none"',
" inputBox.value = '';",
`_inputForm = document.getElementById('inputForm');`,
`_inputBox = document.getElementById('inputBox');`,
"_inputForm.style.display = 'inline-block'",
"_inputBox.focus();",
"const _inputFunc = async () => { ",
" if (_inputBox.value.length > 0) {",
` ${variable_answer} = _inputBox.value;`,
' _inputForm.style.display = "none"',
" _inputBox.value = '';",
" document.getElementById('canvas').getContext('2d').clearRect(ugj_fdRecentBox.x,ugj_fdRecentBox.y,ugj_fdRecentBox.w,ugj_fdRecentBox.h);",
statements_do,
" console.log('Removing listener...');",
" inputForm.removeEventListener('submit', inputFunc );",
" _inputForm.removeEventListener('submit', _inputFunc );",
" }",
"}",
"inputForm.addEventListener('submit', inputFunc );",
"_inputForm.addEventListener('submit', _inputFunc );",
''
].join('\n');
return code;
@ -2863,14 +2886,14 @@ Blockly.JavaScript['ugj_spawn'] = function (block) {
var variable_data = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('data'), Blockly.Variables.NAME_TYPE);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
`let child = require('child_process').spawn(${value_childprocess});`,
`elutil.addChild(child);`,
"child.stderr.on('data', data => { console.error(data.toString()) })",
"child.stdout.on('data', async data => {",
`${variable_data} = data.toString();`,
`let _child = require('child_process').spawn(${value_childprocess});`,
`elutil.addChild(_child);`,
"_child.stderr.on('data', _data => { console.error(_data.toString()) })",
"_child.stdout.on('data', async _data => {",
`${variable_data} = _data.toString();`,
statements_do,
"})",
"child.on('close', (code, signal) => { if (code !== 0) { console.error(`process exited with code ${code}, signal ${signal}`)}",
"_child.on('close', (_code, _signal) => { if (_code !== 0) { console.error(`process exited with code ${_code}, signal ${_signal}`)}",
"})",
''
].join("\n");
@ -3114,7 +3137,7 @@ Blockly.JavaScript['ugj_blackboard_write'] = function (block) {
var dropdown_style = block.getFieldValue('style');
var dropdown_line = block.getFieldValue('line');
var funcAppendDivName = Blockly.JavaScript.provideFunction_(
'appendDiv',
'_appendDiv',
['const ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = termEl => {',
`let el = document.createElement('div');`,
`termEl.appendChild(el);`,
@ -3122,7 +3145,7 @@ Blockly.JavaScript['ugj_blackboard_write'] = function (block) {
'}']
);
var funcTermWriteName = Blockly.JavaScript.provideFunction_(
'blackboardWrite',
'_blackboardWrite',
['const ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = (text, color, style, line) => {',
`let termEl = document.getElementById('blackboard')`,
'let el = null;',
@ -3217,7 +3240,7 @@ Blockly.Blocks['ugj_sleep'] = {
Blockly.JavaScript['ugj_sleep'] = function (block) {
var value_sec = Blockly.JavaScript.valueToCode(block, 'sec', Blockly.JavaScript.ORDER_ATOMIC);
var functionName = Blockly.JavaScript.provideFunction_(
'sleep',
'_sleep',
['const ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = sec =>',
'new Promise(r => setTimeout(r, sec * 1000));']
);
@ -3333,7 +3356,7 @@ Blockly.JavaScript['ugj_set_interval'] = function (block) {
var value_sec = Blockly.JavaScript.valueToCode(block, 'sec', Blockly.JavaScript.ORDER_ATOMIC);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
'let interval = setInterval( async () => {',
'let _interval = setInterval( async () => {',
statements_do,
`}, ${value_sec}*1000);`,
''
@ -3356,7 +3379,7 @@ Blockly.Blocks['ugj_special_clearinterval'] = {
}
};
Blockly.JavaScript['ugj_special_clearinterval'] = function (block) {
var code = 'clearInterval(interval);\n';
var code = 'clearInterval(_interval);\n';
return code;
};
/********+********/
@ -3383,7 +3406,7 @@ Blockly.Blocks['ugj_set_timeout'] = {
Blockly.JavaScript['ugj_set_timeout'] = function (block) {
var value_sec = Blockly.JavaScript.valueToCode(block, 'sec', Blockly.JavaScript.ORDER_ATOMIC);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = `let interval = setTimeout(async () => {\n${statements_do}}, ${value_sec}*1000);\n`;
var code = `let _interval = setTimeout(async () => {\n${statements_do}}, ${value_sec}*1000);\n`;
return code;
};
@ -3407,12 +3430,12 @@ Blockly.JavaScript['ugj_library_load'] = function (block) {
var value_lib = Blockly.JavaScript.valueToCode(block, 'lib', Blockly.JavaScript.ORDER_ATOMIC);
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
var code = [
`let scriptEl = document.createElement('script');`,
`scriptEl.onload = async ev => {`,
`let _scriptEl = document.createElement('script');`,
`_scriptEl.onload = async _ev => {`,
`${statements_do}`,
`};`,
`scriptEl.src = ${value_lib};`,
`document.getElementsByTagName('head')[0].appendChild(scriptEl);`,
`_scriptEl.src = ${value_lib};`,
`document.getElementsByTagName('head')[0].appendChild(_scriptEl);`,
''
].join('\n');
return code;