From a557c8f887756f0790a71b0b141f6569ac91af98 Mon Sep 17 00:00:00 2001 From: smilepmg Date: Wed, 9 Oct 2024 11:01:50 +0900 Subject: [PATCH] =?UTF-8?q?=ED=95=B8=EB=93=9C=EC=9D=B4=EB=85=B8=EC=97=90?= =?UTF-8?q?=20=EC=88=98=EC=96=B4=EB=B0=8F=20=EC=82=B0=EC=88=98=20=EB=B8=94?= =?UTF-8?q?=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blocks/hardware/block_handino.js | 397 ++++++++++++++++-- 1 file changed, 370 insertions(+), 27 deletions(-) diff --git a/src/playground/blocks/hardware/block_handino.js b/src/playground/blocks/hardware/block_handino.js index 96772f235f..a4585f7573 100644 --- a/src/playground/blocks/hardware/block_handino.js +++ b/src/playground/blocks/hardware/block_handino.js @@ -1,16 +1,31 @@ 'use strict'; const SERVO_PIN_NUMBERS = [12, 10, 9, 6, 2]; const HAND_MAX_ANGLE = 165; +const HAND_HALF_ANGLE = 60; const HAND_MIN_ANGLE = 0; const HAND_DELAY = 1000; const HAND_TYPE = { ROCK: 0, PAPER: 1, SCISSORS: 2, - THUMBSUP: 3, - PEACE: 4, - PROMISE: 5, - V: 6 + GESTURE_THUMBSUP: 3, + GESTURE_PEACE: 4, + GESTURE_PROMISE: 5, + GESTURE_V: 6, + LANGUAGE_ㄱ:7, + LANGUAGE_ㄴ:8, + LANGUAGE_ㄷ:9, + LANGUAGE_ㄹ:10, + LANGUAGE_ㅁ:11, + LANGUAGE_ㅂ:12, + LANGUAGE_ㅅ:13, + LANGUAGE_ㅇ:14, + LANGUAGE_ㅈ:15, + LANGUAGE_ㅊ:16, + LANGUAGE_ㅋ:17, + LANGUAGE_ㅌ:18, + LANGUAGE_ㅍ:19, + LANGUAGE_ㅎ:20, }; function Lerp(a, b, t) { @@ -30,6 +45,21 @@ function setFinger(idx, angle) }; } +function setFingerDuration(idx, startAngle, targetAngle, duration) +{ + var delayTime = duration*1000; + let func = (i, maxIdx) => { + sleep(i).then(() => { + setFinger(idx, Lerp(startAngle, targetAngle, i/maxIdx)); + }); + }; + + for (var i = 0; i <= delayTime; i+=delayTime/100) + { + func(i, delayTime); + } +} + function setHandType(type) { switch (type) @@ -59,7 +89,7 @@ function setHandType(type) setFinger(3, HAND_MIN_ANGLE); setFinger(4, HAND_MIN_ANGLE); }break; - case HAND_TYPE.THUMBSUP: + case HAND_TYPE.GESTURE_THUMBSUP: { setFinger(0, HAND_MAX_ANGLE); setFinger(1, HAND_MIN_ANGLE); @@ -67,7 +97,7 @@ function setHandType(type) setFinger(3, HAND_MIN_ANGLE); setFinger(4, HAND_MIN_ANGLE); }break; - case HAND_TYPE.PEACE: + case HAND_TYPE.GESTURE_PEACE: { setFinger(0, HAND_MAX_ANGLE); setFinger(1, HAND_MAX_ANGLE); @@ -75,7 +105,7 @@ function setHandType(type) setFinger(3, HAND_MIN_ANGLE); setFinger(4, HAND_MAX_ANGLE); }break; - case HAND_TYPE.PROMISE: + case HAND_TYPE.GESTURE_PROMISE: { setFinger(0, HAND_MIN_ANGLE); setFinger(1, HAND_MIN_ANGLE); @@ -83,7 +113,31 @@ function setHandType(type) setFinger(3, HAND_MIN_ANGLE); setFinger(4, HAND_MAX_ANGLE); }break; - case HAND_TYPE.V: + case HAND_TYPE.GESTURE_V: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㄱ: + { + setFinger(0, HAND_MAX_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MIN_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㄴ: + { + setFinger(0, HAND_MAX_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MIN_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㄷ: { setFinger(0, HAND_MIN_ANGLE); setFinger(1, HAND_MAX_ANGLE); @@ -91,9 +145,123 @@ function setHandType(type) setFinger(3, HAND_MIN_ANGLE); setFinger(4, HAND_MIN_ANGLE); }break; + case HAND_TYPE.LANGUAGE_ㄹ: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MAX_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅁ: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_HALF_ANGLE); + setFinger(2, HAND_HALF_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅂ: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MAX_ANGLE); + setFinger(4, HAND_MAX_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅅ: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅇ: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_MIN_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MAX_ANGLE); + setFinger(4, HAND_MAX_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅈ: + { + setFinger(0, HAND_MAX_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅊ: + { + setFinger(0, HAND_MAX_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MAX_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅋ: + { + setFinger(0, HAND_MAX_ANGLE); + setFinger(1, HAND_MIN_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅌ: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_MAX_ANGLE); + setFinger(2, HAND_MAX_ANGLE); + setFinger(3, HAND_MAX_ANGLE); + setFinger(4, HAND_HALF_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅍ: + { + setFinger(0, HAND_MIN_ANGLE); + setFinger(1, HAND_MIN_ANGLE); + setFinger(2, HAND_MIN_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; + case HAND_TYPE.LANGUAGE_ㅎ: + { + setFinger(0, HAND_MAX_ANGLE); + setFinger(1, HAND_MIN_ANGLE); + setFinger(2, HAND_MIN_ANGLE); + setFinger(3, HAND_MIN_ANGLE); + setFinger(4, HAND_MIN_ANGLE); + }break; } } +function setFingerNumber(n, duration) +{ + if (n <= 5) + { + for (var i = 0; i < 5; i++) + { + let angle = HAND_MIN_ANGLE; + if (i < n) + angle = HAND_MAX_ANGLE; + + setFingerDuration(i, HAND_HALF_ANGLE, angle, duration); + } + } + else + { + for (var i = 0; i < 5; i++) + { + let angle = HAND_MIN_ANGLE; + if (10 - n <= i) + angle = HAND_MAX_ANGLE; + + setFingerDuration(i, HAND_HALF_ANGLE, angle, duration); + } + } +} + function getRandomArbitraryInt(min, max) { return parseInt(Math.random() * (max - min) + min); } @@ -168,16 +336,20 @@ Entry.ArduinoZin.setLanguage = function() { arduino_ext_set_servo_hand_finger_updown: '%1번째 손가락 %2 %3', arduino_ext_set_servo_hand_play_scissors: '랜덤 가위보기하기 %1 %2', arduino_ext_set_servo_hand_finger_updown_same_time: '%1번째 %2번째 %3번째 %4번째 %5번째 손가락 %6 %7 %8', + arduino_ext_set_servo_hand_language: '수어 %1 %2', + arduino_ext_set_servo_hand_arithmetic: '산수 %1 %2 %3', }, }, en: { template: { arduino_ext_set_servo_hand_scissors: 'Rock Paper Scissors %1 %2', arduino_ext_set_servo_hand_count: 'number count %1 %2', - arduino_ext_set_servo_hand_gesture: 'hand gestur %1 %2', + arduino_ext_set_servo_hand_gesture: 'hand gesture %1 %2', arduino_ext_set_servo_hand_finger_updown: '%1 finger %2 %3', arduino_ext_set_servo_hand_play_scissors: 'play rock paper scissors %1 %2', arduino_ext_set_servo_hand_finger_updown_same_time: '%1st %2ed %3rd %4rd %5rd finger %6 %7 %8', + arduino_ext_set_servo_hand_language: 'hand language %1 %2', + arduino_ext_set_servo_hand_arithmetic: 'arithmetic %1 %2 %3', }, }, }; @@ -190,6 +362,8 @@ Entry.ArduinoZin.blockMenuBlocks = [ 'arduino_ext_set_servo_hand_finger_updown', 'arduino_ext_set_servo_hand_play_scissors', 'arduino_ext_set_servo_hand_finger_updown_same_time', + 'arduino_ext_set_servo_hand_language', + 'arduino_ext_set_servo_hand_arithmetic', ]; //region ArduinoZin 아두이노 확장모드 @@ -351,10 +525,10 @@ Entry.ArduinoZin.getBlocks = function() { { type: "Dropdown", options: [ - [ "따봉", HAND_TYPE.THUMBSUP ], - [ "피스", HAND_TYPE.PEACE ], - [ "약속", HAND_TYPE.PROMISE ], - [ "브이", HAND_TYPE.V ], + [ "따봉", HAND_TYPE.GESTURE_THUMBSUP ], + [ "피스", HAND_TYPE.GESTURE_PEACE ], + [ "약속", HAND_TYPE.GESTURE_PROMISE ], + [ "브이", HAND_TYPE.GESTURE_V ], ], fontSize: 11 }, @@ -367,7 +541,7 @@ Entry.ArduinoZin.getBlocks = function() { events: {}, def: { params: [ - HAND_TYPE.THUMBSUP, + HAND_TYPE.GESTURE_THUMBSUP, null, ], type: 'arduino_ext_set_servo_hand_gesture', @@ -447,7 +621,7 @@ Entry.ArduinoZin.getBlocks = function() { func(sprite, script) { const fingerType = script.getNumberValue('FINGER_TYPE', script); const fingerUpDown = script.getNumberValue('FINGER_UPDOWN', script); - let delayTimes = [100, 50, 25, 1]; + let delayTimes = [10, 2.5, 0.5, 0.1]; let startAngle = HAND_MIN_ANGLE; let targetAngle = HAND_MAX_ANGLE; if (fingerUpDown % 2 == 0) @@ -462,23 +636,14 @@ Entry.ArduinoZin.getBlocks = function() { } let delayTime = delayTimes[parseInt(fingerUpDown/2)]; const sq = Entry.hw.sendQueue; - return delayScriptCallReturn(script, HAND_DELAY+delayTime*delayTime, + return delayScriptCallReturn(script, HAND_DELAY+delayTime*1000, ()=>{ if (!sq.SET) { sq.SET = {}; } - - let func = (idx, maxIdx) => { - sleep(idx*delayTime).then(() => { - setFinger(fingerType, Lerp(startAngle, targetAngle, idx/maxIdx)); - }); - }; - - for (var i = 0; i <= delayTime; i++) - { - func(i, delayTime); - } + + setFingerDuration(fingerType, startAngle, targetAngle, delayTime); } ); @@ -691,6 +856,184 @@ Entry.ArduinoZin.getBlocks = function() { }, }, + arduino_ext_set_servo_hand_language: { + color: EntryStatic.colorSet.block.default.HARDWARE, + outerLine: EntryStatic.colorSet.block.darken.HARDWARE, + skeleton: 'basic', + statements: [], + params: [ + { + type: "Dropdown", + options: [ + [ "ㄱ", HAND_TYPE.LANGUAGE_ㄱ ], + [ "ㄴ", HAND_TYPE.LANGUAGE_ㄴ ], + [ "ㄷ", HAND_TYPE.LANGUAGE_ㄷ ], + [ "ㄹ", HAND_TYPE.LANGUAGE_ㄹ ], + [ "ㅁ", HAND_TYPE.LANGUAGE_ㅁ ], + [ "ㅂ", HAND_TYPE.LANGUAGE_ㅂ ], + [ "ㅅ", HAND_TYPE.LANGUAGE_ㅅ ], + [ "ㅇ", HAND_TYPE.LANGUAGE_ㅇ ], + [ "ㅈ", HAND_TYPE.LANGUAGE_ㅈ ], + [ "ㅊ", HAND_TYPE.LANGUAGE_ㅊ ], + [ "ㅋ", HAND_TYPE.LANGUAGE_ㅋ ], + [ "ㅌ", HAND_TYPE.LANGUAGE_ㅌ ], + [ "ㅍ", HAND_TYPE.LANGUAGE_ㅍ ], + [ "ㅎ", HAND_TYPE.LANGUAGE_ㅎ ], + ], + fontSize: 11 + }, + { + type: 'Indicator', + img: 'block_icon/hardware_icon.svg', + size: 12, + }, + ], + events: {}, + def: { + params: [ + HAND_TYPE.LANGUAGE_ㄱ, + null, + ], + type: 'arduino_ext_set_servo_hand_language', + }, + paramsKeyMap: { + HAND_TYPE: 0 + }, + class: 'ArduinoZin', + isNotFor: ['ArduinoZin'], + func(sprite, script) { + + const sq = Entry.hw.sendQueue; + return delayScriptCallReturn(script, HAND_DELAY, + ()=>{ + if (!sq.SET) { + sq.SET = {}; + } + + const handType = script.getNumberValue('HAND_TYPE', script); + setHandType(handType); + } + ); + }, + }, + arduino_ext_set_servo_hand_arithmetic: { + color: EntryStatic.colorSet.block.default.HARDWARE, + outerLine: EntryStatic.colorSet.block.darken.HARDWARE, + skeleton: 'basic', + statements: [], + params: [ + { + type: 'Block', + accept: 'string', + defaultType: 'number', + }, + { + type: "Dropdown", + options: [ + [ "+", 0 ], + [ "-", 1 ], + [ "x", 2 ], + [ "/", 3 ], + ], + fontSize: 11 + }, + { + type: 'Block', + accept: 'string', + defaultType: 'number', + }, + { + type: 'Indicator', + img: 'block_icon/hardware_icon.svg', + size: 12, + }, + ], + events: {}, + def: { + params: [ + 0, + 0, + 0, + null, + ], + type: 'arduino_ext_set_servo_hand_arithmetic', + }, + paramsKeyMap: { + OPRND1: 0, + OPTOR: 1, + OPRND2: 2 + }, + class: 'ArduinoZin', + isNotFor: ['ArduinoZin'], + func(sprite, script) { + const sq = Entry.hw.sendQueue; + + if (!script.isStart) { + if (!sq.SET) { + sq.SET = {}; + } + + const oprnd1 = script.getNumberValue('OPRND1', script); + const optor = script.getNumberValue('OPTOR', script); + const oprnd2 = script.getNumberValue('OPRND2', script); + let result = 0; + + switch (optor) + { + case 0: + result = oprnd1 + oprnd2; + break; + case 1: + result = oprnd1 - oprnd2; + break; + case 2: + result = oprnd1 * oprnd2; + break; + case 3: + result = oprnd1 / oprnd2; + break; + } + + // 음수 무시 + result = Math.abs(result.toFixed()); + let strResult = result.toString(); + + script.handCount = 0; + + setFinger(0, HAND_HALF_ANGLE); + setFinger(1, HAND_HALF_ANGLE); + setFinger(2, HAND_HALF_ANGLE); + setFinger(3, HAND_HALF_ANGLE); + setFinger(4, HAND_HALF_ANGLE); + + Entry.ArduinoZin.handTimerId = setInterval(() => { + + let number = parseInt(strResult[script.handCount]); + + setFingerNumber(number, 0.5); + script.handCount++; + + if(script.handCount == strResult.length) { + clearInterval(Entry.ArduinoZin.handTimerId); + setTimeout(()=>{ + script.isLoop = 0; + }, HAND_DELAY); + } + }, HAND_DELAY+HAND_DELAY*2); + + script.isStart = true; + script.isLoop = true; + return script; + } else if (script.isLoop == true) { + return script; + } + + delete script.isLoop; + delete script.isStart; + delete script.handCount; + return script.callReturn(); + }, + }, }; }; //endregion ArduinoZin 아두이노 확장모드