@@ -8,109 +8,85 @@ const io = require('./socket.io.min.js');
88 * @type {string }
99 */
1010// eslint-disable-next-line max-len
11- const iconURI = '' ;
11+ const iconURI = '' ;
1212
1313/**
1414 * Url of icon to be displayed in the toolbox menu for the extension category.
1515 * @type {string }
1616 */
1717// eslint-disable-next-line max-len
18- const menuIconURI = ''
18+ const menuIconURI = ''
1919
2020const wsServerURL = `${ window . location . protocol } //${ window . location . hostname } :7000` ;
2121
2222class Scratch3Arduino {
23- constructor ( runtime ) {
24- this . runtime = runtime ;
25- this . io = io ( wsServerURL , {
26- path : '/socket.io' ,
27- transports : [ 'polling' , 'websocket' ] ,
28- autoConnect : true
29- } ) ;
23+ constructor ( runtime ) {
24+ this . runtime = runtime ;
25+ this . io = io ( wsServerURL , {
26+ path : '/socket.io' ,
27+ transports : [ 'polling' , 'websocket' ] ,
28+ autoConnect : true
29+ } ) ;
3030
31- // TODO: move to ModulinoPeripheral
32- this . _button_a_pressed = false ;
33- this . _button_b_pressed = false ;
34- this . _button_c_pressed = false ;
35-
36- this . io . on ( 'modulino_buttons_pressed' , ( data ) => {
37- console . log ( `Modulino button pressed event received: ${ data . btn } ` ) ;
38- if ( data . btn . toUpperCase ( ) == 'A' ) {
39- this . _button_a_pressed = true ;
40- this . _button_b_pressed = false ;
41- this . _button_c_pressed = false ;
42- return ;
43- }
44- if ( data . btn . toUpperCase ( ) == 'B' ) {
45- this . _button_a_pressed = false ;
46- this . _button_b_pressed = true ;
47- this . _button_c_pressed = false ;
48- return ;
49- }
50- if ( data . btn . toUpperCase ( ) == 'C' ) {
51- this . _button_a_pressed = false ;
52- this . _button_b_pressed = false ;
53- this . _button_c_pressed = true ;
54- return ;
55- }
56- return ;
57- } ) ;
58- }
31+ // TODO: move to ModulinoPeripheral
32+ this . _button_pressed = '' ;
33+ this . io . on ( 'modulino_buttons_pressed' , ( data ) => {
34+ console . log ( `Modulino button pressed event received: ${ data . btn } ` ) ;
35+ this . _button_pressed = data . btn . toUpperCase ( ) ;
36+ } ) ;
37+ }
5938} ;
6039
6140Scratch3Arduino . prototype . getInfo = function ( ) {
62- return {
63- id : 'arduino' ,
64- name : "Arduino" ,
65- menuIconURI : menuIconURI ,
66- blockIconURI : iconURI ,
67- blocks : [
68- {
69- opcode : 'matrixDraw' ,
70- blockType : BlockType . COMMAND ,
71- text : 'draw [FRAME] on matrix' ,
72- func : 'matrixDraw' ,
73- arguments : {
74- FRAME : {
75- type : ArgumentType . MATRIX ,
76- defaultValue : '0101010101100010101000100'
77- }
78- }
79- } ,
80- {
81- opcode : 'whenModulinoButtonsPressed' ,
82- blockType : BlockType . HAT ,
83- text : 'when modulino button [BTN] pressed' ,
84- func : 'whenModulinoButtonsPressed' ,
85- arguments : {
86- BTN : {
87- type : ArgumentType . STRING ,
88- menu : 'modulinoButtons' ,
89- defaultValue : "A"
90- }
91- }
92- } ,
93- ] ,
94- menus : {
95- modulinoButtons : [ "A" , "B" , "C" ]
96- }
97- } ;
41+ return {
42+ id : 'arduino' ,
43+ name : "Arduino" ,
44+ menuIconURI : menuIconURI ,
45+ blockIconURI : iconURI ,
46+ blocks : [
47+ {
48+ opcode : 'matrixDraw' ,
49+ blockType : BlockType . COMMAND ,
50+ text : 'draw [FRAME] on matrix' ,
51+ func : 'matrixDraw' ,
52+ arguments : {
53+ FRAME : {
54+ type : ArgumentType . MATRIX ,
55+ defaultValue : '0101010101100010101000100'
56+ }
57+ }
58+ } ,
59+ {
60+ opcode : 'whenModulinoButtonsPressed' ,
61+ blockType : BlockType . HAT ,
62+ text : 'when modulino button [BTN] pressed' ,
63+ func : 'whenModulinoButtonsPressed' ,
64+ arguments : {
65+ BTN : {
66+ type : ArgumentType . STRING ,
67+ menu : 'modulinoButtons' ,
68+ defaultValue : "A"
69+ }
70+ }
71+ } ,
72+ ] ,
73+ menus : {
74+ modulinoButtons : [ "A" , "B" , "C" ]
75+ }
76+ } ;
9877}
9978
10079Scratch3Arduino . prototype . matrixDraw = function ( args ) {
101- console . log ( `Drawing frame on matrix: ${ args } ` ) ;
102- this . io . emit ( "matrix_draw" , { frame : args . FRAME } ) ;
80+ console . log ( `Drawing frame on matrix: ${ args } ` ) ;
81+ this . io . emit ( "matrix_draw" , { frame : args . FRAME } ) ;
10382} ;
10483
10584Scratch3Arduino . prototype . whenModulinoButtonsPressed = function ( args ) {
106- if ( args . BTN === 'A' ) {
107- return this . _button_a_pressed
108- } else if ( args . BTN === 'B' ) {
109- return this . _button_b_pressed
110- } else if ( args . BTN === 'C' ) {
111- return this . _button_c_pressed ;
112- }
113- return false ;
85+ if ( args . BTN === this . _button_pressed ) {
86+ this . _button_pressed = '' ;
87+ return true ;
88+ }
89+ return false ;
11490} ;
11591
116- module . exports = Scratch3Arduino ;
92+ module . exports = Scratch3Arduino ;
0 commit comments