其他
分享一个OLED上的简易图形库(附源代码)
关注+星标公众号,不错过精彩内容
作者 | strongerHuang
微信公众号 | 嵌入式专栏
嵌入式专栏
1
嵌入式专栏
2
底层的通信和驱动,这里就不描述了,讲述主要的API接口:
嵌入式专栏
3
1.InitDisplay()初始化显示
void InitDisplay () {
Wire.beginTransmission(address);
Wire.write(commands);
Wire.write(0xA1);
Wire.write(0xAF);
Wire.endTransmission();
}
void Single (uint8_t x) {
Wire.write(onecommand);
Wire.write(x);
}
void ClearDisplay () {
for (int p = 0 ; p < 8; p++) {
Wire.beginTransmission(address);
Single(0xB0 + p);
Wire.endTransmission();
for (int q = 0 ; q < 8; q++) {
Wire.beginTransmission(address);
Wire.write(data);
for (int i = 0 ; i < 20; i++) Wire.write(0);
Wire.endTransmission();
}
}
}
void PlotPoint (int x, int y) {
Wire.beginTransmission(address); //地址
Single(0x00 + ((x + 2) & 0x0F));
Single(0x10 + ((x + 2)>>4));
Single(0xB0 + (y >> 3));
Single(0xE0); //读取并修改写入
Wire.write(onedata);
Wire.endTransmission();
Wire.requestFrom(address, 2);
Wire.read();
int j = Wire.read();
Wire.beginTransmission(address);
Wire.write(onedata);
Wire.write((1<<(y & 0x07)) | j);
Single(0xEE); // Cancel read modify write
Wire.endTransmission();
}
void MoveTo (int x, int y) {
x0 = x;
y0 = y;
}
void DrawTo (int x, int y) {
int sx, sy, e2, err;
int dx = abs(x - x0);
int dy = abs(y - y0);
if (x0 < x) sx = 1; else sx = -1;
if (y0 < y) sy = 1; else sy = -1;
err = dx - dy;
for (;;) {
PlotPoint(x0, y0);
if (x0==x && y0==y) return;
e2 = err<<1;
if (e2 > -dy) { err = err - dy; x0 = x0 + sx; }
if (e2 < dx) { err = err + dx; y0 = y0 + sy; }
}
}
void PlotChar (int c, int x, int y) {
int h = y & 0x07;
for (int p = 0; p < 2; p++) {
Wire.beginTransmission(address);
Single(0xB0 + (y >> 3) + p); // Page
for (int col=0; col<6; col++) {
Single(0x00 + ((x+2+col) & 0x0F)); // Column low nibble
Single(0x10 + ((x+2+col)>>4)); // Column high nibble
Single(0xE0); // Read modify write
Wire.write(onedata);
Wire.endTransmission();
Wire.requestFrom(address, 2);
Wire.read(); // Dummy read
int j = Wire.read();
Wire.beginTransmission(address);
Wire.write(onedata);
int bits = ReverseByte(pgm_read_byte(&CharMap[c-32][col]));
Wire.write((bits<<h)>>(p<<3) | j);
Single(0xEE); // Cancel read modify write
}
Wire.endTransmission();
}
}
void PlotText(PGM_P s) {
int p = (int)s;
while (1) {
char c = pgm_read_byte(p++);
if (c == 0) return;
PlotChar(c, x0, y0);
x0 = x0 + 6;
}
}
嵌入式专栏
3
const int Now = 1547; // 比如设置时间为:15:47
unsigned long StartMins = (unsigned long)((Now/100)*60 + (Now%100));
void loop () {
unsigned int SampleNo = StartMins/15;
// 绘制温度图
int x1 = 16, y1 = 11;
int yscale = 2;
MoveTo(26, 56); PlotText(PSTR("Temperature ~C"));
//横轴
MoveTo(x1, y1); DrawTo(x1+96, y1);
for (int i=0; i<=24; i=i+4) {
int mark = x1+i*4;
MoveTo(mark, y1); DrawTo(mark, y1-2);
int tens = i/10;
if (tens != 0) {
PlotChar(tens+'0', mark-6, y1-12);
PlotChar(i%10+'0', mark, y1-12);
} else PlotChar(i%10+'0', mark-3, y1-12);
}
//纵轴
MoveTo(x1, y1); DrawTo(x1, y1+50);
for (int i=5; i<=25; i=i+5) {
int mark = y1+i*yscale-10;
MoveTo(x1, mark); DrawTo(x1-2, mark);
int tens = i/10;
if (tens != 0) PlotChar(tens+'0', x1-15, mark-3);
PlotChar(i%10+'0', x1-9, mark-3);
}
for (;;) {
//每15分钟更新一下
while ((unsigned long) ((StartMins + millis()/60000)/15)%96 == SampleNo);
// Time to take a new reading
SampleNo = (SampleNo+1)%96;
int Temperature = (analogRead(A2)*25)/233;
PlotPoint(SampleNo+x1, Temperature-10+y1);
}
}
后台回复『单片机』『嵌入式软件设计与开发』阅读更多相关文章。
点击“阅读原文”查看更多分享,欢迎点分享、收藏、点赞、在看。