zoukankan      html  css  js  c++  java
  • 树莓派 -- oled 续(1) wiringPi

    在上文中,分析了wiringPi 的oled demo是使用devfs来控制spi master和spi slave通讯。
    https://blog.csdn.net/feiwatson/article/details/81149028

    再详细分析一下demo的OLED SSD1306相关代码。

    定义一个buffer

    unsigned char buffer[1024];

    初始化

    void SSD1306_begin()
    {
        pinMode(RST,OUTPUT); //Pin24 设置为输出
        pinMode(DC,OUTPUT);  //Pin27 设置为输出
        wiringPiSPISetup(channel,2000000);    //2M
    
        digitalWrite(RST,HIGH);   //reset
        delay(10);
        digitalWrite(RST,LOW);
        delay(10);
        digitalWrite(RST,HIGH);
    
        command(SSD1306_DISPLAYOFF);
        command(SSD1306_SETDISPLAYCLOCKDIV);
        command(0x80);                              // the suggested ratio 0x80
    
        command(SSD1306_SETMULTIPLEX);
        command(0x3F);
        command(SSD1306_SETDISPLAYOFFSET);
        command(0x0);                               // no offset
        command(SSD1306_SETSTARTLINE | 0x0);        // line #0
        command(SSD1306_CHARGEPUMP);
        command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
    
        command(SSD1306_MEMORYMODE);
        command(0x00);                              // 0x0 act like ks0108
    
        command(SSD1306_SEGREMAP | 0x1);
        command(SSD1306_COMSCANDEC);
        command(SSD1306_SETCOMPINS);
        command(0x12);           // TODO - calculate based on _rawHieght ?
        command(SSD1306_SETCONTRAST);
        command((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF);
        command(SSD1306_SETPRECHARGE);
        command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
        command(SSD1306_SETVCOMDETECT);
        command(0x40);
        command(SSD1306_DISPLAYALLON_RESUME);
        command(SSD1306_NORMALDISPLAY);
        command(SSD1306_DISPLAYON);
    }

    设置一个pixel对应的buffer中的bit. 一列8个pixel对应一个buffer字节

    void SSD1306_pixel(int x,int y,char color)
    {
        if(x > width || y > height)return ;
        if(color)
            buffer[x+(y/8)*width] |= 1<<(y%8);
        else
            buffer[x+(y/8)*width] &= ~(1<<(y%8));
    }

    其他的SSD1306_bitmap , SSD1306_string, SSD1306_char3216, SSD1306_char1616等函数通过调用SSD1306_pixel设置buffer

    将buffer中缓冲的图像显示

    void SSD1306_display()
    {
        command(SSD1306_COLUMNADDR);
        command(0);         //cloumn start address
        command(width -1); //cloumn end address
        command(SSD1306_PAGEADDR);
        command(0);         //page atart address
        command(pages -1); //page end address
        digitalWrite(DC,HIGH);
        wiringPiSPIDataRW(channel,buffer,sizeof(buffer));
    }

    图片中显示图像的代码如下

    int main(int argc,char **argv)
    {
        time_t now;
        struct tm *timenow;
        if(wiringPiSetup() < 0)return 1;
        printf("OLED Test Program !!!
    ");
    
        SSD1306_begin();
        SSD1306_bitmap(0,0,waveshare,128,64);
        SSD1306_display();
        delay(2000);
        SSD1306_clear();
        while(1)
        {
            time(&now);
            timenow = localtime(&now);
    
            SSD1306_bitmap(0, 2, Singal816, 16, 8); 
            SSD1306_bitmap(24, 2,Bluetooth88, 8, 8); 
            SSD1306_bitmap(40, 2, Msg816, 16, 8); 
            SSD1306_bitmap(64, 2, GPRS88, 8, 8); 
            SSD1306_bitmap(90, 2, Alarm88, 8, 8); 
            SSD1306_bitmap(112, 2, Bat816, 16, 8); 
    
            SSD1306_string(0, 52, "MUSIC", 12, 0); 
            SSD1306_string(52, 52, "MENU", 12, 0); 
            SSD1306_string(98, 52, "PHONE", 12, 0);
    
            SSD1306_char3216(0,16, value[timenow->tm_hour/10]);
            SSD1306_char3216(16,16, value[timenow->tm_hour%10]);
            SSD1306_char3216(32,16, ':');
            SSD1306_char3216(48,16, value[timenow->tm_min/10]);
            SSD1306_char3216(64,16, value[timenow->tm_min%10]);
            SSD1306_char3216(80,16, ':');
            SSD1306_char3216(96,16, value[timenow->tm_sec/10]);
            SSD1306_char3216(112,16, value[timenow->tm_sec%10]);
    
            SSD1306_display();
        }
        return 0;
    }

    这里写图片描述

  • 相关阅读:
    两栏自适应布局
    说说bfc 和 HasLayout
    春联式弹动广告
    Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
    2016年新剧《欢乐颂》里面的经典台词
    硬盘怎么保养
    如何保养与维护笔记本硬盘
    仔细看完,你会成为微信高手
    电脑之间用网线直接连接起来快速传送大文件技巧
    阿里云9折优惠码 GQH4IT 使用方法如下:
  • 原文地址:https://www.cnblogs.com/feiwatson/p/9478203.html
Copyright © 2011-2022 走看看