From 04a844130f0a2bc352c8647ae5b52bb68f1a5fde Mon Sep 17 00:00:00 2001 From: Kautism Date: Mon, 24 Mar 2025 20:26:27 +0800 Subject: [PATCH 1/2] Add board lgt8f328pNano --- src/devices/lgt8f328p/lgt8f328pNano.js | 927 +++++++++++++++++++++ src/extension-support/extension-manager.js | 3 +- 2 files changed, 929 insertions(+), 1 deletion(-) create mode 100644 src/devices/lgt8f328p/lgt8f328pNano.js diff --git a/src/devices/lgt8f328p/lgt8f328pNano.js b/src/devices/lgt8f328p/lgt8f328pNano.js new file mode 100644 index 00000000000..da929b371a5 --- /dev/null +++ b/src/devices/lgt8f328p/lgt8f328pNano.js @@ -0,0 +1,927 @@ +const formatMessage = require('format-message'); + +const ArgumentType = require('../../extension-support/argument-type'); +const BlockType = require('../../extension-support/block-type'); +const ProgramModeType = require('../../extension-support/program-mode-type'); + +const ArduinoPeripheral = require('../common/arduino-peripheral'); + +/** + * The list of USB device filters. + * @readonly + */ +const PNPID_LIST = [ + // For chinese clones that use CH340 + 'USB\\VID_1A86&PID_7523', + 'USB\\VID_04D9&PID_B534' +]; + +/** + * Configuration of serialport + * @readonly + */ +const SERIAL_CONFIG = { + baudRate: 57600, + dataBits: 8, + stopBits: 1 +}; + +/** + * Configuration for arduino-cli. + * @readonly + */ +const DIVECE_OPT = { + type: 'arduino', + fqbn: 'lgt8fx:avr:328', + firmware: 'lgt8f328pNano.hex' +}; + +const Pins = { + D0: '0', + D1: '1', + D2: '2', + D3: '3', + D4: '4', + D5: '5', + D6: '6', + D7: '7', + D8: '8', + D9: '9', + D10: '10', + D11: '11', + D12: '12', + D13: '13', + A0: 'A0', + A1: 'A1', + A2: 'A2', + A3: 'A3', + A4: 'A4', + A5: 'A5' +}; + + +const Level = { + High: 'HIGH', + Low: 'LOW' +}; + +const Buadrate = { + B19200: '19200', + B57600: '57600', + B115200: '115200' +}; + +const Eol = { + Warp: 'warp', + NoWarp: 'noWarp' +}; + +const Mode = { + Input: 'INPUT', + Output: 'OUTPUT', + InputPullup: 'INPUT_PULLUP' +}; + +const InterrupMode = { + Rising: 'RISING', + Falling: 'FALLING', + Change: 'CHANGE', + Low: 'LOW' +}; + +const DataType = { + Integer: 'INTEGER', + Decimal: 'DECIMAL', + String: 'STRING' +}; + +/** + * Manage communication with a Arduino Uno peripheral over a OpenBlock Link client socket. + */ +class lgt8f328pNano extends ArduinoPeripheral{ + /** + * Construct a Arduino communication object. + * @param {Runtime} runtime - the OpenBlock runtime + * @param {string} deviceId - the id of the extension + * @param {string} originalDeviceId - the original id of the peripheral, like xxx_lgt8f328pNano + */ + constructor (runtime, deviceId, originalDeviceId) { + super(runtime, deviceId, originalDeviceId, PNPID_LIST, SERIAL_CONFIG, DIVECE_OPT); + } +} + +/** + * OpenBlock blocks to interact with a Arduino Uno peripheral. + */ +class OpenBlocklgt8f328pNanoDevice { + /** + * @return {string} - the ID of this extension. + */ + get DEVICE_ID () { + return 'lgt8f328pNano'; + } + + get PINS_MENU () { + return [ + { + text: '0', + value: Pins.D0 + }, + { + text: '1', + value: Pins.D1 + }, + { + text: '2', + value: Pins.D2 + }, + { + text: '3', + value: Pins.D3 + }, + { + text: '4', + value: Pins.D4 + }, + { + text: '5', + value: Pins.D5 + }, + { + text: '6', + value: Pins.D6 + }, + { + text: '7', + value: Pins.D7 + }, + { + text: '8', + value: Pins.D8 + }, + { + text: '9', + value: Pins.D9 + }, + { + text: '10', + value: Pins.D10 + }, + { + text: '11', + value: Pins.D11 + }, + { + text: '12', + value: Pins.D12 + }, + { + text: '13', + value: Pins.D13 + }, + { + text: 'A0', + value: Pins.A0 + }, + { + text: 'A1', + value: Pins.A1 + }, + { + text: 'A2', + value: Pins.A2 + }, + { + text: 'A3', + value: Pins.A3 + }, + { + text: 'A4', + value: Pins.A4 + }, + { + text: 'A5', + value: Pins.A5 + } + ]; + } + + get MODE_MENU () { + return [ + { + text: formatMessage({ + id: 'lgt8f328pNano.modeMenu.input', + default: 'input', + description: 'label for input pin mode' + }), + value: Mode.Input + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.modeMenu.output', + default: 'output', + description: 'label for output pin mode' + }), + value: Mode.Output + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.modeMenu.inputPullup', + default: 'input-pullup', + description: 'label for input-pullup pin mode' + }), + value: Mode.InputPullup + } + ]; + } + + get ANALOG_PINS_MENU () { + return [ + { + text: 'A0', + value: Pins.A0 + }, + { + text: 'A1', + value: Pins.A1 + }, + { + text: 'A2', + value: Pins.A2 + }, + { + text: 'A3', + value: Pins.A3 + }, + { + text: 'A4', + value: Pins.A4 + }, + { + text: 'A5', + value: Pins.A5 + } + ]; + } + + get LEVEL_MENU () { + return [ + { + text: formatMessage({ + id: 'lgt8f328pNano.levelMenu.high', + default: 'high', + description: 'label for high level' + }), + value: Level.High + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.levelMenu.low', + default: 'low', + description: 'label for low level' + }), + value: Level.Low + } + ]; + } + + get PWM_PINS_MENU () { + return [ + { + text: '3', + value: Pins.D3 + }, + { + text: '5', + value: Pins.D5 + }, + { + text: '6', + value: Pins.D6 + }, + { + text: '9', + value: Pins.D9 + }, + { + text: '10', + value: Pins.D10 + }, + { + text: '11', + value: Pins.D11 + } + ]; + } + + get INTERRUPT_PINS_MENU () { + return [ + { + text: '2', + value: Pins.D2 + }, + { + text: '3', + value: Pins.D3 + } + ]; + } + + get INTERRUP_MODE_MENU () { + return [ + { + text: formatMessage({ + id: 'lgt8f328pNano.InterrupModeMenu.risingEdge', + default: 'rising edge', + description: 'label for rising edge interrup' + }), + value: InterrupMode.Rising + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.InterrupModeMenu.fallingEdge', + default: 'falling edge', + description: 'label for falling edge interrup' + }), + value: InterrupMode.Falling + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.InterrupModeMenu.changeEdge', + default: 'change edge', + description: 'label for change edge interrup' + }), + value: InterrupMode.Change + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.InterrupModeMenu.low', + default: 'low', + description: 'label for low interrup' + }), + value: InterrupMode.Low + } + ]; + } + + get BAUDTATE_MENU () { + return [ + { + text: '19200', + value: Buadrate.B19200 + }, + { + text: '57600', + value: Buadrate.B57600 + }, + { + text: '115200', + value: Buadrate.B115200 + } + ]; + } + + get EOL_MENU () { + return [ + { + text: formatMessage({ + id: 'lgt8f328pNano.eolMenu.warp', + default: 'warp', + description: 'label for warp print' + }), + value: Eol.Warp + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.eolMenu.noWarp', + default: 'no-warp', + description: 'label for no warp print' + }), + value: Eol.NoWarp + } + ]; + } + + get DATA_TYPE_MENU () { + return [ + { + text: formatMessage({ + id: 'lgt8f328pNano.dataTypeMenu.integer', + default: 'integer', + description: 'label for integer' + }), + value: DataType.Integer + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.dataTypeMenu.decimal', + default: 'decimal', + description: 'label for decimal number' + }), + value: DataType.Decimal + }, + { + text: formatMessage({ + id: 'lgt8f328pNano.dataTypeMenu.string', + default: 'string', + description: 'label for string' + }), + value: DataType.String + } + ]; + } + + /** + * Construct a set of Arduino blocks. + * @param {Runtime} runtime - the OpenBlock runtime. + * @param {string} originalDeviceId - the original id of the peripheral, like xxx_lgt8f328pNano + */ + constructor (runtime, originalDeviceId) { + /** + * The OpenBlock runtime. + * @type {Runtime} + */ + this.runtime = runtime; + + // Create a new Arduino uno peripheral instance + this._peripheral = new lgt8f328pNano(this.runtime, this.DEVICE_ID, originalDeviceId); + + this._peripheral.numDigitalPins = 14; + } + + /** + * @returns {Array.} metadata for this extension and its blocks. + */ + getInfo () { + return [ + { + id: 'pin', + name: formatMessage({ + id: 'lgt8f328pNano.category.pins', + default: 'Pins', + description: 'The name of the arduino uno device pin category' + }), + color1: '#4C97FF', + color2: '#3373CC', + color3: '#3373CC', + + blocks: [ + { + opcode: 'setPinMode', + text: formatMessage({ + id: 'lgt8f328pNano.pins.setPinMode', + default: 'set pin [PIN] mode [MODE]', + description: 'lgt8f328pNano set pin mode' + }), + blockType: BlockType.COMMAND, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'pins', + defaultValue: Pins.D0 + }, + MODE: { + type: ArgumentType.STRING, + menu: 'mode', + defaultValue: Mode.Input + } + } + }, + { + opcode: 'setDigitalOutput', + text: formatMessage({ + id: 'lgt8f328pNano.pins.setDigitalOutput', + default: 'set digital pin [PIN] out [LEVEL]', + description: 'lgt8f328pNano set digital pin out' + }), + blockType: BlockType.COMMAND, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'pins', + defaultValue: Pins.D0 + }, + LEVEL: { + type: ArgumentType.STRING, + menu: 'level', + defaultValue: Level.High + } + } + }, + { + + opcode: 'setPwmOutput', + text: formatMessage({ + id: 'lgt8f328pNano.pins.setPwmOutput', + default: 'set pwm pin [PIN] out [OUT]', + description: 'lgt8f328pNano set pwm pin out' + }), + blockType: BlockType.COMMAND, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'pwmPins', + defaultValue: Pins.D3 + }, + OUT: { + type: ArgumentType.UINT8_NUMBER, + defaultValue: '255' + } + } + }, + '---', + { + opcode: 'readDigitalPin', + text: formatMessage({ + id: 'lgt8f328pNano.pins.readDigitalPin', + default: 'read digital pin [PIN]', + description: 'lgt8f328pNano read digital pin' + }), + blockType: BlockType.BOOLEAN, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'pins', + defaultValue: Pins.D0 + } + } + }, + { + opcode: 'readAnalogPin', + text: formatMessage({ + id: 'lgt8f328pNano.pins.readAnalogPin', + default: 'read analog pin [PIN]', + description: 'lgt8f328pNano read analog pin' + }), + blockType: BlockType.REPORTER, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'analogPins', + defaultValue: Pins.A0 + } + } + }, + '---', + { + + opcode: 'setServoOutput', + text: formatMessage({ + id: 'lgt8f328pNano.pins.setServoOutput', + default: 'set servo pin [PIN] out [OUT]', + description: 'lgt8f328pNano set servo pin out' + }), + blockType: BlockType.COMMAND, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'pwmPins', + defaultValue: Pins.D3 + }, + OUT: { + type: ArgumentType.HALF_ANGLE, + defaultValue: '90' + } + } + }, + '---', + { + + opcode: 'attachInterrupt', + text: formatMessage({ + id: 'lgt8f328pNano.pins.attachInterrupt', + default: 'attach interrupt pin [PIN] mode [MODE] executes', + description: 'lgt8f328pNano attach interrupt' + }), + blockType: BlockType.CONDITIONAL, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'interruptPins', + defaultValue: Pins.D3 + }, + MODE: { + type: ArgumentType.STRING, + menu: 'interruptMode', + defaultValue: InterrupMode.Rising + } + }, + programMode: [ProgramModeType.UPLOAD] + }, + { + + opcode: 'detachInterrupt', + text: formatMessage({ + id: 'lgt8f328pNano.pins.detachInterrupt', + default: 'detach interrupt pin [PIN]', + description: 'lgt8f328pNano detach interrupt' + }), + blockType: BlockType.COMMAND, + arguments: { + PIN: { + type: ArgumentType.STRING, + menu: 'interruptPins', + defaultValue: Pins.D3 + } + }, + programMode: [ProgramModeType.UPLOAD] + } + ], + menus: { + pins: { + items: this.PINS_MENU + }, + mode: { + items: this.MODE_MENU + }, + analogPins: { + items: this.ANALOG_PINS_MENU + }, + level: { + acceptReporters: true, + items: this.LEVEL_MENU + }, + pwmPins: { + items: this.PWM_PINS_MENU + }, + interruptPins: { + items: this.INTERRUPT_PINS_MENU + }, + interruptMode: { + items: this.INTERRUP_MODE_MENU + } + } + }, + { + id: 'serial', + name: formatMessage({ + id: 'lgt8f328pNano.category.serial', + default: 'Serial', + description: 'The name of the arduino uno device serial category' + }), + color1: '#9966FF', + color2: '#774DCB', + color3: '#774DCB', + + blocks: [ + { + opcode: 'serialBegin', + text: formatMessage({ + id: 'lgt8f328pNano.serial.serialBegin', + default: 'serial begin baudrate [VALUE]', + description: 'lgt8f328pNano serial begin' + }), + blockType: BlockType.COMMAND, + arguments: { + VALUE: { + type: ArgumentType.STRING, + menu: 'baudrate', + defaultValue: Buadrate.B57600 + } + }, + programMode: [ProgramModeType.UPLOAD] + }, + { + opcode: 'serialPrint', + text: formatMessage({ + id: 'lgt8f328pNano.serial.serialPrint', + default: 'serial print [VALUE] [EOL]', + description: 'lgt8f328pNano serial print' + }), + blockType: BlockType.COMMAND, + arguments: { + VALUE: { + type: ArgumentType.STRING, + defaultValue: 'Hello OpenBlock' + }, + EOL: { + type: ArgumentType.STRING, + menu: 'eol', + defaultValue: Eol.Warp + } + }, + programMode: [ProgramModeType.UPLOAD] + }, + { + opcode: 'serialAvailable', + text: formatMessage({ + id: 'lgt8f328pNano.serial.serialAvailable', + default: 'serial available data length', + description: 'lgt8f328pNano serial available data length' + }), + blockType: BlockType.REPORTER, + disableMonitor: true, + programMode: [ProgramModeType.UPLOAD] + }, + { + opcode: 'serialReadAByte', + text: formatMessage({ + id: 'lgt8f328pNano.serial.serialReadAByte', + default: 'serial read a byte', + description: 'lgt8f328pNano serial read a byte' + }), + blockType: BlockType.REPORTER, + disableMonitor: true, + programMode: [ProgramModeType.UPLOAD] + } + ], + menus: { + baudrate: { + items: this.BAUDTATE_MENU + }, + eol: { + items: this.EOL_MENU + } + } + }, + { + id: 'data', + name: formatMessage({ + id: 'lgt8f328pNano.category.data', + default: 'Data', + description: 'The name of the arduino uno device data category' + }), + color1: '#CF63CF', + color2: '#C94FC9', + color3: '#BD42BD', + blocks: [ + { + opcode: 'dataMap', + text: formatMessage({ + id: 'lgt8f328pNano.data.dataMap', + default: 'map [DATA] from ([ARG0], [ARG1]) to ([ARG2], [ARG3])', + description: 'lgt8f328pNano data map' + }), + blockType: BlockType.REPORTER, + arguments: { + DATA: { + type: ArgumentType.NUMBER, + defaultValue: '50' + }, + ARG0: { + type: ArgumentType.NUMBER, + defaultValue: '1' + }, + ARG1: { + type: ArgumentType.NUMBER, + defaultValue: '100' + }, + ARG2: { + type: ArgumentType.NUMBER, + defaultValue: '1' + }, + ARG3: { + type: ArgumentType.NUMBER, + defaultValue: '1000' + } + }, + programMode: [ProgramModeType.UPLOAD] + }, + { + opcode: 'dataConstrain', + text: formatMessage({ + id: 'lgt8f328pNano.data.dataConstrain', + default: 'constrain [DATA] between ([ARG0], [ARG1])', + description: 'lgt8f328pNano data constrain' + }), + blockType: BlockType.REPORTER, + arguments: { + DATA: { + type: ArgumentType.NUMBER, + defaultValue: '50' + }, + ARG0: { + type: ArgumentType.NUMBER, + defaultValue: '1' + }, + ARG1: { + type: ArgumentType.NUMBER, + defaultValue: '100' + } + }, + programMode: [ProgramModeType.UPLOAD] + }, + '---', + { + opcode: 'dataConvert', + text: formatMessage({ + id: 'lgt8f328pNano.data.dataConvert', + default: 'convert [DATA] to [TYPE]', + description: 'lgt8f328pNano data convert' + }), + blockType: BlockType.REPORTER, + arguments: { + DATA: { + type: ArgumentType.STRING, + defaultValue: '123' + }, + TYPE: { + type: ArgumentType.STRING, + menu: 'dataType', + defaultValue: DataType.Integer + } + }, + programMode: [ProgramModeType.UPLOAD] + }, + { + opcode: 'dataConvertASCIICharacter', + text: formatMessage({ + id: 'lgt8f328pNano.data.dataConvertASCIICharacter', + default: 'convert [DATA] to ASCII character', + description: 'lgt8f328pNano data convert to ASCII character' + }), + blockType: BlockType.REPORTER, + arguments: { + DATA: { + type: ArgumentType.NUMBER, + defaultValue: '97' + } + }, + programMode: [ProgramModeType.UPLOAD] + }, + { + opcode: 'dataConvertASCIINumber', + text: formatMessage({ + id: 'lgt8f328pNano.data.dataConvertASCIINumber', + default: 'convert [DATA] to ASCII nubmer', + description: 'lgt8f328pNano data convert to ASCII nubmer' + }), + blockType: BlockType.REPORTER, + arguments: { + DATA: { + type: ArgumentType.STRING, + defaultValue: 'a' + } + }, + programMode: [ProgramModeType.UPLOAD] + } + ], + menus: { + dataType: { + items: this.DATA_TYPE_MENU + } + } + } + ]; + } + + /** + * Set pin mode. + * @param {object} args - the block's arguments. + * @return {Promise} - a Promise that resolves after the set pin mode is done. + */ + setPinMode (args) { + this._peripheral.setPinMode(args.PIN, args.MODE); + return Promise.resolve(); + } + + /** + * Set pin digital out level. + * @param {object} args - the block's arguments. + * @return {Promise} - a Promise that resolves after the set pin digital out level is done. + */ + setDigitalOutput (args) { + this._peripheral.setDigitalOutput(args.PIN, args.LEVEL); + return Promise.resolve(); + } + + /** + * Set pin pwm out value. + * @param {object} args - the block's arguments. + * @return {Promise} - a Promise that resolves after the set pin pwm out value is done. + */ + setPwmOutput (args) { + this._peripheral.setPwmOutput(args.PIN, args.OUT); + return Promise.resolve(); + } + + /** + * Read pin digital level. + * @param {object} args - the block's arguments. + * @return {boolean} - true if read high level, false if read low level. + */ + readDigitalPin (args) { + return this._peripheral.readDigitalPin(args.PIN); + } + + /** + * Read analog pin. + * @param {object} args - the block's arguments. + * @return {number} - analog value fo the pin. + */ + readAnalogPin (args) { + return this._peripheral.readAnalogPin(args.PIN); + } + + /** + * Set servo out put. + * @param {object} args - the block's arguments. + * @return {Promise} - a Promise that resolves after the set servo out value is done. + */ + setServoOutput (args) { + this._peripheral.setServoOutput(args.PIN, args.OUT); + return Promise.resolve(); + } +} + +module.exports = OpenBlocklgt8f328pNanoDevice; diff --git a/src/extension-support/extension-manager.js b/src/extension-support/extension-manager.js index 338b82fa1f3..5bcd96709c0 100644 --- a/src/extension-support/extension-manager.js +++ b/src/extension-support/extension-manager.js @@ -53,7 +53,8 @@ const builtinDevices = { arduinoRaspberryPiPico: () => require('../devices/arduinoRaspberryPiPico/arduinoRaspberryPiPico'), // Microbit microbit: () => require('../devices/microbit/microbit'), - microbitV2: () => require('../devices/microbit/microbitV2') + microbitV2: () => require('../devices/microbit/microbitV2'), + lgt8f328pNano: () => require('../devices/lgt8f328p/lgt8f328pNano') // TODO: transform these to device extension. // wedo2: () => require('../extensions/scratch3_wedo2'), From f9814b8d30639eb62d2879e846bef6d50cfe2aa9 Mon Sep 17 00:00:00 2001 From: Kautism Date: Mon, 24 Mar 2025 21:02:33 +0800 Subject: [PATCH 2/2] Change using nano to prevent re lang translation --- src/devices/lgt8f328p/lgt8f328pNano.js | 845 +------------------------ 1 file changed, 30 insertions(+), 815 deletions(-) diff --git a/src/devices/lgt8f328p/lgt8f328pNano.js b/src/devices/lgt8f328p/lgt8f328pNano.js index da929b371a5..7b9e13cf1f7 100644 --- a/src/devices/lgt8f328p/lgt8f328pNano.js +++ b/src/devices/lgt8f328p/lgt8f328pNano.js @@ -1,8 +1,11 @@ -const formatMessage = require('format-message'); - -const ArgumentType = require('../../extension-support/argument-type'); -const BlockType = require('../../extension-support/block-type'); -const ProgramModeType = require('../../extension-support/program-mode-type'); +/** + * Arduino Nano + * + * @overview Compared to the Arduino Uno, this control board use CH340 as + * use to uart chip, uese oldbootloader to flash the firmware, and there are + * more A6 and A7 pin options. + */ +const OpenBlockArduinoUnoDevice = require('../arduinoUno/arduinoUno'); const ArduinoPeripheral = require('../common/arduino-peripheral'); @@ -11,8 +14,6 @@ const ArduinoPeripheral = require('../common/arduino-peripheral'); * @readonly */ const PNPID_LIST = [ - // For chinese clones that use CH340 - 'USB\\VID_1A86&PID_7523', 'USB\\VID_04D9&PID_B534' ]; @@ -35,7 +36,7 @@ const DIVECE_OPT = { fqbn: 'lgt8fx:avr:328', firmware: 'lgt8f328pNano.hex' }; - + const Pins = { D0: '0', D1: '1', @@ -56,54 +57,20 @@ const Pins = { A2: 'A2', A3: 'A3', A4: 'A4', - A5: 'A5' -}; - - -const Level = { - High: 'HIGH', - Low: 'LOW' -}; - -const Buadrate = { - B19200: '19200', - B57600: '57600', - B115200: '115200' -}; - -const Eol = { - Warp: 'warp', - NoWarp: 'noWarp' -}; - -const Mode = { - Input: 'INPUT', - Output: 'OUTPUT', - InputPullup: 'INPUT_PULLUP' -}; - -const InterrupMode = { - Rising: 'RISING', - Falling: 'FALLING', - Change: 'CHANGE', - Low: 'LOW' -}; - -const DataType = { - Integer: 'INTEGER', - Decimal: 'DECIMAL', - String: 'STRING' + A5: 'A5', + A6: 'A6', + A7: 'A7' }; /** - * Manage communication with a Arduino Uno peripheral over a OpenBlock Link client socket. + * Manage communication with a Arduino Nano peripheral over a OpenBlock Link client socket. */ -class lgt8f328pNano extends ArduinoPeripheral{ +class Lgt8f328pNano extends ArduinoPeripheral{ /** * Construct a Arduino communication object. * @param {Runtime} runtime - the OpenBlock runtime * @param {string} deviceId - the id of the extension - * @param {string} originalDeviceId - the original id of the peripheral, like xxx_lgt8f328pNano + * @param {string} originalDeviceId - the original id of the peripheral, like xxx_arduinoUno */ constructor (runtime, deviceId, originalDeviceId) { super(runtime, deviceId, originalDeviceId, PNPID_LIST, SERIAL_CONFIG, DIVECE_OPT); @@ -111,130 +78,17 @@ class lgt8f328pNano extends ArduinoPeripheral{ } /** - * OpenBlock blocks to interact with a Arduino Uno peripheral. - */ -class OpenBlocklgt8f328pNanoDevice { + * OpenBlock blocks to interact with a Arduino Nano Ultra peripheral. + */ +class OpenBlockLgt8f328pNanoDevice extends OpenBlockArduinoUnoDevice{ + /** - * @return {string} - the ID of this extension. - */ + * @return {string} - the ID of this extension. + */ get DEVICE_ID () { return 'lgt8f328pNano'; } - get PINS_MENU () { - return [ - { - text: '0', - value: Pins.D0 - }, - { - text: '1', - value: Pins.D1 - }, - { - text: '2', - value: Pins.D2 - }, - { - text: '3', - value: Pins.D3 - }, - { - text: '4', - value: Pins.D4 - }, - { - text: '5', - value: Pins.D5 - }, - { - text: '6', - value: Pins.D6 - }, - { - text: '7', - value: Pins.D7 - }, - { - text: '8', - value: Pins.D8 - }, - { - text: '9', - value: Pins.D9 - }, - { - text: '10', - value: Pins.D10 - }, - { - text: '11', - value: Pins.D11 - }, - { - text: '12', - value: Pins.D12 - }, - { - text: '13', - value: Pins.D13 - }, - { - text: 'A0', - value: Pins.A0 - }, - { - text: 'A1', - value: Pins.A1 - }, - { - text: 'A2', - value: Pins.A2 - }, - { - text: 'A3', - value: Pins.A3 - }, - { - text: 'A4', - value: Pins.A4 - }, - { - text: 'A5', - value: Pins.A5 - } - ]; - } - - get MODE_MENU () { - return [ - { - text: formatMessage({ - id: 'lgt8f328pNano.modeMenu.input', - default: 'input', - description: 'label for input pin mode' - }), - value: Mode.Input - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.modeMenu.output', - default: 'output', - description: 'label for output pin mode' - }), - value: Mode.Output - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.modeMenu.inputPullup', - default: 'input-pullup', - description: 'label for input-pullup pin mode' - }), - value: Mode.InputPullup - } - ]; - } - get ANALOG_PINS_MENU () { return [ { @@ -260,173 +114,14 @@ class OpenBlocklgt8f328pNanoDevice { { text: 'A5', value: Pins.A5 - } - ]; - } - - get LEVEL_MENU () { - return [ - { - text: formatMessage({ - id: 'lgt8f328pNano.levelMenu.high', - default: 'high', - description: 'label for high level' - }), - value: Level.High }, { - text: formatMessage({ - id: 'lgt8f328pNano.levelMenu.low', - default: 'low', - description: 'label for low level' - }), - value: Level.Low - } - ]; - } - - get PWM_PINS_MENU () { - return [ - { - text: '3', - value: Pins.D3 + text: 'A6', + value: Pins.A6 }, { - text: '5', - value: Pins.D5 - }, - { - text: '6', - value: Pins.D6 - }, - { - text: '9', - value: Pins.D9 - }, - { - text: '10', - value: Pins.D10 - }, - { - text: '11', - value: Pins.D11 - } - ]; - } - - get INTERRUPT_PINS_MENU () { - return [ - { - text: '2', - value: Pins.D2 - }, - { - text: '3', - value: Pins.D3 - } - ]; - } - - get INTERRUP_MODE_MENU () { - return [ - { - text: formatMessage({ - id: 'lgt8f328pNano.InterrupModeMenu.risingEdge', - default: 'rising edge', - description: 'label for rising edge interrup' - }), - value: InterrupMode.Rising - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.InterrupModeMenu.fallingEdge', - default: 'falling edge', - description: 'label for falling edge interrup' - }), - value: InterrupMode.Falling - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.InterrupModeMenu.changeEdge', - default: 'change edge', - description: 'label for change edge interrup' - }), - value: InterrupMode.Change - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.InterrupModeMenu.low', - default: 'low', - description: 'label for low interrup' - }), - value: InterrupMode.Low - } - ]; - } - - get BAUDTATE_MENU () { - return [ - { - text: '19200', - value: Buadrate.B19200 - }, - { - text: '57600', - value: Buadrate.B57600 - }, - { - text: '115200', - value: Buadrate.B115200 - } - ]; - } - - get EOL_MENU () { - return [ - { - text: formatMessage({ - id: 'lgt8f328pNano.eolMenu.warp', - default: 'warp', - description: 'label for warp print' - }), - value: Eol.Warp - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.eolMenu.noWarp', - default: 'no-warp', - description: 'label for no warp print' - }), - value: Eol.NoWarp - } - ]; - } - - get DATA_TYPE_MENU () { - return [ - { - text: formatMessage({ - id: 'lgt8f328pNano.dataTypeMenu.integer', - default: 'integer', - description: 'label for integer' - }), - value: DataType.Integer - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.dataTypeMenu.decimal', - default: 'decimal', - description: 'label for decimal number' - }), - value: DataType.Decimal - }, - { - text: formatMessage({ - id: 'lgt8f328pNano.dataTypeMenu.string', - default: 'string', - description: 'label for string' - }), - value: DataType.String + text: 'A7', + value: Pins.A7 } ]; } @@ -434,494 +129,14 @@ class OpenBlocklgt8f328pNanoDevice { /** * Construct a set of Arduino blocks. * @param {Runtime} runtime - the OpenBlock runtime. - * @param {string} originalDeviceId - the original id of the peripheral, like xxx_lgt8f328pNano + * @param {string} originalDeviceId - the original id of the peripheral, like xxx_arduinoUno */ constructor (runtime, originalDeviceId) { - /** - * The OpenBlock runtime. - * @type {Runtime} - */ - this.runtime = runtime; - - // Create a new Arduino uno peripheral instance - this._peripheral = new lgt8f328pNano(this.runtime, this.DEVICE_ID, originalDeviceId); + super(runtime, originalDeviceId); - this._peripheral.numDigitalPins = 14; - } - - /** - * @returns {Array.} metadata for this extension and its blocks. - */ - getInfo () { - return [ - { - id: 'pin', - name: formatMessage({ - id: 'lgt8f328pNano.category.pins', - default: 'Pins', - description: 'The name of the arduino uno device pin category' - }), - color1: '#4C97FF', - color2: '#3373CC', - color3: '#3373CC', - - blocks: [ - { - opcode: 'setPinMode', - text: formatMessage({ - id: 'lgt8f328pNano.pins.setPinMode', - default: 'set pin [PIN] mode [MODE]', - description: 'lgt8f328pNano set pin mode' - }), - blockType: BlockType.COMMAND, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'pins', - defaultValue: Pins.D0 - }, - MODE: { - type: ArgumentType.STRING, - menu: 'mode', - defaultValue: Mode.Input - } - } - }, - { - opcode: 'setDigitalOutput', - text: formatMessage({ - id: 'lgt8f328pNano.pins.setDigitalOutput', - default: 'set digital pin [PIN] out [LEVEL]', - description: 'lgt8f328pNano set digital pin out' - }), - blockType: BlockType.COMMAND, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'pins', - defaultValue: Pins.D0 - }, - LEVEL: { - type: ArgumentType.STRING, - menu: 'level', - defaultValue: Level.High - } - } - }, - { - - opcode: 'setPwmOutput', - text: formatMessage({ - id: 'lgt8f328pNano.pins.setPwmOutput', - default: 'set pwm pin [PIN] out [OUT]', - description: 'lgt8f328pNano set pwm pin out' - }), - blockType: BlockType.COMMAND, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'pwmPins', - defaultValue: Pins.D3 - }, - OUT: { - type: ArgumentType.UINT8_NUMBER, - defaultValue: '255' - } - } - }, - '---', - { - opcode: 'readDigitalPin', - text: formatMessage({ - id: 'lgt8f328pNano.pins.readDigitalPin', - default: 'read digital pin [PIN]', - description: 'lgt8f328pNano read digital pin' - }), - blockType: BlockType.BOOLEAN, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'pins', - defaultValue: Pins.D0 - } - } - }, - { - opcode: 'readAnalogPin', - text: formatMessage({ - id: 'lgt8f328pNano.pins.readAnalogPin', - default: 'read analog pin [PIN]', - description: 'lgt8f328pNano read analog pin' - }), - blockType: BlockType.REPORTER, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'analogPins', - defaultValue: Pins.A0 - } - } - }, - '---', - { - - opcode: 'setServoOutput', - text: formatMessage({ - id: 'lgt8f328pNano.pins.setServoOutput', - default: 'set servo pin [PIN] out [OUT]', - description: 'lgt8f328pNano set servo pin out' - }), - blockType: BlockType.COMMAND, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'pwmPins', - defaultValue: Pins.D3 - }, - OUT: { - type: ArgumentType.HALF_ANGLE, - defaultValue: '90' - } - } - }, - '---', - { - - opcode: 'attachInterrupt', - text: formatMessage({ - id: 'lgt8f328pNano.pins.attachInterrupt', - default: 'attach interrupt pin [PIN] mode [MODE] executes', - description: 'lgt8f328pNano attach interrupt' - }), - blockType: BlockType.CONDITIONAL, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'interruptPins', - defaultValue: Pins.D3 - }, - MODE: { - type: ArgumentType.STRING, - menu: 'interruptMode', - defaultValue: InterrupMode.Rising - } - }, - programMode: [ProgramModeType.UPLOAD] - }, - { - - opcode: 'detachInterrupt', - text: formatMessage({ - id: 'lgt8f328pNano.pins.detachInterrupt', - default: 'detach interrupt pin [PIN]', - description: 'lgt8f328pNano detach interrupt' - }), - blockType: BlockType.COMMAND, - arguments: { - PIN: { - type: ArgumentType.STRING, - menu: 'interruptPins', - defaultValue: Pins.D3 - } - }, - programMode: [ProgramModeType.UPLOAD] - } - ], - menus: { - pins: { - items: this.PINS_MENU - }, - mode: { - items: this.MODE_MENU - }, - analogPins: { - items: this.ANALOG_PINS_MENU - }, - level: { - acceptReporters: true, - items: this.LEVEL_MENU - }, - pwmPins: { - items: this.PWM_PINS_MENU - }, - interruptPins: { - items: this.INTERRUPT_PINS_MENU - }, - interruptMode: { - items: this.INTERRUP_MODE_MENU - } - } - }, - { - id: 'serial', - name: formatMessage({ - id: 'lgt8f328pNano.category.serial', - default: 'Serial', - description: 'The name of the arduino uno device serial category' - }), - color1: '#9966FF', - color2: '#774DCB', - color3: '#774DCB', - - blocks: [ - { - opcode: 'serialBegin', - text: formatMessage({ - id: 'lgt8f328pNano.serial.serialBegin', - default: 'serial begin baudrate [VALUE]', - description: 'lgt8f328pNano serial begin' - }), - blockType: BlockType.COMMAND, - arguments: { - VALUE: { - type: ArgumentType.STRING, - menu: 'baudrate', - defaultValue: Buadrate.B57600 - } - }, - programMode: [ProgramModeType.UPLOAD] - }, - { - opcode: 'serialPrint', - text: formatMessage({ - id: 'lgt8f328pNano.serial.serialPrint', - default: 'serial print [VALUE] [EOL]', - description: 'lgt8f328pNano serial print' - }), - blockType: BlockType.COMMAND, - arguments: { - VALUE: { - type: ArgumentType.STRING, - defaultValue: 'Hello OpenBlock' - }, - EOL: { - type: ArgumentType.STRING, - menu: 'eol', - defaultValue: Eol.Warp - } - }, - programMode: [ProgramModeType.UPLOAD] - }, - { - opcode: 'serialAvailable', - text: formatMessage({ - id: 'lgt8f328pNano.serial.serialAvailable', - default: 'serial available data length', - description: 'lgt8f328pNano serial available data length' - }), - blockType: BlockType.REPORTER, - disableMonitor: true, - programMode: [ProgramModeType.UPLOAD] - }, - { - opcode: 'serialReadAByte', - text: formatMessage({ - id: 'lgt8f328pNano.serial.serialReadAByte', - default: 'serial read a byte', - description: 'lgt8f328pNano serial read a byte' - }), - blockType: BlockType.REPORTER, - disableMonitor: true, - programMode: [ProgramModeType.UPLOAD] - } - ], - menus: { - baudrate: { - items: this.BAUDTATE_MENU - }, - eol: { - items: this.EOL_MENU - } - } - }, - { - id: 'data', - name: formatMessage({ - id: 'lgt8f328pNano.category.data', - default: 'Data', - description: 'The name of the arduino uno device data category' - }), - color1: '#CF63CF', - color2: '#C94FC9', - color3: '#BD42BD', - blocks: [ - { - opcode: 'dataMap', - text: formatMessage({ - id: 'lgt8f328pNano.data.dataMap', - default: 'map [DATA] from ([ARG0], [ARG1]) to ([ARG2], [ARG3])', - description: 'lgt8f328pNano data map' - }), - blockType: BlockType.REPORTER, - arguments: { - DATA: { - type: ArgumentType.NUMBER, - defaultValue: '50' - }, - ARG0: { - type: ArgumentType.NUMBER, - defaultValue: '1' - }, - ARG1: { - type: ArgumentType.NUMBER, - defaultValue: '100' - }, - ARG2: { - type: ArgumentType.NUMBER, - defaultValue: '1' - }, - ARG3: { - type: ArgumentType.NUMBER, - defaultValue: '1000' - } - }, - programMode: [ProgramModeType.UPLOAD] - }, - { - opcode: 'dataConstrain', - text: formatMessage({ - id: 'lgt8f328pNano.data.dataConstrain', - default: 'constrain [DATA] between ([ARG0], [ARG1])', - description: 'lgt8f328pNano data constrain' - }), - blockType: BlockType.REPORTER, - arguments: { - DATA: { - type: ArgumentType.NUMBER, - defaultValue: '50' - }, - ARG0: { - type: ArgumentType.NUMBER, - defaultValue: '1' - }, - ARG1: { - type: ArgumentType.NUMBER, - defaultValue: '100' - } - }, - programMode: [ProgramModeType.UPLOAD] - }, - '---', - { - opcode: 'dataConvert', - text: formatMessage({ - id: 'lgt8f328pNano.data.dataConvert', - default: 'convert [DATA] to [TYPE]', - description: 'lgt8f328pNano data convert' - }), - blockType: BlockType.REPORTER, - arguments: { - DATA: { - type: ArgumentType.STRING, - defaultValue: '123' - }, - TYPE: { - type: ArgumentType.STRING, - menu: 'dataType', - defaultValue: DataType.Integer - } - }, - programMode: [ProgramModeType.UPLOAD] - }, - { - opcode: 'dataConvertASCIICharacter', - text: formatMessage({ - id: 'lgt8f328pNano.data.dataConvertASCIICharacter', - default: 'convert [DATA] to ASCII character', - description: 'lgt8f328pNano data convert to ASCII character' - }), - blockType: BlockType.REPORTER, - arguments: { - DATA: { - type: ArgumentType.NUMBER, - defaultValue: '97' - } - }, - programMode: [ProgramModeType.UPLOAD] - }, - { - opcode: 'dataConvertASCIINumber', - text: formatMessage({ - id: 'lgt8f328pNano.data.dataConvertASCIINumber', - default: 'convert [DATA] to ASCII nubmer', - description: 'lgt8f328pNano data convert to ASCII nubmer' - }), - blockType: BlockType.REPORTER, - arguments: { - DATA: { - type: ArgumentType.STRING, - defaultValue: 'a' - } - }, - programMode: [ProgramModeType.UPLOAD] - } - ], - menus: { - dataType: { - items: this.DATA_TYPE_MENU - } - } - } - ]; - } - - /** - * Set pin mode. - * @param {object} args - the block's arguments. - * @return {Promise} - a Promise that resolves after the set pin mode is done. - */ - setPinMode (args) { - this._peripheral.setPinMode(args.PIN, args.MODE); - return Promise.resolve(); - } - - /** - * Set pin digital out level. - * @param {object} args - the block's arguments. - * @return {Promise} - a Promise that resolves after the set pin digital out level is done. - */ - setDigitalOutput (args) { - this._peripheral.setDigitalOutput(args.PIN, args.LEVEL); - return Promise.resolve(); - } - - /** - * Set pin pwm out value. - * @param {object} args - the block's arguments. - * @return {Promise} - a Promise that resolves after the set pin pwm out value is done. - */ - setPwmOutput (args) { - this._peripheral.setPwmOutput(args.PIN, args.OUT); - return Promise.resolve(); - } - - /** - * Read pin digital level. - * @param {object} args - the block's arguments. - * @return {boolean} - true if read high level, false if read low level. - */ - readDigitalPin (args) { - return this._peripheral.readDigitalPin(args.PIN); - } - - /** - * Read analog pin. - * @param {object} args - the block's arguments. - * @return {number} - analog value fo the pin. - */ - readAnalogPin (args) { - return this._peripheral.readAnalogPin(args.PIN); - } - - /** - * Set servo out put. - * @param {object} args - the block's arguments. - * @return {Promise} - a Promise that resolves after the set servo out value is done. - */ - setServoOutput (args) { - this._peripheral.setServoOutput(args.PIN, args.OUT); - return Promise.resolve(); + // Create a new Arduino Nano peripheral instance + this._peripheral = new Lgt8f328pNano(this.runtime, this.DEVICE_ID, originalDeviceId); } } -module.exports = OpenBlocklgt8f328pNanoDevice; +module.exports = OpenBlockLgt8f328pNanoDevice; \ No newline at end of file