Last active
March 12, 2016 06:52
-
-
Save CLCL/ab5be8ddc4caa0d72aa5 to your computer and use it in GitHub Desktop.
秋月電子7セグメントLED DIP化キット [AE-7SEG-BOARD] 10個をRaspberry Piにつないで、数字をnode.jsで表示する。
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
'use strict'; | |
// akizuki-7seg-10col.js: | |
// Usage: | |
// (Raspberry PiにRaspbian Jessieをインストールして、 | |
// sudo raspi-config で SPI使用を有効にしてリブート後、 | |
// piユーザでログイン) | |
// sudo apt-get update | |
// sudo apt-get -y remove nodered nodejs nodejs-legacy | |
// curl -LO http://node-arm.herokuapp.com/node_latest_armhf.deb | |
// sudo dpkg -i node_latest_armhf.deb | |
// mkdir 7seg | |
// cd $_ | |
// npm install onoff pi-spi | |
// curl -LO https://gist.githubusercontent.com/CLCL/ab5be8ddc4caa0d72aa5/raw/80600eec8e20584d945c7b249ebb001cd2661b59/akizuki-7seg-10col.js | |
// sudo node akizuki-7seg-10col.js | |
// | |
// #Use libs/Global Objects | |
// | |
var SPI = require('pi-spi'); | |
var Gpio = require('onoff').Gpio; | |
// SPI初期化 | |
var spi = SPI.initialize('/dev/spidev0.0'); | |
spi.clockSpeed(16000000); // 16MHz | |
// | |
// #define | |
// | |
// 信号値定義 | |
var HIGH = 1; | |
var LOW = 0; | |
// 信号ピン定義 | |
var LATCH = 25; // GPIO25(22pin):74HC595 LATCH -> LAT | |
// MOSI(19pin)[変更不可]:SPI DATA OUTPUT -> /DAT | |
// SCLK(23pin)[変更不可]:SPI CLOCK -> CLK | |
// | |
// Class | |
// | |
var SEG7 = function(f) { | |
this.gpio = { | |
lat : new Gpio(f.LAT, 'out') | |
}; | |
this.digits = [ | |
0x3f, 0x06, 0x5b, 0x4f, | |
0x66, 0x6d, 0x7d, 0x27, | |
0x7f, 0x6f | |
]; | |
this.gpio.lat.writeSync(LOW); // ラッチ(LED表示固定) | |
}; | |
SEG7.prototype = { | |
write: function(buf) { | |
return new Promise(function(resolve, reject) { | |
spi.write( buf, function(e) { | |
if (e) console.error(e); | |
}); | |
}); | |
}, | |
latch: function() { | |
var self = this; | |
return new Promise(function(resolve, reject) { | |
self.gpio.lat.writeSync(HIGH); // ラッチ解除(シフトレジスタ内容表示) | |
self.gpio.lat.writeSync(LOW); // ラッチ(LED表示固定) | |
}); | |
}, | |
show: function(f) { | |
var self = this; | |
var buf = Buffer(f); | |
var buf2 = new Buffer(10); | |
var j = 0; | |
for (var i = 0; i < buf.length; i++) { | |
if (buf[i].toString() == '.'.charCodeAt(0)) { | |
buf2[j-1] |= 128; | |
} | |
else { | |
buf2[j] = self.digits[buf[i]-48]; | |
j++; | |
} | |
} | |
self.write(buf2).then(self.latch()); | |
} | |
}; | |
// | |
// Main Routine | |
// | |
var seg7 = new SEG7({LAT: LATCH}); | |
var timer = Date.now(); | |
(function loop() { | |
var now = Date.now(); | |
if (now - timer > 16) { | |
seg7.show('3.141592653'); | |
timer = now; | |
} | |
process.nextTick(loop); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment