This repository has been archived by the owner on Dec 1, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
das Projekt nun nach Geräten organisiert und der adc0832 mit Eventlistener hinzugefügt.
- Loading branch information
1 parent
e39f19e
commit efb3b09
Showing
4 changed files
with
137 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const nodePi = require('../../raspberryPi/nodepi-frog'); | ||
const {execSync} = require('child_process'); | ||
|
||
console.log = function(str){}; | ||
|
||
let sleeptime = 0.00002; | ||
/** | ||
* create a adc object with the wished gpio pins | ||
* @param {number} CS | ||
* @param {number} CLK | ||
* @param {number} DIO | ||
* @return {object} | ||
*/ | ||
let setup = function (CS = 17, CLK = 18, DIO = 27) { | ||
let adc = {}; | ||
adc.CS = nodePi.setup(CS); | ||
adc.CLK = nodePi.setup(CLK); | ||
|
||
nodePi.direction(adc.CS, 'out'); | ||
nodePi.direction(adc.CLK, 'out'); | ||
|
||
adc.DIO = nodePi.setup(DIO); | ||
return adc; | ||
}; | ||
|
||
|
||
let getRes = function(adc, channel = 0){ | ||
|
||
let dat1 = 0; | ||
let dat2 = 0; | ||
|
||
nodePi.direction(adc.DIO, 'out'); | ||
nodePi.write(adc.CS, nodePi.LOW); | ||
|
||
nodePi.write(adc.CLK, nodePi.LOW); | ||
nodePi.write(adc.DIO, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
nodePi.write(adc.CLK, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
nodePi.write(adc.CLK, nodePi.LOW); | ||
nodePi.write(adc.DIO, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
nodePi.write(adc.CLK, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
nodePi.write(adc.CLK, nodePi.LOW); | ||
nodePi.write(adc.DIO, channel === 0 ? nodePi.LOW : nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
nodePi.write(adc.CLK, nodePi.HIGH); | ||
nodePi.write(adc.DIO, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
nodePi.write(adc.CLK, nodePi.LOW); | ||
nodePi.write(adc.DIO, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
nodePi.direction(adc.DIO, 'in'); | ||
for(let i = 0; i < 8; i++){ | ||
nodePi.write(adc.CLK, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
nodePi.write(adc.CLK, nodePi.LOW); | ||
execSync(`sleep ${sleeptime}`); | ||
|
||
console.log(JSON.stringify(adc.DIO)); | ||
console.log((nodePi.read(adc.DIO))); | ||
dat1 = (dat1 << 1) | (nodePi.read(adc.DIO)); | ||
console.log(dat1); | ||
} | ||
|
||
for(let j = 0; j < 8; j++){ | ||
console.log(dat2); | ||
console.log(Number(nodePi.read(adc.DIO))); | ||
dat2 = dat2 | (nodePi.read(adc.DIO) << j); | ||
nodePi.write(adc.CLK, nodePi.HIGH); | ||
execSync(`sleep ${sleeptime}`); | ||
nodePi.write(adc.CLK, nodePi.LOW); | ||
execSync(`sleep ${sleeptime}`); | ||
} | ||
|
||
nodePi.write(adc.CS, nodePi.HIGH); | ||
nodePi.direction(adc.DIO, 'out'); | ||
|
||
if(dat1 === dat2){ | ||
return dat1; | ||
} | ||
else{ | ||
return 0; | ||
} | ||
}; | ||
|
||
module.exports.setup = setup; | ||
module.exports.getResult = getRes; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const events = require('events'); | ||
const adc0832 = require('./adc0832'); | ||
|
||
let emitter = new events.EventEmitter(); | ||
|
||
/** | ||
* listen to changes | ||
* @param {object} adc | ||
* @returns {Object} | ||
*/ | ||
let initChangeEmitter = function(adc){ | ||
return setInterval(()=>{ | ||
let lastValue = adc.DIO.value; | ||
adc.DIO.value = adc0832.getResult(adc); | ||
|
||
if(lastValue !== adc.DIO.value){ | ||
emitter.emit(`changedAdc${adc.DIO.pin}`, adc.DIO.value); | ||
} | ||
|
||
}, 10); | ||
}; | ||
|
||
let initChangeListener = function(adc, callback = (j)=>{ | ||
console.info(`adc Value changed to ${j}`); | ||
}){ | ||
emitter.on(`changedAdc${adc.DIO.pin}`, callback); | ||
}; | ||
|
||
|
||
module.exports.initChangeEmitter = initChangeEmitter; | ||
module.exports.initChangeListener = initChangeListener; |
This file was deleted.
Oops, something went wrong.
This file contains 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