oled_ssd1306: I2C slave address is now a variable instead of constant

This commit is contained in:
King Kévin 2019-12-04 23:14:27 +01:00
parent 0f8b9e34b0
commit 5bb15a2519
2 changed files with 12 additions and 10 deletions

View File

@ -32,7 +32,7 @@
#include "i2c_master.h" // I2C header and definitions
/** SSD1306 OLED display I2C slave address */
#define OLED_SSD1306_SLAVE 0x3c
static uint8_t oled_ssd1306_slave_addr = 0x3c;
/** @defgroup oled_ssd1306_i2c I2C peripheral to communicate with the SSD1306 OLED
* @{
@ -40,11 +40,12 @@
#define OLED_SSD1306_I2C I2C1 /**< I2C peripheral */
/** @} */
bool oled_ssd1306_setup(void)
bool oled_ssd1306_setup(uint8_t slave_addr)
{
if (!i2c_master_check_signals(OLED_SSD1306_I2C)) { // check if there are pull-ups to operator I2C
return false;
}
oled_ssd1306_slave_addr = slave_addr; // save I2C slave address of SSD1306
i2c_master_setup(OLED_SSD1306_I2C, 400); // setup I2C bus ( SSD1306 supports an I2C cleck up to 400 kHz)
const uint8_t oled_init[] = {
0x00, // control byte: continuous (multiple byes), command
@ -68,7 +69,7 @@ bool oled_ssd1306_setup(void)
// Addressing Setting
0x20, 0x00 // Set Memory Addressing Mode: Horizontal Addressing Mode
}; // command to initialize the display
return I2C_MASTER_RC_NONE == i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_init, LENGTH(oled_init)); // send command to initialize display
return I2C_MASTER_RC_NONE == i2c_master_slave_write(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, oled_init, LENGTH(oled_init)); // send command to initialize display
}
void oled_ssd1306_on(void)
@ -77,7 +78,7 @@ void oled_ssd1306_on(void)
0x80, // control byte: no continuation, command
0xaf, // Set Display ON/OFF: ON
}; // command to switch on display
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
i2c_master_slave_write(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, oled_display_on, LENGTH(oled_display_on)); // sent command to switch on display
}
void oled_ssd1306_off(void)
@ -86,7 +87,7 @@ void oled_ssd1306_off(void)
0x80, // control byte: no continuation, command
0xae, // Set Display ON/OFF: OFF
}; // command to switch off display
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_display_off, LENGTH(oled_display_off)); // sent command to switch onff display
i2c_master_slave_write(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, oled_display_off, LENGTH(oled_display_off)); // sent command to switch onff display
}
void oled_ssd1306_test(void)
@ -95,7 +96,7 @@ void oled_ssd1306_test(void)
0x80, // control byte: no continuation, command
0xa5 // Entire Display ON: Entire display ON Output ignores RAM content
}; // command to set entire display on
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_entire_display_on, LENGTH(oled_entire_display_on)); // send command to switch entire display on
i2c_master_slave_write(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, oled_entire_display_on, LENGTH(oled_entire_display_on)); // send command to switch entire display on
oled_ssd1306_on(); // set display on
sleep_ms(200); // let is on for a bit (enough for the user to see it is completely on
oled_ssd1306_off(); // set display off
@ -103,7 +104,7 @@ void oled_ssd1306_test(void)
0x80, // control byte: no continuation, command
0xa4 // Entire Display ON: Resume to RAM content display
}; // command to display RAM
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_entire_display_ram, LENGTH(oled_entire_display_ram)); // send command to display RAM
i2c_master_slave_write(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, oled_entire_display_ram, LENGTH(oled_entire_display_ram)); // send command to display RAM
}
void oled_ssd1306_display(const uint8_t* display_data, uint16_t display_length)
@ -119,11 +120,11 @@ void oled_ssd1306_display(const uint8_t* display_data, uint16_t display_length)
0x00, // Set Lower Column Start Address for Page Addressing Mode: 0
0x10 // Set Higher Column Start Address for Page Addressing Mode: 0
}; // command to set addressing mode
i2c_master_slave_write(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
i2c_master_slave_write(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, oled_start_page, LENGTH(oled_start_page)); // send command to set addressing mode
if (I2C_MASTER_RC_NONE != i2c_master_start(OLED_SSD1306_I2C)) { // send start condition
return;
}
if (I2C_MASTER_RC_NONE != i2c_master_select_slave(OLED_SSD1306_I2C, OLED_SSD1306_SLAVE, false, true)) { // select OLED display
if (I2C_MASTER_RC_NONE != i2c_master_select_slave(OLED_SSD1306_I2C, oled_ssd1306_slave_addr, false, true)) { // select OLED display
return;
}
const uint8_t oled_data[] = {

View File

@ -20,9 +20,10 @@
*/
/** setup OLED display
* @param[in] slave_addr I2C slave address of SSD1306 device (least significant 7-bit)
* @return if the display setup is successful, else the display is probably not on the I2C bus
*/
bool oled_ssd1306_setup(void);
bool oled_ssd1306_setup(uint8_t slave_addr);
/** switch OLED display on */
void oled_ssd1306_on(void);
/** switch OLED display off */