imu.c 1.4 KB

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