Last active
March 30, 2016 17:26
-
-
Save dudleyjosh/905cafcec2ba3bad6a0fc32c68905bee to your computer and use it in GitHub Desktop.
Test Pin Interrupts on Tessel 1 & 2 Hardware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #tessel run pins.js T1 or #t2 run pins.js T2 | |
var tesselHardware = process.argv[2] || 'T2'; | |
var tessel = require('tessel'); | |
var PORT_A, PORT_B; | |
if ( tesselHardware == 'T1' ) { | |
PORT_A = { | |
"port": 'A', | |
"pins": ['G1', 'G2', 'G3'], | |
"counter": 0 | |
}; | |
PORT_B = { | |
"port": 'B', | |
"pins": { | |
"G1": tessel.port['B'].pin['G1'], | |
"G2": tessel.port['B'].pin['G2'], | |
"G3": tessel.port['B'].pin['G3'] | |
}, | |
"counter": 0 | |
}; | |
} | |
else if ( tesselHardware == 'T2' ) { | |
PORT_A = { | |
"port": 'A', | |
"pins": [2, 5, 6, 7], | |
"counter": 0 | |
}; | |
PORT_B = { | |
"port": 'B', | |
"pins": { | |
2: tessel.port['B'].pin[2], | |
5: tessel.port['B'].pin[5], | |
6: tessel.port['B'].pin[6], | |
7: tessel.port['B'].pin[7] | |
}, | |
"counter": 0 | |
}; | |
} | |
else { | |
console.log('#tessel run pins.js T1 or #t2 run pins.js T2'); | |
} | |
function setPinEvents(PORT) { | |
var port = PORT.port; | |
var pins = PORT.pins; | |
var counter = PORT.counter; | |
if ( Array.isArray(pins) ) { | |
pins.forEach(function(pin) { | |
console.log('port:', port, 'pin:', pin); | |
tessel.port[port].pin[pin].pull('pulldown'); | |
tessel.port[port].pin[pin].on('change', function() { | |
counter++; | |
if ( tesselHardware == 'T1' ) { | |
console.log('port:', port, 'pin:', pin, 'val:', this.read(), 'count:', counter); | |
} | |
else { | |
// this works | |
this.rawRead(function (err, val) { | |
console.log('port:', port, 'pin:', pin, 'val:', val, 'count:', counter); | |
}); | |
// this doesn't work | |
/* | |
this.read(function(err, val) { | |
console.log('port:', port, 'pin:', pin, 'val:', val, 'count:', counter); | |
}); | |
*/ | |
} | |
}); | |
}); | |
} | |
else if ( typeof pins === 'object' ) { | |
for ( var pin in pins ) { | |
console.log('port:', port, 'pin:', pin); | |
pins[pin].pull('pulldown'); | |
// this only works properly with T1 hardware | |
pins[pin].on('change', function () { | |
counter++; | |
if ( tesselHardware == 'T1' ) { | |
console.log('port:', port, 'pin:', pin, 'val:', this.read(), 'count:', counter); | |
} | |
else { | |
// this works | |
this.rawRead(function (err, val) { | |
console.log('port:', port, 'pin:', pin, 'val:', val, 'count:', counter); | |
}); | |
// this doesn't work | |
//this.read(function(err, val) { | |
// console.log('port:', port, 'pin:', pin, 'val:', val, 'count:', counter); | |
//}); | |
} | |
}); | |
// this works properly with both T1 & T2 hardware, but I'm not sure it should be necessary | |
/* | |
(function(pinId) { | |
pins[pinId].on('change', function () { | |
if ( tesselHardware == 'T1' ) { | |
console.log('port:', port, 'pin:', pinId, 'val:', this.read()); | |
} | |
else { | |
// this works | |
this.rawRead(function (err, val) { | |
console.log('port:', port, 'pin:', pinId, 'val:', val); | |
}); | |
// this doesn't work | |
//this.read(function(err, val) { | |
// console.log('port:', port, 'pin:', pin, 'val:', val, 'count:', counter); | |
//}); | |
} | |
}); | |
})(pin); | |
*/ | |
} | |
} | |
} | |
setImmediate(function() { | |
setPinEvents(PORT_A); | |
setPinEvents(PORT_B); | |
console.log(tesselHardware + ' - GPIO button test ready for PORT_A and PORT_B'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment