@@ -14,45 +14,49 @@ struct imu_t* imu_types[] = {
1414
1515static const int imu_count = sizeof (imu_types ) / sizeof (struct imu_t * );
1616
17- struct imu_t * imu_found ;
17+ static struct imu_t * imu_found ;
1818
19- bool imu_begin () {
20- furi_hal_i2c_acquire (& furi_hal_i2c_handle_external );
21- if (imu_found == NULL ) {
22- imu_found = find_imu ();
19+ struct imu_t * find_imu () {
20+ unsigned int i ;
21+ for (i = 0 ; i < imu_count ; i ++ ) {
22+ if (furi_hal_i2c_is_device_ready (& furi_hal_i2c_handle_external , imu_types [i ]-> address , 50 )) {
23+ FURI_LOG_E (IMU_TAG , "found i2c device address 0x%X" , imu_types [i ]-> address );
24+ return imu_types [i ];
25+ }
2326 }
27+ return NULL ;
28+ }
29+
30+ bool imu_begin () {
31+ if (imu_found != NULL )
32+ return true;
2433
2534 bool ret = false;
35+ furi_hal_i2c_acquire (& furi_hal_i2c_handle_external );
36+ imu_found = find_imu ();
37+
2638 if (imu_found != NULL ) {
2739 FURI_LOG_E (IMU_TAG , "Found Device %s" , imu_found -> name );
2840 ret = imu_found -> begin ();
2941 }
42+
3043 furi_hal_i2c_release (& furi_hal_i2c_handle_external );
3144 return ret ;
3245}
3346
3447void imu_end () {
35- if (imu_found == NULL ) return ;
48+ if (imu_found == NULL )
49+ return ;
3650 furi_hal_i2c_acquire (& furi_hal_i2c_handle_external );
3751 imu_found -> end ();
3852 furi_hal_i2c_release (& furi_hal_i2c_handle_external );
3953}
4054
4155int imu_read (double * vec ) {
42- if (imu_found == NULL ) return 0 ;
56+ if (imu_found == NULL )
57+ return 0 ;
4358 furi_hal_i2c_acquire (& furi_hal_i2c_handle_external );
4459 int ret = imu_found -> read (vec );
4560 furi_hal_i2c_release (& furi_hal_i2c_handle_external );
4661 return ret ;
4762}
48-
49- struct imu_t * find_imu () {
50- unsigned int i ;
51- for (i = 0 ; i < imu_count ; i ++ ) {
52- if (furi_hal_i2c_is_device_ready (& furi_hal_i2c_handle_external , imu_types [i ]-> address , 50 )) {
53- FURI_LOG_E (IMU_TAG , "found i2c device address 0x%X" , imu_types [i ]-> address );
54- return imu_types [i ];
55- }
56- }
57- return NULL ;
58- }
0 commit comments