Code Monkey home page Code Monkey logo

pigpio-mock's Introduction

Pigpio-mock

Article: https://viktorkirilov.me/post/mocking-raspberry-gpios/

A mock library used for raspberry pi development on your local machine. It copies all method from https://github.com/fivdi/pigpio and logs information to the console, instead of controlling the Raspberry's GPIO pins.

Build Status Coverage Status

Example

You can also check examples folder.

const Gpio = require("pigpio-mock").Gpio;
const led = new Gpio(13, { mode: Gpio.OUTPUT });
let dutyCycle = 0;

setInterval(function() {
	led.pwmWrite(dutyCycle);

	dutyCycle += 50;
	if (dutyCycle > 255) {
		dutyCycle = 0;
	}
}, 500);

should output:

[GPIO 13 / constructor] Initializing...
[GPIO 13 / constructor] Setting mode = 1
[GPIO 13 / pwmWrite-analogWrite] Setting dutyCycle = 0
[GPIO 13 / pwmWrite-analogWrite] Setting dutyCycle = 50
[GPIO 13 / pwmWrite-analogWrite] Setting dutyCycle = 100
[GPIO 13 / pwmWrite-analogWrite] Setting dutyCycle = 150
[GPIO 13 / pwmWrite-analogWrite] Setting dutyCycle = 200
[GPIO 13 / pwmWrite-analogWrite] Setting dutyCycle = 250

Installation

It is as simple as:

yarn add pigpio-mock

and you can use it in your code as:

const pigpio = process.env.NODE_ENV !== "production" ? 
    require("pigpio-mock") : 
    require("pigpio");

Implemented classes:

  • Gpio
  • GpioBank
  • Notifier

Contribution

All kind of contribution will be greatly appreciated.

Pull requests

Running tests

yarn test

pigpio-mock's People

Contributors

cinderblock avatar coollision avatar dan-pcces avatar deepsyx avatar edob avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pigpio-mock's Issues

initialize and terminate methods

Hello.

Congratulations by mock.

Can you implements initialize and terminate methods, for I can use your lib?

Or need I make a fork and implements it?

Pigpio.initialize() and Pigpio.terminate();

Thanks

Update on npm

would it be possible to update the version of pigpio-mock on npm?

pwmRange is not a function

It looks like the mock Gpio object uses a variable called "pwmRange" as well as a function called "pwmRange".

When the constructor is executed, the pwmRange variable replaces the pwmRange function of the Gpio object.

Sample code to demonstrate:

const Gpio = require( "pigpio-mock" ).Gpio;

PWM_FREQ = 50;
PWM_RANGE = 1000;

VAL_MIN = 10;
VAL_MAX = 100;

// Output the pwmRange value of the Gpio object prototype
// This should be (and is) the pwmRange function
console.log( "Gpio prototype pwmRange is:", Gpio.prototype.pwmRange );

// Create a Gpio (mock) object
const servo = new Gpio( 17, { mode: Gpio.OUTPUT } );

// Output the pwmRange value of a Gpio instance
// This *should* be a function, but it is actually the current pwmRange value (default=0)
console.log( "Gpio instance pwmRange is:", servo.pwmRange );

// Set PWM freq
servo.pwmFrequency( PWM_FREQ );

// This will fail because it is a function call on an integer value, rather than on the
// desired pwmRange function
servo.pwmRange( PWM_RANGE );

// Write some PWM values
servo.pwmWrite( VAL_MIN );
servo.pwmWrite( VAL_MAX );

A fix would probably be to change the internal pwmRange variable to include a prefix such as an underscore (e.g., _pwmrange) to separate it from the function with the same name.

extend eventemitter

For me to use mock and to be compatible with pigpio which "inherts" event emitter and thus for use when mocking interrupts (which I am doing) your Gpio class should extend eventemitter. A simple change.

class Gpio extends EventEmitter {
  constructor(pin, options) {
    super()

pr to follow

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.