The best developer experience is found by installing the extension in Visual Studio Code. A command line interface is also available, but a lot of work was done with the extension to improve the developer experience.
The extension bundles the compiler, firmware flasher, debugger, simulators, console log and other goodies.
console.debug("debug") console.log("log") console.warn("warn") console.error("error")
DeviceScript also introduce a new logging function, console.data
, that specializes on logging and collecting numerical values. Data streams collected with console.data
can be exported to CSV files and Jupyter notebooks.
const temp = 20 const humi = 60 console.data({ temp, humi })
Simulation
Before testing your code on hardware, you can use simulators. Simulators will run on your computer (even on containers) and can greatly help with the development experience.
In Visual Studio Code, when you try to start this sample, DeviceScript will first try to use any connected microcontrollers. If it does not find any, it will launch a device simulator. The device simulator runs the DeviceScript native runtime compiled to Web assembly. Simulated sensors can also be spun up using the simulator dashboard so that the entire system runs virtually on your computer (this also works if you are developing from a container).
When you start running a program, the DeviceScript simulator view will start. If it detects that you need a service that is not available, for example you need a temperature sensor but it is not detected, the view will sugest to start one for you. Make sure to click AUTO START to spin off a temperature sensor since you need one to run this program.
import { Temperature } from "@devicescript/core" // needs a simulator const temperature = new Temperature() setInterval(async () => { // blocks until a temperature server is found const temp = await temperature.reading.read() console.data({ temp }) }, 1000)
Once the simulated sensors are running, you will typically have a slider or other interaction UI to change the values.
Debugging
You can set breakpoints, step and inspect variables in the debugger... As long as your program is not too time sensitive, this can aid with debugging your programs.
You can also use console.log. DeviceScript also provides console.data
which is a specialized logging function for sensor data, which can be exported to a Jupyter Notebook for later analysis.
Running on Hardware
You can run and debug the code running on the physical device as well. Click on the connect (plug) button in the DeviceScript view and select Serial. The device will be automatically discovered and the simulator will be stopped. Make sure to flash the firmware before this step.
The developer experience is pretty much the same from there. You can set breakpoints, use console.log
, etc... Unlike the simulated sensors, you cannot drag a slider to change the sensor data... you might have to blow on it or put it in the fridge!
Remote workspaces
DeviceScript also supporting developing in remote workspaces, like Docker, GitHub Codespaces, Windows WSL or other virtualization frameworks.
The Visual Studio Code extension runs in the container and does not have access to serial or USB on the host operating system; so it launches a seperate browser page to handle the connection to the physical device through WebSerial/WebUSB. Other than that, the experience is pretty much the same.
Text editor powered by tinymce.