Skip to content

Commit

Permalink
修正microbit積木
Browse files Browse the repository at this point in the history
  • Loading branch information
estea8968 committed Nov 13, 2023
1 parent bd672ae commit b52ee35
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 75 deletions.
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="google" value="notranslate">
<link rel="shortcut icon" href="static/favicon.ico">
<title>OSEP_scratch 1121102</title>
<title>OSEP_scratch 1121113</title>

</head>
<body>
Expand Down
81 changes: 54 additions & 27 deletions app/lib.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -438649,6 +438649,7 @@ var mic_port;
var the_locale = null; //tone

var freq_ary = [];
var serial_isbusy = false;

for (var i = 24; i < 96; i++) {
freq_ary.push(i);
Expand Down Expand Up @@ -438983,7 +438984,7 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'analogPins',
defaultValue: '0'
}
}
Expand All @@ -438994,7 +438995,7 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'digitalPins',
defaultValue: '0'
}
}
Expand All @@ -439005,7 +439006,7 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'analogPins',
defaultValue: '0'
},
VALUE: {
Expand All @@ -439020,7 +439021,7 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'digitalPins',
defaultValue: '0'
},
VALUE: {
Expand Down Expand Up @@ -439144,6 +439145,14 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
acceptReporters: true,
items: ['0', '1', '2']
},
analogPins: {
acceptReporters: true,
items: ['0', '1', '2', '3', '4', '10']
},
digitalPins: {
acceptReporters: true,
items: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']
},
num05: {
acceptReporters: true,
items: ['0', '1', '2', '3', '4']
Expand Down Expand Up @@ -439200,41 +439209,55 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
while (1) {
switch (_context.prev = _context.next) {
case 0:
this.serialSend('b#');
_context.next = 3;
_context.next = 2;
return this.serialSend('b#');

case 2:
new Promise(function (resolve) {
setTimeout(function () {
resolve();
}, 5);
});
_context.next = 5;
return this.serialRead();

case 3:
case 5:
r_data = _context.sent;
console.log("r_data", r_data);
v_ary = r_data.split(':'); //console.log("v_ary[0]",v_ary[0]);

if (!(v_ary[0] == 'b')) {
_context.next = 16;
_context.next = 19;
break;
}

btn_ary = v_ary[1].split(',');

if (!(btn_ary.length == 2)) {
_context.next = 19;
break;
}

_context.t0 = args.BTN;
_context.next = _context.t0 === 'A' ? 11 : _context.t0 === 'B' ? 12 : _context.t0 === 'any' ? 13 : _context.t0 === 'A+B' ? 14 : 15;
_context.next = _context.t0 === 'A' ? 14 : _context.t0 === 'B' ? 15 : _context.t0 === 'any' ? 16 : _context.t0 === 'A+B' ? 17 : 18;
break;

case 11:
case 14:
return _context.abrupt("return", Boolean(btn_ary[0] == 0));

case 12:
case 15:
return _context.abrupt("return", Boolean(btn_ary[1] == 0));

case 13:
case 16:
return _context.abrupt("return", Boolean(btn_ary[0] == 0 || btn_ary[1] == 0));

case 14:
case 17:
return _context.abrupt("return", Boolean(btn_ary[0] == 0 && btn_ary[1] == 0));

case 15:
case 18:
return _context.abrupt("return", false);

case 16:
case 19:
case "end":
return _context.stop();
}
Expand Down Expand Up @@ -440692,14 +440715,16 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
case 0:
console.log("serial sendData=", sendData);
encoder = new TextEncoder();
serial_isbusy = true;
writer = mic_port.writable.getWriter();
_context26.next = 5;
_context26.next = 6;
return writer.write(encoder.encode(sendData));

case 5:
case 6:
writer.releaseLock();
serial_isbusy = false;

case 6:
case 8:
case "end":
return _context26.stop();
}
Expand All @@ -440722,12 +440747,13 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
while (1) {
switch (_context27.prev = _context27.next) {
case 0:
serial_isbusy = true;
mic_reader = mic_port.readable.getReader();
_context27.prev = 1;
_context27.next = 4;
_context27.prev = 2;
_context27.next = 5;
return mic_reader.read();

case 4:
case 5:
value = _context27.sent;
//console.log('value',value);
//let uint8array = new TextEncoder().encode();
Expand All @@ -440737,23 +440763,24 @@ var Scratch3WebserialMicroBitBlocks = /*#__PURE__*/function () {
console.log("string[0]=", string[0]);
return _context27.abrupt("return", string[0]);

case 12:
_context27.prev = 12;
_context27.t0 = _context27["catch"](1);
case 13:
_context27.prev = 13;
_context27.t0 = _context27["catch"](2);
console.log(_context27.t0);

case 15:
case 16:
// finally {
// reader.releaseLock();
//}
mic_reader.releaseLock();
serial_isbusy = false;

case 16:
case 18:
case "end":
return _context27.stop();
}
}
}, _callee27, null, [[1, 12]]);
}, _callee27, null, [[2, 13]]);
}));

function serialRead() {
Expand Down
2 changes: 1 addition & 1 deletion app/lib.min.js.map

Large diffs are not rendered by default.

118 changes: 72 additions & 46 deletions source/scratch-vm/src/extensions/scratch3_webserialMicrobit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let mic_port;
let the_locale = null;
//tone
let freq_ary=[];
let serial_isbusy = false;
for(var i=24;i<96;i++){
freq_ary.push(i);
}
Expand Down Expand Up @@ -379,7 +380,7 @@ class Scratch3WebserialMicroBitBlocks {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'analogPins',
defaultValue: '0'
}
}
Expand All @@ -391,7 +392,7 @@ class Scratch3WebserialMicroBitBlocks {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'digitalPins',
defaultValue: '0'
}
}
Expand All @@ -403,7 +404,7 @@ class Scratch3WebserialMicroBitBlocks {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'analogPins',
defaultValue: '0'
},
VALUE: {
Expand All @@ -419,7 +420,7 @@ class Scratch3WebserialMicroBitBlocks {
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
menu: 'digitalPins',
defaultValue: '0'
},
VALUE: {
Expand Down Expand Up @@ -550,6 +551,14 @@ class Scratch3WebserialMicroBitBlocks {
acceptReporters: true,
items: ['0', '1', '2']
},
analogPins: {
acceptReporters: true,
items: ['0', '1', '2','3','4','10']
},
digitalPins: {
acceptReporters: true,
items: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']
},
num05:{
acceptReporters: true,
items: ['0', '1', '2','3','4']
Expand Down Expand Up @@ -596,28 +605,37 @@ class Scratch3WebserialMicroBitBlocks {
* @return {boolean} - true if the button is pressed.
*/
async isButtonPressed (args) {
this.serialSend('b#');
const r_data = await this.serialRead();
console.log("r_data",r_data);
const v_ary = r_data.split(':');
//console.log("v_ary[0]",v_ary[0]);
if(v_ary[0]=='b'){
const btn_ary = v_ary[1].split(',');
switch (args.BTN){
case 'A':
return Boolean(btn_ary[0]==0);

await this.serialSend('b#');
new Promise(resolve => {
setTimeout(() => {
resolve();
}, 5);
});
const r_data = await this.serialRead();
console.log("r_data",r_data);
const v_ary = r_data.split(':');
//console.log("v_ary[0]",v_ary[0]);
if(v_ary[0]=='b'){
const btn_ary = v_ary[1].split(',');
if(btn_ary.length==2){
switch (args.BTN){
case 'A':
return Boolean(btn_ary[0]==0);

case 'B':
return Boolean(btn_ary[1]==0);
case 'any':
return Boolean(btn_ary[0]==0 || btn_ary[1]==0);
case 'A+B':
return Boolean(btn_ary[0]==0 && btn_ary[1]==0);
default :
return false;
}
}

case 'B':
return Boolean(btn_ary[1]==0);
case 'any':
return Boolean(btn_ary[0]==0 || btn_ary[1]==0);
case 'A+B':
return Boolean(btn_ary[0]==0 && btn_ary[1]==0);
default :
return false;
}

}
}


}

Expand Down Expand Up @@ -1199,30 +1217,38 @@ class Scratch3WebserialMicroBitBlocks {
}

async serialSend(sendData) {
console.log("serial sendData=",sendData);
const encoder = new TextEncoder();
const writer = mic_port.writable.getWriter();
await writer.write(encoder.encode(sendData));
writer.releaseLock();

console.log("serial sendData=",sendData);
const encoder = new TextEncoder();
serial_isbusy = true;
const writer = mic_port.writable.getWriter();
await writer.write(encoder.encode(sendData));
writer.releaseLock();
serial_isbusy = false;

}

async serialRead() {
let mic_reader = mic_port.readable.getReader();
try{
let value = await mic_reader.read();
//console.log('value',value);
//let uint8array = new TextEncoder().encode();
let string = new TextDecoder().decode(value.value);
string = string.split('\r\n');
mic_reader.releaseLock();
console.log("string[0]=",string[0]);
return string[0];
}catch (error) {
console.log(error);
}// finally {
// reader.releaseLock();
//}
mic_reader.releaseLock();

serial_isbusy = true;
let mic_reader = mic_port.readable.getReader();
try{
let value = await mic_reader.read();
//console.log('value',value);
//let uint8array = new TextEncoder().encode();
let string = new TextDecoder().decode(value.value);
string = string.split('\r\n');
mic_reader.releaseLock();
console.log("string[0]=",string[0]);
return string[0];
}catch (error) {
console.log(error);
}// finally {
// reader.releaseLock();
//}
mic_reader.releaseLock();
serial_isbusy = false;

}

async listener(){
Expand Down

0 comments on commit b52ee35

Please sign in to comment.