在上文中,分析了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;
}