fixup examples based on default pin #define-s and also support them being undefined

This commit is contained in:
graham sanderson
2021-02-28 09:00:36 -06:00
committed by Graham Sanderson
parent 82c7a506de
commit 3c82d8aa2b
10 changed files with 101 additions and 43 deletions

View File

@@ -58,7 +58,6 @@ const int LCD_BACKLIGHT = 0x08;
const int LCD_ENABLE_BIT = 0x04;
#define I2C_PORT i2c0
// By default these LCD display drivers are on bus address 0x27
static int addr = 0x27;
@@ -71,7 +70,9 @@ static int addr = 0x27;
/* Quick helper function for single byte transfers */
void i2c_write_byte(uint8_t val) {
i2c_write_blocking(I2C_PORT, addr, &val, 1, false);
#ifdef i2c_default
i2c_write_blocking(i2c_default, addr, &val, 1, false);
#endif
}
void lcd_toggle_enable(uint8_t val) {
@@ -129,14 +130,17 @@ void lcd_init() {
}
int main() {
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL)
i2c_init(I2C_PORT, 100 * 1000);
gpio_set_function(4, GPIO_FUNC_I2C);
gpio_set_function(5, GPIO_FUNC_I2C);
gpio_pull_up(4);
gpio_pull_up(5);
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c/bus_scane example requires a board with I2C pins
#else
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
i2c_init(i2c_default, 100 * 1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
// Make the I2C pins available to picotool
bi_decl( bi_2pins_with_func(4, 5, GPIO_FUNC_I2C));
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
lcd_init();
@@ -161,4 +165,5 @@ int main() {
}
return 0;
#endif
}