Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / accgyro / accgyro_fake.c
bloba6dd3711783630798fa7e50abd04a8027fe4f116
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
27 #include <stdio.h>
28 #include <pthread.h>
29 #endif
31 #ifdef USE_FAKE_GYRO
33 #include "build/build_config.h"
35 #include "common/axis.h"
36 #include "common/utils.h"
38 #include "drivers/accgyro/accgyro.h"
39 #include "drivers/accgyro/accgyro_fake.h"
41 static int16_t fakeGyroADC[XYZ_AXIS_COUNT];
42 gyroDev_t *fakeGyroDev;
44 static void fakeGyroInit(gyroDev_t *gyro)
46 fakeGyroDev = gyro;
47 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
48 if (pthread_mutex_init(&gyro->lock, NULL) != 0) {
49 printf("Create gyro lock error!\n");
51 #endif
54 void fakeGyroSet(gyroDev_t *gyro, int16_t x, int16_t y, int16_t z)
56 gyroDevLock(gyro);
58 fakeGyroADC[X] = x;
59 fakeGyroADC[Y] = y;
60 fakeGyroADC[Z] = z;
62 gyro->dataReady = true;
64 gyroDevUnLock(gyro);
67 STATIC_UNIT_TESTED bool fakeGyroRead(gyroDev_t *gyro)
69 gyroDevLock(gyro);
70 if (gyro->dataReady == false) {
71 gyroDevUnLock(gyro);
72 return false;
74 gyro->dataReady = false;
76 gyro->gyroADCRaw[X] = fakeGyroADC[X];
77 gyro->gyroADCRaw[Y] = fakeGyroADC[Y];
78 gyro->gyroADCRaw[Z] = fakeGyroADC[Z];
80 gyroDevUnLock(gyro);
81 return true;
84 static bool fakeGyroReadTemperature(gyroDev_t *gyro, int16_t *temperatureData)
86 UNUSED(gyro);
87 UNUSED(temperatureData);
88 return true;
91 bool fakeGyroDetect(gyroDev_t *gyro)
93 gyro->initFn = fakeGyroInit;
94 gyro->readFn = fakeGyroRead;
95 gyro->temperatureFn = fakeGyroReadTemperature;
96 #if defined(SIMULATOR_BUILD)
97 gyro->scale = GYRO_SCALE_2000DPS;
98 #else
99 gyro->scale = 1.0f;
100 #endif
101 return true;
103 #endif // USE_FAKE_GYRO
106 #ifdef USE_FAKE_ACC
108 static int16_t fakeAccData[XYZ_AXIS_COUNT];
109 accDev_t *fakeAccDev;
111 static void fakeAccInit(accDev_t *acc)
113 fakeAccDev = acc;
114 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
115 if (pthread_mutex_init(&acc->lock, NULL) != 0) {
116 printf("Create acc lock error!\n");
118 #endif
121 void fakeAccSet(accDev_t *acc, int16_t x, int16_t y, int16_t z)
123 accDevLock(acc);
125 fakeAccData[X] = x;
126 fakeAccData[Y] = y;
127 fakeAccData[Z] = z;
129 acc->dataReady = true;
131 accDevUnLock(acc);
134 static bool fakeAccRead(accDev_t *acc)
136 accDevLock(acc);
137 if (acc->dataReady == false) {
138 accDevUnLock(acc);
139 return false;
141 acc->dataReady = false;
143 acc->ADCRaw[X] = fakeAccData[X];
144 acc->ADCRaw[Y] = fakeAccData[Y];
145 acc->ADCRaw[Z] = fakeAccData[Z];
147 accDevUnLock(acc);
148 return true;
151 bool fakeAccDetect(accDev_t *acc)
153 acc->initFn = fakeAccInit;
154 acc->readFn = fakeAccRead;
155 acc->revisionCode = 0;
156 return true;
158 #endif // USE_FAKE_ACC