2 * This file is part of INAV Project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
32 #include "build/build_config.h"
33 #include "build/debug.h"
35 #include "common/maths.h"
37 #include "io/serial.h"
39 #if defined(USE_OPFLOW_CXOF)
40 #include "drivers/opflow/opflow_virtual.h"
41 #include "drivers/time.h"
42 #include "io/opflow.h"
44 #define CXOF_PACKET_SIZE 9
45 static serialPort_t
* flowPort
= NULL
;
46 static serialPortConfig_t
* portConfig
;
47 static uint8_t buffer
[10];
50 typedef struct __attribute__((packed
)) {
51 uint8_t header
; // 0xFE
52 uint8_t res0
; // Seems to be 0x04 all the time
55 int8_t motionT
; // ???
56 uint8_t squal
; // Not sure about this
57 uint8_t footer
; // 0xAA
60 static bool cxofOpflowDetect(void)
62 portConfig
= findSerialPortConfig(FUNCTION_OPTICAL_FLOW
);
70 static bool cxofOpflowInit(void)
76 flowPort
= openSerialPort(portConfig
->identifier
, FUNCTION_OPTICAL_FLOW
, NULL
, NULL
, baudRates
[portConfig
->gps_baudrateIndex
], MODE_RX
, SERIAL_NOT_INVERTED
);
86 static bool cxofOpflowUpdate(opflowData_t
* data
)
88 static timeUs_t previousTimeUs
= 0;
89 const timeUs_t currentTimeUs
= micros();
91 bool newPacket
= false;
92 opflowData_t tmpData
= { 0 };
94 while (serialRxBytesWaiting(flowPort
) > 0) {
95 uint8_t c
= serialRead(flowPort
);
104 // Consume received byte
105 if (bufferPtr
< CXOF_PACKET_SIZE
) {
106 buffer
[bufferPtr
++] = c
;
108 if (bufferPtr
== CXOF_PACKET_SIZE
) {
109 cxofPacket_t
* pkt
= (cxofPacket_t
*)&buffer
[0];
111 if (pkt
->header
== 0xFE && pkt
->footer
== 0xAA) {
113 tmpData
.deltaTime
+= (currentTimeUs
- previousTimeUs
);
114 tmpData
.flowRateRaw
[0] += pkt
->motionX
;
115 tmpData
.flowRateRaw
[1] += pkt
->motionY
;
116 tmpData
.flowRateRaw
[2] = 0;
117 tmpData
.quality
= (constrain(pkt
->squal
, 64, 78) - 64) * 100 / 14;
119 previousTimeUs
= currentTimeUs
;
128 // In case of buffer overflow - reset the decoder
140 virtualOpflowVTable_t opflowCxofVtable
= {
141 .detect
= cxofOpflowDetect
,
142 .init
= cxofOpflowInit
,
143 .update
= cxofOpflowUpdate