• 香橙派配合IIC驱动OLED & 使用SourceInsight解读源码


    OLED屏幕介绍 & 硬件接线

    OLED也是老熟人了,详细的介绍见:

    IIC 协议 和 OLED_iic oled-CSDN博客

    再次回顾香橙派硬件接线:

    根据之前的学习了解到,OLED屏幕的驱动是需要IIC协议的,在学习C52时我使用了代码模拟IIC协议,在学习STM32时我使用CubeMX直接可以配置IIC协议,在现在的香橙派上,Linux同样封装了IIC协议,不需要使用代码模拟,具体可以查看Linux系统的dev路径下的文件:

    可见,Linux系统自动封装了如GPIO,tty,i2c,等等重要的协议

    且关于i2c有两个文件,i2c-3和i2c-5,根据引脚图,香橙派的SCL和SDA都是.3结尾,所以适用i2c-3:

    根据上图,物理引脚3和5(wPi引脚0和1)就是IIC的SDA和SCL口了, 分别接OLED的SDA和SCL,然后将OLED的VCC和GND分别接到物理引脚2和6:

    sudo apt-get install i2c-tools

     

    香橙派Linux IIC的测试

    刚刚提到Linux已经封装了IIC的协议,但是要进行测试还需要安装 i2c-tools,执行以下命令进行安装:

    sudo apt-get install i2c-tools

     然后执行以下命令测试:

    sudo i2cdetect -y 3

    显示3c的地方就是OLED屏幕的地址,说明系统识别到了这个屏幕,至此,IIC测试成功

    例程解读

    关于OLED的驱动,wiringPI库同样有例程,将其拷贝到当前目录下:

    先试运行一下,注意运行的时候第二个参数是/dev路径下的i2c-3驱动

      

    说明代码没什么问题,接下来开始解读代码,使用的工具是"SourceInsight3.5"

    SourceInsight3.5的安装和使用

    直接点击SourceInsight.exe进行傻瓜式安装:

     然后使用魔法注册一下,就可以进入了:

    然后解压一下wiringOP库的源码:

    进入该文件夹,然后新建文件夹“si”用来存放source insight工程:

    然后回到source insight,点击左上方project并创建一个新的项目,存放的地址就在“si”文件夹中:

    点击左侧的源码文件夹,再点击右侧的"Add Tree",(显示有169个file),添加完成点击close即可:

    然后就会在右侧显示所有的源码,想要看哪个直接点击就可以,不需要在linux中一个个 vi 了,大大提升了效率,也可以直接搜索想要看的代码:

     

    接着点击右上角的“Project” --> 点击“Synchronize Files”来同步所有文件,这样就可以在不同文件中进行跳转:

    现在打开oled_demo.c:

     此时,按住“CTRL”再点击“display_info”这个结构体,就可以跳转到这个结构体定义的地方了:

    可见,这个结构体其实定义在Oled.h中,并且有4个成员,其中第三个成员字体又是一个结构体,可以再次按住CTRL跳转:

    可见,这个结构体定义在Font.h中,有5个成员来描述字体信息。

    所以SourceInsight在读取代码,尤其是大工程的代码时,有很大的优势

    了解了这个最重要的结构体之后,回到oled_demo.c:

    (代码出现的函数只要不懂就去跳转帮助理解)

    1. /*
    2. * Copyright (c) 2015, Vladimir Komendantskiy
    3. * MIT License
    4. *
    5. * SSD1306 demo of block and font drawing.
    6. */
    7. //
    8. // fixed for OrangePiZero by HypHop
    9. //
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include "oled.h"
    17. #include "font.h"
    18. //该函数会在main中oled_open函数开启成功时被调用
    19. int oled_demo(struct display_info *disp) { //函数参数就是指向display_info结构体的指针
    20. int i;
    21. char buf[100];
    22. //putstrto(disp, 0, 0, "Spnd spd 2468 rpm");
    23. // oled_putstrto(disp, 0, 9+1, "Spnd cur 0.46 A");
    24. oled_putstrto(disp, 0, 9+1, "Welcome to"); //这个oled_putstrto函数就是最核心的显示函数,实现在x,y坐标显示指定内容
    25. disp->font = font1; //指定内容的字体
    26. // oled_putstrto(disp, 0, 18+2, "Spnd tmp 53 C");
    27. oled_putstrto(disp, 0, 18+2, "----OrangePi----");
    28. disp->font = font2;
    29. // oled_putstrto(disp, 0, 27+3, "DrvX tmp 64 C");
    30. oled_putstrto(disp, 0, 27+3, "This is 0.96OLED");
    31. oled_putstrto(disp, 0, 36+4, "");
    32. oled_putstrto(disp, 0, 45+5, "");
    33. disp->font = font1;
    34. // oled_putstrto(disp, 0, 54, "Total cur 2.36 A");
    35. oled_putstrto(disp, 0, 54, "*****************");
    36. oled_send_buffer(disp); //oled_putstrto()只是将内容写到缓存中,这个函数才真正将缓存中的数据发送到屏幕
    37. //接下来的代码也是一套显示指令,即核心还是先oled_putstrto() -> oled_send_buffer()
    38. //只不过这里采用了for循环,不停的更新写入的xy轴位置,所以视觉上这段代码实现的就是一段动画
    39. disp->font = font3;
    40. for (i=0; i<100; i++) {
    41. sprintf(buf, "Spnd spd %d rpm", i);
    42. oled_putstrto(disp, 0, 0, buf);
    43. oled_putstrto(disp, 135-i, 36+4, "===");
    44. oled_putstrto(disp, 100, 0+i/2, ".");
    45. oled_send_buffer(disp);
    46. }
    47. //oled_putpixel(disp, 60, 45);
    48. //oled_putstr(disp, 1, "hello");
    49. return 0;
    50. }
    51. void show_error(int err, int add) {
    52. //const gchar* errmsg;
    53. //errmsg = g_strerror(errno);
    54. printf("\nERROR: %i, %i\n\n", err, add);
    55. //printf("\nERROR\n");
    56. }
    57. void show_usage(char *progname) {
    58. printf("\nUsage:\n%s \n", progname);
    59. }
    60. int main(int argc, char **argv) {
    61. int e;
    62. char filename[32];
    63. struct display_info disp;
    64. if (argc < 2) {
    65. show_usage(argv[0]);
    66. return -1;
    67. }
    68. memset(&disp, 0, sizeof(disp));
    69. sprintf(filename, "%s", argv[1]);
    70. disp.address = OLED_I2C_ADDR;
    71. disp.font = font2;
    72. e = oled_open(&disp, filename); //通过驱动文件开启屏幕,跳转两次就可以看到open函数,Linux系统一切皆文件,想要打开一个硬件,反映在Linux上层就是打开驱动文件
    73. if (e < 0) {
    74. show_error(1, e);
    75. } else {
    76. e = oled_init(&disp); //如果oled_open返回0,则调用oled_init进行初始化
    77. if (e < 0) {
    78. show_error(2, e);
    79. } else { //如果开启屏幕成功,就会调用oled_demo()这个函数了!
    80. printf("---------start--------\n");
    81. if (oled_demo(&disp) < 0)
    82. show_error(3, 777);
    83. printf("----------end---------\n");
    84. }
    85. }
    86. return 0;
    87. }

    自己编写代码

    在解读了例程之后,我尝试修改代码显示自己想要的字符:

    其实很简单,就是修改oled_putstrto函数显示的内容和位置就可以了

    oled_test.c:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include "oled.h"
    8. #include "font.h"
    9. int oled_demo(struct display_info *disp) {
    10. oled_putstrto(disp, 0, 9+1, "This is a ");
    11. disp->font = font1;
    12. oled_putstrto(disp, 1, 9+10, "oled demo");
    13. disp->font = font2;
    14. oled_putstrto(disp, 2, 9+20, "From MJM");
    15. disp->font = font3;
    16. oled_send_buffer(disp);
    17. return 0;
    18. }
    19. void show_error(int err, int add) {
    20. //const gchar* errmsg;
    21. //errmsg = g_strerror(errno);
    22. printf("\nERROR: %i, %i\n\n", err, add);
    23. //printf("\nERROR\n");
    24. }
    25. void show_usage(char *progname) {
    26. printf("\nUsage:\n%s \n", progname);
    27. }
    28. int main(int argc, char **argv) {
    29. int e;
    30. char filename[32];
    31. struct display_info disp; //一个结构体,封装了显示的信息,如屏幕的地址,显示的字体
    32. if (argc < 2) {
    33. show_usage(argv[0]);
    34. return -1;
    35. }
    36. memset(&disp, 0, sizeof(disp));
    37. sprintf(filename, "%s", argv[1]);
    38. disp.address = OLED_I2C_ADDR; //屏幕设备地址
    39. disp.font = font2; //显示字体
    40. e = oled_open(&disp, filename);
    41. if (e < 0) {
    42. show_error(1, e);
    43. } else {
    44. e = oled_init(&disp);
    45. if (e < 0) {
    46. show_error(2, e);
    47. } else {
    48. printf("---------start--------\n");
    49. if (oled_demo(&disp) < 0)
    50. show_error(3, 777);
    51. printf("----------end---------\n");
    52. }
    53. }
    54. return 0;
    55. }

    实现效果:

     

     

  • 相关阅读:
    JVM优化(OOM,内存溢出),查看线程快照,堆内存情况等问题
    聊聊 golang 中 channel
    系统集成|第十一章(笔记)
    Java面向对象编程
    [音视频学习笔记]六、自制音视频播放器Part1 -新版本ffmpeg,Qt +VS2022,都什么年代了还在写传统播放器?
    Tomcat进程占用CPU过高怎么办?
    IOS手机耗电量测试
    力扣题解22-25
    2022全国大学生数学建模竞赛获奖难么?国赛求解过程技巧及方案
    计算机竞赛 基于深度学习的人脸识别系统
  • 原文地址:https://blog.csdn.net/mjmmm/article/details/132842873