Arduino--读u8lib之I2C代码

来源:互联网 发布:数据量化方法 编辑:程序博客网 时间:2024/05/20 02:30

u8lib库下载:https://github.com/olikraus/u8glib

 尝试研究一下u8lib库的I2C的代码

1、首先,u8lib库调用之后会指明使用I2C协议

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);

就是这段代码,以其为研究的入口

  • 类名:U8GLIB_SSD1306_128X64
  • 类的构造函数:u8g()
  • 构造函数的参数:U8G_I2C_OPT_NONE

2、类名为:U8GLIB_SSD1306_128X64

  • SSD1306:OLED屏控制器的型号
  • 128X64:OLED屏的参数尺寸

在cppsrc/u8glib.h中首先找到这样(U8GLIB_SSD1306_128X64)子类继承至(U8GLIB)父类;
它有3个重载的构造函数

class U8GLIB_SSD1306_128X64 : public U8GLIB {  public:    U8GLIB_SSD1306_128X64(uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset = U8G_PIN_NONE)       : U8GLIB(&u8g_dev_ssd1306_128x64_sw_spi, sck, mosi, cs, a0, reset)      { }    U8GLIB_SSD1306_128X64(uint8_t cs, uint8_t a0, uint8_t reset = U8G_PIN_NONE)       : U8GLIB(&u8g_dev_ssd1306_128x64_hw_spi, cs, a0, reset)      { }    U8GLIB_SSD1306_128X64(uint8_t options = U8G_I2C_OPT_NONE)       : U8GLIB(&u8g_dev_ssd1306_128x64_i2c, options) //使用的是这个构造函数,对应了父类构造函数中的I2C初始化      { }};

也在cppsrc/u8glib.h中找到了父类U8GLIB,并找到了U8GLIB的8个重载的构造函数。

class U8GLIB : public Print{    ...    /* constructor */    U8GLIB(void)      { }    U8GLIB(u8g_dev_t *dev)      { prepare(); u8g_Init(&u8g, dev); }    U8GLIB(u8g_dev_t *dev, u8g_com_fnptr com_fn)      { prepare(); u8g_InitComFn(&u8g, dev, com_fn); }    U8GLIB(u8g_dev_t *dev, uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset)       { initSPI(dev, sck, mosi, cs, a0, reset); }    U8GLIB(u8g_dev_t *dev, uint8_t cs, uint8_t a0, uint8_t reset)       { initHWSPI(dev, cs, a0, reset); }    U8GLIB(u8g_dev_t *dev, uint8_t options)  // 两个参数对应了初始化I2C构造函数      { initI2C(dev, options); }    U8GLIB(u8g_dev_t *dev, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t en, uint8_t cs1, uint8_t cs2, uint8_t di, uint8_t rw, uint8_t reset)       { init8Bit(dev, d0, d1, d2, d3, d4, d5, d6, d7, en, cs1, cs2, di, rw, reset); }    U8GLIB(u8g_dev_t *dev, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t cs, uint8_t a0, uint8_t wr, uint8_t rd, uint8_t reset)       { initRW8Bit(dev, d0, d1, d2, d3, d4, d5, d6, d7, cs, a0, wr, rd, reset); }}

3、类的构造函数:u8g()

4、类的构造函数的参数:U8G_I2C_OPT_NONE