Hacker News new | ask | show | jobs
by z3t4 3882 days ago
With JavaScript (nodejs)

  var lightsOn = true;

  setInterval(blink, 2000);

  function blink() {
    var fs = require("fs");
	
    fs.writeFile("/sys/class/leds/beaglebone:green:usr0/brightness", lightsOn, function (err) {
      if (err) throw err;
    });
	
    lightsOn = lightsOn ? false : true;

  }
2 comments

It makes no difference but for some reason I feel the urge to shorten the last line to:

lightsOn = !lightsOn;

I'll see myself out.

Every time I write that in JavaScript, I delete it and just use ! As well haha
Don't forget to turn off the light on your way out.

lightsOn = !lightsOn || lightsOn

Writing a boolean type to SysFS/GPIO from NodeJS doesn't quite work so I tweaked your example to following similar pattern of the other languages. Thanks.
I wanted to show the power of JavaScript types, and you got a type bug? What the irony. I'll probably report that as a bug in nodejs or an undocumented "feature".