Skip to content

Instantly share code, notes, and snippets.

@hierophect
Created January 8, 2020 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hierophect/8d304e7d44c9816e294874529937019d to your computer and use it in GitHub Desktop.
Save hierophect/8d304e7d44c9816e294874529937019d to your computer and use it in GitHub Desktop.
Less nesting pin search
//SCK is not optional. MOSI and MISO are
for (uint i=0; i<sck_len;i++) {
if (mcu_spi_sck_list[i].pin == sck) {
uint j = 0;
if (miso != mp_const_none) {
for (j=0; j<miso_len;j++) {
RETRY:
if ((mcu_spi_miso_list[j].pin == miso) //only SCK and MISO need the same index
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_miso_list[j].spi_index)) {
//store pins if not
if (mosi == mp_const_none) {
self->sck = &mcu_spi_sck_list[i];
self->mosi = NULL;
self->miso = &mcu_spi_miso_list[j];
}
break;
}
}
if (mosi != mp_const_none) {
for (uint k=0; k<mosi_len;k++) {
if ((mcu_spi_mosi_list[k].pin == mosi) //only SCK and MOSI need the same index
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_mosi_list[k].spi_index)) {
if (miso == mp_const_none) {
//keep looking if the SPI is taken, edge case
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
spi_taken = true;
continue;
}
//store pins if not
self->sck = &mcu_spi_sck_list[i];
self->mosi = &mcu_spi_mosi_list[j];
self->miso = NULL;
} else if (mcu_spi_sck_list[i].spi_index == mcu_spi_miso_list[j].spi_index) {
//keep looking if the SPI is taken, edge case
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
spi_taken = true;
continue;
}
//store pins if not
self->sck = &mcu_spi_sck_list[i];
self->mosi = &mcu_spi_mosi_list[j];
self->miso = &mcu_spi_mosi_list[k];
}
break;
}
}
}
if (self->mosi == NULL && mosi != mp_const_none && j<miso_len) {
goto: RETRY;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment