GithubHelp home page GithubHelp logo

tessel-mocks's Introduction

#Tessel-Mocks

Allows local runtime for software designed for the Tessel 2.

Tessel 2 makes the require('tessel') package available once your code has been run on or pushed to the device. Unfortunately, the tessel package is not available locally, so when the script is run on your development machine the execution will halt with a missing package error.

This repo allows users to mock the functionality of the actual tessel device on a local machine.

Based loosely on https://github.com/samilamti/tessel-fakes

##Usage

In your main script, near the rest of the require statements, replace the var tessel = require("tessel") with the following:

try {
    var tessel = require("tessel");
} catch(e) {
    console.error("Tessel not found - running mocks instead");
    var Tessel = require('tessel-mocks');
    tessel = new Tessel();
}

From this point on when running locally, the Tessel commands will print statements in the console instead of performing actions on the device.

  • To run code on the Tessel: t2 run index.js
  • To run code locally: node index.js

###Example Comparison

When running on the Tessel, this code will flash LED3 (blue) every 2 seconds:

setInterval(function(){
  tessel.led[3].toggle();
}, 2000);

... but when running locally with node index.js, the console prints this:

Tessel not found - running mocks instead
TM | Toggled LED2 on
TM | Toggled LED2 off
TM | Toggled LED2 on

###Mocking Responses

Serial communication devices can be mocked by passing additional parameters in any method that supports a callback. These extra parameters do not affect Tessel runtime and will be ignored on the device, but while running locally they will return the values specified.

To do this, pass 2 extra parameters (as if they were the callback values) to the target functions. Take the I2C Transfer method:

i2c.transfer(txbuf, rxlen, callback);

... and this becomes:

i2c.transfer(txbuf, rxlen, callback, mockError, mockReturn);

Example: BME280 I2C

This code prints no error and returns the ID of the BME280 device (which is 60):

var slaveAddress = 0x77;
var readLength = 1;
var i2c = new tessel.port.A.I2C(slaveAddress)

i2c.transfer(new Buffer([0xD0]), readLength, function (err, rx) {
  console.log('error returned by I2C Slave: ', err)
  console.log('buffer returned by I2C slave ('+slaveAddress.toString(16)+'):', rx);
});

Output:

INFO Running index.js...
error returned by I2C Slave:  null
buffer returned by I2C slave (77): <Buffer 60>

If this script is run locally with node index.js, the callback will not contain data:

INFO Running index.js...
Tessel not found - running mocks instead
TM | I2C Transfer: �
error returned by I2C Slave:  [Error]
buffer returned by I2C slave (77): undefined

Instead, if mock responses are passed in:

var slaveAddress = 0x77;
var readLength = 1;
var i2c = new tessel.port.A.I2C(slaveAddress)

i2c.transfer(new Buffer([0xD0]), readLength, function (err, rx) {
  console.log('error returned by I2C Slave: ', err)
  console.log('buffer returned by I2C slave ('+slaveAddress.toString(16)+'):', rx);
}, 'Test Error', '99');

... then the mock data will be populated in console:

Tessel not found - running mocks instead
TM | I2C Transfer: �
error returned by I2C Slave:  [Error: Test Error]
buffer returned by I2C slave (77): 99

##Todo Mocks

  • LEDs
  • Digital Pins
  • Analog Pins
  • Interrupt Pins
  • Ports
  • UART
  • SPI
  • I2C
  • PWM
  • Button

tessel-mocks's People

Contributors

emcniece avatar

Watchers

James Cloos avatar  avatar

Forkers

bertpareyn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.