Skip to content

Commit 2efda69

Browse files
author
harbaum
committed
Update scratch 3
1 parent bb25dc9 commit 2efda69

1 file changed

Lines changed: 184 additions & 65 deletions

File tree

webusb/scratch3/lib.min.js

Lines changed: 184 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -265530,6 +265530,7 @@ var IOSTATE = {
265530265530
DONE: 5
265531265531
};
265532265532
var MODE = {
265533+
UNSPEC: "unspecified",
265533265534
SWITCH: "switch",
265534265535
VOLTAGE: "voltage",
265535265536
RESISTANCE: "resistance"
@@ -265697,6 +265698,15 @@ function () {
265697265698
value: pwm
265698265699
});
265699265700
}
265701+
}, {
265702+
key: "ftdSetMotor",
265703+
value: function ftdSetMotor(port, dir, pwm) {
265704+
this.ftdSet({
265705+
port: port,
265706+
mode: dir,
265707+
value: pwm
265708+
});
265709+
}
265700265710
}, {
265701265711
key: "ftdSetMode",
265702265712
value: function ftdSetMode(port, mode) {
@@ -265757,16 +265767,17 @@ function () {
265757265767

265758265768
this.setButton(STATE.CONNECTED); // no IO pending yet
265759265769

265760-
this.iostate = IOSTATE.IDLE;
265770+
this.iostate = IOSTATE.IDLE; // current input modes
265771+
265761265772
this.input_mode = {
265762-
"i1": MODE.SWITCH,
265763-
"i2": MODE.SWITCH,
265764-
"i3": MODE.SWITCH,
265765-
"i4": MODE.SWITCH,
265766-
"i5": MODE.SWITCH,
265767-
"i6": MODE.SWITCH,
265768-
"i7": MODE.SWITCH,
265769-
"i8": MODE.SWITCH
265773+
"i1": MODE.UNSPEC,
265774+
"i2": MODE.UNSPEC,
265775+
"i3": MODE.UNSPEC,
265776+
"i4": MODE.UNSPEC,
265777+
"i5": MODE.UNSPEC,
265778+
"i6": MODE.UNSPEC,
265779+
"i7": MODE.UNSPEC,
265780+
"i8": MODE.UNSPEC
265770265781
};
265771265782
}
265772265783
}, {
@@ -265978,6 +265989,7 @@ function () {
265978265989
text: formatMessage({
265979265990
id: 'ftduino.led',
265980265991
default: 'LED [VALUE]',
265992+
// \u26ef
265981265993
description: 'set the ftDuino led'
265982265994
}),
265983265995
blockType: BlockType.COMMAND,
@@ -266004,30 +266016,50 @@ function () {
266004266016
}
266005266017
}
266006266018
}, {
266019+
opcode: 'output',
266020+
text: formatMessage({
266021+
id: 'ftduino.output',
266022+
default: '[OUTPUT] [VALUE]',
266023+
description: 'set an ftDuino output'
266024+
}),
266025+
blockType: BlockType.COMMAND,
266026+
arguments: {
266027+
OUTPUT: {
266028+
type: ArgumentType.STRING,
266029+
menu: 'OUTPUT',
266030+
defaultValue: 'o1'
266031+
},
266032+
VALUE: {
266033+
type: ArgumentType.STRING,
266034+
menu: 'ONOFFSTATE',
266035+
defaultValue: '1'
266036+
}
266037+
}
266038+
}, '---', {
266007266039
opcode: 'input_analog',
266008266040
text: formatMessage({
266009266041
id: 'ftduino.input_analog',
266010-
default: '[MODE] on [INPUT]',
266042+
default: '[INPUT] [MODE]',
266011266043
description: 'read an ftDuino input'
266012266044
}),
266013266045
blockType: BlockType.REPORTER,
266014266046
arguments: {
266015-
MODE: {
266016-
type: ArgumentType.STRING,
266017-
menu: 'MODE',
266018-
defaultValue: MODE.RESISTANCE
266019-
},
266020266047
INPUT: {
266021266048
type: ArgumentType.STRING,
266022266049
menu: 'INPUT',
266023266050
defaultValue: 'i1'
266051+
},
266052+
MODE: {
266053+
type: ArgumentType.STRING,
266054+
menu: 'MODE',
266055+
defaultValue: MODE.RESISTANCE
266024266056
}
266025266057
}
266026266058
}, {
266027-
opcode: 'output',
266059+
opcode: 'output_analog',
266028266060
text: formatMessage({
266029-
id: 'ftduino.output',
266030-
default: 'output [OUTPUT] to [VALUE]',
266061+
id: 'ftduino.output_analog',
266062+
default: '[OUTPUT] [VALUE] %',
266031266063
description: 'set an ftDuino output'
266032266064
}),
266033266065
blockType: BlockType.COMMAND,
@@ -266038,9 +266070,52 @@ function () {
266038266070
defaultValue: 'o1'
266039266071
},
266040266072
VALUE: {
266073+
type: ArgumentType.NUMBER,
266074+
defaultValue: '100'
266075+
}
266076+
}
266077+
}, {
266078+
opcode: 'motor',
266079+
text: formatMessage({
266080+
id: 'ftduino.motor',
266081+
default: '[MOTOR] [DIR] [VALUE] %',
266082+
description: 'set an ftDuino motor output'
266083+
}),
266084+
blockType: BlockType.COMMAND,
266085+
arguments: {
266086+
MOTOR: {
266041266087
type: ArgumentType.STRING,
266042-
menu: 'ONOFFSTATE',
266043-
defaultValue: '1'
266088+
menu: 'MOTOR',
266089+
defaultValue: 'm1'
266090+
},
266091+
DIR: {
266092+
type: ArgumentType.STRING,
266093+
menu: 'DIR',
266094+
defaultValue: 'left'
266095+
},
266096+
VALUE: {
266097+
type: ArgumentType.NUMBER,
266098+
defaultValue: '100'
266099+
}
266100+
}
266101+
}, {
266102+
opcode: 'motor_stop',
266103+
text: formatMessage({
266104+
id: 'ftduino.motor_stop',
266105+
default: '[MOTOR] [STOPMODE]',
266106+
description: 'stop an ftDuino motor output'
266107+
}),
266108+
blockType: BlockType.COMMAND,
266109+
arguments: {
266110+
MOTOR: {
266111+
type: ArgumentType.STRING,
266112+
menu: 'MOTOR',
266113+
defaultValue: 'm1'
266114+
},
266115+
STOPMODE: {
266116+
type: ArgumentType.STRING,
266117+
menu: 'STOPMODE',
266118+
defaultValue: 'off'
266044266119
}
266045266120
}
266046266121
}],
@@ -266059,10 +266134,10 @@ function () {
266059266134
value: '0'
266060266135
}],
266061266136
MODE: [{
266062-
text: 'Ohms',
266137+
text: "\u2126",
266063266138
value: MODE.RESISTANCE
266064266139
}, {
266065-
text: 'Volt',
266140+
text: 'V',
266066266141
value: MODE.VOLTAGE
266067266142
}],
266068266143
INPUT: [{
@@ -266114,22 +266189,55 @@ function () {
266114266189
}, {
266115266190
text: 'O8',
266116266191
value: 'o8'
266192+
}],
266193+
MOTOR: [{
266194+
text: 'M1',
266195+
value: 'm1'
266196+
}, {
266197+
text: 'M2',
266198+
value: 'm2'
266199+
}, {
266200+
text: 'M3',
266201+
value: 'm3'
266202+
}, {
266203+
text: 'M4',
266204+
value: 'm4'
266205+
}],
266206+
DIR: [{
266207+
text: "\u21BA",
266208+
value: 'left'
266209+
}, {
266210+
text: "\u21BB",
266211+
value: 'right'
266212+
}],
266213+
STOPMODE: [{
266214+
text: 'Stop',
266215+
value: 'off'
266216+
}, {
266217+
text: 'Brake',
266218+
value: 'brake'
266117266219
}]
266118266220
},
266119266221
translation_map: {
266120266222
en: {
266121266223
'extensionName': 'ftDuino (en)',
266122-
'input': 'input [INPUT]',
266123-
'ftduino.input': 'input [INPUT]'
266224+
'input': '[INPUT]',
266225+
'ftduino.input': '[INPUT]'
266124266226
},
266125266227
de: {
266126266228
'extensionName': 'ftDuino (de)',
266127-
'input': 'Eingang [INPUT]',
266128-
'ftduino.input': 'Eingang [INPUT]'
266229+
'input': '[INPUT]',
266230+
'ftduino.input': '[INPUT]'
266129266231
}
266130266232
}
266131266233
};
266132266234
}
266235+
}, {
266236+
key: "handle_io",
266237+
value: function handle_io(util) {
266238+
// all output commands
266239+
if (this.iostate == IOSTATE.IN || this.iostate == IOSTATE.OUT || this.iostate == IOSTATE.MODE || this.iostate == IOSTATE.REPLY) util.yield();else if (this.iostate == IOSTATE.DONE) this.iostate = IOSTATE.IDLE;
266240+
}
266133266241
}, {
266134266242
key: "led",
266135266243
value: function led(args, util) {
@@ -266139,8 +266247,9 @@ function () {
266139266247
if (this.iostate == IOSTATE.IDLE) {
266140266248
this.iostate = IOSTATE.OUT;
266141266249
this.ftdSetLed(Cast.toBoolean(args.VALUE));
266142-
util.yield();
266143-
} else if (this.iostate == IOSTATE.OUT) util.yield();else if (this.iostate == IOSTATE.DONE) this.iostate = IOSTATE.IDLE;
266250+
}
266251+
266252+
this.handle_io(util);
266144266253
}
266145266254
}, {
266146266255
key: "inputCallback",
@@ -266151,34 +266260,9 @@ function () {
266151266260
}, {
266152266261
key: "input",
266153266262
value: function input(args, util) {
266154-
// check if ftDuino is connected at all
266155-
if (this.port == null) return false;
266156-
266157-
if (this.iostate == IOSTATE.IDLE) {
266158-
if (MODE.SWITCH != this.input_mode[args.INPUT]) {
266159-
console.log("change mode", this.input_mode[args.INPUT], MODE.SWITCH);
266160-
this.iostate == IOSTATE.MODE;
266161-
this.iostate_port = args.INPUT;
266162-
this.input_mode[args.INPUT] = MODE.SWITCH;
266163-
this.ftdSetMode(args.INPUT, MODE.SWITCH);
266164-
} else {
266165-
this.iostate = IOSTATE.IN;
266166-
this.iostate_port = args.INPUT;
266167-
this.ftdGet({
266168-
"port": args.INPUT
266169-
}, {
266170-
"func": this.inputCallback.bind(this),
266171-
"value": "value",
266172-
"expect": {
266173-
"port": args.INPUT
266174-
}
266175-
});
266176-
}
266177-
266178-
util.yield();
266179-
} else if (this.iostate == IOSTATE.MODE || this.iostate == IOSTATE.IN || this.iostate == IOSTATE.REPLY) util.yield();else if (this.iostate == IOSTATE.DONE) this.iostate = IOSTATE.IDLE;
266180-
266181-
return this.input_result;
266263+
// input is like input_analog but implicitely assumes MODE.SWITCH
266264+
args.MODE = MODE.SWITCH;
266265+
return this.input_analog(args, util);
266182266266
}
266183266267
}, {
266184266268
key: "input_analog",
@@ -266188,8 +266272,8 @@ function () {
266188266272

266189266273
if (this.iostate == IOSTATE.IDLE) {
266190266274
if (args.MODE != this.input_mode[args.INPUT]) {
266191-
console.log("change mode", this.input_mode[args.INPUT], args.MODE);
266192-
this.iostate == IOSTATE.MODE;
266275+
console.log("change mode for", args.INPUT, "from", this.input_mode[args.INPUT], "to", args.MODE);
266276+
this.iostate = IOSTATE.MODE;
266193266277
this.iostate_port = args.INPUT;
266194266278
this.input_mode[args.INPUT] = args.MODE;
266195266279
this.ftdSetMode(args.INPUT, args.MODE);
@@ -266206,23 +266290,58 @@ function () {
266206266290
}
266207266291
});
266208266292
}
266293+
}
266209266294

266210-
util.yield();
266211-
} else if (this.iostate == IOSTATE.MODE || this.iostate == IOSTATE.IN || this.iostate == IOSTATE.REPLY) util.yield();else if (this.iostate == IOSTATE.DONE) this.iostate = IOSTATE.IDLE;
266212-
266295+
this.handle_io(util);
266213266296
return this.input_result;
266214266297
}
266215266298
}, {
266216266299
key: "output",
266217266300
value: function output(args, util) {
266218-
// check if ftDuino is connected at all
266219-
if (this.port == null) return;
266301+
if (this.port == null) return; // check if ftDuino is connected at all
266220266302

266221266303
if (this.iostate == IOSTATE.IDLE) {
266222266304
this.iostate = IOSTATE.OUT;
266223266305
this.ftdSetOutput(args.OUTPUT, Cast.toBoolean(args.VALUE));
266224-
util.yield();
266225-
} else if (this.iostate == IOSTATE.OUT) util.yield();else if (this.iostate == IOSTATE.DONE) this.iostate = IOSTATE.IDLE;
266306+
}
266307+
266308+
this.handle_io(util);
266309+
}
266310+
}, {
266311+
key: "output_analog",
266312+
value: function output_analog(args, util) {
266313+
if (this.port == null) return; // check if ftDuino is connected at all
266314+
266315+
if (this.iostate == IOSTATE.IDLE) {
266316+
this.iostate = IOSTATE.OUT;
266317+
this.ftdSetOutput(args.OUTPUT, Cast.toNumber(args.VALUE));
266318+
}
266319+
266320+
this.handle_io(util);
266321+
}
266322+
}, {
266323+
key: "motor",
266324+
value: function motor(args, util) {
266325+
if (this.port == null) return; // check if ftDuino is connected at all
266326+
266327+
if (this.iostate == IOSTATE.IDLE) {
266328+
this.iostate = IOSTATE.OUT;
266329+
this.ftdSetMotor(args.MOTOR, args.DIR, Cast.toNumber(args.VALUE));
266330+
}
266331+
266332+
this.handle_io(util);
266333+
}
266334+
}, {
266335+
key: "motor_stop",
266336+
value: function motor_stop(args, util) {
266337+
if (this.port == null) return; // check if ftDuino is connected at all
266338+
266339+
if (this.iostate == IOSTATE.IDLE) {
266340+
this.iostate = IOSTATE.OUT;
266341+
this.ftdSetMotor(args.MOTOR, args.STOPMODE, 100);
266342+
}
266343+
266344+
this.handle_io(util);
266226266345
}
266227266346
}]);
266228266347

0 commit comments

Comments
 (0)