imu.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "imu.h"
  2. #define IMU_TAG "IMU_H"
  3. extern struct imu_t imu_bmi160;
  4. extern struct imu_t imu_lsm6ds3trc;
  5. extern struct imu_t imu_lsm6dso;
  6. struct imu_t* imu_types[] = {&imu_bmi160, &imu_lsm6ds3trc, &imu_lsm6dso};
  7. static const int imu_count = sizeof(imu_types) / sizeof(struct imu_t*);
  8. static struct imu_t* imu_found;
  9. struct imu_t* find_imu() {
  10. unsigned int i;
  11. for(i = 0; i < imu_count; i++) {
  12. if(furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, imu_types[i]->address, 50)) {
  13. FURI_LOG_E(IMU_TAG, "found i2c device address 0x%X", imu_types[i]->address);
  14. return imu_types[i];
  15. }
  16. }
  17. return NULL;
  18. }
  19. bool imu_begin() {
  20. bool ret = false;
  21. furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
  22. if(imu_found == NULL) {
  23. imu_found = find_imu();
  24. if(imu_found != NULL) FURI_LOG_E(IMU_TAG, "Found Device %s", imu_found->name);
  25. }
  26. if(imu_found != NULL) ret = imu_found->begin();
  27. furi_hal_i2c_release(&furi_hal_i2c_handle_external);
  28. return ret;
  29. }
  30. void imu_end() {
  31. if(imu_found == NULL) return;
  32. furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
  33. imu_found->end();
  34. furi_hal_i2c_release(&furi_hal_i2c_handle_external);
  35. }
  36. int imu_read(double* vec) {
  37. if(imu_found == NULL) return 0;
  38. furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
  39. int ret = imu_found->read(vec);
  40. furi_hal_i2c_release(&furi_hal_i2c_handle_external);
  41. return ret;
  42. }