| 486 | } |
| 487 | |
| 488 | int GoPiGo3::get_grove_value(uint8_t port, void *value_ptr){ |
| 489 | uint8_t msg_type; |
| 490 | uint8_t port_index; |
| 491 | switch(port){ |
| 492 | case GROVE_1: |
| 493 | msg_type = GPGSPI_MESSAGE_GET_GROVE_VALUE_1; |
| 494 | port_index = 0; |
| 495 | break; |
| 496 | case GROVE_2: |
| 497 | msg_type = GPGSPI_MESSAGE_GET_GROVE_VALUE_2; |
| 498 | port_index = 1; |
| 499 | break; |
| 500 | default: |
| 501 | fatal_error("get_grove_value error. Must be one grove port at a time. GROVE_1 or GROVE_2."); |
| 502 | } |
| 503 | spi_array_out[0] = Address; |
| 504 | spi_array_out[1] = msg_type; |
| 505 | |
| 506 | uint8_t spi_transfer_length; |
| 507 | |
| 508 | // Determine the SPI transaction byte length based on the grove type |
| 509 | switch(GroveType[port_index]){ |
| 510 | case GROVE_TYPE_IR_DI_REMOTE: |
| 511 | spi_transfer_length = 7; |
| 512 | break; |
| 513 | case GROVE_TYPE_IR_EV3_REMOTE: |
| 514 | spi_transfer_length = 10; |
| 515 | break; |
| 516 | case GROVE_TYPE_US: |
| 517 | spi_transfer_length = 8; |
| 518 | break; |
| 519 | case GROVE_TYPE_I2C: |
| 520 | spi_transfer_length = 6 + GroveI2CInBytes[port_index]; |
| 521 | break; |
| 522 | default: |
| 523 | return GROVE_STATUS_NOT_CONFIGURED; |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | // Get the grove value(s), and if error |
| 528 | // assign error to the value returned by spi_transfer_array, and if not 0: |
| 529 | if(int error = spi_transfer_array(spi_transfer_length, spi_array_out, spi_array_in)){ |
| 530 | return error; |
| 531 | } |
| 532 | // If the fourth byte received is not 0xA5 |
| 533 | if(spi_array_in[3] != 0xA5){ |
| 534 | return ERROR_SPI_RESPONSE; |
| 535 | } |
| 536 | // If the grove type is not what it should be |
| 537 | if(spi_array_in[4] != GroveType[port_index]){ |
| 538 | printf("get_grove_value %d != %d for port index %d\n", spi_array_in[4], GroveType[port_index], port_index); |
| 539 | return ERROR_GROVE_TYPE_MISMATCH; |
| 540 | } |
| 541 | // If the sensor value(s) is not valid (still configuring the grove port, or error communicating with grove device) |
| 542 | if(spi_array_in[5] != GROVE_STATUS_VALID_DATA){ |
| 543 | return spi_array_in[5]; |
| 544 | } |
| 545 |
no test coverage detected