初始化提交
This commit is contained in:
@@ -0,0 +1,441 @@
|
||||
'use strict';
|
||||
|
||||
pbc.globalFunctionD['mixly_servo_write_angle'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_pwm";
|
||||
var pinblock = py2block.convert(args[0]);
|
||||
pbc.pinType = null;
|
||||
var angblock = py2block.convert(args[1]);
|
||||
if (pinblock != null && angblock != null) {
|
||||
return [block("servo_move", func.lineno, {},
|
||||
{
|
||||
"PIN": pinblock,
|
||||
"DEGREE": angblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('Servo')['angle'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_pwm";
|
||||
var pinblock = py2block.convert(args[0]);
|
||||
pbc.pinType = null;
|
||||
var angblock = py2block.convert(args[1]);
|
||||
if (pinblock != null && angblock != null) {
|
||||
return [block("servo_move", func.lineno, {},
|
||||
{
|
||||
"PIN": pinblock,
|
||||
"DEGREE": angblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('motor_control')['MotorRun'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock1 = args[0].n.v;
|
||||
var argblock2 = args[1].left.n.v + "*";
|
||||
var speedblock = {
|
||||
_astname: "Num",
|
||||
n: {
|
||||
'v': args[1].right.n.v
|
||||
}
|
||||
}
|
||||
return [block("bit_motor_control", func.lineno, {
|
||||
'Motor': argblock1,
|
||||
'mode': argblock2
|
||||
}, {
|
||||
'speed': py2block.convert(speedblock)
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('music')['pitch'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length === 1 && keywords.length === 1) {
|
||||
var roundAstName = args[0]._astname;
|
||||
pbc.pinType = "pins_pwm";
|
||||
var pinblock = py2block.convert(keywords[0].value);
|
||||
pbc.pinType = null;
|
||||
pbc.pinType = "pins_tone_notes";
|
||||
var pitchblock;
|
||||
if (roundAstName === "Call"
|
||||
&& args[0].func._astname == "Name" && py2block.Name_str(args[0].func) === "round") {
|
||||
pitchblock = py2block.convert(args[0].args[0])
|
||||
} else {
|
||||
pitchblock = py2block.convert(args[0])
|
||||
}
|
||||
pbc.pinType = null;
|
||||
return [block("microbit_music_pitch", func.lineno, {}, {
|
||||
'pitch': pitchblock,
|
||||
'PIN': pinblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else if (args.length === 3 && keywords.length === 1 && keywords[0].value._astname == "Name") {
|
||||
pbc.pinType = "pins_pwm";
|
||||
var pinblock = py2block.convert(args[2]);
|
||||
pbc.pinType = null;
|
||||
var roundAstName = args[0]._astname;
|
||||
var durationAstName = args[1]._astname;
|
||||
var param = keywords[0];
|
||||
var wait = py2block.Name_str(param.value);
|
||||
var pitchblock = null;
|
||||
var durationblock = null;
|
||||
pbc.pinType = "pins_tone_notes";
|
||||
if (roundAstName === "Call"
|
||||
&& args[0].func._astname == "Name" && py2block.Name_str(args[0].func) === "round") {
|
||||
pitchblock = py2block.convert(args[0].args[0])
|
||||
} else {
|
||||
pitchblock = py2block.convert(args[0]);
|
||||
}
|
||||
pbc.pinType = null;
|
||||
if (durationAstName === "Call"
|
||||
&& args[1].func._astname == "Name" && py2block.Name_str(args[1].func) === "round") {
|
||||
durationblock = py2block.convert(args[1].args[0]);
|
||||
} else {
|
||||
durationblock = py2block.convert(args[1]);
|
||||
}
|
||||
return [block("microbit_music_pitch_delay", func.lineno, {
|
||||
'wait': wait
|
||||
}, {
|
||||
'pitch': pitchblock,
|
||||
'duration': durationblock,
|
||||
'PIN': pinblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('music')['play'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length == 1 && keywords.length == 3) {//music.DADADADUM, pin=pin0, wait=True, loop=False
|
||||
var musicblock = null;
|
||||
var pinblock = null;
|
||||
var waitblock = null;
|
||||
var loopblock = null;
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === "pin") {
|
||||
pbc.pinType = "pins_pwm";
|
||||
pinblock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
} else if (key === "wait" && param.value._astname == "Name") {
|
||||
waitblock = py2block.Name_str(param.value);
|
||||
} else if (key === "loop" && param.value._astname == "Name") {
|
||||
loopblock = py2block.Name_str(param.value);
|
||||
}
|
||||
}
|
||||
if (args[0]._astname == "Attribute"
|
||||
&& args[0].value._astname == "Name" && py2block.Name_str(args[0].value) == "music") {
|
||||
musicblock = py2block.identifier(args[0].attr);
|
||||
if (musicblock != null && pinblock != null && waitblock != null && loopblock != null) {
|
||||
return [block("microbit_music_play_built_in", func.lineno, {
|
||||
'melody': musicblock,
|
||||
'wait': waitblock,
|
||||
'loop': loopblock
|
||||
}, {
|
||||
'PIN': pinblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
} else {
|
||||
if (pinblock != null && waitblock != null && loopblock != null) {
|
||||
return [block("microbit_music_play_list_of_notes", func.lineno, {
|
||||
'wait': waitblock,
|
||||
'loop': loopblock
|
||||
}, {
|
||||
'PIN': pinblock,
|
||||
'notes': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
}else if(args.length == 1 && keywords.length == 1){
|
||||
if (args[0]._astname == "Attribute"
|
||||
&& args[0].value._astname == "Name" && py2block.Name_str(args[0].value) == "music"
|
||||
&& py2block.identifier(keywords[0].arg) == "pin") {
|
||||
musicblock = py2block.identifier(args[0].attr);
|
||||
var param = keywords[0];
|
||||
var key = py2block.identifier(param.arg);
|
||||
pbc.pinType = "pins_pwm";
|
||||
pinblock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
return [block("microbit_music_play_built_in_easy", func.lineno, {
|
||||
'melody': musicblock,
|
||||
}, {
|
||||
'PIN': pinblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('music')['stop'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_pwm";
|
||||
var argblock = py2block.convert(args[0]);
|
||||
pbc.pinType = null;
|
||||
return [block("microbit_music_stop", func.lineno, {}, {
|
||||
'PIN': argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('music')['reset'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("microbit_music_reset", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('music')['get_tempo'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("microbit_music_get_tempo", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('music')['set_tempo'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0 || keywords.length !== 2) { //music.set_tempo(ticks=4, bpm=120)
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var bpmblock = null;
|
||||
var ticksblock = null;
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === 'bpm') {
|
||||
bpmblock = py2block.convert(param.value);
|
||||
} else if (key === 'ticks') {
|
||||
ticksblock = py2block.convert(param.value);
|
||||
}
|
||||
}
|
||||
if (bpmblock != null && ticksblock != null) {
|
||||
return [block("tone_set_tempo", func.lineno, {}, {
|
||||
'BPM': bpmblock,
|
||||
'TICKS': ticksblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('speech')['translate'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return block("speech_translate", func.lineno, {}, {
|
||||
'VAR': argblock
|
||||
}, {
|
||||
"inline": "False"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function speechSayOrSingOrPronounce(mode){
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length === 1 && keywords.length === 4) {//('Mixly 0.997 is great!', pitch=64, speed=72, mouth=128, throat=128)
|
||||
var argblock = py2block.convert(args[0]);
|
||||
var pitchblock = null;
|
||||
var speedblock = null;
|
||||
var mouthblock = null;
|
||||
var throatblock = null;
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === 'pitch') {
|
||||
pitchblock = py2block.convert(param.value);
|
||||
} else if (key === 'speed') {
|
||||
speedblock = py2block.convert(param.value);
|
||||
} else if (key === 'mouth') {
|
||||
mouthblock = py2block.convert(param.value);
|
||||
} else if (key === 'throat') {
|
||||
throatblock = py2block.convert(param.value);
|
||||
}
|
||||
}
|
||||
if (argblock != null && pitchblock != null && speedblock != null && mouthblock != null && throatblock != null) {
|
||||
return [block("speech_say", func.lineno, {
|
||||
"MODE": mode
|
||||
}, {
|
||||
'VAR': argblock,
|
||||
'pitch': pitchblock,
|
||||
'speed': speedblock,
|
||||
'mouth': mouthblock,
|
||||
'throat': throatblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}else if(args.length === 1 && keywords.length === 0){
|
||||
return [block("speech_" + mode + "_easy", func.lineno, {}, {
|
||||
'VAR': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
|
||||
}
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('speech')['say'] = speechSayOrSingOrPronounce('say');
|
||||
pbc.moduleFunctionD.get('speech')['sing'] = speechSayOrSingOrPronounce('sing');
|
||||
pbc.moduleFunctionD.get('speech')['pronounce'] = speechSayOrSingOrPronounce('pronounce');
|
||||
|
||||
pbc.moduleFunctionD.get('rgb')['show'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 5) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock1 = py2block.convert(args[1]);
|
||||
var argblock2 = py2block.convert(args[2]);
|
||||
var argblock3 = py2block.convert(args[3]);
|
||||
var argblock4 = py2block.convert(args[4]);
|
||||
return [block("display_rgb", func.lineno, {}, {
|
||||
'_LED_': argblock1,
|
||||
'RVALUE': argblock2,
|
||||
'GVALUE': argblock3,
|
||||
'BVALUE': argblock4
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('np')['show'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("display_rgb_show", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.assignD.get('QJ00X_MP3')['check_assign'] = function (py2block, node, targets, value) {
|
||||
if(value.func._astname != "Name"){
|
||||
return false;
|
||||
}
|
||||
var funcName = value.func.id.v;
|
||||
if (value._astname === "Call" && funcName === "QJ00X_MP3" && value.keywords.length === 2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.assignD.get('QJ00X_MP3')['create_block'] = function (py2block, node, targets, value) {
|
||||
var rxblock = null;
|
||||
var txblock = null;
|
||||
for (var i = 0; i < value.keywords.length; i++) {
|
||||
var param = value.keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === "mp3_rx") {
|
||||
pbc.pinType = "pins_serial";
|
||||
rxblock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
} else if (key === "mp3_tx") {
|
||||
pbc.pinType = "pins_serial";
|
||||
txblock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
}
|
||||
}
|
||||
//注意:赋值语句里,即使图形块上下可接,也不需要加[]
|
||||
return block('MP3_INIT', node.lineno, {}, {
|
||||
'RX': rxblock,
|
||||
'TX': txblock
|
||||
});
|
||||
}
|
||||
|
||||
function QJ00X_mp3_set(mode){
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1 && args.length !== 0 && args.length !== 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
if (args.length == 0) {
|
||||
return [block("MP3_CONTROL", func.lineno, {
|
||||
'CONTROL_TYPE': mode
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else if (args.length == 2) {
|
||||
var argblock1 = py2block.convert(args[0]);
|
||||
var argblock2 = py2block.convert(args[1]);
|
||||
return [block("MP3_PLAY_FOLDER", func.lineno, {}, {
|
||||
"FOLDER": argblock1,
|
||||
"NUM": argblock2
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else {
|
||||
if (mode == "set_loop") {
|
||||
return [block("MP3_LOOP_MODE", func.lineno, {
|
||||
'LOOP_MODE': py2block.Num_value(args[0])
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else if (mode == "set_eq") {
|
||||
return [block("MP3_EQ_MODE", func.lineno, {
|
||||
'EQ_MODE': py2block.Num_value(args[0])
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else if (mode == "set_vol") {
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("MP3_VOL", func.lineno, {}, {
|
||||
"vol": argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else {
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("MP3_PLAY_NUM", func.lineno, {}, {
|
||||
"NUM": argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('play')['QJ00X_MP3'] = QJ00X_mp3_set('play');
|
||||
pbc.objectFunctionD.get('pause')['QJ00X_MP3'] = QJ00X_mp3_set('pause');
|
||||
pbc.objectFunctionD.get('next_track')['QJ00X_MP3'] = QJ00X_mp3_set('next_track');
|
||||
pbc.objectFunctionD.get('prev_track')['QJ00X_MP3'] = QJ00X_mp3_set('prev_track');
|
||||
pbc.objectFunctionD.get('inc_vol')['QJ00X_MP3'] = QJ00X_mp3_set('inc_vol');
|
||||
pbc.objectFunctionD.get('dec_vol')['QJ00X_MP3'] = QJ00X_mp3_set('dec_vol');
|
||||
pbc.objectFunctionD.get('set_loop')['QJ00X_MP3'] = QJ00X_mp3_set('set_loop');
|
||||
pbc.objectFunctionD.get('set_eq')['QJ00X_MP3'] = QJ00X_mp3_set('set_eq');
|
||||
pbc.objectFunctionD.get('set_vol')['QJ00X_MP3'] = QJ00X_mp3_set('set_vol');
|
||||
pbc.objectFunctionD.get('playFileByIndexNumber')['QJ00X_MP3'] = QJ00X_mp3_set('playFileByIndexNumber');
|
||||
pbc.objectFunctionD.get('set_folder')['QJ00X_MP3'] = QJ00X_mp3_set('set_folder');
|
||||
@@ -0,0 +1,263 @@
|
||||
'use strict';
|
||||
var pbc = Py2blockConfig.prototype;
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['on'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("radio_ons", func.lineno, {"type": "on"}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['off'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("radio_ons", func.lineno, {"type": "off"}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['reset'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("radio_ons", func.lineno, {"type": "reset"}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['config'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0 || keywords.length !== 7) { //length=32, queue=3, channel=7, power=6, address=0x75626974, group=0, data_rate=radio.RATE_1MBIT
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var lengthblock = null;
|
||||
var queueblock = null;
|
||||
var channelblock = null;
|
||||
var powerblock = null;
|
||||
var addressblock = null;
|
||||
var groupblock = null;
|
||||
var datarateblock = null;
|
||||
// var baudrate = null;
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === "length") {
|
||||
lengthblock = py2block.convert(param.value);
|
||||
} else if (key === "queue") {
|
||||
queueblock = py2block.convert(param.value);
|
||||
} else if (key === "channel") {
|
||||
channelblock = py2block.convert(param.value);
|
||||
} else if (key === "power") {
|
||||
powerblock = py2block.convert(param.value);
|
||||
} else if (key === "address") {
|
||||
addressblock = py2block.convert(param.value);
|
||||
} else if (key === "group") {
|
||||
groupblock = py2block.convert(param.value);
|
||||
} else if (key === "data_rate") {
|
||||
datarateblock = py2block.convert(param.value);
|
||||
}
|
||||
}
|
||||
if (lengthblock != null && queueblock != null && channelblock != null && powerblock != null && addressblock != null && groupblock != null/*&& data_rate!=null*/) {
|
||||
return [block("microbit_radio_config", func.lineno, {}, {
|
||||
"length": lengthblock,
|
||||
"queue": queueblock,
|
||||
"channel": channelblock,
|
||||
"power": powerblock,
|
||||
"address": addressblock,
|
||||
"group": groupblock,
|
||||
"data_rate": datarateblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['send'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length === 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("radio_send_string", func.lineno, {}, {
|
||||
"data": py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['send_bytes'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length === 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("radio_send_string", func.lineno, {"type": "send_bytes"}, {
|
||||
"data": py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['receive'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("radio_receive_string", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['receive_bytes'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("radio_receive_string", func.lineno, {"type": "receive_bytes"}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('radio')['receive_full'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("radio_receive_string", func.lineno, {"type": "receive_full"}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('i2c')['init'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0 || keywords.length !== 3) { //i2c.init(sda=20, scl=19, freq=10000)
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var sdablock = null;
|
||||
var sclblock = null;
|
||||
var freqblock = null;
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === "sda") {
|
||||
pbc.inScope = "i2c_init";
|
||||
pbc.pinType = "pins_digital";
|
||||
sdablock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
pbc.inScope = null;
|
||||
} else if (key === "scl") {
|
||||
pbc.inScope = "i2c_init";
|
||||
pbc.pinType = "pins_digital";
|
||||
sclblock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
pbc.inScope = null;
|
||||
} else if (key === "freq") {
|
||||
freqblock = py2block.convert(param.value);
|
||||
}
|
||||
}
|
||||
if (sdablock != null && sclblock != null && freqblock != null) {
|
||||
return [block("i2c_init", func.lineno, {}, {
|
||||
'RX': sdablock,
|
||||
'TX': sclblock,
|
||||
"freq": freqblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('i2c')['read'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 3 || args[2]._astname != "Name") {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var adblock = py2block.convert(args[0]);
|
||||
var dtblock = py2block.convert(args[1]);
|
||||
return block("i2c_read", func.lineno, {
|
||||
'is_repeated': py2block.Name_str(args[2])
|
||||
}, {
|
||||
'address': adblock,
|
||||
'data': dtblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('i2c')['write'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 3 || args[2]._astname != "Name") {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var adblock = py2block.convert(args[0]);
|
||||
var dtblock = py2block.convert(args[1]);
|
||||
return [block("i2c_write", func.lineno, {
|
||||
'is_repeated': py2block.Name_str(args[2])
|
||||
}, {
|
||||
'address': adblock,
|
||||
'data': dtblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('spi')['init'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0 || keywords.length !== 6
|
||||
|| keywords[0].value._astname != "Num"
|
||||
|| keywords[1].value._astname != "Num"
|
||||
|| keywords[2].value._astname != "Num"
|
||||
|| keywords[3].value._astname != "Num"
|
||||
|| keywords[4].value._astname != "Num"
|
||||
|| keywords[5].value._astname != "Num"
|
||||
) { //(baudrate=1000000, bits=8, mode=0, mosi=15, miso= 14, sclk=13)
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var baudrate = null;
|
||||
var bitsblock = null;
|
||||
var modeblock = null;
|
||||
var mosiblock = null;
|
||||
var misoblock = null;
|
||||
var sclkblock = null;
|
||||
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === "baudrate") {
|
||||
baudrate = py2block.Num_value(param.value);
|
||||
} else if (key === "bits") {
|
||||
bitsblock = py2block.Num_value(param.value);
|
||||
} else if (key === "mode") {
|
||||
modeblock = py2block.Num_value(param.value);
|
||||
} else if (key === "mosi") {
|
||||
mosiblock = py2block.Num_value(param.value);
|
||||
} else if (key === "miso") {
|
||||
misoblock = py2block.Num_value(param.value);
|
||||
} else if (key === "sclk") {
|
||||
sclkblock = py2block.Num_value(param.value);
|
||||
}
|
||||
}
|
||||
if (bitsblock != null && modeblock != null && baudrate != null
|
||||
&& mosiblock != null && misoblock != null && sclkblock != null) {
|
||||
return [block("spi_init", func.lineno, {
|
||||
"freq": baudrate,
|
||||
"bits": bitsblock,
|
||||
"mode": modeblock,
|
||||
"mosi": mosiblock,
|
||||
"miso": misoblock,
|
||||
"sck": sclkblock//
|
||||
}, {}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('spi')['write'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length === 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("spi_write", func.lineno, {}, {
|
||||
'data': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,448 @@
|
||||
'use strict';
|
||||
|
||||
function show_or_scroll(blockid1, blockid2, blockid3 = null) {
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length === 1 && keywords.length === 4) {//Image.ALL_CLOCKS, delay=120, wait=True, loop=False, clear=False
|
||||
var imagesblock = null;
|
||||
var delayblock = null;
|
||||
var waitblock = null;
|
||||
var loopblock = null;
|
||||
var clearblock = null;
|
||||
imagesblock = py2block.convert(args[0]);
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === "delay") {
|
||||
delayblock = py2block.convert(param.value);
|
||||
} else if (key === "wait" && param.value._astname == "Name") {
|
||||
waitblock = py2block.Name_str(param.value);
|
||||
} else if (key === "loop" && param.value._astname == "Name") {
|
||||
loopblock = py2block.Name_str(param.value);
|
||||
} else if (key === "clear" && param.value._astname == "Name") {
|
||||
clearblock = py2block.Name_str(param.value);
|
||||
}
|
||||
}
|
||||
if (imagesblock != null && delayblock != null
|
||||
&& waitblock != null && loopblock != null && clearblock != null) {
|
||||
return [block(blockid2, func.lineno, {
|
||||
'wait': waitblock,
|
||||
'loop': loopblock,
|
||||
'clear': clearblock,
|
||||
},
|
||||
{
|
||||
'images': imagesblock,
|
||||
'delay': delayblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
} else if (args.length === 1 && keywords.length === 0) {
|
||||
return [block(blockid1, func.lineno, {},
|
||||
{
|
||||
'data': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
} else if (args.length === 1 && keywords.length === 1 && blockid3) {
|
||||
var delayblock = py2block.convert(keywords[0].value);
|
||||
return [block(blockid3, func.lineno, {},
|
||||
{
|
||||
'data': py2block.convert(args[0]),
|
||||
'delay': delayblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('display')['show'] = show_or_scroll('monitor_show_image_or_string', 'microbit_display_show_animation');
|
||||
pbc.moduleFunctionD.get('display')['scroll'] = show_or_scroll('monitor_scroll_string', 'microbit_display_scroll_string_animation', 'monitor_scroll_string_with_delay');
|
||||
|
||||
//创建图像
|
||||
pbc.globalFunctionD['Image'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1 || args[0]._astname != "Str") {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var colours = [
|
||||
"#000000",
|
||||
"#440000",
|
||||
"#660000",
|
||||
"#880000",
|
||||
"#aa0000",
|
||||
"#bb0000",
|
||||
"#cc0000",
|
||||
"#dd0000",
|
||||
"#ee0000",
|
||||
"#ff0000"
|
||||
];
|
||||
var flag = 0;
|
||||
var tempblock = py2block.Str_value(args[0]);
|
||||
var temptext = new Array();
|
||||
temptext = tempblock.split(':');
|
||||
|
||||
if (temptext.length == 5) {
|
||||
for (var i = 0; i < 5; i++) {
|
||||
if (temptext[i].length == 5) {
|
||||
flag++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flag == 5) {
|
||||
return block('microbit_image_create', func.lineno, {
|
||||
"00": colours[temptext[0].charAt(0)],
|
||||
"01": colours[temptext[0].charAt(1)],
|
||||
"02": colours[temptext[0].charAt(2)],
|
||||
"03": colours[temptext[0].charAt(3)],
|
||||
"04": colours[temptext[0].charAt(4)],
|
||||
"10": colours[temptext[1].charAt(0)],
|
||||
"11": colours[temptext[1].charAt(1)],
|
||||
"12": colours[temptext[1].charAt(2)],
|
||||
"13": colours[temptext[1].charAt(3)],
|
||||
"14": colours[temptext[1].charAt(4)],
|
||||
"20": colours[temptext[2].charAt(0)],
|
||||
"21": colours[temptext[2].charAt(1)],
|
||||
"22": colours[temptext[2].charAt(2)],
|
||||
"23": colours[temptext[2].charAt(3)],
|
||||
"24": colours[temptext[2].charAt(4)],
|
||||
"30": colours[temptext[3].charAt(0)],
|
||||
"31": colours[temptext[3].charAt(1)],
|
||||
"32": colours[temptext[3].charAt(2)],
|
||||
"33": colours[temptext[3].charAt(3)],
|
||||
"34": colours[temptext[3].charAt(4)],
|
||||
"40": colours[temptext[4].charAt(0)],
|
||||
"41": colours[temptext[4].charAt(1)],
|
||||
"42": colours[temptext[4].charAt(2)],
|
||||
"43": colours[temptext[4].charAt(3)],
|
||||
"44": colours[temptext[4].charAt(4)],
|
||||
}, {}, {
|
||||
"inline": "false"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('image')['height'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block('display_image_size', func.lineno, {
|
||||
'OP': 'height'
|
||||
}, {
|
||||
'VAR': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('image')['width'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block('display_image_size', func.lineno, {
|
||||
'OP': 'width'
|
||||
}, {
|
||||
'VAR': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
function imageShift(mode){
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var imageblock = py2block.convert(func.value);
|
||||
return block('image_shift', func.lineno, {
|
||||
'OP': mode
|
||||
}, {
|
||||
'img': imageblock,
|
||||
'val': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('shift_up')['Image'] = imageShift('up');
|
||||
pbc.objectFunctionD.get('shift_down')['Image'] = imageShift('down');
|
||||
pbc.objectFunctionD.get('shift_left')['Image'] = imageShift('left');
|
||||
pbc.objectFunctionD.get('shift_right')['Image'] = imageShift('right');
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('copy')['Image'] = function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var imageblock = py2block.convert(func.value);
|
||||
return block('microbit_image_copy', func.lineno, {}, {
|
||||
'image': imageblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('invert')['Image'] = function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var imageblock = py2block.convert(func.value);
|
||||
return block('microbit_image_invert', func.lineno, {}, {
|
||||
'image': imageblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('display')['get_pixel'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 2){
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var astname = args[0]._astname;
|
||||
var astname1 = args[1]._astname;
|
||||
var xblock;
|
||||
var yblock;
|
||||
pbc.pinType = "pins_axis";
|
||||
if(astname === "Call" && args[0].func._astname == "Name" && py2block.Name_str(args[0].func) === "int"){ //display.get_pixel(int(0), int(0))
|
||||
xblock = py2block.convert(args[0].args[0]);
|
||||
}else{
|
||||
xblock = py2block.convert(args[0]);
|
||||
}
|
||||
if(astname1 === "Call" && args[1].func._astname == "Name" && py2block.Name_str(args[1].func) === "int"){ //display.get_pixel(int(0), int(0))
|
||||
yblock = py2block.convert(args[1].args[0]);
|
||||
}else{
|
||||
yblock = py2block.convert(args[1]);
|
||||
}
|
||||
pbc.pinType = null;
|
||||
return block("monitor_get_pixel", func.lineno, {}, {
|
||||
'x':xblock,
|
||||
'y':yblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('display')['set_pixel'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 3){
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var astname = args[0]._astname;
|
||||
var astname1 = args[1]._astname;
|
||||
pbc.pinType = "pins_brightness";
|
||||
var brightblock = py2block.convert(args[2]);
|
||||
pbc.pinType = null;
|
||||
var xblock;
|
||||
var yblock;
|
||||
pbc.pinType = "pins_axis";
|
||||
if(astname === "Call" && args[0].func._astname == "Name" && py2block.Name_str(args[0].func) === "int"){ //display.set_pixel(int(0), int(0))
|
||||
xblock = py2block.convert(args[0].args[0]);
|
||||
}else{
|
||||
xblock = py2block.convert(args[0]);
|
||||
}
|
||||
if(astname1 === "Call" && args[1].func._astname == "Name" && py2block.Name_str(args[1].func) === "int"){ //display.set_pixel(int(0), int(0))
|
||||
yblock = py2block.convert(args[1].args[0]);
|
||||
}else{
|
||||
yblock = py2block.convert(args[1]);
|
||||
}
|
||||
pbc.pinType = null;
|
||||
return [block("monitor_bright_point", func.lineno, {}, {
|
||||
'x':xblock,
|
||||
'y':yblock,
|
||||
'brightness':brightblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
function displayOnOrOff(mode){
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("microbit_display_on", func.lineno, {
|
||||
'on_off': mode
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('display')['on'] = displayOnOrOff('on');
|
||||
pbc.moduleFunctionD.get('display')['off'] = displayOnOrOff('off');
|
||||
|
||||
pbc.moduleFunctionD.get('display')['is_on'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("microbit_display_is_on", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('display')['clear'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("microbit_display_clear", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.assignD.get('Rgb')['check_assign'] = function(py2block, node, targets, value) {
|
||||
if(value._astname != "Call" || value.func._astname != "Attribute" || value.func.value._astname != "Name"){
|
||||
return false;
|
||||
}
|
||||
var moduleName = py2block.Name_str(value.func.value);
|
||||
var funcName = py2block.identifier(value.func.attr);
|
||||
if(value._astname === "Call" && moduleName === "neopixel"
|
||||
&& funcName === "NeoPixel" && value.args.length === 2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.assignD.get('Rgb')['create_block'] = function(py2block, node, targets, value){
|
||||
pbc.pinType = "pins_digital";
|
||||
var pinblock = py2block.convert(value.args[0]);
|
||||
pbc.pinType = null;
|
||||
var countblock = py2block.convert(value.args[1]);
|
||||
return block("display_rgb_init", node.lineno, {}, {
|
||||
"PIN":pinblock,
|
||||
"LEDCOUNT":countblock
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['mixly_rgb_show'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 4) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("display_rgb", func.lineno, {
|
||||
}, {
|
||||
'_LED_':py2block.convert(args[0]),
|
||||
"RVALUE":py2block.convert(args[1]),
|
||||
"GVALUE":py2block.convert(args[2]),
|
||||
"BVALUE":py2block.convert(args[3])
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.assignD.get('Lcd')['check_assign'] = function(py2block, node, targets, value) {
|
||||
if(value._astname != "Call" || value.func._astname != "Name"){
|
||||
return false;
|
||||
}
|
||||
var className = py2block.Name_str(value.func);
|
||||
if(value._astname === "Call" && className === "LCD1602" && value.args.length === 1)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.assignD.get('Lcd')['create_block'] = function(py2block, node, targets, value){
|
||||
pbc.inScope = "lcd_init";
|
||||
var argblock = py2block.convert(value.args[0]);
|
||||
pbc.inScope = null;
|
||||
return block("group_lcd_init", node.lineno, {}, {
|
||||
"device":argblock
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('mylcd')['mixly_puts_two_lines'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 2) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("group_lcd_print", func.lineno, {
|
||||
}, {
|
||||
"TEXT":py2block.convert(args[0]),
|
||||
"TEXT2":py2block.convert(args[1])
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('mylcd')['mixly_puts'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 3) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
|
||||
return [block("group_lcd_print2", func.lineno, {
|
||||
}, {
|
||||
"row":py2block.convert(args[1]),
|
||||
"column":py2block.convert(args[2]),
|
||||
"TEXT":py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
function mylcdOnOrOffOrClear(mode){
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("group_lcd_power", func.lineno, {
|
||||
'STAT': mode
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('mylcd')['on'] = mylcdOnOrOffOrClear('on()');
|
||||
pbc.moduleFunctionD.get('mylcd')['off'] = mylcdOnOrOffOrClear('off()');
|
||||
pbc.moduleFunctionD.get('mylcd')['clear'] = mylcdOnOrOffOrClear('clear()');
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('mylcd')['backlight'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1 || args[0]._astname != "Name") {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var stat = py2block.Name_str(args[0]);
|
||||
return [block("group_lcd_power", func.lineno, {
|
||||
'STAT': "backlight(" + stat + ")"
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['mixly_oled_text'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 4) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("lp2i_u8g_draw_4strings", func.lineno, {
|
||||
}, {
|
||||
"Text_line1":py2block.convert(args[0]),
|
||||
"Text_line2":py2block.convert(args[1]),
|
||||
"Text_line3":py2block.convert(args[2]),
|
||||
"Text_line4":py2block.convert(args[3])
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.objectFunctionD.get('show_fill')['monitor'] = function(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if(args.length!=1){
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var objblock = py2block.convert(func.value);
|
||||
var flagblock = py2block.identifier(args[0].n.v);
|
||||
|
||||
|
||||
return [block("display_fill", func.lineno, {'key':flagblock}, { "SUB":objblock,
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('write_digital')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_digital";
|
||||
var pinblock = py2block.convert(func.value);
|
||||
var argblock = py2block.convert(args[0]);
|
||||
pbc.pinType = null;
|
||||
return [block("inout_digital_write", func.lineno, {}, {
|
||||
"PIN": pinblock,
|
||||
"STAT": argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('read_digital')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_digital";
|
||||
var pinblock = py2block.convert(func.value);
|
||||
pbc.pinType = null;
|
||||
return block("inout_digital_read", func.lineno, {}, {
|
||||
"PIN": pinblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('write_analog')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_analog";
|
||||
var pinblock = py2block.convert(func.value);
|
||||
var argblock = py2block.convert(args[0]);
|
||||
pbc.pinType = null;
|
||||
return [block("inout_analog_write", func.lineno, {}, {
|
||||
"PIN": pinblock,
|
||||
"NUM": argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('read_analog')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_analog";
|
||||
var pinblock = py2block.convert(func.value);
|
||||
pbc.pinType = null;
|
||||
return block("inout_analog_read", func.lineno, {}, {
|
||||
"PIN": pinblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('set_analog_period')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_pwm";
|
||||
var pinblock = py2block.convert(func.value);
|
||||
var argblock = py2block.convert(args[0]);
|
||||
pbc.pinType = null;
|
||||
return [block("inout_analog_write_set", func.lineno, {
|
||||
'key': 'period'
|
||||
}, {
|
||||
"PIN": pinblock,
|
||||
"NUM": argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('set_analog_period_microseconds')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_pwm";
|
||||
var pinblock = py2block.convert(func.value);
|
||||
var argblock = py2block.convert(args[0]);
|
||||
pbc.pinType = null;
|
||||
return [block("inout_analog_write_set", func.lineno,
|
||||
{
|
||||
'key': 'period_microseconds'
|
||||
}, {
|
||||
"PIN": pinblock,
|
||||
"NUM": argblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('is_touched')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_interrupt";
|
||||
var pinblock = py2block.convert(func.value);
|
||||
pbc.pinType = null;
|
||||
|
||||
return block("sensor_pin_pressed", func.lineno, {}, {
|
||||
"pin": pinblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
function defDict(type) {
|
||||
var dict = {};
|
||||
return {
|
||||
get: function (key) {
|
||||
if (!dict[key]) {
|
||||
dict[key] = type.constructor();
|
||||
}
|
||||
return dict[key];
|
||||
},
|
||||
dict: dict
|
||||
};
|
||||
}
|
||||
|
||||
function Py2blockConfig (){
|
||||
this.initIgnoreS();
|
||||
this.initModuleAttrD();
|
||||
this.initKnownModuleS();
|
||||
this.initObjectTypeD();
|
||||
}
|
||||
|
||||
var pbc = Py2blockConfig.prototype;
|
||||
pbc.MICROBITPY = "microbit[py]";
|
||||
pbc.board = pbc.MICROBITPY;
|
||||
pbc.objectFunctionD = defDict({});
|
||||
pbc.moduleFunctionD = defDict({});
|
||||
pbc.moduleAttrD = defDict({});
|
||||
pbc.objectAttrD = defDict({});
|
||||
pbc.globalFunctionD = {};
|
||||
pbc.assignD = defDict({});
|
||||
pbc.ifStatementD= defDict({});
|
||||
pbc.whileStatementD= defDict({});
|
||||
pbc.forStatementD= defDict({});
|
||||
pbc.reservedNameD= {};
|
||||
pbc.knownModuleS = new Set();
|
||||
pbc.objectTypeD = {}; //key:变量名,value:变量类型,如{'a':'List'}
|
||||
pbc.ignoreS = new Set();
|
||||
pbc.pinType = null;
|
||||
pbc.inScope = null;
|
||||
pbc.formatModuleKeyL = [];
|
||||
pbc.formatModuleL = [];
|
||||
|
||||
//忽略某些方法、类、赋值
|
||||
pbc.initIgnoreS = function(){
|
||||
var pythonIgnoreL = [
|
||||
];
|
||||
var boardIgnoreL = [
|
||||
//actuator.js
|
||||
'Servo', 'mixly_servo_write_angle',
|
||||
//display.js
|
||||
"LCD1602", "LCD_I2C_ADDR", "OLED12864_I2C","mixly_rgb_show", "oled", "mixly_oled_text",
|
||||
//sensor.js
|
||||
'DS1307', 'DS1307_I2C_ADDRESS', 'DS1307_REG_SECOND', 'DS1307_REG_MINUTE',
|
||||
'DS1307_REG_HOUR', 'DS1307_REG_WEEKDAY', 'DS1307_REG_DAY', 'DS1307_REG_MONTH', 'DS1307_REG_YEAR',
|
||||
'DS1307_REG_CTRL', 'DS1307_REG_RAM', 'ds'
|
||||
];
|
||||
|
||||
var ignoreL = pythonIgnoreL.concat(boardIgnoreL);
|
||||
for (var i = 0; i < ignoreL.length; i++) {
|
||||
this.ignoreS.add(ignoreL[i]);
|
||||
}
|
||||
}
|
||||
|
||||
pbc.initModuleAttrD = function(){
|
||||
//communicate.js,例如 radio.RATE_2MBIT
|
||||
for (var i = 0; i < profile.default.radio_datarate.length; i++) {
|
||||
this.moduleAttrD.get('radio')[profile.default.radio_datarate[i][1]] = function (node, module, attr) {
|
||||
return block("pins_radio_datarate", node.lineno, {
|
||||
"PIN": attr
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//display.js
|
||||
for (var i = 0; i < profile.default.builtinimg.length; i++) {
|
||||
pbc.moduleAttrD.get('Image')[profile.default.builtinimg[i][0]] = function (node, module, attr) {
|
||||
return block("pins_builtinimg", node.lineno, {
|
||||
"PIN": module + "." + attr
|
||||
});
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < profile.default.imglist.length; i++) {
|
||||
pbc.moduleAttrD.get('Image')[profile.default.imglist[i][0]] = function (node, module, attr) {
|
||||
return block("pins_imglist", node.lineno, {
|
||||
"PIN": module + "." + attr
|
||||
});
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < profile.default.digital.length; i++) {
|
||||
pbc.moduleAttrD.get('PIN')[profile.default.digital[i][0]] = function (node, module, attr) {
|
||||
return block("pins_digital", node.lineno, {
|
||||
"PIN": module + "." + attr
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pbc.initKnownModuleS = function(){
|
||||
var pythonModuleL = [
|
||||
'math', 'random'
|
||||
];
|
||||
var boardModuleL = [];
|
||||
|
||||
var moduleL = pythonModuleL.concat(boardModuleL);
|
||||
for (var i = 0; i < moduleL.length; i++) {
|
||||
this.knownModuleS.add(moduleL[i]);
|
||||
}
|
||||
}
|
||||
|
||||
pbc.initObjectTypeD = function () {
|
||||
this.objectTypeD = {
|
||||
'f': 'open'
|
||||
}
|
||||
}
|
||||
|
||||
pbc.reset = function(){
|
||||
this.initObjectTypeD();
|
||||
}
|
||||
|
||||
var py2block_config = new Py2blockConfig();
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
'use strict';
|
||||
|
||||
pbc.objectFunctionD.get('is_pressed')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_button";
|
||||
var objblock = py2block.convert(func.value);
|
||||
pbc.pinType = null;
|
||||
return block("sensor_button_is_pressed", func.lineno, {}, {
|
||||
"btn": objblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('was_pressed')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_button";
|
||||
var objblock = py2block.convert(func.value);
|
||||
pbc.pinType = null;
|
||||
return block("sensor_button_was_pressed", func.lineno, {}, {
|
||||
"btn": objblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.objectFunctionD.get('get_presses')['Pin'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
pbc.pinType = "pins_button";
|
||||
var objblock = py2block.convert(func.value);
|
||||
pbc.pinType = null;
|
||||
return block("sensor_button_get_presses", func.lineno, {}, {
|
||||
"btn": objblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
pbc.ifStatementD.get('if_is_gesture')['check_condition'] = function (py2block, node, test, body, orelse) {
|
||||
if (test._astname == "Call" && test.func._astname == "Attribute"
|
||||
&& test.func.value._astname == "Name"
|
||||
&& py2block.Name_str(test.func.value) == "accelerometer"
|
||||
&& py2block.identifier(test.func.attr) == "is_gesture"
|
||||
&& test.args.length == 1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
pbc.ifStatementD.get('if_is_gesture')['create_block'] = function (py2block, node, test, body, orelse) {
|
||||
return block("controls_attachGestureInterrupt", node.lineno, {
|
||||
'gesture': py2block.Str_value(test.args[0])
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
}, {}, {
|
||||
"DO": py2block.convertBody(body)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.ifStatementD.get('if_was_gesture')['check_condition'] = function (py2block, node, test, body, orelse) {
|
||||
if (test._astname == "Call" && test.func._astname == "Attribute"
|
||||
&& test.func.value._astname == "Name"
|
||||
&& py2block.Name_str(test.func.value) == "accelerometer"
|
||||
&& py2block.identifier(test.func.attr) == "was_gesture"
|
||||
&& test.args.length == 1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
pbc.ifStatementD.get('if_was_gesture')['create_block'] = function (py2block, node, test, body, orelse) {
|
||||
return block("controls_attachGestureInterrupt2", node.lineno, {
|
||||
'gesture': py2block.Str_value(test.args[0])
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
}, {}, {
|
||||
"DO": py2block.convertBody(body)
|
||||
});
|
||||
}*/
|
||||
|
||||
function acceleromeerGetDir(mode) {
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_get_acceleration", func.lineno, {
|
||||
"key": mode
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('accelerometer')['get_x'] = acceleromeerGetDir('x');
|
||||
pbc.moduleFunctionD.get('accelerometer')['get_y'] = acceleromeerGetDir('y');
|
||||
pbc.moduleFunctionD.get('accelerometer')['get_z'] = acceleromeerGetDir('z');
|
||||
pbc.moduleFunctionD.get('accelerometer')['get_values'] = acceleromeerGetDir('values');
|
||||
|
||||
|
||||
function accelerometerGetGestrues(mode) {
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_get_gestures", func.lineno, {
|
||||
'GES': mode
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('accelerometer')['get_gestures'] = accelerometerGetGestrues('all');
|
||||
pbc.moduleFunctionD.get('accelerometer')['current_gesture'] = accelerometerGetGestrues('current');
|
||||
|
||||
pbc.moduleFunctionD.get('accelerometer')['is_gesture'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_current_gesture1", func.lineno, {'gesture': py2block.Str_value(args[0])}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('accelerometer')['was_gesture'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_current_gesture2", func.lineno, {'gesture': py2block.Str_value(args[0])}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('compass')['calibrate'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("sensor_calibrate_compass", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('compass')['is_calibrated'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_is_compass_calibrated", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function getStrength(mode) {
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_field_strength", func.lineno, {
|
||||
'compass': mode
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('compass')['get_field_strength'] = getStrength('get_field_strength');
|
||||
pbc.moduleFunctionD.get('compass')['heading'] = getStrength('heading');
|
||||
pbc.moduleFunctionD.get('compass')['get_x'] = getStrength('get_x');
|
||||
pbc.moduleFunctionD.get('compass')['get_y'] = getStrength('get_y');
|
||||
pbc.moduleFunctionD.get('compass')['get_z'] = getStrength('get_z');
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('compass')['clear_calibration'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("sensor_compass_reset", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['temperature'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_temperature", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('display')['read_light_level'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("sensor_light", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('ultrasonic')['distance_cm'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var trig = keywords[0].value.id.v.replace("pin", "");
|
||||
var echo = keywords[1].value.id.v.replace("pin", "");
|
||||
return block("sensor_distance_hrsc04", func.lineno, {
|
||||
"Trig": trig,
|
||||
"Echo": echo
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function ds1307GetTime(mode) {
|
||||
function converter(py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("RTC_get_time", func.lineno, {
|
||||
'TIME_TYPE': mode
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('ds')['Year'] = ds1307GetTime('Year');
|
||||
pbc.moduleFunctionD.get('ds')['Month'] = ds1307GetTime('Month');
|
||||
pbc.moduleFunctionD.get('ds')['Day'] = ds1307GetTime('Day');
|
||||
pbc.moduleFunctionD.get('ds')['Hour'] = ds1307GetTime('Hour');
|
||||
pbc.moduleFunctionD.get('ds')['Minute'] = ds1307GetTime('Minute');
|
||||
pbc.moduleFunctionD.get('ds')['Second'] = ds1307GetTime('Second');
|
||||
pbc.moduleFunctionD.get('ds')['Week'] = ds1307GetTime('Week');
|
||||
pbc.moduleFunctionD.get('ds')['get_time'] = ds1307GetTime('Mix2');
|
||||
pbc.moduleFunctionD.get('ds')['get_date'] = ds1307GetTime('Mix1');
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('ds')['set_date'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 3) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var yearblock = py2block.convert(args[0]);
|
||||
var monthblock = py2block.convert(args[1]);
|
||||
var dayblock = py2block.convert(args[2]);
|
||||
return [block("RTC_set_date", func.lineno, {}, {
|
||||
'year': yearblock,
|
||||
"month": monthblock,
|
||||
"day": dayblock,
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('ds')['set_time'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 3) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var hourblock = py2block.convert(args[0]);
|
||||
var minuteblock = py2block.convert(args[1]);
|
||||
var secondblock = py2block.convert(args[2]);
|
||||
return [block("RTC_set_time", func.lineno, {}, {
|
||||
"hour": hourblock,
|
||||
"minute": minuteblock,
|
||||
"second": secondblock,
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.assignD.get('HCSR04')['check_assign'] = function (py2block, node, targets, value) {
|
||||
if(value.func._astname != "Name"){
|
||||
return false;
|
||||
}
|
||||
var funcName = py2block.identifier(value.func.id);
|
||||
value.args = value.args || [];
|
||||
if (value._astname === "Call" && funcName === "HCSR04" && !value.args.length)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.assignD.get('HCSR04')['create_block'] = function (py2block, node, targets, value) {
|
||||
var name = py2block.Name_str(node.targets[0]);
|
||||
var Trig = value.keywords[0].value.id.v;
|
||||
Trig = Trig.replace(/[^0-9]/ig,"");
|
||||
var Echo = value.keywords[1].value.id.v;;
|
||||
Echo = Echo.replace(/[^0-9]/ig,"");
|
||||
//注意:赋值语句里,即使图形块上下可接,也不需要加[]
|
||||
return block('sensor_hrsc04_init', node.lineno, {
|
||||
"Trig": Trig,
|
||||
"Echo": Echo
|
||||
}, {});
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('tcs')['getRawRGBData'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var arg = args[0].n.v;
|
||||
return block("TCS34725_Get_RGB", func.lineno, {'TCS34725_COLOR': arg}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
'use strict';
|
||||
|
||||
pbc.globalFunctionD['print'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length === 1 && keywords.length === 1
|
||||
&& py2block.identifier(keywords[0].arg) === "end"
|
||||
&& keywords[0].value._astname === "Str"
|
||||
&& py2block.Str_value(keywords[0].value) === ""
|
||||
){
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("IO_print_inline", func.lineno, {}, {
|
||||
'VAR':argblock
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}else if (args.length === 1 && keywords.length === 0) {
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return [block("IO_print", func.lineno, {}, {
|
||||
'VAR':argblock
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}else{
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['input'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var argblock = py2block.convert(args[0]);
|
||||
return block("IO_input", func.lineno, {}, {
|
||||
'VAR':argblock
|
||||
}, {
|
||||
"inline": "false"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('uart')['init'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length === 0 && keywords.length === 1
|
||||
&& keywords[0].value._astname == "Num") { //uart.init(baudrate=9600)
|
||||
if(py2block.identifier(keywords[0].arg) === "baudrate") {
|
||||
return [block("serial_begin", func.lineno, {
|
||||
"baudrate": py2block.Num_value(keywords[0].value)
|
||||
}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}else if(args.length === 0 && keywords.length === 3) { //uart.init(rx=0, tx=1, baudrate=115200)
|
||||
var rxblock = null;
|
||||
var txblock = null;
|
||||
var baudrate = null;
|
||||
for (var i = 0; i < keywords.length; i++) {
|
||||
var param = keywords[i];
|
||||
var key = py2block.identifier(param.arg);
|
||||
if (key === "rx") {
|
||||
pbc.pinType = "pins_serial";
|
||||
rxblock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
} else if (key === "tx") {
|
||||
pbc.pinType = "pins_serial";
|
||||
txblock = py2block.convert(param.value);
|
||||
pbc.pinType = null;
|
||||
} else if (key === "baudrate" && param.value._astname == "Num") {
|
||||
baudrate = py2block.Num_value(param.value);
|
||||
}
|
||||
}
|
||||
|
||||
if (rxblock != null && txblock != null && baudrate != null) {
|
||||
return [block("serial_softserial", func.lineno, {
|
||||
"baudrate": baudrate
|
||||
}, {
|
||||
"RX":rxblock,
|
||||
"TX":txblock
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
}
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('uart')['write'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
var astname = args[0]._astname;
|
||||
if(astname === "Call"){
|
||||
if(args[0].func._astname == "Name" && py2block.Name_str(args[0].func) === "str"){ //serial.write(str("XXX"))
|
||||
return [block("serial_print", func.lineno, {}, {
|
||||
"CONTENT":py2block.convert(args[0].args[0]),
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
}else if(astname === "BinOp"){
|
||||
if(args[0].op.name === "Add" && args[0].right._astname === "Str"
|
||||
&& py2block.Str_value(args[0].right) === "\r\n"
|
||||
&& args[0].left._astname === "Call" && args[0].left.func._astname == "Name"
|
||||
&& py2block.Name_str(args[0].left.func) === "str"
|
||||
){
|
||||
if(args[0].left.args[0]._astname === "Call"
|
||||
&& args[0].left.args[0].func._astname === "Name"
|
||||
&& py2block.Name_str(args[0].left.args[0].func) === "hex"){ //serial.write(str(hex(XX)) + "\r\n")
|
||||
pbc.inScope = "lcd_init";
|
||||
var numblock=py2block.convert(args[0].left.args[0].args[0]);
|
||||
pbc.inScope=null;
|
||||
return [block("serial_print_hex", func.lineno, {}, {
|
||||
"CONTENT": numblock,
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}else{
|
||||
return [block("serial_println", func.lineno, {}, { //serial.write(str("XX") + "\r\n")
|
||||
"CONTENT": py2block.convert(args[0].left.args[0]),
|
||||
}, {
|
||||
"inline": "false"
|
||||
})];
|
||||
}
|
||||
}
|
||||
}
|
||||
return [block("serial_print", func.lineno, {}, {
|
||||
"CONTENT":py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
pbc.moduleFunctionD.get('uart')['any'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("serial_any", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('uart')['read'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("serial_readstr", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('uart')['readline'] = function(py2block, func, args, keywords, starargs, kwargs, node){
|
||||
if (args.length !== 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("serial_readline", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
|
||||
//microbit[py]的停止程序
|
||||
pbc.whileStatementD.get('while_stop_program')['check_condition'] = function (py2block, node, test, body, orelse) {
|
||||
if (test._astname == "Name" && py2block.Name_str(test) == "True"
|
||||
&& body.length == 1 && body[0]._astname == "Pass") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
pbc.whileStatementD.get('while_stop_program')['create_block'] = function (py2block, node, test, body, orelse) {
|
||||
return [block("controls_end_program", node.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['sleep'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("base_delay", func.lineno, {}, {
|
||||
'DELAY_TIME': py2block.convert(args[0]),
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['running_time'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("controls_millis", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['panic'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 1) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("Panic_with_status_code", func.lineno, {}, {
|
||||
'STATUS_CODE': py2block.convert(args[0])
|
||||
}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.globalFunctionD['reset'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return [block("reset", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
})];
|
||||
}
|
||||
|
||||
|
||||
pbc.moduleFunctionD.get('os')['uname'] = function (py2block, func, args, keywords, starargs, kwargs, node) {
|
||||
if (args.length != 0) {
|
||||
throw new Error("Incorrect number of arguments");
|
||||
}
|
||||
return block("controls_uname", func.lineno, {}, {}, {
|
||||
"inline": "true"
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user