@user100558, now I see where your are coming from.
I'll try to explain what is going on:
In Line 27 you are pulling in a piece of communication software so that your angular app running in the browser can use to connect to Puck via BLE and then send data - requests - to Puck and in return receive data - responses - from Puck - all via the BLE connection and Puck's console.
Think of puck's console like a command or terminal window of a computer. You can type something, and if the terminal / console program - understands it, it will respond. Similar you enter ls in the linux shell and you get the list of files of the current directory you are in, or dir in windows bat shell und you get the list files of the current directory you are in.
Puck's operating system - so to speak - though does not understand sh or bat shell commands, but it understands JavaScript. The data - or more precisely - source code you send to Puck as request is tried to be interpreted / executed and the response created in Puck's console is sent back to you.
Where the confusion comes is that in your browser you have an object referred to with the global variable Puck that understands certain commands / has certain methods. On the Puck there is also an object referred to with the global variable Puck with its methods, but ***these 'Puck' objects are not the same', and therefore, do not understand the same commands / have not the same methods.
Let's have a look at the key method of the Puck object residing in you browser. This key method is .write(arg1, arg2) and accepts two arguments:
LED1.set();
and
LED1.reset();
I'm sure you have the Espruino Web IDE installed and you have connected to the Puck via BLE. Enter any of these strings in the console - left pane - and you see the same effect by directly entering into Puck's console. The console of Espruino Web IDE is nothing else than a terminal that sends your key strokes - command - to Puck, and displays what Puck responds: echoing your key strokes and on pressing enter printing the response of your entered command.
Since there is no meaningful response to setting and resetting a LED and these methods on the LED objects return nothing - in this case ```undefined``` - you see
undefinded
as response in the console - as the response to your command (beside the echo of what you typed).
arg2is a so calledcallbackfunction that is invoked with one argument - the response from Puck. Both of these callback functions inLine 3andLine 9are defined as anonymous functions and without accepting an argument (even though in the JavaScript environment passed arguments are available in the local and predefined variablearguments). There is nothing wrong with not providing an argument in this example of turning LED on and off, because the response is anyway the string"undefined"```. (If you though would have a syntax or interpretation error, you don't catch it.)
The arg2is optional and could be dropped completely in your code. (Your try to read Puck's battery percentage throws actually an error, because it is directed towards your Puck object in the browser, but you meant to talk to the Puck object in Puck to return the battery percentage. Former Puck object does not understand, but latter one does, and you can get the battery percentage - just a little bit different - as I'll show later).
To be clear what the browser side Puck.write() method does, is:
sending the JavaScript source as string as given as 1st argument to Puck
receive Pucks response as string and then invoke the callback function - which runs in the browser - with the response string as sole argument.
Since ```..getBatteryPercentage()``` is ***deprecated*** in the newest version of Espruino (Puck/Pixel/MDBT42Q) firmware, we do it right and use the ```E.getBattery()``` expression:
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
@user100558, now I see where your are coming from.
I'll try to explain what is going on:
In
Line 27
you are pulling in a piece of communication software so that your angular app running in the browser can use to connect to Puck via BLE and then send data - requests - to Puck and in return receive data - responses - from Puck - all via the BLE connection and Puck's console.Think of puck's console like a command or terminal window of a computer. You can type something, and if the terminal / console program - understands it, it will respond. Similar you enter
ls
in the linux shell and you get the list of files of the current directory you are in, ordir
in windows bat shell und you get the list files of the current directory you are in.Puck's operating system - so to speak - though does not understand sh or bat shell commands, but it understands JavaScript. The data - or more precisely - source code you send to Puck as request is tried to be interpreted / executed and the response created in Puck's console is sent back to you.
Where the confusion comes is that in your browser you have an object referred to with the global variable
Puck
that understands certain commands / has certain methods. On thePuck
there is also an object referred to with the global variablePuck
with its methods, but ***these 'Puck' objects are not the same', and therefore, do not understand the same commands / have not the same methods.Let's have a look at the key method of the
Puck
object residing in you browser. This key method is.write(arg1, arg2)
and accepts two arguments:LED1.set();
LED1.reset();
undefinded
arg2
is a so called
callbackfunction that is invoked with one argument - the response from Puck. Both of these callback functions in
Line 3and
Line 9are defined as anonymous functions and without accepting an argument (even though in the JavaScript environment passed arguments are available in the local and predefined variable
arguments). There is nothing wrong with not providing an argument in this example of turning LED on and off, because the response is anyway the string
"undefined"```. (If you though would have a syntax or interpretation error, you don't catch it.)The
arg2
is optional and could be dropped completely in your code. (Your try to read Puck's battery percentage throws actually an error, because it is directed towards your Puck object in the browser, but you meant to talk to the Puck object in Puck to return the battery percentage. Former Puck object does not understand, but latter one does, and you can get the battery percentage - just a little bit different - as I'll show later).To be clear what the browser side
Puck.write()
method does, is:To see these details look at https://www.puck-js.com/puck.js - the function beginning with
```
Now you are able to get your Magnetometer example going in a similar way.
Recap: