Created
August 4, 2017 07:06
-
-
Save Palmr/d452815ff4d8d33265760909cd9a0751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sleep = require('sleep'); | |
var Gpio = require('onoff').Gpio; | |
SO = new Gpio(6, 'out'), | |
SI = new Gpio(13, 'in', 'both'), | |
SD = new Gpio(19, 'in', 'rising'), | |
SC = new Gpio(26, 'out'); | |
function sendByte(byte) { | |
for (var b = 0; b < 8; b++) { | |
if ((byte << b) & 0x80) { | |
SO.writeSync(1); | |
} | |
else { | |
SO.writeSync(0); | |
} | |
sleep.usleep(60); | |
SC.writeSync(0); | |
sleep.usleep(120); | |
SC.writeSync(1); | |
sleep.usleep(120); | |
} | |
sleep.usleep(240); | |
} | |
function writeString(msg) { | |
for (var c = 0; c < msg.length; c++) { | |
var charCode = msg.charCodeAt(c); | |
sendByte(charCode); | |
} | |
sendByte(0); | |
} | |
// Assuming sending is using external clock | |
function getByte() { | |
var byteReceived = 0x00; | |
for (var b = 7; b >= 0; b--) { | |
sleep.usleep(60); | |
SC.writeSync(0); | |
sleep.usleep(120); | |
if (SI.readSync()) { | |
byteReceived |= 0x01 << b; | |
} | |
SC.writeSync(1); | |
sleep.usleep(120); | |
} | |
console.log("Got value: " + byteReceived); | |
} | |
process.on('SIGINT', function () { | |
SO.unexport(); | |
SI.unexport(); | |
SD.unexport(); | |
SC.unexport(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment