-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
96 lines (71 loc) · 2.65 KB
/
example.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
#include <Arduino.h>
#include "main.h"
// ########################################################################################
//
// Delcare and initialise the various classes
//
// Initialize variables
volatile PULSE rawBitBuffer[RAWBITBUFFERSIZE];
volatile unsigned int rawBitBufferPosition = 0;
volatile byte baseProtocolState = BASEPROTOCOL433_RX_LISTENMODE;
void IRAM_ATTR qam_rx4_isr() {
static unsigned int prevTime = 0; // lastTime
unsigned int newTime = micros(); // now
//unsigned int newTime = ESP.getCycleCount()/240;
// Calculate the duration of the current signal state
unsigned int duration = newTime - prevTime;
bool state = digitalRead(35);
// If we find the preample (4.5ms < x < 12.5ms) trigger baseProtocolState to receiving mode
if ((duration > BASEPROTOCOL433_RX_PREAMPLE_THRESOLD_MIN) && (duration < BASEPROTOCOL433_RX_PREAMPLE_THRESOLD_MAX) ) {
if (baseProtocolState == BASEPROTOCOL433_RX_RECEIVEMODE) {
baseProtocolState = BASEPROTOCOL433_RX_MESSAGERECEIVED;
detachInterrupt(35);
} else {
baseProtocolState = BASEPROTOCOL433_RX_RECEIVEMODE;
}
}
if (baseProtocolState == BASEPROTOCOL433_RX_RECEIVEMODE) {
// Store the duration of the received pulse
rawBitBuffer[rawBitBufferPosition].state = !state;
rawBitBuffer[rawBitBufferPosition].duration = duration;
rawBitBufferPosition++;
}
if (rawBitBufferPosition == (RAWBITBUFFERSIZE - 1)) {
baseProtocolState =BASEPROTOCOL433_RX_LISTENMODE;
rawBitBufferPosition = 0;
}
prevTime = newTime;
}
#endif
void setup() {
Serial.begin(115200);
rawBitBufferPosition = 0;
baseProtocolState = BASEPROTOCOL433_SETUP;
// Configure QAM-RX4 data pin as an input pin
pinMode(35, INPUT);
Serial.println("Setup() done");
}
void loop() {
if (baseProtocolState == BASEPROTOCOL433_SETUP) {
attachInterrupt(digitalPinToInterrupt(35), qam_rx4_isr, CHANGE);
baseProtocolState = BASEPROTOCOL433_RX_LISTENMODE;
}
if (baseProtocolState == BASEPROTOCOL433_RX_MESSAGERECEIVED) {
sleep(1);
Serial.print("rBBP = ");
Serial.println(rawBitBufferPosition);
Serial.print("bPS = ");
Serial.println(baseProtocolState);
for (x = 0; x <= rawBitBufferPosition; x++) {
Serial.print("x = ");
Serial.print(x);
Serial.print(", S = ");
Serial.print(rawBitBuffer[x].state);
Serial.print(", D = ");
Serial.println(rawBitBuffer[x].duration);
}
baseProtocolState = BASEPROTOCOL433_RX_LISTENMODE;
attachInterrupt(digitalPinToInterrupt(35), qam_rx4_isr, CHANGE);
}
vTaskDelay(1);
}