aboutsummaryrefslogtreecommitdiff
path: root/firmware/cc1101.cpp
blob: dcbbbeef440512261716a8563fdd4c7f4f17bf57 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <Arduino.h>
#include "ELECHOUSE_CC1101_SRC_DRV.h"
#include "cc1101.h"
#include "config.h"

// ===== CONFIG =====
#define RAW_BUF_MAX 512

// ===== STATE =====
static bool cc1101Inited = false;

// ===== CAPTURE STATE =====
volatile unsigned long captureBuffer[RAW_BUF_MAX];
volatile int pulseIndex = 0;
volatile unsigned long lastEdgeTime = 0;
volatile bool capturing = false;

// ===== RF SETTINGS =====
float currentFreq = 433.92;
float dataRate = 3.79372;
float deviation = 0.0;
float rxBW = 325.0;
int powerLevel = 10;

// ===== ISR =====
void IRAM_ATTR pulseISR()
{
    unsigned long now = micros();

    if (!capturing) return;
    if (pulseIndex >= RAW_BUF_MAX) return;

    unsigned long duration = now - lastEdgeTime;

    if (duration < 50) return;

    captureBuffer[pulseIndex++] = duration;
    lastEdgeTime = now;
}

// ===== OOK SETUP =====
void setupOOKMode()
{
    ELECHOUSE_cc1101.SetRx();
    ELECHOUSE_cc1101.setMHZ(currentFreq);

    ELECHOUSE_cc1101.setModulation(0); // ASK/OOK
    ELECHOUSE_cc1101.setDRate(dataRate);
    ELECHOUSE_cc1101.setDeviation(0);
    ELECHOUSE_cc1101.setRxBW(rxBW);
    ELECHOUSE_cc1101.setSyncMode(0);
    ELECHOUSE_cc1101.setPA(powerLevel);
}

// ===== INIT (LAZY, SAFE) =====
bool initCC1101()
{
    if (cc1101Inited)
        return true;

    Serial.println("Initializing CC1101...");

    // IMPORTANT: no detachInterrupt here (causes crash if not installed)

    ELECHOUSE_cc1101.setSpiPin(
        cc1101_SCK,
        cc1101_MISO,
        cc1101_MOSI,
        CC1101_CS
    );

    ELECHOUSE_cc1101.setGDO(CC1101_GDO0, CC1101_GDO2);

    if (!ELECHOUSE_cc1101.getCC1101())
    {
        Serial.println("❌ CC1101 NOT FOUND");
        return false;
    }

    delay(10); // let SPI settle

    ELECHOUSE_cc1101.Init();   // THIS WAS YOUR FREEZE POINT
    setupOOKMode();

    pinMode(CC1101_GDO0, INPUT);

    Serial.println("✅ CC1101 READY");

    cc1101Inited = true;
    return true;
}

// ===== CAPTURE CONTROL =====
void startCapture()
{
    pulseIndex = 0;
    capturing = true;
    lastEdgeTime = micros();

    attachInterrupt(
        digitalPinToInterrupt(CC1101_GDO0),
        pulseISR,
        CHANGE
    );

    Serial.println("Capture started");
}

bool isCC1101Ready() {
    return cc1101Inited;
}

void stopCapture()
{
    capturing = false;

    detachInterrupt(digitalPinToInterrupt(CC1101_GDO0));

    Serial.println("Capture stopped");
}

// ===== DEBUG PRINT =====
void printCapture()
{
    Serial.println("Captured pulses:");

    for (int i = 0; i < pulseIndex; i++)
    {
        Serial.println(captureBuffer[i]);
    }
}


// ===== REPLAY =====
void replaySignal()
{
    Serial.println("Replaying...");

    ELECHOUSE_cc1101.SetTx();
    pinMode(CC1101_GDO0, OUTPUT);

    for (int i = 0; i < pulseIndex; i++)
    {
        digitalWrite(CC1101_GDO0, (i % 2 == 0) ? HIGH : LOW);
        delayMicroseconds(captureBuffer[i]);
    }

    digitalWrite(CC1101_GDO0, LOW);
    ELECHOUSE_cc1101.SetRx();
}