imu.c 1.5 KB

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