firmata / firmata.js

JavaScript implementation of the Firmata protocol
711 stars 147 forks source link

Allow multiple pins to be updated before writing to the board #189

Closed dtex closed 6 years ago

dtex commented 6 years ago

I had previously suggested updating the ports for all digitalWrites on nextTick but then remembered that there are devices whose state is changed from read to write by toggling a digital pin. If a user toggled the pin and expected to be able to read immediately (in the same pass through the event loop), their code would fail and it would be unclear why.

This option requires that a user pass an additional parameter to digitalWrite to cause it to be enqueued instead of written imediately and then call writeQueuedDigitalPorts() to actually update the pins on the Arduino.

This API change will not break other io-plugins when used like so:

  this.pinsArray.forEach((pin, index) => {
    this.io.digitalWrite(pin, (frame >> (pinCount - index - 1) & 0x01), true);
  });

  if (this.io.flushDigitalPorts) {
    this.io.flushDigitalPorts();
  }

It's working great. Max step speed went from about 550 to 1000 steps per second.

Open to suggestions on method names as "writeQueuedDigitalPorts" is hard on the fingers.

coveralls commented 6 years ago

Coverage Status

Coverage increased (+0.002%) to 99.885% when pulling 2717ad4da2381aa7fa34d272d16792a43f2e899f on dtex:master into d08665e8d43996fdceec940540415aed79904eea on firmata:master.