I am new to Node.js. I have a file on my file system that controls some kind of output on my hardware. Content of the file could be an integer between 0 to 100.
I want to write to this file with a fixed delay (say every 100ms). So I wrode this code:
var duty_percentfile = fs.openSync("/sys/class/pwm/ehrpwm.1:0/duty_percent", "w");
var fade = function(){
fs.writeSync(duty_percentfile, i, null);
i = i + 5;
}
setInterval(fade, 100);
First of all, is this the best way?
Secondly, I am getting this error dispute the fact it accomplishes the job:
fs.js:321
return binding.write(fd, buffer, offset, length, position);
^
Error: EINVAL, invalid argument
at Object.writeSync (fs.js:321:18)
at Timer.<anonymous> (/var/lib/cloud9/myApps/test.js:22:8)
at Timer.ontimeout (timers.js:223:14)
I found the answer here: https://github.com/isaacs/node-graceful-fs/issues/6
It is a known issue that is fixed in node 0.6.15 about 22 days ago. Time to upgrade I guess!
Instead of null
you should specify 0
as a position (if that's what you want).
Also your i
is not initialized and will not increment.
Try this instead:
var duty_percentfile = fs.openSync("/sys/class/pwm/ehrpwm.1:0/duty_percent", "w")
, value = 0 // or whatever your initial value is
;
setInterval(function(){
fs.writeSync(duty_percentfile, value, 0);
value += 5;
}, 100);