Advertisement
Guest User

Display driver

a guest
Jul 31st, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // display.js
  2. function Display(x, y) {
  3.     this.ws281x = require('rpi-ws281x-native');
  4.     this.x = x;
  5.     this.y = y;
  6.     this.ledsAmount = x * y;
  7.     this.dmaNum = 10;
  8.     this.pixelData = new Uint32Array(this.ledsAmount);
  9.     this.ledDirection = 'top-bottom-right-up';
  10.     this.animationHandler = null;
  11. }
  12.  
  13. Display.prototype.init = function () {
  14.     this.ws281x.init(this.ledsAmount, {dmaNum: this.dmaNum});
  15.  
  16.     var self = this;
  17.     if (this.animationHandler) {
  18.         clearInterval(this.animationHandler);
  19.     }
  20.     this.animationHandler = setInterval(function(){
  21.         self.ws281x.render(self.pixelData);
  22.     }, 5);
  23. }
  24.  
  25. Display.prototype.setDMA = function (number) {
  26.     this.dmaNum = number;
  27. }
  28.  
  29. Display.prototype.setLedDirection = function (direction) {
  30.     this.ledDirection = direction;
  31. }
  32.  
  33. Display.prototype.setPixel = function(x, y, color) {
  34.     if (x > this.x - 1) {
  35.         throw 'X dimension exceeded ' + this.x + ': ' + x;
  36.     }
  37.     if (y > this.y - 1) {
  38.         throw 'Y dimension exceeded ' + this.y + ': ' + y;
  39.     }
  40.  
  41.     var targetColor = [0, 0, 0];
  42.     if (Array.isArray(color)) {
  43.         targetColor = color;
  44.     }
  45.     else if (typeof color == 'string') {
  46.         targetColor = _parseColorString(color);
  47.     }
  48.  
  49.     function _parseColorString(color) {
  50.         if (color.substr(0, 1) == '#') {
  51.             color = color.substr(1);
  52.         }
  53.         var color = color.match(/.{1,2}/g);
  54.         color = color.map(function(e){
  55.             return parseInt(e, 16);
  56.         });
  57.         return color;
  58.     }
  59.  
  60.     var finalCoord = this.calculatePosition(x, y);
  61.     this.pixelData[finalCoord] = this.rgb2int(targetColor[0], targetColor[1], targetColor[2]);
  62. }
  63.  
  64. Display.prototype.calculatePosition = function(x, y) {
  65.     var position = 0;
  66.     switch(this.ledDirection) {
  67.         default:
  68.         case 'top-bottom-right-up':
  69.             position = this.y * x + (x % 2 ? (this.y - y - 1) : y );
  70.             break;
  71.     }
  72.     return position;
  73. }
  74.  
  75. Display.prototype.rainbow = function() {
  76.     var offset = 0;
  77.     var self = this;
  78.  
  79.     if (this.animationHandler) {
  80.         clearInterval(this.animationHandler);
  81.     }
  82.  
  83.     this.animationHandler = setInterval(function () {
  84.         for (var i = 0; i < self.ledsAmount; i++) {
  85.             self.pixelData[i] = colorwheel((offset + i) % 256);
  86.         }
  87.         offset = (offset + 1) % 256;
  88.     }, 5);
  89.  
  90.     function colorwheel(pos) {
  91.         pos = 255 - pos;
  92.         if (pos < 85) { return self.rgb2int(255 - pos * 3, 0, pos * 3); }
  93.         else if (pos < 170) { pos -= 85; return self.rgb2int(0, pos * 3, 255 - pos * 3); }
  94.         else { pos -= 170; return self.rgb2int(pos * 3, 255 - pos * 3, 0); }
  95.     }
  96. }
  97. Display.prototype.rgb2int = function(r, g, b) {
  98.     return ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
  99. }
  100.  
  101. Display.prototype.reset = function () {
  102.     this.ws281x.reset();
  103. }
  104.  
  105. Display.prototype.ws281x = function () {
  106.     return this.ws281x;
  107. }
  108.  
  109. module.exports = Display;
  110. // end display.js
  111.  
  112. // index.js:
  113. var DisplayModule = require('./display.js');
  114. var display = new DisplayModule(32, 8);
  115. display.init();
  116.  
  117. process.on('SIGINT', function () {
  118.     display.reset();
  119.     process.nextTick(function () {
  120.         process.exit(0);
  121.     });
  122. });
  123.  
  124. display.setPixel(5, 0, '#0F0F0F');
  125. console.log('Press <ctrl>+C to exit.');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement