In this tutorial, I will show you 5 working “Hello World” example for Box0.
Purpose
All the 5 example does one thing.
Collect 100 samples from AIN0.CH0 at 100KSPS and 12bit in static mode.
Code
Python
- Speciality:
- Easy
- Beginner Friendly
- Widely Used in Science & Enginnering
- Well Known
- Lots of community support
Python support is provided by pyBox0
import box0
import numpy as np
dev = box0.usb.open_supported()
ain = dev.ain()
ain.static_prepare()
ain.speed.current = 100000 # 100KSPS
ain.bitsize.current = 12 # 12bit
values = np.empty(100)
ain.static_start(values)
print("collected data", values)
ain.close()
dev.close()
Julia
- Speciality:
- Powerful
- Effecient
- Designed for Scientific Computing
Julia support is provided by Box0.jl
using Box0
dev = Box0.Usb.open_supported()
ain = Box0.ain(dev)
Box0.static_prepare(ain)
Box0.set(Box0.speed(ain), 100000)
Box0.set(Box0.bitsize(ain), 12)
data = Array{Float32}(100)
Box0.static_start(ain, data)
println("collected data ", data)
Box0.close(ain)
Box0.close(dev)
Java (and Android)
Speciality:
- Widely used in “Industry”
- Well Know
Java/Android support is provided by jBox0
import com.madresistor.box0.Device;
import com.madresistor.box0.backend.Usb;
import com.madresistor.box0.ResultException;
import com.madresistor.box0.module.Ain;
class java
{
public static void main(String[] args) {
double[] data = new double[100];
Device dev = null;
Ain ain = null;
try {
dev = Usb.openSupported();
ain = dev.ain(0);
ain.staticPrepare();
ain.speed.set((short)12);
ain.speed.set((long)100000);
ain.staticStart(data);
} catch(ResultException e) {
System.out.println("error:" + e.toString() + "|" + e.explain());
return;
}
System.out.print("collected data");
for(double value : data) {
System.out.print(" " + value);
}
System.out.println();
try {
ain.close();
dev.close();
} catch(ResultException e) {
System.out.println("error:" + e.toString() + "|" + e.explain());
}
}
}
Javascript (using NodeJs)
Speciality:
- Used in Browser (Client Side)
- Well Know between Web development
Note: Using NodeJS, Javscript is run on Server side
NodeJS support is provided by node-box0
var box0 = require("box0")
var dev = box0.usb.open_supported()
var ain = dev.ain()
ain.static_prepare()
ain.speed.set(100000)
ain.bitsize.set(12)
var data = new Float32Array(100)
ain.static_start(data)
console.log("collected data " + data)
ain.close()
dev.close()
C
- Speciality:
- Widely know
- All of the above langauges have atleast some part written in C
- Run on all platform
- Lots of work done
(The same code will work C++ too)
C/C++ support is provided by libbox0
#include <libbox0/libbox0.h>
#include <libbox0/backend/usb/usb.h>
#include <stdio.h>
#define DATA_COUNT 1000
int main(int argc, char *argv[])
{
b0_device *dev;
b0_ain *ain;
float data[DATA_COUNT];
if (B0_ERR_RC(b0_usb_open_supported(&dev))) {
fprintf(stderr, "Unable to open device");
goto done;
}
if (B0_ERR_RC(b0_ain_open(dev, &ain, 0))) {
fprintf(stderr, "Unable to open device");
goto close_device;
}
if (B0_ERR_RC(b0_ain_static_prepare(ain))) {
fprintf(stderr, "Unable to prepare module for static mode");
goto close_module;
}
if (B0_ERR_RC(b0_speed_set(ain->speed, 100000))) {
fprintf(stderr, "Unable to set speed");
goto close_module;
}
if (B0_ERR_RC(b0_bitsize_set(ain->bitsize, 12))) {
fprintf(stderr, "Unable to set speed");
goto close_module;
}
if (B0_ERR_RC(b0_ain_static_start_float(ain, data, DATA_COUNT))) {
fprintf(stderr, "Unable to start");
goto close_module;
}
printf("collected data");
for (size_t i = 0; i < DATA_COUNT; i++) {
printf(" %f", data[i]);
}
putchar('\n');
close_module:
if (B0_ERR_RC(b0_ain_close(ain))) {
fprintf(stderr, "problem closing module");
}
close_device:
if (B0_ERR_RC(b0_device_close(dev))) {
fprintf(stderr, "problem closing device");
}
done:
return 0;
}
Output
Above example will print something like (Python in the below case)
collected data [-0.06606445 -0.06606445 -0.06606445 -0.06606445 -0.06606445 -0.06767578
-0.06928711 -0.06928711 -0.06767578 -0.06928711 -0.06767578 -0.06928711
-0.06928711 -0.06928711 -0.06928711 -0.07089844 -0.06928711 -0.06928711
-0.07089844 -0.06928711 -0.07089844 -0.07089844 -0.07089844 -0.07089844
-0.07250976 -0.07089844 -0.07250976 -0.07250976 -0.07250976 -0.07089844
-0.07250976 -0.07250976 -0.07412109 -0.07412109 -0.07250976 -0.07412109
-0.07250976 -0.07250976 -0.07412109 -0.07412109 -0.07573242 -0.07573242
-0.07573242 -0.07573242 -0.07573242 -0.07412109 -0.07573242 -0.07734375
-0.07573242 -0.07573242 -0.07734375 -0.07573242 -0.07734375 -0.07734375
-0.07573242 -0.07734375 -0.07895508 -0.07734375 -0.07895508 -0.07895508
-0.07895508 -0.07895508 -0.07895508 -0.07734375 -0.07895508 -0.08056641
-0.08056641 -0.08217773 -0.08217773 -0.08056641 -0.08056641 -0.08056641
-0.08056641 -0.08217773 -0.08217773 -0.08217773 -0.08217773 -0.08378906
-0.08217773 -0.08378906 -0.08378906 -0.08217773 -0.08540039 -0.08378906
-0.08540039 -0.08540039 -0.08540039 -0.08540039 -0.08540039 -0.08540039
-0.08540039 -0.08701172 -0.08701172 -0.08701172 -0.08862305 -0.08701172
-0.08701172 -0.08701172 -0.08540039 -0.08701172]
Hope you liked the code dump!
Not happy because you favourite language not listed?
No problem! you can write libbox0 binding for your favourite language.
Tip: libbox0 is written in C and you will probebly find a Foreign Function Interfacing module/library for your favourite language.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.