-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrc_tx.c
166 lines (139 loc) · 3.51 KB
/
rc_tx.c
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
163
164
165
166
#define MODULE_NAME RC_TX
#include <string.h>
#include <stdio.h>
#include "bsp.h"
#include "mcal.h"
#include "trace.h"
#include "blf.h"
#define TIME_TABLE_SIZE 256
static pwm_t *rc_pwm = NULL;
static volatile uint8_t tx_busy = 0;
static uint16_t tx_time_table[TIME_TABLE_SIZE];
static volatile uint8_t tx_time_table_length;
static volatile uint8_t tx_time_table_idx;
static uint16_t tx_pin_state;
static uint16_t tx_repeats;
static gpio_t tx_gpio;
static BLFQueue * tx_queue = NULL;
static void pwm_cb(void *ctx)
{
tx_time_table_idx++;
if(tx_time_table_idx < tx_time_table_length) {
tx_pin_state = !tx_pin_state;
GPIO_write(tx_gpio, tx_pin_state);
PWM_setDelay(rc_pwm, tx_time_table[tx_time_table_idx]);
} else {
if(tx_repeats) { // Repeat transmission
tx_repeats--;
tx_time_table_idx = 0;
tx_pin_state = 1;
GPIO_write(tx_gpio, 1);
PWM_setDelay(rc_pwm, tx_time_table[tx_time_table_idx]);
} else { // Done
TTRACE(TTRACE_INFO, "RC_TX: Tx finished: pulses %d\n", tx_time_table_idx);
GPIO_write(tx_gpio, 0);
tx_busy = 0;
}
}
}
static void send_common(gpio_t pin, uint8_t repeat, uint16_t length)
{
tx_busy = 1;
tx_time_table_idx = 0;
tx_time_table_length = length;
tx_repeats = repeat;
tx_gpio = pin;
tx_pin_state = 1;
// Set and configure pin as output
GPIO_write(tx_gpio, 1);
// Enable timer
PWM_setDelay(rc_pwm, tx_time_table[0]);
}
static int decode_and_send(const char *str, size_t length)
{
const char *p = str;
int got_defines = 0;
int value = 0;
int lidx = 0, pidx = 0;
int repeats = 0;
int ok = 1;
uint16_t lengths[8];
uint16_t *pulses = tx_time_table;
while(p < (str + length) && ok) {
char c = *p++;
if(got_defines == 0) {
if(c == ':') {
repeats = value;
if(repeats > 20) {
ok = 0;
}
got_defines = 1;
} else if(c == ',') {
if(value > 100000 || lidx >= 8) {
ok = 0;
} else {
lengths[lidx++] = value;
value = 0;
}
} else if(c >= '0' && c <= '9') {
value = value * 10 + (c - '0');
}
} else {
if(c >= 'A' && c <= 'Z') {
int pt = c - 'A';
if(pt > lidx) {
ok = 0;
break;
}
if(pidx < TIME_TABLE_SIZE) {
pulses[pidx++] = lengths[pt];
} else {
ok = 0;
}
}
}
}
if(ok && pidx > 0) {
send_common(BSP_433MHZ_TX, repeats, pidx);
}
return ok ? 0 : -1;
}
//-------------------------------------------------------------------------------------------------------
// Send raw pulse string
/* Example string: 430,1290,13330,3: ABBAABABABBAABBAABBAABABABBAABBAABBAABBAABBAABBAAC
Three pulse lengths defined, A = 430, B = 1290 and C = 13300
3 Repeats
Data, A, B, C represents the pulselengths defined first in string
*/
int rc_tx_raw(const char *str)
{
if(BLF_queueLength(tx_queue) > 8) {
return -1;
}
char *qi = (char *)BLF_alloc(strlen(str) + 1);
strcpy(qi, str);
BLF_appendQueue(tx_queue, qi);
return 0;
}
void rc_tx_init(void)
{
TTRACE(TTRACE_INFO, "RC_TX init\n");
tx_queue = BLF_createQueue();
tim_t *pwmTim = TIM_create(0);
TIM_configure(pwmTim, 1000000, TIM_PERIOD_MAX);
rc_pwm = PWM_create(pwmTim, 0);
PWM_configure(rc_pwm, TIM_MODE_DELAY, pwm_cb, 0);
TIM_start(pwmTim);
}
void rc_tx_poll(void)
{
if(tx_busy) {
return;
}
char *qi = BLF_takeFirstInQueue(tx_queue);
if(qi == NULL) {
return;
}
decode_and_send(qi, strlen(qi));
BLF_free(qi);
}