Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / irlock.c
blob2ce13213cb7cd0cc7a4b0f5ea1ffb5029bc6e673
1 /*
2 * This file is part of INAV.
4 * INAV is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * INAV is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with INAV. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include <math.h>
23 #include "platform.h"
25 #include "drivers/sensor.h"
26 #include "drivers/irlock.h"
27 #include "drivers/time.h"
29 #define IRLOCK_OBJECT_SYNC ((uint16_t)0xaa55)
30 #define IRLOCK_FRAME_SYNC ((uint32_t)(IRLOCK_OBJECT_SYNC | (IRLOCK_OBJECT_SYNC << 16)))
33 #if defined(USE_IRLOCK)
35 static bool irlockHealthy = false;
37 static bool irlockFrameSync(irlockDev_t *irlockDev)
39 uint32_t sync_word = 0;
40 uint8_t count = 10;
41 while (count-- && sync_word != IRLOCK_FRAME_SYNC) {
42 uint8_t sync_byte;
43 irlockHealthy = busRead(irlockDev->busDev, 0xFF, &sync_byte);
44 if (!(irlockHealthy && sync_byte)) return false;
45 sync_word = (sync_word >> 8) | (((uint32_t)sync_byte) << 24);
47 return sync_word == IRLOCK_FRAME_SYNC;
50 static bool irlockRead(irlockDev_t *irlockDev, irlockData_t *irlockData)
52 if (irlockFrameSync(irlockDev) && busReadBuf(irlockDev->busDev, 0xFF, (void*)irlockData, sizeof(*irlockData))) {
53 uint16_t cksum = irlockData->signature + irlockData->posX + irlockData->posY + irlockData->sizeX + irlockData->sizeY;
54 if (irlockData->cksum == cksum) return true;
56 return false;
59 static bool deviceDetect(irlockDev_t *irlockDev)
61 uint8_t buf;
62 bool detected = busRead(irlockDev->busDev, 0xFF, &buf);
63 return !!detected;
66 bool irlockDetect(irlockDev_t *irlockDev)
68 irlockDev->busDev = busDeviceInit(BUSTYPE_I2C, DEVHW_IRLOCK, 0, OWNER_IRLOCK);
69 if (irlockDev->busDev == NULL) {
70 return false;
73 if (!deviceDetect(irlockDev)) {
74 busDeviceDeInit(irlockDev->busDev);
75 return false;
78 irlockDev->read = irlockRead;
80 return true;
83 bool irlockIsHealthy(void)
85 return irlockHealthy;
88 #endif /* USE_IRLOCK */