Richard Kuo
/
NTOUEE-mbed-I2C_LCD_movingcircle
mbed I2C to LCD for drawing a moving circle
main.cpp@0:5e40d147a8e1, 2016-10-20 (annotated)
- Committer:
- rkuo2000
- Date:
- Thu Oct 20 13:05:10 2016 +0000
- Revision:
- 0:5e40d147a8e1
mbed I2C to LCD for drawing a moving circle
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rkuo2000 | 0:5e40d147a8e1 | 1 | // using NuMaker-PFM-NUC472 I2C1 to draw a moving circle on LCD |
rkuo2000 | 0:5e40d147a8e1 | 2 | #include "mbed.h" |
rkuo2000 | 0:5e40d147a8e1 | 3 | #include "math.h" |
rkuo2000 | 0:5e40d147a8e1 | 4 | #include "ssd1306.h" |
rkuo2000 | 0:5e40d147a8e1 | 5 | #include "draw2D.h" |
rkuo2000 | 0:5e40d147a8e1 | 6 | |
rkuo2000 | 0:5e40d147a8e1 | 7 | #define PI 3.1415926535 |
rkuo2000 | 0:5e40d147a8e1 | 8 | |
rkuo2000 | 0:5e40d147a8e1 | 9 | #define X0 15 // Circle initial X |
rkuo2000 | 0:5e40d147a8e1 | 10 | #define Y0 10 // Circle initial Y |
rkuo2000 | 0:5e40d147a8e1 | 11 | |
rkuo2000 | 0:5e40d147a8e1 | 12 | I2C i2c1(PD_12, PD_10); // I2C1_SDA, I2C1_SCL |
rkuo2000 | 0:5e40d147a8e1 | 13 | |
rkuo2000 | 0:5e40d147a8e1 | 14 | SSD1306 LCD; // LCD connected on I2C1 |
rkuo2000 | 0:5e40d147a8e1 | 15 | Draw2D D2D; // Draw2D library |
rkuo2000 | 0:5e40d147a8e1 | 16 | |
rkuo2000 | 0:5e40d147a8e1 | 17 | int main() { |
rkuo2000 | 0:5e40d147a8e1 | 18 | |
rkuo2000 | 0:5e40d147a8e1 | 19 | int dirX, dirY; |
rkuo2000 | 0:5e40d147a8e1 | 20 | int movX, movY; |
rkuo2000 | 0:5e40d147a8e1 | 21 | int r; |
rkuo2000 | 0:5e40d147a8e1 | 22 | int x, y; |
rkuo2000 | 0:5e40d147a8e1 | 23 | |
rkuo2000 | 0:5e40d147a8e1 | 24 | i2c1.frequency(400000); |
rkuo2000 | 0:5e40d147a8e1 | 25 | |
rkuo2000 | 0:5e40d147a8e1 | 26 | LCD.initialize(); |
rkuo2000 | 0:5e40d147a8e1 | 27 | LCD.clearscreen(); |
rkuo2000 | 0:5e40d147a8e1 | 28 | |
rkuo2000 | 0:5e40d147a8e1 | 29 | x = X0; // circle center x |
rkuo2000 | 0:5e40d147a8e1 | 30 | y = Y0; // circle center y |
rkuo2000 | 0:5e40d147a8e1 | 31 | r = 3; // circle radius |
rkuo2000 | 0:5e40d147a8e1 | 32 | |
rkuo2000 | 0:5e40d147a8e1 | 33 | movX = 3; // x movement |
rkuo2000 | 0:5e40d147a8e1 | 34 | movY = 3; // y movement |
rkuo2000 | 0:5e40d147a8e1 | 35 | dirX = 1; // x direction |
rkuo2000 | 0:5e40d147a8e1 | 36 | dirY = 1; // y direction |
rkuo2000 | 0:5e40d147a8e1 | 37 | |
rkuo2000 | 0:5e40d147a8e1 | 38 | while(true) { |
rkuo2000 | 0:5e40d147a8e1 | 39 | D2D.drawCircle(x, y, r, FG_COLOR, BG_COLOR); // draw a circle |
rkuo2000 | 0:5e40d147a8e1 | 40 | |
rkuo2000 | 0:5e40d147a8e1 | 41 | Thread::wait(1); // Delay for Vision |
rkuo2000 | 0:5e40d147a8e1 | 42 | |
rkuo2000 | 0:5e40d147a8e1 | 43 | D2D.drawCircle(x, y, r, BG_COLOR, BG_COLOR); // erase a circle |
rkuo2000 | 0:5e40d147a8e1 | 44 | |
rkuo2000 | 0:5e40d147a8e1 | 45 | x = x + dirX * movX; // change x of circle center |
rkuo2000 | 0:5e40d147a8e1 | 46 | y = y + dirY * movY; // change y of circle center |
rkuo2000 | 0:5e40d147a8e1 | 47 | |
rkuo2000 | 0:5e40d147a8e1 | 48 | // boundary check for changing direction |
rkuo2000 | 0:5e40d147a8e1 | 49 | if ((x-r) <=0) {dirX= 1; x= x + dirX*movX;} |
rkuo2000 | 0:5e40d147a8e1 | 50 | else if ((x+r) >=LCD_Xmax) {dirX=-1; x= x + dirX*movX;} |
rkuo2000 | 0:5e40d147a8e1 | 51 | |
rkuo2000 | 0:5e40d147a8e1 | 52 | if ((y-r) <=0) {dirY= 1; y= y + dirY*movY;} |
rkuo2000 | 0:5e40d147a8e1 | 53 | else if ((y+r) >=LCD_Ymax) {dirY=-1; y= y + dirY*movY;} |
rkuo2000 | 0:5e40d147a8e1 | 54 | } |
rkuo2000 | 0:5e40d147a8e1 | 55 | } |