Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // display.js
- function Display(x, y) {
- this.ws281x = require('rpi-ws281x-native');
- this.x = x;
- this.y = y;
- this.ledsAmount = x * y;
- this.dmaNum = 10;
- this.pixelData = new Uint32Array(this.ledsAmount);
- this.ledDirection = 'top-bottom-right-up';
- this.animationHandler = null;
- }
- Display.prototype.init = function () {
- this.ws281x.init(this.ledsAmount, {dmaNum: this.dmaNum});
- var self = this;
- if (this.animationHandler) {
- clearInterval(this.animationHandler);
- }
- this.animationHandler = setInterval(function(){
- self.ws281x.render(self.pixelData);
- }, 5);
- }
- Display.prototype.setDMA = function (number) {
- this.dmaNum = number;
- }
- Display.prototype.setLedDirection = function (direction) {
- this.ledDirection = direction;
- }
- Display.prototype.setPixel = function(x, y, color) {
- if (x > this.x - 1) {
- throw 'X dimension exceeded ' + this.x + ': ' + x;
- }
- if (y > this.y - 1) {
- throw 'Y dimension exceeded ' + this.y + ': ' + y;
- }
- var targetColor = [0, 0, 0];
- if (Array.isArray(color)) {
- targetColor = color;
- }
- else if (typeof color == 'string') {
- targetColor = _parseColorString(color);
- }
- function _parseColorString(color) {
- if (color.substr(0, 1) == '#') {
- color = color.substr(1);
- }
- var color = color.match(/.{1,2}/g);
- color = color.map(function(e){
- return parseInt(e, 16);
- });
- return color;
- }
- var finalCoord = this.calculatePosition(x, y);
- this.pixelData[finalCoord] = this.rgb2int(targetColor[0], targetColor[1], targetColor[2]);
- }
- Display.prototype.calculatePosition = function(x, y) {
- var position = 0;
- switch(this.ledDirection) {
- default:
- case 'top-bottom-right-up':
- position = this.y * x + (x % 2 ? (this.y - y - 1) : y );
- break;
- }
- return position;
- }
- Display.prototype.rainbow = function() {
- var offset = 0;
- var self = this;
- if (this.animationHandler) {
- clearInterval(this.animationHandler);
- }
- this.animationHandler = setInterval(function () {
- for (var i = 0; i < self.ledsAmount; i++) {
- self.pixelData[i] = colorwheel((offset + i) % 256);
- }
- offset = (offset + 1) % 256;
- }, 5);
- function colorwheel(pos) {
- pos = 255 - pos;
- if (pos < 85) { return self.rgb2int(255 - pos * 3, 0, pos * 3); }
- else if (pos < 170) { pos -= 85; return self.rgb2int(0, pos * 3, 255 - pos * 3); }
- else { pos -= 170; return self.rgb2int(pos * 3, 255 - pos * 3, 0); }
- }
- }
- Display.prototype.rgb2int = function(r, g, b) {
- return ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
- }
- Display.prototype.reset = function () {
- this.ws281x.reset();
- }
- Display.prototype.ws281x = function () {
- return this.ws281x;
- }
- module.exports = Display;
- // end display.js
- // index.js:
- var DisplayModule = require('./display.js');
- var display = new DisplayModule(32, 8);
- display.init();
- process.on('SIGINT', function () {
- display.reset();
- process.nextTick(function () {
- process.exit(0);
- });
- });
- display.setPixel(5, 0, '#0F0F0F');
- console.log('Press <ctrl>+C to exit.');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement