diff --git a/README.md b/README.md index af867fe..76cbdea 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ +# !!! THIS is FORK !!! +# Very small change done to support Cubieboard's GPIO instead of Pi. + + + + + + + + + + + + # gpio - talk to your Raspberry Pi's gpio headers * demo using LED: http://www.youtube.com/watch?v=2Juo-CJ6eu4 diff --git a/lib/gpio.js b/lib/gpio.js index 17744bd..1b63891 100644 --- a/lib/gpio.js +++ b/lib/gpio.js @@ -6,6 +6,37 @@ var exists = fs.exists || path.exists; var gpiopath = '/sys/class/gpio/'; +// remap any cubieboard pins (here array from 0 to 26 with pin names and correct path) +var mapping = [ + { path:'gpio32_pd0', name:'PD0', pin:32 }, + { path:'gpio31_pd1', name:'PD1', pin:31 }, + { path:'gpio30_pd2', name:'PD2', pin:30 }, + { path:'gpio29_pd3', name:'PD3', pin:29 }, + { path:'gpio28_pd4', name:'PD4', pin:28 }, + { path:'gpio43_pd5', name:'PD5', pin:43 }, + { path:'gpio42_pd6', name:'PD6', pin:42 }, + { path:'gpio41_pd7', name:'PD7', pin:41 }, + { path:'gpio40_pd8', name:'PD8', pin:40 }, + { path:'gpio39_pd9', name:'PD9', pin:39 }, + { path:'gpio38_pd10', name:'PD10', pin:38 }, + { path:'gpio37_pd11', name:'PD11', pin:37 }, + { path:'gpio36_pd12', name:'PD12', pin:36 }, + { path:'gpio51_pd13', name:'PD13', pin:51 }, + { path:'gpio50_pd14', name:'PD14', pin:50 }, + { path:'gpio49_pd15', name:'PD15', pin:49 }, + { path:'gpio48_pd16', name:'PD16', pin:48 }, + { path:'gpio47_pd17', name:'PD17', pin:47 }, + { path:'gpio46_pd18', name:'PD18', pin:46 }, + { path:'gpio45_pd19', name:'PD19', pin:45 }, + { path:'gpio44_pd20', name:'PD20', pin:44 }, + { path:'gpio59_pd21', name:'PD21', pin:59 }, + { path:'gpio58_pd22', name:'PD22', pin:58 }, + { path:'gpio57_pd23', name:'PD23', pin:57 }, + { path:'gpio54_pd24', name:'PD24', pin:54 }, + { path:'gpio53_pd25', name:'PD25', pin:53 }, + { path:'gpio55_pd26', name:'PD26', pin:55 } +]; + var logError = function(e) { if(e) console.log(e.code, e.action, e.path); }; var logMessage = function() { if (exports.logging) console.log.apply(console, arguments); }; @@ -35,24 +66,27 @@ var _read = function(file, fn) { }; var _unexport = function(number, fn) { - _write(number, gpiopath + 'unexport', function(err) { - if(err) return logError(err); - if(typeof fn === 'function') fn(); - }, 1); + rn = mapping[number].pin; + _write(rn, gpiopath + 'unexport', function(err) { + if(err) return logError(err); + if(typeof fn === 'function') fn(); + }, 1); }; var _export = function(n, fn) { - if(exists(gpiopath + 'gpio'+n)) { - // already exported, unexport and export again - logMessage('Header already exported'); - _unexport(n, function() { _export(n, fn); }); - } else { - logMessage('Exporting gpio' + n); - _write(n, gpiopath + 'export', function(err) { - // if there's an error when exporting, unexport and repeat - if(err) _unexport(n, function() { _export(n, fn); }); - else if(typeof fn === 'function') fn(); - }, 1); - } + rp = mapping[n].path; + rn = mapping[n].pin; + if(exists(gpiopath + rp)) { + // already exported, unexport and export again + logMessage('Header already exported'); + _unexport(rn, function() { _export(rn, fn); }); + } else { + logMessage('Exporting gpio' + n + '(' + rn + ')'); + _write(rn, gpiopath + 'export', function(err) { + // if there's an error when exporting, unexport and repeat + if(err) _unexport(rn, function() { _export(rn, fn); }); + else if(typeof fn === 'function') fn(); + }, 1); + } }; // fs.watch doesn't get fired because the file never @@ -91,9 +125,12 @@ var GPIO = function(headerNum, opts) { this.headerNum = headerNum; this.value = 0; + + rp = mapping[headerNum].path; + rn = mapping[headerNum].pin; this.PATH = {}; - this.PATH.PIN = gpiopath + 'gpio' + headerNum + '/'; + this.PATH.PIN = gpiopath + rp + '/'; this.PATH.VALUE = this.PATH.PIN + 'value'; this.PATH.DIRECTION = this.PATH.PIN + 'direction'; diff --git a/test/test-sainsmart16.js b/test/test-sainsmart16.js new file mode 100644 index 0000000..b394b28 --- /dev/null +++ b/test/test-sainsmart16.js @@ -0,0 +1,34 @@ +var gpio = require("gpio"); +var intervalTimer; + +var items = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]; +var gpios = []; +var current = 0; + +function async(i, callback) { + console.log('prepare : ' + i); + callback(gpio.export(i, { + ready: function() { + console.log('ready : ' + i); + } + })); +} +function final() { + console.log('Done', ''); + + setInterval(function() { + current = (current + 1) % 16; + gpio = gpios[current] + gpio.set(gpio.value - 1); + console.log('switching : ' + current + ' to ' + gpio.value); + }, 100); +} + +items.forEach(function(item) { + async(item, function(result) { + gpios.push(result); + if(gpios.length == items.length) { + final(); + } + }) +});