| 398 | } |
| 399 | |
| 400 | int GoPiGo3::grove_i2c_transfer(uint8_t port, i2c_struct_t *i2c_struct){ |
| 401 | double Timeout = get_time() + 0.005; |
| 402 | bool Continue = false; |
| 403 | int error; |
| 404 | while(!Continue){ |
| 405 | if(error = grove_i2c_start(port, i2c_struct)){ |
| 406 | if(get_time() >= Timeout){ |
| 407 | return error; |
| 408 | } |
| 409 | }else{ |
| 410 | Continue = true; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | double DelayTime = 0; |
| 415 | if(i2c_struct->length_write){ |
| 416 | DelayTime += 1 + i2c_struct->length_write; |
| 417 | } |
| 418 | if(i2c_struct->length_read){ |
| 419 | DelayTime += 1 + i2c_struct->length_read; |
| 420 | } |
| 421 | DelayTime *= 0.000115; // each I2C byte takes about 115uS at full speed (about 100kbps) |
| 422 | // No point trying to read the values before they are ready. |
| 423 | usleep((DelayTime * 1000000)); // delay for as long as it will take to do the I2C transaction. |
| 424 | Timeout = get_time() + 0.005; // timeout after 5ms of failed attempted reads |
| 425 | |
| 426 | while(true){ |
| 427 | if(error = get_grove_value(port, i2c_struct)){ |
| 428 | if(get_time() >= Timeout){ |
| 429 | return error; |
| 430 | } |
| 431 | }else{ |
| 432 | return ERROR_NONE; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | int GoPiGo3::grove_i2c_start(uint8_t port, i2c_struct_t *i2c_struct){ |
| 438 | uint8_t msg_type; |