NuMaker-PFM-NUC472: I2C1 LCD draw a moving circle

Committer:
ccli8
Date:
Tue Jul 11 13:41:06 2017 +0800
Revision:
1:219b99b987f0
Parent:
0:72afeb131817
Update mbed-os to mbed-os-5.5.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rkuo2000 0:72afeb131817 1 #include <mbed.h>
rkuo2000 0:72afeb131817 2 #include "draw2D.h"
rkuo2000 0:72afeb131817 3 #include "ssd1306.h"
rkuo2000 0:72afeb131817 4
rkuo2000 0:72afeb131817 5 SSD1306 LCD_2D;
rkuo2000 0:72afeb131817 6
rkuo2000 0:72afeb131817 7 // draw Bresenham Line
rkuo2000 0:72afeb131817 8 void Draw2D::drawLine(int x1, int y1, int x2, int y2, int fgColor, int bgColor)
rkuo2000 0:72afeb131817 9 {
rkuo2000 0:72afeb131817 10 int dy = y2 - y1;
rkuo2000 0:72afeb131817 11 int dx = x2 - x1;
rkuo2000 0:72afeb131817 12 int stepx, stepy;
rkuo2000 0:72afeb131817 13
rkuo2000 0:72afeb131817 14 if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
rkuo2000 0:72afeb131817 15 if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
rkuo2000 0:72afeb131817 16 dy <<= 1; // dy is now 2*dy
rkuo2000 0:72afeb131817 17 dx <<= 1; // dx is now 2*dx
rkuo2000 0:72afeb131817 18
rkuo2000 0:72afeb131817 19 LCD_2D.drawPixel(x1,y1, fgColor, bgColor);
rkuo2000 0:72afeb131817 20 if (dx > dy)
rkuo2000 0:72afeb131817 21 {
rkuo2000 0:72afeb131817 22 int fraction = dy - (dx >> 1); // same as 2*dy - dx
rkuo2000 0:72afeb131817 23 while (x1 != x2)
rkuo2000 0:72afeb131817 24 {
rkuo2000 0:72afeb131817 25 if (fraction >= 0)
rkuo2000 0:72afeb131817 26 {
rkuo2000 0:72afeb131817 27 y1 += stepy;
rkuo2000 0:72afeb131817 28 fraction -= dx; // same as fraction -= 2*dx
rkuo2000 0:72afeb131817 29 }
rkuo2000 0:72afeb131817 30 x1 += stepx;
rkuo2000 0:72afeb131817 31 fraction += dy; // same as fraction -= 2*dy
rkuo2000 0:72afeb131817 32 LCD_2D.drawPixel(x1, y1, fgColor, bgColor);
rkuo2000 0:72afeb131817 33 }
rkuo2000 0:72afeb131817 34 } else {
rkuo2000 0:72afeb131817 35 int fraction = dx - (dy >> 1);
rkuo2000 0:72afeb131817 36 while (y1 != y2) {
rkuo2000 0:72afeb131817 37 if (fraction >= 0) {
rkuo2000 0:72afeb131817 38 x1 += stepx;
rkuo2000 0:72afeb131817 39 fraction -= dy;
rkuo2000 0:72afeb131817 40 }
rkuo2000 0:72afeb131817 41 y1 += stepy;
rkuo2000 0:72afeb131817 42 fraction += dx;
rkuo2000 0:72afeb131817 43 LCD_2D.drawPixel(x1, y1, fgColor, bgColor);
rkuo2000 0:72afeb131817 44 }
rkuo2000 0:72afeb131817 45 }
rkuo2000 0:72afeb131817 46 }
rkuo2000 0:72afeb131817 47
rkuo2000 0:72afeb131817 48 // draw Bresenham Circle
rkuo2000 0:72afeb131817 49 void Draw2D::drawCircle(int xc, int yc, int r, int fgColor, int bgColor)
rkuo2000 0:72afeb131817 50 {
rkuo2000 0:72afeb131817 51 int x = 0;
rkuo2000 0:72afeb131817 52 int y = r;
rkuo2000 0:72afeb131817 53 int p = 3 - 2 * r;
rkuo2000 0:72afeb131817 54 if (!r) return;
rkuo2000 0:72afeb131817 55 while (y >= x) // only formulate 1/8 of circle
rkuo2000 0:72afeb131817 56 {
rkuo2000 0:72afeb131817 57 LCD_2D.drawPixel(xc-x, yc-y, fgColor, bgColor);//upper left left
rkuo2000 0:72afeb131817 58 LCD_2D.drawPixel(xc-y, yc-x, fgColor, bgColor);//upper upper left
rkuo2000 0:72afeb131817 59 LCD_2D.drawPixel(xc+y, yc-x, fgColor, bgColor);//upper upper right
rkuo2000 0:72afeb131817 60 LCD_2D.drawPixel(xc+x, yc-y, fgColor, bgColor);//upper right right
rkuo2000 0:72afeb131817 61 LCD_2D.drawPixel(xc-x, yc+y, fgColor, bgColor);//lower left left
rkuo2000 0:72afeb131817 62 LCD_2D.drawPixel(xc-y, yc+x, fgColor, bgColor);//lower lower left
rkuo2000 0:72afeb131817 63 LCD_2D.drawPixel(xc+y, yc+x, fgColor, bgColor);//lower lower right
rkuo2000 0:72afeb131817 64 LCD_2D.drawPixel(xc+x, yc+y, fgColor, bgColor);//lower right right
rkuo2000 0:72afeb131817 65 if (p < 0) p += 4*(x++) + 6;
rkuo2000 0:72afeb131817 66 else p += 4*((x++) - y--) + 10;
rkuo2000 0:72afeb131817 67 }
rkuo2000 0:72afeb131817 68 }
rkuo2000 0:72afeb131817 69
rkuo2000 0:72afeb131817 70 void Draw2D::drawRectangle(int x0, int y0, int x1, int y1, int fgColor, int bgColor)
rkuo2000 0:72afeb131817 71 {
rkuo2000 0:72afeb131817 72 int x,y, tmp;
rkuo2000 0:72afeb131817 73 if (x0>x1) { tmp = x1; x1 = x0; x0 = tmp; }
rkuo2000 0:72afeb131817 74 if (y0>y1) { tmp = y1; y1 = y0; y0 = tmp; }
rkuo2000 0:72afeb131817 75 for (x=x0; x<=x1; x++) LCD_2D.drawPixel(x,y0,fgColor, bgColor);
rkuo2000 0:72afeb131817 76 for (y=y0; y<=y1; y++) LCD_2D.drawPixel(x0,y,fgColor, bgColor);
rkuo2000 0:72afeb131817 77 for (x=x0; x<=x1; x++) LCD_2D.drawPixel(x,y1,fgColor, bgColor);
rkuo2000 0:72afeb131817 78 for (y=y0; y<=y1; y++) LCD_2D.drawPixel(x1,y,fgColor, bgColor);
rkuo2000 0:72afeb131817 79 }
rkuo2000 0:72afeb131817 80
rkuo2000 0:72afeb131817 81 void Draw2D::drawTriangle(int x0, int y0, int x1, int y1, int x2, int y2, int fgColor, int bgColor)
rkuo2000 0:72afeb131817 82 {
rkuo2000 0:72afeb131817 83 drawLine(x0, y0, x1, y1, fgColor, bgColor);
rkuo2000 0:72afeb131817 84 drawLine(x1, y1, x2, y2, fgColor, bgColor);
rkuo2000 0:72afeb131817 85 drawLine(x0, y0, x2, y2, fgColor, bgColor);
rkuo2000 0:72afeb131817 86 }
rkuo2000 0:72afeb131817 87