/
RawSignal.cpp
96 lines (82 loc) · 4.74 KB
/
RawSignal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <wiringPi.h>
#include "config.h"
uint8_t portInputs[10];
#define portInputRegister(P) &portInputs[P]
struct RawSignalStruct RawSignal={0,0,0,0,0,0L};
const unsigned long LoopsPerMilli=345;
const unsigned long Overhead=0;
// Because this is a time critical routine, we use global variables so that the variables
// do not need to be initialized at each function call.
int RawCodeLength=0;
unsigned long PulseLength=0L;
unsigned long numloops=0L;
unsigned long maxloops=0L;
boolean Ftoggle=false;
uint8_t Fbit=0;
uint8_t Fport=0;
uint8_t FstateMask=0;
/*********************************************************************************************/
boolean FetchSignal(byte DataPin, boolean StateSignal) {
//FIXME uint8_t Fbit = digitalPinToBitMask(DataPin);
//FIXME uint8_t Fport = digitalPinToPort(DataPin);
uint8_t FstateMask = (StateSignal ? Fbit : 0);
if ((*portInputRegister(Fport) & Fbit) == FstateMask) { // Als er signaal is
// Als het een herhalend signaal is, dan is de kans groot dat we binnen hele korte tijd weer in deze
// routine terugkomen en dan midden in de volgende herhaling terecht komen. Daarom wordt er in dit
// geval gewacht totdat de pulsen voorbij zijn en we met het capturen van data beginnen na een korte
// rust tussen de signalen.Op deze wijze wordt het aantal zinloze captures teruggebracht.
if (RawSignal.Time) { // Eerst een snelle check, want dit bevindt zich in een tijdkritisch deel...
if (RawSignal.Repeats && (RawSignal.Time+SIGNAL_REPEAT_TIME)>millis()) { // ...want deze check duurt enkele micro's langer!
PulseLength=micros()+SIGNAL_TIMEOUT*1000; // delay
while ((RawSignal.Time+SIGNAL_REPEAT_TIME)>millis() && PulseLength>micros())
if ((*portInputRegister(Fport) & Fbit) == FstateMask)
PulseLength=micros()+SIGNAL_TIMEOUT*1000;
while((RawSignal.Time+SIGNAL_REPEAT_TIME)>millis() && (*portInputRegister(Fport) & Fbit) != FstateMask);
}
}
RawCodeLength=1; // Start at 1 for legacy reasons. Element 0 can be used to pass special information like plugin number etc.
Ftoggle=false;
maxloops = (SIGNAL_TIMEOUT * LoopsPerMilli);
do{ // read the pulses in microseconds and place them in temporary buffer RawSignal
numloops = 0;
while (((*portInputRegister(Fport) & Fbit) == FstateMask) ^ Ftoggle) // while() loop *A*
if (numloops++ == maxloops) break; // timeout
PulseLength=((numloops + Overhead)* 1000) / LoopsPerMilli; // Contains pulslength in microseconds
if (PulseLength<MIN_PULSE_LENGTH) break; // Pulse length too short
Ftoggle=!Ftoggle;
RawSignal.Pulses[RawCodeLength++]=PulseLength/(unsigned long)(RAWSIGNAL_SAMPLE_RATE); // store in RawSignal !!!!
} while (RawCodeLength<RAW_BUFFER_SIZE && numloops<=maxloops); // For as long as there is space in the buffer, no timeout etc.
if (RawCodeLength>=MIN_RAW_PULSES) {
RawSignal.Repeats=0; // no repeats
RawSignal.Multiply=RAWSIGNAL_SAMPLE_RATE; // sample size.
RawSignal.Number=RawCodeLength-1; // Number of received pulse times (pulsen *2)
RawSignal.Pulses[RawSignal.Number+1]=0; // Last element contains the timeout.
RawSignal.Time=millis(); // Time the RF packet was received (to keep track of retransmits
return true;
} else {
RawSignal.Number=0;
}
}
return false;
}
/*********************************************************************************************/
boolean ScanEvent(void) { // Deze routine maakt deel uit van de hoofdloop en wordt iedere 125uSec. doorlopen
unsigned long Timer=millis()+SCAN_HIGH_TIME;
while(Timer>millis() || RepeatingTimer>millis()) {
if (FetchSignal(PIN_RF_RX_DATA,HIGH)) { // RF: *** data start ***
if ( PluginRXCall(0,0) ) { // Check all plugins to see which plugin can handle the received signal.
RepeatingTimer=millis()+SIGNAL_REPEAT_TIME;
return true;
}
}
}// while
return false;
}
void RawSendRF(void)
{
//FIXME
}