Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 107 additions & 5 deletions src/playground/blocks/hardwareLite/block_microbit2_lite.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';

(function() {
const _throttle = require('lodash/throttle');

const EVENT_INTERVAL = 150;

(function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Delete ·

Suggested change
(function () {
(function() {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <space-before-function-paren> reported by reviewdog 🐶
Unexpected space before function parentheses.

Suggested change
(function () {
(function() {

Entry.Microbit2lite = new (class Microbit2Lite {
constructor() {
this.commandStatus = {};
this.btnEventIntervalId = -1;
this.retryLimitCnt = 5;
this.portData = {
baudRate: 115200,
Expand All @@ -13,7 +18,7 @@
bufferSize: 512,
connectionType: 'ascii',
};
this.duration = 32;
this.duration = 64;
this.functionKeys = {
LOCALDATA: 'localdata',
GET_ANALOG: 'get-analog',
Expand Down Expand Up @@ -258,11 +263,19 @@
'microbit2lite_set_pwm',
'microbit2lite_v2_title',
'microbit2lite_get_logo',
'microbit2lite_btn_event',
'microbit2lite_speaker_toggle',
'microbit2lite_play_sound_effect',
'microbit2lite_get_sound_level',
];
this.version = '2';
this.firePressedBtnEventWithThrottle = _throttle(
(pressedBtn) => {
Entry.engine.fireEventWithValue('microbit2lite_btn_pressed', pressedBtn);
},
EVENT_INTERVAL,
{ leading: true, trailing: false }
);
}
_clamp(value, min, max) {
if (value < min) {
Expand All @@ -277,6 +290,48 @@
return Entry.hwLite.sendAsyncWithThrottle(this.functionKeys.RESET);
}

async initialHandshake() {
const defaultCMD = `${this.functionKeys.LOCALDATA}`;
const response = await Entry.hwLite.sendAsync(defaultCMD);
if (response && response.indexOf('localdata') > -1) {
const version = response.split(';')[1];
if (!version) {
return;
}
const major = version[0];
if (this.version !== major) {
this.version = major;
}
}

if (this.version === '2') {
Entry.addEventListener('run', this.handleBtnEventInterval.bind(this));
Entry.addEventListener('beforeStop', () => { clearInterval(this.btnEventIntervalId) });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <max-len> reported by reviewdog 🐶
This line has a length of 103. Maximum allowed is 100.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <brace-style> reported by reviewdog 🐶
Statement inside of curly braces should be on next line.

Suggested change
Entry.addEventListener('beforeStop', () => { clearInterval(this.btnEventIntervalId) });
Entry.addEventListener('beforeStop', () => {
clearInterval(this.btnEventIntervalId) });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Replace ·clearInterval(this.btnEventIntervalId) with ⏎····················clearInterval(this.btnEventIntervalId);⏎···············

Suggested change
Entry.addEventListener('beforeStop', () => { clearInterval(this.btnEventIntervalId) });
Entry.addEventListener('beforeStop', () => {
clearInterval(this.btnEventIntervalId);
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <semi> reported by reviewdog 🐶
Missing semicolon.

Suggested change
Entry.addEventListener('beforeStop', () => { clearInterval(this.btnEventIntervalId) });
Entry.addEventListener('beforeStop', () => { clearInterval(this.btnEventIntervalId); });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <brace-style> reported by reviewdog 🐶
Closing curly brace should be on the same line as opening curly brace or on the line after the previous block.

Suggested change
Entry.addEventListener('beforeStop', () => { clearInterval(this.btnEventIntervalId) });
Entry.addEventListener('beforeStop', () => { clearInterval(this.btnEventIntervalId)
});

}

return response;
}

handleBtnEventInterval() {
this.btnEventIntervalId = setInterval(this.listenBtnPressedEvent.bind(this), this.duration);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <max-len> reported by reviewdog 🐶
This line has a length of 104. Maximum allowed is 100.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Replace this.listenBtnPressedEvent.bind(this),·this.duration with ⏎················this.listenBtnPressedEvent.bind(this),⏎················this.duration⏎············

Suggested change
this.btnEventIntervalId = setInterval(this.listenBtnPressedEvent.bind(this), this.duration);
this.btnEventIntervalId = setInterval(
this.listenBtnPressedEvent.bind(this),
this.duration
);

}

async listenBtnPressedEvent() {
if (Object.keys(this.commandStatus).length > 0) {
return;
}

const defaultCMD = `${this.functionKeys.LOCALDATA};`;
const response = await Entry.hwLite.sendAsyncWithThrottle(defaultCMD);
// const response = await this.getResponseWithSync(defaultCMD);

// INFO: A,B 버튼이벤트 관련 로직
const pressedBtn = response.split(':btn:')[1];
if (pressedBtn) {
this.firePressedBtnEventWithThrottle(pressedBtn);
}
}

waitMilliSec(milli) {
this.blockReq = true;
setTimeout(() => {
Expand Down Expand Up @@ -318,7 +373,11 @@
}
const result = await Entry.hwLite.sendAsyncWithThrottle(command);

if (!result || this.getCommandType(command) !== this.getCommandType(result)) {
if (!result ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Insert ⏎················

Suggested change
if (!result ||
if (
!result ||

this.getCommandType(command) !== this.getCommandType(result) ||
// INFO : localdata 명령어는 우선순위가 낮으므로 반복하지 않음
command !== `${this.functionKeys.LOCALDATA};`
) {
if (!this.commandStatus[command]) {
this.commandStatus[command] = 1;
throw new Entry.Utils.AsyncError();
Expand Down Expand Up @@ -367,6 +426,7 @@
microbit2lite_get_logo: '로고를 터치했는가?',
microbit2lite_get_gesture: '움직임이 %1 인가?',
microbit2lite_get_acc: '%1 의 가속도 값',
microbit2lite_btn_event: '%1 %2 버튼을 눌렀을 때',
microbit2lite_get_direction: '나침반 방향',
microbit2lite_get_field_strength_axis: '%1 의 자기장 세기 값',
microbit2lite_get_light_level: '빛 센서 값',
Expand Down Expand Up @@ -522,7 +582,8 @@
microbit2lite_get_btn: "선택한 버튼이 눌렸다면 '참'으로 판단합니다.",
microbit2lite_get_logo: "로고를 터치했다면 '참'으로 판단합니다.",
microbit2lite_get_gesture: "선택한 움직임이 감지되면 '참'으로 판단합니다.",
microbit2lite_get_acc: '선택한 축의 가속도 값입니다.',
microbit2lite_get_acc: '선택한 버튼이 눌리면 아래에 연결된 블록들을 실행합니다.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Insert ⏎···························

Suggested change
microbit2lite_get_acc: '선택한 버튼이 눌리면 아래에 연결된 블록들을 실행합니다.',
microbit2lite_get_acc:
'선택한 버튼이 눌리면 아래에 연결된 블록들을 실행합니다.',

microbit2lite_btn_event: '%1 %2 버튼을 눌렀을 때',
microbit2lite_get_direction: '나침반 방향 값입니다. (0~360) ',
microbit2lite_get_field_strength_axis: '선택한 축의 자기장 세기 값입니다.',
microbit2lite_get_light_level: '빛 센서의 값입니다.',
Expand Down Expand Up @@ -565,6 +626,7 @@
microbit2lite_get_logo: 'logo touched?',
microbit2lite_get_gesture: 'Is the movement %1?',
microbit2lite_get_acc: 'acceleration value of %1',
microbit2lite_btn_event: '%1 When %2 button pressed',
microbit2lite_get_direction: 'compass direction',
microbit2lite_get_field_strength_axis:
'magnetic field strength value of %1 ',
Expand Down Expand Up @@ -731,6 +793,7 @@
microbit2lite_get_gesture:
"When the selected movement is detected, it is judged as 'True'.",
microbit2lite_get_acc: 'The acceleration value of the selected axis.',
microbit2lite_btn_event: 'When the selected button is pressed, the connected blocks below will run',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <max-len> reported by reviewdog 🐶
This line has a length of 124. Maximum allowed is 100.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Insert ⏎···························

Suggested change
microbit2lite_btn_event: 'When the selected button is pressed, the connected blocks below will run',
microbit2lite_btn_event:
'When the selected button is pressed, the connected blocks below will run',

microbit2lite_get_direction: 'The compass direction value. (0~360)',
microbit2lite_get_field_strength_axis:
'The magnetic field strength value of the selected axis.',
Expand Down Expand Up @@ -949,7 +1012,7 @@
};
}

getBlocks = function() {
getBlocks = function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Delete ·

Suggested change
getBlocks = function () {
getBlocks = function() {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <space-before-function-paren> reported by reviewdog 🐶
Unexpected space before function parentheses.

Suggested change
getBlocks = function () {
getBlocks = function() {

return {
microbit2lite_common_title: {
skeleton: 'basic_text',
Expand Down Expand Up @@ -1862,6 +1925,45 @@
}
},
},
microbit2lite_btn_event: {
color: EntryStatic.colorSet.block.default.HARDWARE,
outerLine: EntryStatic.colorSet.block.darken.HARDWARE,
fontColor: '#fff',
skeleton: 'basic_event',
statements: [],
params: [
{
type: 'Indicator',
img: 'block_icon/start_icon_hardware.svg',
size: 14,
position: { x: 0, y: -2 },
},
{
type: 'Dropdown',
options: [
['A', '1'],
['B', '2'],
['A+B', '3'],
],
value: '1',
fontSize: 11,
bgColor: EntryStatic.colorSet.block.darken.HARDWARE,
arrowColor: EntryStatic.colorSet.arrow.default.HARDWARE,
},
],
def: {
type: 'microbit2lite_btn_event'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Insert ,

Suggested change
type: 'microbit2lite_btn_event'
type: 'microbit2lite_btn_event',

},
paramsKeyMap: {
VALUE: 1,
},
class: 'microbit2litev2',
isNotFor: ['Microbit2lite'],
event: 'microbit2lite_btn_pressed',
func: (sprite, script) => {
return script.callReturn();
},
Comment on lines +1963 to +1965
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <arrow-body-style> reported by reviewdog 🐶
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.

Suggested change
func: (sprite, script) => {
return script.callReturn();
},
func: (sprite, script) => script.callReturn(),

},
microbit2lite_get_acc: {
color: EntryStatic.colorSet.block.default.HARDWARE,
outerLine: EntryStatic.colorSet.block.darken.HARDWARE,
Expand Down