Skip to content
This repository has been archived by the owner on Dec 1, 2017. It is now read-only.

Commit

Permalink
added the adc0832
Browse files Browse the repository at this point in the history
das Projekt nun nach Geräten organisiert und der adc0832 mit Eventlistener hinzugefügt.
  • Loading branch information
milleniumfrog committed Aug 11, 2017
1 parent e39f19e commit efb3b09
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 19 deletions.
96 changes: 96 additions & 0 deletions adc/0832/adc0832.js
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;
31 changes: 31 additions & 0 deletions adc/0832/eventEmitter.js
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;
16 changes: 0 additions & 16 deletions led_blink.js

This file was deleted.

13 changes: 10 additions & 3 deletions modules/nodepi-frog.js → raspberryPi/nodepi-frog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const fs = require('fs');
const {execSync} = require('child_process');
const events = require('events');

console.log = function(string){};

let emitter = new events.EventEmitter();


Expand Down Expand Up @@ -62,7 +65,7 @@ let direction = function(gpio, direction = 'out'){
}
else{
console.log(`the pin ${gpio.pin} had the direction ${gpio.direction}`);
console.log(`set the direction for the pin ${gpio.pin}`);
console.log(`set the direction for the pin ${gpio.pin} to ${direction}`);
let direct = `echo "${direction}" > /sys/class/gpio/gpio${gpio.pin}/direction`;
execSync(direct);

Expand All @@ -79,11 +82,11 @@ let direction = function(gpio, direction = 'out'){
let write = function(gpio, value = true){

if(!fs.existsSync(`/sys/class/gpio/gpio${gpio.pin}/direction`)){
throw new Error('you cannot write to a pin that has not the direction "out"');
throw new Error(`you cannot write to the pin ${gpio.pin} which has not the direction "out" `);
}

if(gpio.direction !== 'out'){
throw new Error('you cannot write to a pin that has not the direction "out"');
throw new Error(`you cannot write to the pin ${gpio.pin} which has not the direction "out" `);
}

if(typeof(value) === "boolean"){
Expand Down Expand Up @@ -116,6 +119,7 @@ let write = function(gpio, value = true){
* read the Input of a gpio pin
* @param {object} gpio
* @param {boolean} invert
* @return {boolean}
*/
let read = function(gpio, invert = false){

Expand All @@ -128,10 +132,13 @@ let read = function(gpio, invert = false){
}

if(invert){
console.log('read');
gpio.value = Number(fs.readFileSync(`/sys/class/gpio/gpio${gpio.pin}/value`)) === 1 ? LOW : HIGH;
return gpio.value;
}
else {
gpio.value = Number(fs.readFileSync(`/sys/class/gpio/gpio${gpio.pin}/value`)) === 1 ? HIGH : LOW;
return gpio.value;
}
};

Expand Down

0 comments on commit efb3b09

Please sign in to comment.