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 = ; Linuxduino { console; // 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 = ; Linuxduino { console; var ledPin = 4; // RPi GPIO4 Linuxduino; while 1 console; Linuxduino; // Turn on LED Linuxduino; // Delay 1 sec console; Linuxduino; // Turn off LED Linuxduino; // 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 = ; Linuxduino { console; // Set serial device name Linuxduino; console; // Open serial Serial = ; Serial; // Write something Serial; }
Second, directly Serial.begin("/dev/...", 9600):
var Linuxduino = ; Linuxduino { console; // Open serial Serial = ; Serial; // Write something Serial; }
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 = ; Linuxduino { console; // Set SPI device name Linuxduino; console; // Open SPI port settingsA = LinuxduinoSPI_CLOCK_DIV64 LinuxduinoMSBFIRST LinuxduinoSPI_MODE3; SPI = ; SPI; // Send Hello World to SPI device and listen for received data every second buff = "Hello World\n"; var ret; while1 SPI; forvar i=0; i<bufflength; i++ ret = SPI; // Transmit and receive one byte processstdout; processstdout; SPI; Linuxduino; SPI; }
Second, directly in SPI.begin("/dev/..."):
var Linuxduino = ; Linuxduino { console; // Open SPI port settingsA = LinuxduinoSPI_CLOCK_DIV64 LinuxduinoMSBFIRST LinuxduinoSPI_MODE3; SPI = ; SPI; // 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 = ; Linuxduino { console; // Set I2C device name Linuxduino; console; // Open I2C port Wire = ; Wire; // Send Hello World! to device Wire; // 7-bit device address Wire; Wire; var world = "World!"; Wire; Wire; Wire; }
Second, directly in Wire.begin("/dev/..."):
var Linuxduino = ; Linuxduino { console; // Open I2C port Wire = ; Wire; // Send Hello World! to device Wire; // 7-bit device address Wire; Wire; var world = "World!"; Wire; Wire; Wire; }
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