Miss any of our Open RFC calls?Watch the recordings here! »

linuxduino

0.2.5 • Public • Published

Linuxduino.js

A javscript library for communicating with hardware in a Arduino style programming for any Linux platform.

Note: This library is currently in Beta.
This library is intended to be used with Node >= v8.0.0 and Electron (Todo version).

Documentation currently under construction:
https://nvsl.github.io/Linuxduino/

Install

npm install linuxduino 

Basics

The linuxduino library was built using webassembly and thus it requires a little extra code to know when the webassembly (wasm) binary has been loaded.

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
    // Your linuxduino code
}

I/O

Currenlty webassembly doesn't support pthreads which is required for inputs (e.g. pinMode(..., INPUT)), but is under development. This is the main reason this library is still in Beta. For outputs (e.g. blinking an LED) here is an example for raspberry pi:

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
    var ledPin = 4; // RPi GPIO4
    Linuxduino.pinMode(ledPin, Linuxduino.OUTPUT);
    while (1) {
     console.log("LED ON");
     Linuxduino.digitalWrite(ledPin, 1); // Turn on LED
     Linuxduino.delay(1000); // Delay 1 sec
     console.log("LED OFF");
     Linuxduino.digitalWrite(ledPin, 0); // Turn off LED
     Linuxduino.delay(1000); // Delay 1 sec
    }
}

Serial

For communicating with a Serial port in linux you need to know it's device path and name (e.g. /dev/ttyUSB0). There are two ways for setting the device name and path:

First, using setSerial("/dev/..."):

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
    // Set serial device name
    Linuxduino.setSerial("/dev/ttyUSB0");
    console.log(Linuxduino.getSerial());
    
    // Open serial
    Serial =  new Linuxduino.Serial();
    Serial.begin(9600);
    
    // Write something
    Serial.println("Hello World");
    
}

Second, directly Serial.begin("/dev/...", 9600):

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
    // Open serial
    Serial =  new Linuxduino.Serial();
    Serial.begin("/dev/ttyUSB0", 9600);
    
    // Write something
    Serial.println("Hello World");
 
}

SPI

For communicating with a SPI port in linux you need to know it's device path and name (e.g. /dev/spidev0.0). There are two ways for setting the device name and path:

First, using setSPI("/dev/..."):

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
     // Set SPI device name
    Linuxduino.setSPI("/dev/spidev0.0");
    console.log(Linuxduino.getSPI());
    
    // Open SPI port
    settingsA = new Linuxduino.SPISettings(Linuxduino.SPI_CLOCK_DIV64, 
        Linuxduino.MSBFIRST, Linuxduino.SPI_MODE3);
    SPI =  new Linuxduino.SPI();
    SPI.begin();
    
    // Send Hello World to SPI device and listen for received data every second
    buff = new Buffer("Hello World\n");
    var ret;
    while(1) {
     SPI.beginTransaction(settingsA);
     for(var i=0; i<buff.length; i++) {
     ret = SPI.transfer(buff[i]); // Transmit and receive one byte
     process.stdout.write(String.fromCharCode(ret));
     }
     process.stdout.write('\n');
     SPI.endTransaction();
     Linuxduino.delay(1000);
    }
    SPI.end();
    
}

Second, directly in SPI.begin("/dev/..."):

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
    // Open SPI port
    settingsA = new Linuxduino.SPISettings(Linuxduino.SPI_CLOCK_DIV64, 
        Linuxduino.MSBFIRST, Linuxduino.SPI_MODE3);
    SPI =  new Linuxduino.SPI();
    SPI.begin("/dev/spidev0.0");
    
    // Your SPI code
    
}

Wire (I2C)

For communicating with a I2C port in linux you need to know it's device path and name (e.g. /dev/i2c-1). There are two ways for setting the device name and path:

First, using setI2C("/dev/..."):

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
    // Set I2C device name
    Linuxduino.setWire("/dev/i2c-1");
    console.log(Linuxduino.getWire());
    
    // Open I2C port
    Wire = new Linuxduino.Wire();
    Wire.begin();
    
    // Send Hello World! to device
    Wire.beginTransmission(8); // 7-bit device address
    Wire.write("Hello");
    Wire.write(" ");
    var world = new Buffer("World!");
    Wire.write(world, world.length);
    Wire.endTransmission();
    Wire.end();
    
}

Second, directly in Wire.begin("/dev/..."):

var Linuxduino = require("linuxduino");
 
Linuxduino.onRuntimeInitialized = function() {
    console.log('wasm file loaded');
    
    // Open I2C port
    Wire = new Linuxduino.Wire();
    Wire.begin("/dev/i2c-1");
    
    // Send Hello World! to device
    Wire.beginTransmission(8); // 7-bit device address
    Wire.write("Hello");
    Wire.write(" ");
    var world = new Buffer("World!");
    Wire.write(world, world.length);
    Wire.endTransmission();
    Wire.end();
    
}

Note, if you want to send exactly one byte in I2C for example 0xFF you need to use Wire.write_byte(0xFF).

List of Implemented Funtions

Digital I/O

Function C++ Javascript / wasm
pinMode(pin, mode) ✔️ ✔️
digitalWrite(pin, value) ✔️ ✔️
digitalRead(pin) ✔️ ✔️

Analog I/O

Function C++ Javascript / wasm
analogReference(type) ❌ -- No hardware support
analogRead(pin) ❌ --No hardware support
analogWrite(pin, value) - PWM ✔️ ❌ --Waiting for pthreads
(EXTRA) setPwmDutyCycle (pin, dutycycle) ✔️ ❌ --Waiting for pthreads
(EXTRA) setPwmFrequency (pin, frequency, dutycycle) ✔️ ❌ --Waiting for pthreads
(EXTRA) setPwmFrequency (pin, frequency) ✔️ ❌ --Waiting for pthreads
(EXTRA) setPwmPeriod (pin, microseconds) ✔️ ❌ --Waiting for pthreads

Advanced I/O

Function C++ Javascript / wasm
tone(pin, frequency) ✔️ ❌ --Waiting for pthreads
tone(pin, frequency, duration) ✔️ ❌ --Waiting for pthreads
noTone(pin) ✔️ ❌ --Waiting for pthreads
shiftOut(dataPin, clockPin, bitOrder, value) ✔️ ✔️
byte incoming = shiftIn(dataPin, clockPin, bitOrder) ✔️ ✔️
pulseIn(pin, value) ✔️ ✔️
pulseIn(pin, value, timeout) ✔️ ✔️

Serial

Function C++ Javascript / wasm
if (Serial) ✔️
Serial.available() ✔️ ✔️
Serial.availableForWrite() ✔️ ✔️
Serial.begin(speed) ✔️ ✔️
Serial.begin(speed, config) ✔️ ✔️
(EXTRA) Serial.begin(driverName, speed) ✔️ ✔️
(EXTRA) Serial.begin(driverName, speed, config) ✔️ ✔️
Serial.end() ✔️ ✔️
Serial.find(target) ✔️ ✔️
Serial.findUntil(target, terminal) ✔️ ✔️
Serial.flush() ✔️ ✔️
Serial.parseFloat() ✔️ ✔️
Serial.parseInt() ✔️ ✔️
Serial.parseInt(char skipChar) ✔️ ✔️
Serial.peek() ✔️ ✔️
Serial.print(val) ✔️ ✔️
Serial.print(val, format) ✔️ ✔️
Serial.println(val) ✔️ ✔️
Serial.println(val, format) ✔️ ✔️
(EXTRA) Serial.printf(format, ...) ✔️ ✔️
Serial.read() ✔️ ✔️
Serial.readBytes(buffer, length) ✔️ ✔️
Serial.readBytesUntil(character, buffer, length) ✔️ ✔️
Serial.readString() ✔️ ✔️
Serial.readStringUntil(terminator) ✔️ ✔️
(EXRTA) Serial.readStringCommand(character, buffer, length) ✔️ ✔️
Serial.setTimeout(time) ✔️ ✔️
Serial.write(val) ✔️ ✔️
Serial.write(str) ✔️ ✔️
Serial.write(buf, len) ✔️ ✔️
serialEvent() In Progress In Progress

Wire (I2C)

Function C++ Javascript / wasm
Wire.begin() ✔️ ✔️
(EXTRA) Wire.begin(driverName) ✔️ ✔️
Wire.begin(address) ❌ --Linux I2C Slave driver was added recently, still verifying
Wire.requestFrom(address, quantity) ✔️ ✔️
Wire.requestFrom(address, quantity, stop) ❌ --There is no way to send an I2C stop msg to the driver
Wire.beginTransmission(address) ✔️ ✔️
Wire.endTransmission() ✔️ ✔️
Wire.endTransmission(stop) ❌ --There is no way to send an I2C stop msg to the driver
Wire.write(value) ✔️ ✔️
Wire.write(string) ✔️ ✔️
Wire.write(data, length) ✔️ ✔️
Wire.available() ✔️ ✔️
Wire.read() ✔️ ✔️
Wire.onReceive(handler) ❌ --Linux I2C Slave driver was added recently, still verifying
Wire.onRequest(handler) ❌ --Linux I2C Slave driver was added recently, still verifying

SPI

Function C++ Javascript / wasm
SPISettings ✔️ ✔️
SPI.begin() ✔️ ✔️
(EXTRA) SPI.begin(driverName) ✔️ ✔️
SPI.end() ✔️ ✔️
SPI.beginTransaction(mySettings) ✔️ ✔️
SPI.endTransaction() ✔️ ✔️
SPI.setBitOrder(order) ✔️ ✔️
SPI.setClockDivider(divider) ✔️ ✔️
SPI.setDataMode(mode) ✔️ ✔️
receivedVal = SPI.transfer(val) ✔️ ✔️
receivedVal16 = SPI.transfer16(val16) ❌ --Not used
SPI.transfer(buffer, size) ✔️ ✔️
SPI.usingInterrupt(interruptNumber) In Progress In Progress

Time

Function C++ Javascript / wasm
time = millis() ✔️ ✔️
time = micros() ✔️ ✔️
delay(ms) ✔️ ✔️
delayMicroseconds(us) ✔️ ✔️

Math

Function C++ Javascript / wasm
min(x, y) ✔️ ✔️
max(x, y) ✔️ ✔️
abs(x) ✔️ ✔️
constrain(x, a, b) ✔️ ✔️
map(value, fromLow, fromHigh, toLow, toHigh) ✔️ ✔️
pow(base, exponent) ✔️ ✔️
radians(deg) ✔️ ✔️
degrees(rad) ✔️ ✔️
sqrt(x) ✔️ ✔️
sq(x) ✔️ ✔️

Trigonometry

Function C++ Javascript / wasm
sin(rad) ✔️ ✔️
cos(rad) ✔️ ✔️
tan(rad) ✔️ ✔️

Characters

Function C++ Javascript / wasm
isAlphaNumeric(thisChar) ✔️ ✔️
isAlpha(thisChar) ✔️ ✔️
isAscii(thisChar) ✔️ ✔️
isWhitespace(thisChar) ✔️ ✔️
isControl(thisChar) ✔️ ✔️
isDigit(thisChar) ✔️ ✔️
isGraph(thisChar) ✔️ ✔️
isLowerCase(thisChar) ✔️ ✔️
isPrintable(thisChar) ✔️ ✔️
isPunct(thisChar) ✔️ ✔️
isSpace(thisChar) ✔️ ✔️
isUpperCase(thisChar) ✔️ ✔️
isHexadecimalDigit(thisChar) ✔️ ✔️

Random Numbers

Function C++ Javascript / wasm
randomSeed(seed) ✔️ ✔️
random(max) ✔️ ✔️
random(min, max) ✔️ ✔️

Bits and Bytes

Function C++ Javascript / wasm
lowByte(x) ✔️ ✔️
highByte(x) ✔️ ✔️
bitRead(x, n) ✔️ ✔️
bitWrite(x, n, b) ✔️ ✔️
bitSet(x, n) ✔️ ✔️
bitClear(x, n) ✔️ ✔️
bit(n) ✔️ ✔️

External Interrupts

Function C++ Javascript / wasm
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) ✔️ ❌ --Waiting for pthreads
detachInterrupt(interrupt) ✔️ ❌ --Waiting for pthreads
detachInterrupt(digitalPinToInterrupt(pin)) ✔️ ❌ --Waiting for pthreads

Interrupts

Function C++ Javascript / wasm
interrupts() In Progress In Progress
noInterrupts() In Progress In Progress

Check the Arduino Language Reference for more info about each function.

Support for Electron

TODO

Install

npm i linuxduino

DownloadsWeekly Downloads

3

Version

0.2.5

License

MIT

Unpacked Size

544 kB

Total Files

5

Last publish

Collaborators

  • avatar