MCPcopy Create free account
hub / github.com/DexterInd/GoPiGo3 / grove_i2c_transfer

Method grove_i2c_transfer

Software/C/GoPiGo3.cpp:400–435  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

398}
399
400int 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
437int GoPiGo3::grove_i2c_start(uint8_t port, i2c_struct_t *i2c_struct){
438 uint8_t msg_type;

Callers 1

mainFunction · 0.45

Calls 1

get_timeFunction · 0.85

Tested by

no test coverage detected