tessel/t2-firmware

v0.0.12 firmware - pin.on('change', function(pinVal){}) not always providing pinVal properly

Opened this issue · 2 comments

The simple, single pin example seems to work:

Using external pulldown resistor
img_4843

var tessel = require('tessel');
tessel.ports.A.pin[2].input();
tessel.ports.A.pin[2].on('change', function(pinVal) {
    console.log(pinVal);
});

Using internal pulldown resistor pin.pull('pulldown') mode
img_4841

var tessel = require('tessel');
tessel.ports.A.pin[2].pull('pulldown');
tessel.ports.A.pin[2].input();
tessel.ports.A.pin[2].on('change', function(pinVal) {
    console.log(pinVal);
});

However, if I try to setup multiple pins at once using an array.forEach loop and, in this case, only have pin 2 connected to the button... pinVal output never changes (always 0 when using pulldown, always 1 when using pullup):

[2, 5].forEach(function(pin) {
    tessel.ports.A.pin[pin].input();
    tessel.ports.A.pin[pin].on('change', function(pinVal) {
        console.log(pin, pinVal);
    });
});

See Related conversation on Slack

To clarify a bit about this: the issue doesn't appear until a user program tries to observe more than one interrupt pin (set to input). For example:

var tessel = require('tessel');
var pinMode = 'pulldown';

tessel.ports.A.pin[2].pull(pinMode);
tessel.ports.A.pin[2].on('change', function(pinVal) {
  console.log(pinVal);
});

Is fine:

$ t2 run index.js
INFO Looking for your Tessel...
INFO Connected to bishop.
INFO Building project.
WARN The following items were found in your project directory, but not deployed.
INFO Writing project to RAM on bishop (3.072 kB)...
INFO Deployed.
INFO Running index.js...
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
...

And...

var tessel = require('tessel');
var pinMode = 'pulldown';

[2, 5].forEach(function(pin) {
  tessel.ports.A.pin[pin].pull(pinMode);
  tessel.ports.A.pin[pin].on('change', function(pinVal) {
      console.log(pin, pinVal);
  });
});

Is fine...

$ t2 run index.js
INFO Looking for your Tessel...
INFO Connected to bishop.
INFO Building project.
INFO Writing project to RAM on bishop (3.072 kB)...
INFO Deployed.
INFO Running index.js...
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0

But, then...

var tessel = require('tessel');

[2, 5].forEach(function(pin) {
  tessel.ports.A.pin[pin].input();
  tessel.ports.A.pin[pin].on('change', function(pinVal) {
      console.log(pin, pinVal);
  });
});

Has the following result:

$ t2 run index.js
INFO Looking for your Tessel...
INFO Connected to bishop.
INFO Building project.
INFO Writing project to RAM on bishop (3.072 kB)...
INFO Deployed.
INFO Running index.js...
2 0
5 0
2 0
5 0
2 1
5 1
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0

Something else odd is going on here too? Running the following code:

[2, 5].forEach(function(pin) {
    tessel.ports.A.pin[pin].pull(pinMode);
    tessel.ports.A.pin[pin].on('change', function(pinVal) {
        console.log(pin, pinVal);
    });
});

Console with both pins 2 & 5 connected to the button:

$ t2 run pintest.js
INFO Looking for your Tessel...
INFO Connected to rde.
INFO Building project.
INFO Writing project to RAM on rde (5.12 kB)...
INFO Deployed.
INFO Running pintest.js...
2 1
2 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0
2 1
5 1
2 0
5 0

With pin 2 connected & pin 5 disconnected:

2 0
2 0
2 0
2 0
2 0
2 0
2 0
2 0
2 0

With pin 5 connected & pin 2 disconnected:

5 1
5 1
5 0
5 1
5 1
5 0
5 1
5 0
5 1
5 0