bombatuino/source/libraries/bombatuino_INPUT_74HC4051/bombatuino_INPUT_74HC4051.cpp

77 lines
1.7 KiB
C++
Raw Normal View History

2013-02-14 02:59:15 +01:00
#include "Arduino.h"
2013-03-12 14:53:27 +01:00
#include "bombatuino_INPUT_74HC4051.h"
2013-02-14 02:59:15 +01:00
2013-02-14 03:12:31 +01:00
//should be called in setup()
2013-02-14 02:59:15 +01:00
void INPUT_74HC4051::begin(uint8_t analog, uint8_t s0, uint8_t s1, uint8_t s2,CallbackFunction cbF) {
2013-02-14 03:12:31 +01:00
//analog pin which output Z is connected to
2013-02-14 02:59:15 +01:00
_analog = analog;
2013-02-14 03:12:31 +01:00
//3 digital pins where S0,S1,S2 are connected to
2013-02-14 02:59:15 +01:00
_s0 = s0;
_s1 = s1;
_s2 = s2;
2013-02-14 03:12:31 +01:00
2013-02-14 02:59:15 +01:00
pinMode(_analog,INPUT);
pinMode(_s0,OUTPUT);
pinMode(_s1,OUTPUT);
pinMode(_s2,OUTPUT);
_callbackFunction = cbF;
2013-02-14 03:12:31 +01:00
//initalize start values
2013-02-14 02:59:15 +01:00
uint8_t pin,r0,r1,r2;
2013-02-14 03:12:31 +01:00
//run through the 8 I/O pins
2013-02-14 02:59:15 +01:00
for (pin=0; pin<8; pin++) {
r0 = bitRead(pin,0);
r1 = bitRead(pin,1);
r2 = bitRead(pin,2);
digitalWrite(_s0, r0);
digitalWrite(_s1, r1);
digitalWrite(_s2, r2);
//delayMicroseconds(10);
_value[pin] = analogRead(_analog);
}
}
2013-02-14 03:12:31 +01:00
//should be called in loop(), read values and call callback function on change
2013-02-14 02:59:15 +01:00
void INPUT_74HC4051::loop() {
uint8_t pin,r0,r1,r2;
int value;
for (pin=0; pin<8; pin++) {
r0 = bitRead(pin,0);
r1 = bitRead(pin,1);
r2 = bitRead(pin,2);
digitalWrite(_s0, r0);
digitalWrite(_s1, r1);
digitalWrite(_s2, r2);
2013-02-14 03:12:31 +01:00
//not sure if nessesary
2013-02-14 02:59:15 +01:00
//delayMicroseconds(10);
2013-05-14 08:28:05 +02:00
value = analogRead(_analog) / 8;
2013-02-14 02:59:15 +01:00
if (_value[pin] < value - INPUT_74HC4051_TOLERANCE || _value[pin] > value + INPUT_74HC4051_TOLERANCE) {
_value[pin] = value;
(*_callbackFunction)(_analog,pin,value);
}
}
}
2013-02-14 03:12:31 +01:00
//maybe useful later
2013-02-14 02:59:15 +01:00
int INPUT_74HC4051::getSpecificValue(uint8_t pin) {
if (pin >= 8)
return -1;
uint8_t r0,r1,r2;
r0 = bitRead(pin,0);
r1 = bitRead(pin,1);
r2 = bitRead(pin,2);
digitalWrite(_s0, r0);
digitalWrite(_s1, r1);
digitalWrite(_s2, r2);
//delayMicroseconds(10);
2013-05-14 08:28:05 +02:00
int value = analogRead(_analog) / 8;
2013-02-14 02:59:15 +01:00
_value[pin] = value;
return value;
}