rwaldron/particle-io

new five.Button ( TypeError: pin.replace is not a function )

Closed this issue · 7 comments

Hi folks,

I’ve come across an error when adding a new five.Button() to spark-io board.

I’ve looked through the examples in /eg, but there doesn’t seem to be one using a button,
so forgive me if Im missing something about buttons and spark-io - I’m kind of new to this.

"devDependencies": {
  "johnny-five": "^0.8.85",
  "spark-io": "^0.6.1"
}

Here’s the script

var five = require('johnny-five')
var Spark = require('spark-io')

var board = new five.Board({
  io: new Spark({
    token: process.env.SPARK_TOKEN,
    deviceId: process.env.SPARK_DEVICE_ID
  })
})

board.on('ready', function() {
  var button = new five.Button('D3') // Booo!

  button.on('down', function() {
    console.log('down')
  })
})

Here’s the error:

nodebots % node button.js
1438999226193 Device(s) spark-io  
1438999227426 Connected spark-io  
1438999227601 Repl Initialized  
>> /Users/chrisbuttery/Web/nodebots/node_modules/spark-io/lib/spark.js:318
  pinInt = (pin.replace(/A|D/, "") | 0) + offset;
                ^
TypeError: pin.replace is not a function
    at Spark.pinMode (/Users/chrisbuttery/Web/nodebots/node_modules/spark-io/lib/spark.js:318:17)
    at Button.Controllers.DEFAULT.initialize.value (/Users/chrisbuttery/Web/nodebots/node_modules/johnny-five/lib/button.js:29:17)
    at new Button (/Users/chrisbuttery/Web/nodebots/node_modules/johnny-five/lib/button.js:189:10)
    at Board.<anonymous> (/Users/chrisbuttery/Web/nodebots/game-buzzer.js:12:16)
    at emitNone (events.js:67:13)
    at Board.emit (events.js:163:7)
    at doNTCallback0 (node.js:415:9)
    at process._tickDomainCallback (node.js:385:13)

So here are the opts when johnny-five calls it’s button initialisation:

https://github.com/rwaldron/johnny-five/blob/master/lib/button.js#L187

if (typeof this.initialize === "function") {
  this.initialize(opts, function(data) {

    // opts = Options {pin: "D3", pinValue: "D3"}
  }
}

Here are the opts when the button is initialised:

https://github.com/rwaldron/johnny-five/blob/master/lib/button.js#L21

var Controllers = {
  DEFAULT: {
    initialize: {
      value: function(opts, dataHandler) {

        if (Pins.isFirmata(this) && typeof opts.pinValue === "string" && opts.pinValue[0] === "A") {
          opts.pinValue = this.io.analogPins[+opts.pinValue.slice(1)];
        }

        this.pin = +opts.pinValue;

        // opts = Options {pin: "D3", pinValue: "D3"}
        // this = Button {board: Board, io: Spark, id: null, pin: NaN, holdtime: 500…}
        // this.board.io.name = "spark-io"
        // 'Pins.isFirmata(this) = false
        // opts.pinValue = "D3"
        // opts.pinValue[0] === "A" = false
      }
    }
  }
}

… and the spark-io throws an error here:

https://github.com/rwaldron/spark-io/blob/master/lib/spark.js#L325

pinInt = (pin.replace(/A|D/, "") | 0) + offset;

// pin = NaN

There is no mistake in your code, this is just a bug that appears to be the result of some newer changes to Johnny-Five. I'll take a look at it this evening.

new version of Johnny-Five will push out later this evening.

Yeah that works a treat, thanks heaps!

All set—fix is released!

BOOM! 💣
Cheers @rwaldron

👏

Hi--

I am also running into the same error. I am pretty to new to this so any guidance is greatly appreciated.

  • Particle Photon Board
  • Shield Shield
  • Johnny Five
  • Particle-io
  • Continous Servo

Here's the script:

Pretty much this example

var five = require("johnny-five");
var Particle = require("particle-io");
var board = new five.Board({
  io: new Particle({
    token: "...",
    deviceId: "..."
  })
});

board.on("ready", function() {

  console.log("Use Up and Down arrows for CW and CCW respectively. Space to stop.");

  var servo = new five.Servo.Continuous(10);

  process.stdin.resume();
  process.stdin.setEncoding("utf8");
  process.stdin.setRawMode(true);

  process.stdin.on("keypress", function(ch, key) {

    if (!key) {
      return;
    }

    if (key.name === "q") {
      console.log("Quitting");
      process.exit();
    } else if (key.name === "up") {
      console.log("CW");
      servo.cw();
    } else if (key.name === "down") {
      console.log("CCW");
      servo.ccw();
    } else if (key.name === "space") {
      console.log("Stopping");
      servo.stop();
    }
  });
});

Here's the error:

1471134659325 Device(s) particle-io  
1471134661721 Connected particle-io  
1471134661821 Repl Initialized  
>> Use Up and Down arrows for CW and CCW respectively. Space to stop.
/Users/IanTMoritz/Desktop/Dash of Love/node_modules/particle-io/lib/particle.js:458
  pinInt = (pin.replace(/A|D/, "") | 0) + offset;
                ^

TypeError: pin.replace is not a function
    at Particle.pinMode (/Users/IanTMoritz/Desktop/Dash of Love/node_modules/particle-io/lib/particle.js:458:17)
    at Servo.Controllers.Standard.initialize.value (/Users/IanTMoritz/Desktop/Dash of Love/node_modules/johnny-five/lib/servo.js:59:19)
    at new Servo (/Users/IanTMoritz/Desktop/Dash of Love/node_modules/johnny-five/lib/servo.js:189:8)
    at new Servo.Continuous (/Users/IanTMoritz/Desktop/Dash of Love/node_modules/johnny-five/lib/servo.js:514:10)
    at Board.<anonymous> (/Users/IanTMoritz/Desktop/Dash of Love/bot4.js:16:15)
    at emitNone (events.js:72:20)
    at Board.emit (events.js:166:7)
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickDomainCallback (node.js:390:13)

Does it have something to do with the mapping of the shield?

Thanks!