Skip to content

Commit 9a46a9b

Browse files
committed
new data structure for channel data
1 parent bb86977 commit 9a46a9b

File tree

3 files changed

+127
-6
lines changed

3 files changed

+127
-6
lines changed

components/crsf/crsf.c

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ uint8_t crcMessage(uint8_t message[], uint8_t length)
2020
return crc;
2121
}
2222

23+
//scale from range [173,1811] to range [0,2047]
24+
uint16_t scale_Range_Analog(uint16_t value){
25+
return (uint16_t)((value-173)*1.25);
26+
}
27+
28+
//scale from range [173,1811] to range [0,1]
29+
uint8_t scale_Range_Digital(uint16_t value){
30+
if(value <= 992){
31+
return 0;
32+
} else {
33+
return 1;
34+
}
35+
}
36+
2337
void initCRSF_read(){
2438
/* Configure parameters of an UART driver,
2539
* communication pins and install the driver */
@@ -56,6 +70,9 @@ void crsf_get_ChannelData_task(void *arg)
5670
uint8_t *data = (uint8_t *) malloc(1024);
5771

5872
while (1) {
73+
//bool if channel data changed from channeldata t-1
74+
bool changed = false;
75+
5976
// get size in uart buffer
6077
int length = 0;
6178
ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_2, (size_t*)&length));
@@ -118,12 +135,94 @@ void crsf_get_ChannelData_task(void *arg)
118135
used = 11-availableBits;
119136
}
120137

121-
//Kanaldaten abspeichern
122-
channelData[j] = value;
138+
//Kanaldaten abspeichern --> ersten 8 kanäle sind analog rest digital
139+
uint16_t newVal;
140+
uint8_t origDataButtons;
141+
switch(j){
142+
case 0:
143+
newVal = scale_Range_Analog(value);
144+
if(channelData.throttle != newVal){
145+
changed = true;
146+
channelData.throttle = newVal;
147+
}
148+
break;
149+
case 1:
150+
newVal = scale_Range_Analog(value);
151+
if(channelData.yaw != newVal){
152+
changed = true;
153+
channelData.yaw = newVal;
154+
}
155+
break;
156+
case 2:
157+
newVal = scale_Range_Analog(value);
158+
if(channelData.pitch != newVal){
159+
changed = true;
160+
channelData.pitch = newVal;
161+
}
162+
break;
163+
case 3:
164+
newVal = scale_Range_Analog(value);
165+
if(channelData.roll != newVal){
166+
changed = true;
167+
channelData.roll = newVal;
168+
}
169+
break;
170+
case 4:
171+
newVal = scale_Range_Analog(value);
172+
if(channelData.aux1 != newVal){
173+
changed = true;
174+
channelData.aux1 = newVal;
175+
}
176+
break;
177+
case 5:
178+
newVal = scale_Range_Analog(value);
179+
if(channelData.aux2 != newVal){
180+
changed = true;
181+
channelData.aux2 = newVal;
182+
}
183+
break;
184+
case 6:
185+
newVal = scale_Range_Analog(value);
186+
if(channelData.aux3 != newVal){
187+
changed = true;
188+
channelData.aux3 = newVal;
189+
}
190+
break;
191+
case 7:
192+
newVal = scale_Range_Analog(value);
193+
if(channelData.aux4 != newVal){
194+
changed = true;
195+
channelData.aux4 = newVal;
196+
}
197+
break;
198+
case 8:
199+
case 9:
200+
case 10:
201+
case 11:
202+
case 12:
203+
case 13:
204+
case 14:
205+
case 15:
206+
origDataButtons = channelData.buttons;
207+
if(scale_Range_Digital(value) == 1){
208+
channelData.buttons = channelData.buttons | (0x01 << (j-8));
209+
} else {
210+
channelData.buttons = channelData.buttons & (~(0x01 << (j-8)));
211+
}
212+
213+
if(origDataButtons != channelData.buttons){
214+
changed = true;
215+
}
216+
217+
break;
218+
}
123219
}
124220
//Kanaldaten ausgeben
125221
//wenn esp_logi ausgegeben wird dann kann es sein das watchdog timer für den task nicht zurückgesetzt wird ist aber nicht so schlimm solang der output einfach weggelassen wird in stpäteren code
126-
//ESP_LOGI("Channel-Data","%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d", channelData[0], channelData[1], channelData[2], channelData[3], channelData[4], channelData[5], channelData[6], channelData[7], channelData[8], channelData[9], channelData[10], channelData[11], channelData[12], channelData[13], channelData[14], channelData[15]);
222+
if(changed){
223+
ESP_LOGI("","NOTIFY");
224+
}
225+
ESP_LOGI("Channel-Data","%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d", channelData.throttle, channelData.yaw, channelData.pitch, channelData.roll, channelData.aux1, channelData.aux2, channelData.aux3, channelData.aux4, (channelData.buttons & (0x01<<0)), (channelData.buttons & (0x01<<1)), (channelData.buttons & (0x01<<2)), (channelData.buttons & (0x01<<3)), (channelData.buttons & (0x01<<4)), (channelData.buttons & (0x01<<5)), (channelData.buttons & (0x01<<6)), (channelData.buttons & (0x01<<7)));
127226
break;
128227
}
129228
}

components/crsf/include/crsf.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@
55
#include "driver/uart.h"
66
#include "freertos/task.h"
77
#include "esp_log.h"
8+
#include <stdbool.h>
89

9-
//store channel data in array
10-
static uint16_t channelData[16] = {0};
10+
//struct for channel data
11+
typedef struct ChannelDataStruct{
12+
uint16_t roll; //roll = x
13+
uint16_t pitch; //pitch = y
14+
uint16_t aux3; //aux3 = z
15+
uint16_t yaw; //yaw = rx
16+
uint16_t aux1; //aux1 = rz
17+
uint16_t throttle; //throttle = ry
18+
uint16_t aux4; //aux4 = slide
19+
uint16_t aux2; //aux2 = slide
20+
uint8_t buttons; //buttons = aux12(b8) .. aux5(b1)
21+
} ChannelDataStruct;
22+
23+
//store for channeldata
24+
static ChannelDataStruct channelData = {0};
1125

1226
uint8_t crcSingleChar(uint8_t crc, uint8_t a);
1327
uint8_t crcMessage(uint8_t message[], uint8_t length);
1428
void initCRSF_read();
1529
void crsf_get_ChannelData_task(void *arg);
30+
uint16_t scale_Range_Analog(uint16_t value);
31+
uint8_t scale_Range_Digital(uint16_t value);
1632

1733
#endif

docs/todo

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
- Report deskriptor austauschen mit neuen --> report id noch hinzufügen
2+
- struct verschicken mittels bluetooth --> nur wenn sich was verändert hat an den Daten
3+
- hr functionen löschen
4+
- testen mit velocidrone
5+
- wenn verbindung getrennt wird oder notify beendet wird nicht mehr notifies schicken
6+
7+
18
- Update connection parameter wieder hinzufügen (bleprh anschauen)
29
- Update connection parameters are not allowed during iPhone HID encryption, slave turns off the ability to automatically update connection parameters during encryption.
310
- Bei einem erneuten verbindungsaufbau wird das gestoppte updaten der daten nicht wieder gestartet
411

512
- Code cleanup und kommentieren
613
- ESP muss seine persistent private Adresse irgendwie zwischenspeichern für neustarts
7-
- der Report descriptor schickt einfach für alle 16 kanäle die kompletten 11 bit und macht da nichts spezielles somit sollte es dann egal sein, was damit gemacht wird --> vergleichen mit originalen Descriptor
814
- sdkconfig.defaults anpassen damit sdkconfig wieder in gitignore hinzugefügt werden kann
915

1016

0 commit comments

Comments
 (0)