-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransmit.c
More file actions
162 lines (128 loc) · 4 KB
/
Copy pathtransmit.c
File metadata and controls
162 lines (128 loc) · 4 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
#include "transmit.h"
#include <furi.h>
#include <furi_hal.h>
#include <lib/subghz/devices/cc1101_configs.h>
#include <stdlib.h>
#define TX_GAP_US 10000 // 10ms gap between repeats
typedef enum {
TxCmdNone,
TxCmdStart,
TxCmdStop,
TxCmdExit,
} TxCommand;
struct OpenshockTx {
FuriThread* thread;
FuriMessageQueue* cmd_queue;
// Pulse data (written by main thread before sending TxCmdStart)
OokPulse pulses[OPENSHOCK_MAX_PULSES];
size_t count;
// Async TX state (only accessed by TX thread + callback)
size_t pulse_index;
bool phase_high;
volatile bool stop_requested;
volatile bool active;
};
static LevelDuration tx_callback(void* context) {
OpenshockTx* tx = context;
if(tx->stop_requested) {
return level_duration_reset();
}
if(tx->pulse_index >= tx->count) {
tx->pulse_index = 0;
tx->phase_high = true;
return level_duration_make(false, TX_GAP_US);
}
const OokPulse* p = &tx->pulses[tx->pulse_index];
if(tx->phase_high) {
tx->phase_high = false;
return level_duration_make(true, p->high_us);
} else {
tx->phase_high = true;
tx->pulse_index++;
return level_duration_make(false, p->low_us);
}
}
static void tx_do_start(OpenshockTx* tx) {
tx->pulse_index = 0;
tx->phase_high = true;
tx->stop_requested = false;
// Use direct HAL for CC1101 control
furi_hal_subghz_reset();
furi_hal_subghz_idle();
furi_hal_subghz_load_custom_preset(subghz_device_cc1101_preset_ook_650khz_async_regs);
furi_hal_subghz_set_frequency_and_path(OPENSHOCK_FREQUENCY);
furi_hal_power_suppress_charge_enter();
if(!furi_hal_subghz_start_async_tx(tx_callback, tx)) {
furi_hal_subghz_sleep();
furi_hal_power_suppress_charge_exit();
tx->active = false;
return;
}
tx->active = true;
// Wait for stop or exit command
TxCommand cmd;
while(true) {
if(furi_message_queue_get(tx->cmd_queue, &cmd, 5) == FuriStatusOk) {
if(cmd == TxCmdStop || cmd == TxCmdExit) break;
}
}
// Teardown
tx->stop_requested = true;
while(!furi_hal_subghz_is_async_tx_complete()) {
furi_delay_ms(1);
}
furi_hal_subghz_stop_async_tx();
furi_hal_subghz_sleep();
furi_hal_power_suppress_charge_exit();
tx->active = false;
if(cmd == TxCmdExit) {
furi_message_queue_put(tx->cmd_queue, &cmd, 0);
}
}
static int32_t tx_thread(void* context) {
OpenshockTx* tx = context;
TxCommand cmd;
while(true) {
if(furi_message_queue_get(tx->cmd_queue, &cmd, FuriWaitForever) != FuriStatusOk) {
continue;
}
if(cmd == TxCmdExit) break;
if(cmd == TxCmdStart) {
tx_do_start(tx);
}
}
return 0;
}
OpenshockTx* openshock_tx_alloc(void) {
OpenshockTx* tx = malloc(sizeof(OpenshockTx));
if(!tx) return NULL;
memset(tx, 0, sizeof(OpenshockTx));
tx->cmd_queue = furi_message_queue_alloc(4, sizeof(TxCommand));
tx->thread = furi_thread_alloc_ex("OpenshockTx", 2048, tx_thread, tx);
furi_thread_start(tx->thread);
return tx;
}
void openshock_tx_free(OpenshockTx* tx) {
if(!tx) return;
TxCommand cmd = TxCmdExit;
furi_message_queue_put(tx->cmd_queue, &cmd, FuriWaitForever);
furi_thread_join(tx->thread);
furi_thread_free(tx->thread);
furi_message_queue_free(tx->cmd_queue);
free(tx);
}
bool openshock_tx_start(OpenshockTx* tx, const OokPulse* pulses, size_t count) {
if(!tx || !pulses || count == 0 || count > OPENSHOCK_MAX_PULSES) return false;
memcpy(tx->pulses, pulses, count * sizeof(OokPulse));
tx->count = count;
TxCommand cmd = TxCmdStart;
return furi_message_queue_put(tx->cmd_queue, &cmd, 100) == FuriStatusOk;
}
void openshock_tx_stop(OpenshockTx* tx) {
if(!tx) return;
TxCommand cmd = TxCmdStop;
furi_message_queue_put(tx->cmd_queue, &cmd, 100);
}
bool openshock_tx_is_active(OpenshockTx* tx) {
return tx && tx->active;
}