1. 程式人生 > >樹莓派 -- oled 續(1) wiringPi

樹莓派 -- oled 續(1) wiringPi

再詳細分析一下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 !!!\n");

    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;
}

這裡寫圖片描述