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)
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/>.
26 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
31 #ifdef USE_VIRTUAL_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_virtual.h"
41 static int16_t virtualGyroADC
[XYZ_AXIS_COUNT
];
42 gyroDev_t
*virtualGyroDev
;
44 static void virtualGyroInit(gyroDev_t
*gyro
)
46 virtualGyroDev
= 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");
54 void virtualGyroSet(gyroDev_t
*gyro
, int16_t x
, int16_t y
, int16_t z
)
58 virtualGyroADC
[X
] = x
;
59 virtualGyroADC
[Y
] = y
;
60 virtualGyroADC
[Z
] = z
;
62 gyro
->dataReady
= true;
67 STATIC_UNIT_TESTED
bool virtualGyroRead(gyroDev_t
*gyro
)
70 if (gyro
->dataReady
== false) {
74 gyro
->dataReady
= false;
76 gyro
->gyroADCRaw
[X
] = virtualGyroADC
[X
];
77 gyro
->gyroADCRaw
[Y
] = virtualGyroADC
[Y
];
78 gyro
->gyroADCRaw
[Z
] = virtualGyroADC
[Z
];
84 static bool virtualGyroReadTemperature(gyroDev_t
*gyro
, int16_t *temperatureData
)
87 UNUSED(temperatureData
);
91 bool virtualGyroDetect(gyroDev_t
*gyro
)
93 gyro
->initFn
= virtualGyroInit
;
94 gyro
->readFn
= virtualGyroRead
;
95 gyro
->temperatureFn
= virtualGyroReadTemperature
;
96 #if defined(SIMULATOR_BUILD)
97 gyro
->scale
= GYRO_SCALE_2000DPS
;
103 #endif // USE_VIRTUAL_GYRO
105 #ifdef USE_VIRTUAL_ACC
107 static int16_t virtualAccData
[XYZ_AXIS_COUNT
];
108 accDev_t
*virtualAccDev
;
110 static void virtualAccInit(accDev_t
*acc
)
113 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
114 if (pthread_mutex_init(&acc
->lock
, NULL
) != 0) {
115 printf("Create acc lock error!\n");
120 void virtualAccSet(accDev_t
*acc
, int16_t x
, int16_t y
, int16_t z
)
124 virtualAccData
[X
] = x
;
125 virtualAccData
[Y
] = y
;
126 virtualAccData
[Z
] = z
;
128 acc
->dataReady
= true;
133 static bool virtualAccRead(accDev_t
*acc
)
136 if (acc
->dataReady
== false) {
140 acc
->dataReady
= false;
142 acc
->ADCRaw
[X
] = virtualAccData
[X
];
143 acc
->ADCRaw
[Y
] = virtualAccData
[Y
];
144 acc
->ADCRaw
[Z
] = virtualAccData
[Z
];
150 bool virtualAccDetect(accDev_t
*acc
)
152 acc
->initFn
= virtualAccInit
;
153 acc
->readFn
= virtualAccRead
;
154 acc
->revisionCode
= 0;
157 #endif // USE_VIRTUAL_ACC