Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. const ws281x = require('rpi-ws281x-native');
  2. const EventEmitter = require('events');
  3.  
  4. function parse(i, min, max) {
  5. const num = Number(i);
  6.  
  7. if (isNaN((num))) {
  8. return min;
  9. }
  10.  
  11. return Math.min(max, Math.max(min, num));
  12. }
  13.  
  14. const DEFAULT_OPTIONS = {
  15. frequency: 700000,
  16. dmaNum: 10,
  17. gpioPin: 18,
  18. };
  19.  
  20. const LED_MAX = 1000;
  21.  
  22. class FastPixel extends EventEmitter {
  23. constructor() {
  24. super();
  25. this.ledCount = 1;
  26. this.pixelData = new Uint32Array(1);
  27. this.brightness = 100;
  28. this.timeout = null;
  29. this.initalized = false;
  30. }
  31.  
  32. setLedCount(str) {
  33. const num = parse(str, 0, LED_MAX);
  34.  
  35. if (this.ledCount === num) {
  36. return;
  37. }
  38.  
  39. if (this.initalized) {
  40. this.clearAnimation();
  41. this.pixelData = null;
  42. ws281x.reset();
  43. }
  44.  
  45. try {
  46. ws281x.init(num, DEFAULT_OPTIONS);
  47. this.ledCount = num;
  48. this.pixelData = new Uint32Array(num);
  49. this.initalized = true;
  50. } catch (e) {
  51. this.emit('error', e.message);
  52. }
  53. }
  54.  
  55. setBrightness(percent) {
  56. const num = parse(percent, 0, 100);
  57.  
  58. if (this.brightness === num) {
  59. return;
  60. }
  61. const brightness = Math.floor(num * 255 / 100);
  62. if (this.initalized) {
  63. try {
  64. ws281x.setBrightness(brightness);
  65. this.brightness = num;
  66. } catch (e) {
  67. this.emit('error', e.message);
  68. }
  69. }
  70. }
  71.  
  72. clearAnimation() {
  73. if (this.timeout != null) {
  74. clearTimeout(this.timeout);
  75. this.timeout = null;
  76. }
  77. }
  78.  
  79. rgb2Int(r, g, b) {
  80. return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
  81. }
  82.  
  83. setPixel(i, { r = 0, g = 0, b = 0 }) {
  84. this.pixelData[i] = this.rgb2Int(r, g, b);
  85. }
  86.  
  87. render() {
  88. if (!this.initalized) {
  89. return;
  90. }
  91.  
  92. try {
  93. ws281x.render(this.pixelData);
  94. } catch (e) {
  95. this.emit('error', e.message);
  96. }
  97. }
  98.  
  99. animate(fn, time = 1000/30) {
  100. if (!this.initalized) {
  101. this.emit('error', 'The driver has not been initialized.');
  102. return;
  103. }
  104.  
  105. this.clearAnimation();
  106. const execute = () => {
  107. const start = new Date();
  108. fn(this.pixelData, Math.floor(start / time));
  109.  
  110. try {
  111. ws281x.render(this.pixelData);
  112. } catch (e) {
  113. this.emit('error', e.message);
  114. this.clearAnimation();
  115. }
  116. const elapsed = new Date() - start;
  117. if (elapsed < time) {
  118. this.timeout = setTimeout(execute, time - elapsed);
  119. } else {
  120. this.emit('warn', `animation loop ran long ${elapsed}ms`);
  121. execute();
  122. }
  123. };
  124.  
  125. execute();
  126. }
  127. }
  128.  
  129. module.exports = new FastPixel();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement