我正在为嵌入式系统编写一些代码。当我在同一外围设备上添加更多不同的通道时,我得到的是真正重复的代码。例如:
void pos1_write_read(int *pwriteData)
{
// Reset rx buffer and transfer done flag
memset(m_rx_buf0, 0, m_length0);
spi0_xfer_done = false;
nrfx_spi_xfer_desc_t m_pos1_write;
m_pos1_write.p_tx_buffer = pwriteData;
m_pos1_write.tx_length = m_length0;
m_pos1_write.p_rx_buffer = m_rx_buf0;
m_pos1_write.rx_length = m_length0;
nrf_gpio_pin_clear(SPI0_CS0_PIN); //Set CS0 to 0 (on)
APP_ERROR_CHECK(nrfx_spi_xfer(&spi0, &spi0_transfer, NULL));
while (!spi0_xfer_done){} //Wait until the tranfser is done
nrf_gpio_pin_set(SPI0_CS0_PIN); //Set CS0 to 1 (off)
}
void pos2_write_read(int *pwriteData)
{
// Reset rx buffer and transfer done flag
memset(m_rx_buf0, 0, m_length0);
spi0_xfer_done = false;
nrfx_spi_xfer_desc_t m_pos2_write;
m_pos2_write.p_tx_buffer = pwriteData;
m_pos2_write.tx_length = m_length0;
m_pos2_write.p_rx_buffer = m_rx_buf0;
m_pos2_write.rx_length = m_length0;
nrf_gpio_pin_clear(SPI0_CS1_PIN); //Set CS1 to 0 (on)
APP_ERROR_CHECK(nrfx_spi_xfer(&spi0, &spi0_transfer, NULL));
while (!spi0_xfer_done){} //Wait until the tranfser is done
nrf_gpio_pin_set(SPI0_CS1_PIN); //Set CS1 to 1 (off)
}
我将如何编写可以在两个示例中使用的单个函数?是否有任何一般的好习惯来避免重复相同的代码?
找到正确的设计取决于打算如何使用它,您感觉通用性如何,等等。没有唯一正确的方法可以做到这一点。但是仅基于您的代码段的幼稚解决方案是这样的:
void write_read(int *pwriteData, int pin)
{
// Reset rx buffer and transfer done flag
memset(m_rx_buf0, 0, m_length0);
spi0_xfer_done = false;
nrfx_spi_xfer_desc_t m_pos1_write;
m_pos1_write.p_tx_buffer = pwriteData;
m_pos1_write.tx_length = m_length0;
m_pos1_write.p_rx_buffer = m_rx_buf0;
m_pos1_write.rx_length = m_length0;
nrf_gpio_pin_clear(pin); //Set CS0 to 0 (on)
APP_ERROR_CHECK(nrfx_spi_xfer(&spi0, &spi0_transfer, NULL));
while (!spi0_xfer_done){} //Wait until the tranfser is done
nrf_gpio_pin_set(pin); //Set CS0 to 1 (off)
}
void pos1_write_read(int *pwriteData)
{
write_read(pwriteData, SPI0_CS0_PIN);
}
void pos2_write_read(int *pwriteData)
{
write_read(pwriteData, SPI0_CS1_PIN);
}
想法是采用通用代码,然后对它们之间的不同之处进行参数化。
唯一真正的区别是SC引脚,您可以将其作为参数传递:
void pos_write_read(int *pwriteData, uint32_t pin)
{
// Reset rx buffer and transfer done flag
memset(m_rx_buf0, 0, m_length0);
spi0_xfer_done = false;
nrfx_spi_xfer_desc_t m_pos_write;
m_pos_write.p_tx_buffer = pwriteData;
m_pos_write.tx_length = m_length0;
m_pos_write.p_rx_buffer = m_rx_buf0;
m_pos_write.rx_length = m_length0;
nrf_gpio_pin_clear(pin); //Set CS to 0 (on)
APP_ERROR_CHECK(nrfx_spi_xfer(&spi0, &spi0_transfer, NULL));
while (!spi0_xfer_done){} //Wait until the tranfser is done
nrf_gpio_pin_set(pin); //Set CS to 1 (off)
}
/* This allows you to do the following: */
void pos1_write_read(int *pwriteData)
{
pos_write_read(pwriteData, SPI0_CS0_PIN);
}
void pos2_write_read(int *pwriteData)
{
pos_write_read(pwriteData, SPI0_CS1_PIN);
}