11import {
2- type BlockUtilityWithID ,
32 type Environment ,
43 extension ,
54 type ExtensionMenuDisplayDetails ,
6- Language ,
75 scratch ,
86} from "$common" ;
97import { io , Socket } from "socket.io-client" ;
@@ -20,9 +18,20 @@ const details: ExtensionMenuDisplayDetails = {
2018 menuSelectColor : "#62AEB2" ,
2119} ;
2220
23- const DEFAULT_HOST = "192.168.1.39" ;
21+ // Get Arduino board IP or hostname from URL parameter - required
22+ const getArduinoBoardHost = ( ) => {
23+ if ( typeof window !== 'undefined' && window . location ) {
24+ const urlParams = new URLSearchParams ( window . location . search ) ;
25+ const boardHost = urlParams . get ( 'host' ) ;
26+ if ( boardHost ) {
27+ console . log ( `Connecting to Arduino board: ${ boardHost } ` ) ;
28+ return boardHost ;
29+ }
30+ }
31+ throw new Error ( 'Arduino board host required. Add ?host=arduino_board_ip_or_name to the URL' ) ;
32+ } ;
2433
25- // TODO: support the brightness `0-7' of the leds
34+ // TODO: make the block to support the brightness `0-7' of the leds
2635const PATTERNS = {
2736 heart : [
2837 [ 0 , 0 , 0 , 7 , 7 , 0 , 0 , 0 , 7 , 7 , 0 , 0 , 0 ] ,
@@ -41,7 +50,8 @@ export default class ArduinoBasics extends extension(details, "ui", "customArgum
4150 private socket : Socket | null = null ;
4251
4352 init ( env : Environment ) {
44- var serverURL = `wss://${ DEFAULT_HOST } :7000` ; // Changed from wss to ws
53+ const arduinoBoardHost = getArduinoBoardHost ( ) ;
54+ var serverURL = `wss://${ arduinoBoardHost } :7000` ;
4555
4656 this . socket = io ( serverURL , {
4757 path : "/socket.io" ,
@@ -77,9 +87,12 @@ export default class ArduinoBasics extends extension(details, "ui", "customArgum
7787 }
7888 }
7989
80- @scratch . command `Clear matrix `
90+
91+ @( scratch . command `Clear matrix `)
8192 clearMatrix ( matrix : number [ ] [ ] ) {
8293 var matrixString = PATTERNS . empty . flat ( ) . join ( "" ) ;
83- this . socket . emit ( "matrix_draw" , { frame : matrixString } ) ;
94+ if ( this . socket ) {
95+ this . socket . emit ( "matrix_draw" , { frame : matrixString } ) ;
96+ }
8497 }
8598}
0 commit comments