Ecco la seconda classe non scritta da me, ma semplicemente riadattata per i miei scopi (in classe).
È praticamente identica a quella originale che potete trovare su http://www.elenafrancesco.org/arduino/baronpilot/
altro progetto italiano di "Arduino based copilot autostabilizer with Nintendo Wii component"
Molto interessante notare l'uso degli interrupt. Arduino fornisce funzioni precotte per ascoltare gli interrupt per 2 pin, ma in realtà tutti i pin sono "ascoltabili".
Fare attenzione che si lavora a basso livello con i pin e si usa la nomenclatura vera dell'Atmega; la trovate quì: http://arduino.cc/en/Hacking/PinMapping168
InputPin.h
#ifndef InputPin_h
#define InputPin_h
#include "WProgram.h"
class InputPin{
public:
InputPin();
bool getAndSetFalseHasChanged(); // send the initilization handshake
int getDuration(int);
private:
};
#endif
InputPin.cpp
#include "InputPin.h"
/*
Let You read digital pin 2,4,5,6
FROM BIRONPILOT V59, tnx to ciskje
*/
//#define MAXUNSIGNEDLONGCVALUE 4294967295
#define MASKPCINT0 (1<<2)
#define MASKPCINT1 (1<<4)
#define MASKPCINT2 (1<<5)
#define MASKPCINT3 (1<<6)
InputPin::InputPin(){
pinMode(2, INPUT); // 3 is used for esc
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
// interrupt on pin change FROM PCINT16 to PCINT23
PCICR |= (1 << PCIE2);
PCMSK2 = (1 << PCINT18) | // digital pin2
(1 << PCINT20) | // digital pin4
(1 << PCINT21) | // digital pin5
(1 << PCINT22); // digital pin6
}
//putted here because interrupt dosn't want it in .h
unsigned long _startIn[4];
unsigned long _rawIn[4];
unsigned long _time;
byte _oldbit, _newbit, _changed;
boolean _hasChanged=false;
ISR(PCINT2_vect) {
_time=micros();
_newbit=PIND;
_changed=_newbit^_oldbit;
if (_changed&MASKPCINT0){//if digital PIN2 has changed
if (_newbit&MASKPCINT0) { //if PIN2 now is high
_startIn[0]=_time;
}else{
_rawIn[0]=_time-_startIn[0];
}
}
if (_changed&MASKPCINT1)//if digital PIN4 has changed
if (_newbit&MASKPCINT1)
_startIn[1]=_time;
else
_rawIn[1]=_time-_startIn[1];
if (_changed&MASKPCINT2)//if digital PIN5 has changed
if (_newbit&MASKPCINT2)
_startIn[2]=_time;
else
_rawIn[2]=_time-_startIn[2];
if (_changed&MASKPCINT3)//if digital PIN6 has changed
if (_newbit&MASKPCINT3)
_startIn[3]=_time;
else
_rawIn[3]=_time-_startIn[3];
_hasChanged=true;
_oldbit=_newbit;
}
bool InputPin::getAndSetFalseHasChanged(){
bool temp = _hasChanged;
_hasChanged = false;
return temp;
}
int InputPin::getDuration(int i){
return _rawIn[i];
}