Skip to content

Commit 2fc70fa

Browse files
authored
Merge pull request #2517 from RoboroboLab/develop-hw
RoE 관련 코드 전면 수정
2 parents 1ac5082 + 618d14e commit 2fc70fa

File tree

5 files changed

+2368
-1072
lines changed

5 files changed

+2368
-1072
lines changed

images/hw/roborobo_cube.png

32.1 KB
Loading

src/playground/blocks/hardware/block_roborobo_base.js

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PinMode = {
55
OUTPUT: 0x01,
66
ANALOG: 0x02,
77
I2C: 0x06,
8-
}
8+
};
99

1010
class ArduinoBase {
1111
constructor () {
@@ -23,11 +23,11 @@ class ArduinoBase {
2323
afterReceive = function (data) {
2424
const keys = data.state ? Object.keys(data.state) : [];
2525
keys.forEach(key => this.state[key] = data.state[key]);
26-
}
26+
};
2727

2828
afterSend = function () {
2929
Entry.hw.sendQueue = {};
30-
}
30+
};
3131

3232
request (func, subkey, value, updateNow = false) {
3333
if (!Entry.hw.sendQueue[func]) Entry.hw.sendQueue[func] = {};
@@ -153,7 +153,7 @@ class ArduinoBase {
153153
const state = script.getStringValue('STATE');
154154

155155
const speed = [speed1, speed2];
156-
let motor = []
156+
let motor = [];
157157
if (motors == 12) {
158158
motor = [1, 2];
159159
} else if (motors == 34) {
@@ -192,24 +192,10 @@ class ArduinoBase {
192192

193193
set_rgbled_color (sprite, script) {
194194
const pin = this.pinToNumber(script.getStringValue('PIN'));
195-
let colorString = script.getStringValue('COLOR');
196-
197-
let color;
198-
if (typeof colorString === 'string' && colorString.substring(0, 1) === '#') {
199-
const shorThandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
200-
colorString = colorString.replace(shorThandRegex, (m, r, g, b) => r + r + g + g + b + b);
201-
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(colorString);
202-
color = result ? {
203-
r: parseInt(result[1], 16),
204-
g: parseInt(result[2], 16),
205-
b: parseInt(result[3], 16)
206-
} : null;
207-
}
208-
if (!color) color = {r: 0, g: 0, b: 0};
195+
const color = Cast.toRgbColorObject(script.getStringValue('COLOR'));
209196

210197
this.request('setRgbLedColor', pin, {pin, color});
211198
return script.callReturn();
212-
213199
}
214200

215201
change_rgbled_brightness_by (sprite, script) {
@@ -240,9 +226,18 @@ class ArduinoBase {
240226
}
241227

242228
play_piezobuzzer_until_done (sprite, script) {
243-
const duration = script.getNumberValue('DURATION');
244-
this.play_piezobuzzer(sprite, script);
245-
return new Promise(resolve => setTimeout(() => resolve(), duration * 1000));
229+
if (script.executeState) {
230+
const duration = (script.getNumberValue('DURATION') * 1000);
231+
if (Date.now() - script.executeState.startTime > duration) {
232+
return script.callReturn();
233+
} else {
234+
return script;
235+
}
236+
} else {
237+
script.executeState = {startTime: Date.now()};
238+
this.play_piezobuzzer(sprite, script);
239+
return script;
240+
}
246241
}
247242

248243
get_digital_value (sprite, script) {
@@ -419,4 +414,60 @@ class ArduinoBase {
419414
}
420415
}
421416

422-
module.exports = {ArduinoBase, PinMode};
417+
class Cast {
418+
static toRgbColorObject (value) {
419+
let color;
420+
if (typeof value === 'string' && value.substring(0, 1) === '#') {
421+
color = Color.hexToRgb(value);
422+
423+
// If the color wasn't *actually* a hex color, cast to black
424+
if (!color) color = {r: 0, g: 0, b: 0};
425+
} else {
426+
color = Color.decimalToRgb(Cast.toNumber(value));
427+
if (color.hasOwnProperty('a')) {
428+
delete color.a;
429+
}
430+
}
431+
return color;
432+
}
433+
}
434+
435+
class Color {
436+
static decimalToHex (decimal) {
437+
if (decimal < 0) {
438+
decimal += 0xFFFFFF + 1;
439+
}
440+
let hex = Number(decimal).toString(16);
441+
hex = `#${'000000'.substr(0, 6 - hex.length)}${hex}`;
442+
return hex;
443+
}
444+
445+
static decimalToRgb (decimal) {
446+
const a = (decimal >> 24) & 0xFF;
447+
const r = (decimal >> 16) & 0xFF;
448+
const g = (decimal >> 8) & 0xFF;
449+
const b = decimal & 0xFF;
450+
return {r: r, g: g, b: b, a: a > 0 ? a : 255};
451+
}
452+
453+
static hexToRgb (hex) {
454+
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
455+
hex = hex.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b);
456+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
457+
return result ? {
458+
r: parseInt(result[1], 16),
459+
g: parseInt(result[2], 16),
460+
b: parseInt(result[3], 16)
461+
} : null;
462+
}
463+
464+
static rgbToHex (rgb) {
465+
return Color.decimalToHex(Color.rgbToDecimal(rgb));
466+
}
467+
468+
static rgbToDecimal (rgb) {
469+
return (rgb.r << 16) + (rgb.g << 8) + rgb.b;
470+
}
471+
}
472+
473+
module.exports = {ArduinoBase, PinMode, Cast, Color};

0 commit comments

Comments
 (0)