-
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.
- Loading branch information
Christopher Ávila Holguín
committed
Apr 4, 2016
1 parent
6ee3b78
commit 3cb4cca
Showing
3 changed files
with
43 additions
and
52 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 |
---|---|---|
@@ -1,70 +1,46 @@ | ||
#pragma once | ||
|
||
uint16_t m; | ||
volatile uint16_t s; | ||
|
||
void change( void ) | ||
class RCChannel | ||
{ | ||
if( digitalRead( 7 ) ) | ||
s = 1;//micros() - m; | ||
else | ||
m = micros(); | ||
} | ||
public: | ||
virtual void print() = 0; | ||
}; | ||
|
||
|
||
struct RCChannel | ||
template<int pin> | ||
class RCChannelPin : public RCChannel | ||
{ | ||
uint8_t pin; | ||
uint16_t value; | ||
public: | ||
|
||
RCChannel( uint8_t pin = 0 ) | ||
{ | ||
this->value = 0; | ||
this->pin = pin; | ||
volatile static uint32_t last_value; | ||
volatile static uint32_t last_high; | ||
|
||
if( pin != 0 ) | ||
{ | ||
pinMode( pin, INPUT ); | ||
attachInterrupt( digitalPinToInterrupt( pin ), change, CHANGE ); | ||
} | ||
} | ||
|
||
void read() | ||
void static rising( void ) | ||
{ | ||
if( pin != 0 && s != 0 ) | ||
{ | ||
value = s; | ||
s = 0; | ||
} | ||
last_high = micros(); | ||
} | ||
}; | ||
|
||
template<uint8_t num_channels> | ||
class RC | ||
{ | ||
RCChannel rc_channels[ num_channels ]; | ||
|
||
public: | ||
|
||
RC( uint8_t pins[ num_channels ] ) | ||
void static falling( void ) | ||
{ | ||
for( uint8_t i = 0 ; i < num_channels ; i ++ ) | ||
rc_channels[ i ] = RCChannel( pins[ i ] ); | ||
last_value = micros() - last_high; | ||
} | ||
|
||
void loop() | ||
RCChannelPin() | ||
{ | ||
for( uint8_t i = 0 ; i < num_channels ; i ++ ) | ||
rc_channels[ i ].read(); | ||
pinMode( pin, INPUT ); | ||
attachInterrupt( digitalPinToInterrupt( pin ), rising, RISING ); | ||
attachInterrupt( digitalPinToInterrupt( pin ), falling, FALLING ); | ||
} | ||
|
||
void print() | ||
{ | ||
for( uint8_t i = 0 ; i < num_channels ; i ++ ) | ||
{ | ||
Serial.print( rc_channels[ i ].value ); | ||
Serial.print( "\t" ); | ||
} | ||
Serial.print( last_value ); | ||
Serial.print( "\t" ); | ||
} | ||
}; | ||
|
||
template<int pin> | ||
volatile uint32_t RCChannelPin<pin>::last_value = 0; | ||
|
||
template<int pin> | ||
volatile uint32_t RCChannelPin<pin>::last_high = 0; |
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
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