Blockly-IO积木开发
今天出一期关于Blockly-IO积木开发的教程,废话不多说,上代码
IO积木
积木借鉴mixly部分积木,python代码可以参考micropython.circuitpython.com.cn/en/latet/li…
一:构建积木
1,涉及要修改得文件(blocks,io这个是要自己创建)
2,blocks文件引入io文件,并在export导出积木
io文件代码
/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Former goog.module ID: Blockly.libraryBlocks.io
import '../core/field_dropdown.js';
import {
createBlockDefinitionsFromJsonArray,
defineBlocks,
} from '../core/common.js';
const pin = [
["0", "0"],
["1", "1"],
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
["14", "14"],
["15", "15"],
["16", "16"],
["17", "17"],
["18", "18"],
["19", "19"],
["20", "20"],
["21", "21"],
["22", "22"],
["23", "23"],
["24", "24"],
["25", "25"],
["26", "26"],
["27", "27"],
["28", "28"]
]
const pinVar = [
["pin0", "pin0"],
["pin1", "pin1"],
["pin2", "pin2"],
["pin3", "pin3"],
["pin4", "pin4"],
["pin5", "pin5"],
["pin6", "pin6"],
["pin7", "pin7"],
["pin8", "pin8"],
["pin9", "pin9"],
["pin10", "pin10"],
["pin11", "pin11"],
["pin12", "pin12"],
["pin13", "pin13"],
["pin14", "pin14"],
["pin15", "pin15"],
["pin16", "pin16"],
["pin17", "pin17"],
["pin18", "pin18"],
["pin19", "pin19"],
["pin20", "pin20"],
["pin21", "pin21"],
["pin22", "pin22"],
["pin23", "pin23"],
["pin24", "pin24"],
["pin25", "pin25"],
["pin26", "pin26"],
["pin27", "pin27"],
["pin28", "pin28"]
]
const pinPro = [
["abc1", "abc1"],
["abc2", "abc2"],
["abc3", "abc3"],
["abc4", "abc4"],
["abc5", "abc5"],
["abc6", "abc6"],
["abc7", "abc7"],
["abc8", "abc8"],
["abc9", "abc9"],
["abc10", "abc10"],
["abc11", "abc11"],
["abc12", "abc12"],
["abc13", "abc13"],
["abc14", "abc14"],
["abc15", "abc15"],
["abc16", "abc16"],
["abc17", "abc17"],
["abc18", "abc18"]
]
const pinPwm = [
["pwm0", "pwm0"],
["pwm1", "pwm1"],
["pwm2", "pwm2"],
["pwm3", "pwm3"],
["pwm4", "pwm4"],
["pwm5", "pwm5"],
["pwm6", "pwm6"],
["pwm7", "pwm7"],
["pwm8", "pwm8"],
["pwm9", "pwm9"],
["pwm10", "pwm10"],
["pwm11", "pwm11"],
["pwm12", "pwm12"],
["pwm13", "pwm13"],
["pwm14", "pwm14"],
["pwm15", "pwm15"],
["pwm16", "pwm16"],
["pwm17", "pwm17"],
["pwm18", "pwm18"],
["pwm19", "pwm19"],
["pwm20", "pwm20"],
["pwm21", "pwm21"],
["pwm22", "pwm22"],
["pwm23", "pwm23"],
["pwm24", "pwm24"],
["pwm25", "pwm25"],
["pwm26", "pwm26"],
["pwm27", "pwm27"],
["pwm28", "pwm28"]
]
const pinTc = [
["tc0", "tc0"],
["tc1", "tc1"],
["tc2", "tc2"],
["tc3", "tc3"],
["tc4", "tc4"],
["tc5", "tc5"],
["tc6", "tc6"],
["tc7", "tc7"],
["tc8", "tc8"],
["tc9", "tc9"],
["tc10", "tc10"],
["tc11", "tc11"],
["tc12", "tc12"],
["tc13", "tc13"],
["tc14", "tc14"],
["tc15", "tc15"],
["tc16", "tc16"],
["tc17", "tc17"],
["tc18", "tc18"],
["tc19", "tc19"],
["tc20", "tc20"],
["tc21", "tc21"],
["tc22", "tc22"],
["tc23", "tc23"],
["tc24", "tc24"],
["tc25", "tc25"],
["tc26", "tc26"],
["tc27", "tc27"],
["tc28", "tc28"]
]
export const blocks = createBlockDefinitionsFromJsonArray([
{
type: 'io_pin',
style: 'set_blocks',
helpUrl: "",
tooltip: "",
inputsInline: true,
output: "Number",
message0: "%1",
args0: [
{
type: "field_dropdown",
name: "RATE",
options: pin
}
]
},
{
type: 'io_pinvar',
style: 'set_blocks',
helpUrl: "",
tooltip: "",
inputsInline: true,
output: "String",
message0: "%1",
args0: [
{
type: "field_dropdown",
name: "RATE",
options: pinVar
}
]
},
{
type: 'io_pinpro',
style: 'set_blocks',
helpUrl: "",
tooltip: "",
inputsInline: true,
output: "String",
message0: "%1",
args0: [
{
type: "field_dropdown",
name: "RATE",
options: pinPro
}
]
},
{
type: 'io_pinpwm',
style: 'set_blocks',
helpUrl: "",
tooltip: "",
inputsInline: true,
output: "String",
message0: "%1",
args0: [
{
type: "field_dropdown",
name: "RATE",
options: pinPwm
}
]
},
{
type: 'io_pintc',
style: 'set_blocks',
helpUrl: "",
tooltip: "",
inputsInline: true,
output: "String",
message0: "%1",
args0: [
{
type: "field_dropdown",
name: "RATE",
options: pinTc
}
]
},
{
type: "io_rate",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
output: "Number",
message0: "%{BKY_IO_RATE}",
args0: [
{
type: "field_dropdown",
name: "RATE",
options: [
["高", '1'],
["低", '0'],
],
},
],
},
{
type: "io_init_pin",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_INIT_PIN}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{
type: "field_dropdown",
name: "OPTION",
options: [
["数字输出", "OUT"],
["数字输入", "IN"],
["上拉输入", "PULL_UP"],
["下拉输入", "PULL_DOWN"],
],
},
{ type: "input_value", name: "NUM", check: "Number" },
],
},
{
type: "io_pinout_set",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_PINOUT_SET}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{ type: "input_value", name: "RATE" },
],
},
{
type: "io_pinin_set",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
output: "Number",
message0: "%{BKY_IO_PININ_SET}",
args0: [{ type: "input_value", name: "PIN", check: "String" }],
},
{
type: "io_init_pin_in",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_INIT_PIN_IN}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{ type: "input_value", name: "NUM", check: "Number" },
],
},
{
type: "io_in_voltage",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_IN_VOLTAGE}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{
type: "field_dropdown",
name: "VOLTAGE",
options: [
["3.3V", "ATTN_11DB"],
["2.2V", "ATTN_6DB"],
["1.5V", "ATTN_2_5DB"],
["1.2V", "ATTN_0DB"],
],
},
],
},
{
type: "io_in_get",
style: "set_blocks",
helpUrl: "",
tooltip: "",
output: "Number",
message0: "%{BKY_IO_IN_GET}",
args0: [{ type: "input_value", name: "PIN", check: "String" }],
},
{
type: "io_init_pwmout_set",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_INIT_PWMOUT_SET}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{ type: "field_dropdown", name: "RATE", options: pin },
],
},
{
type: "io_pwmout_set",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_PWMOUT_SET}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{ type: "input_value", name: "RATE", check: "Number" },
],
},
{
type: "io_pwmout_rate_set",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_PWMOUT_RATE_SET}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{ type: "input_value", name: "RATE", check: "Number" },
],
},
{
type: "io_init_touch_sensor_set",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_INIT_TOUCH_SENSOR_SET}",
args0: [
{ type: "input_value", name: "PIN", check: "String" },
{ type: "input_value", name: "TATE", check: "Number" },
],
},
{
type: "io_touch_sensor_get",
style: "set_blocks",
helpUrl: "",
tooltip: "",
output: "Number",
message0: "%{BKY_IO_TOUCH_SENSOR_GET}",
args0: [{
type: "field_dropdown",
name: "RATE",
options: pinTc
}],
},
{
type: "io_stop",
style: "set_blocks",
helpUrl: "",
tooltip: "",
inputsInline: true,
previousStatement: null,
nextStatement: null,
message0: "%{BKY_IO_STOP}",
args0: [
{ type: "input_value", name: "PIN", check: "Number" },
{
type: "field_dropdown",
name: "DIR",
options: [
["上升", "IRQ_RISING"],
["下降", "IRQ_FALLING"],
["改变", "ALL"],
],
},
{ type: "input_value", name: "FUNC" },
],
},
])
// Register provided blocks.
defineBlocks(blocks);
至此IO积木已经完成,看一下效果图
二:构建python
1,涉及要修改得文件(python,io这个是要自己创建)
2,python文件引入io文件,并在export导出python代码
io文件代码
/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file Generating Python for io blocks.
*/
// Former goog.module ID: Blockly.Python.io
import type {Block} from '../../core/block.js';
import type {PythonGenerator} from './python_generator.js';
import {Order} from './python_generator.js';
export function io_pin(
block: Block,
generator: PythonGenerator,
): [string, Order] {
let RATE = block.getFieldValue('RATE');
return [RATE, Order.NONE]
}
export function io_pinvar(
block: Block,
generator: PythonGenerator,
): [string, Order] {
let RATE = block.getFieldValue('RATE');
return [RATE, Order.NONE]
}
export function io_pinpro(
block: Block,
generator: PythonGenerator,
): [string, Order] {
let RATE = block.getFieldValue('RATE');
return [RATE, Order.NONE]
}
export function io_pinpwm(
block: Block,
generator: PythonGenerator,
): [string, Order] {
let RATE = block.getFieldValue('RATE');
return [RATE, Order.NONE]
}
export function io_pintc(
block: Block,
generator: PythonGenerator,
): [string, Order] {
let RATE = block.getFieldValue('RATE');
return [RATE, Order.NONE]
}
export function io_rate(
block: Block,
generator: PythonGenerator,
): [string, Order] {
let RATE = block.getFieldValue('RATE');
let code = RATE;
return [code, Order.NONE];
}
export function io_init_pin(block: Block, generator: PythonGenerator) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.MEMBER) || "''";
let OPTION = block.getFieldValue('OPTION');
let NUM = generator.valueToCode(block, 'NUM', Order.NONE) || '0';
let params = ''
if (OPTION === 'OUT') {
params = 'machine.Pin.OUT'
} else if (OPTION === 'IN') {
params = 'machine.Pin.IN'
} else if (OPTION === 'PULL_UP') {
params = 'machine.Pin.IN, machine.Pin.PULL_UP'
} else if (OPTION === 'PULL_DOWN') {
params = 'machine.Pin.IN, machine.Pin.PULL_DOWN'
}
let code = `${(PIN).replace(/'/g, '')} = machine.Pin(${NUM}, ${params})\n`;
return code;
}
export function io_pinout_set(block: Block, generator: PythonGenerator) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let RATE = generator.valueToCode(block, 'RATE', Order.NONE) || '0';
let code = `${PIN}.value(${RATE})\n`;
return code;
}
export function io_pinin_set(
block: Block,
generator: PythonGenerator,
): [string, Order] {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let code = `${PIN}.value()\n`;
return [code, Order.NONE];
}
export function io_init_pin_in(block: Block, generator: PythonGenerator) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let NUM = generator.valueToCode(block, 'NUM', Order.NONE) || '0';
let code = `${(PIN).replace(/'/g, '')} = machine.ADC(machine.Pin(${NUM}))\n`;
return code;
}
export function io_in_voltage(
block: Block,
generator: PythonGenerator,
) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let VOLTAGE = block.getFieldValue('VOLTAGE');
let code = `${PIN}.atten(machine.ADC.${VOLTAGE})\n`;
return code;
}
export function io_in_get(
block: Block,
generator: PythonGenerator,
): [string, Order] {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let code = `${PIN}.read_u16()\n`;
return [code, Order.NONE];
}
export function io_init_pwmout_set(block: Block, generator: PythonGenerator) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let RATE = block.getFieldValue('RATE');
let code = `${(PIN).replace(/'/g, '')} = machine.PWM(machine.Pin(${RATE}))\n`;
return code;
}
export function io_pwmout_set(block: Block, generator: PythonGenerator) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let RATE = generator.valueToCode(block, 'RATE', Order.NONE) || '0';
let code = `${PIN}.duty_u16(${RATE})\n`;
return code;
}
export function io_pwmout_rate_set(block: Block, generator: PythonGenerator) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let RATE = generator.valueToCode(block, 'RATE', Order.NONE) || '0';
let code = `${PIN}.freq(${RATE})\n`;
return code;
}
export function io_init_touch_sensor_set(
block: Block,
generator: PythonGenerator,
) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let TATE = generator.valueToCode(block, 'TATE', Order.NONE) || '0';
let code = `${(PIN).replace(/'/g, '')} = machine.TouchPad(machine.Pin(${TATE}))\n`;
return code;
}
export function io_touch_sensor_get(
block: Block,
generator: PythonGenerator,
): [string, Order] {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let RATE = block.getFieldValue('RATE');
let code = `${RATE}.read()\n`;
return [code, Order.NONE]
}
export function io_stop(block: Block, generator: PythonGenerator) {
(generator as AnyDuringMigration).definitions_['import_machine'] =
'import machine';
let PIN = generator.valueToCode(block, 'PIN', Order.NONE) || '0';
let DIR = block.getFieldValue('DIR');
let FUNC = generator.valueToCode(block, 'FUNC', Order.NONE) || '0';
let trigger = ''
if (DIR === 'IRQ_RISING') {
trigger = 'machine.Pin.IRQ_RISING'
} else if (DIR === 'IRQ_FALLING') {
trigger = 'machine.Pin.IRQ_FALLING'
} else if (DIR === 'ALL') {
trigger = '(machine.Pin.IRQ_RISING | machine.Pin.IRQ_FALLING)'
}
let code = `machine.Pin(${PIN}).irq(hander = ${(FUNC).replace(/'/g, '')}, trigger = ${trigger})\n`
return code;
}
至此io,python已经完成,看一下效果图
总结:IO积木开发难度也不算太大,对于能否运行这些功能,得看板子支不支持这些python内容