Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mbed.h"
- Serial pc( USBTX, USBRX );
- #include "lcd_lib.h"
- #include "graph_lib.h"
- // two dimensional array with fixed size font
- extern uint8_t font8x8[ 256 ][ 8 ];
- int main()
- {
- LCD_init(); // LCD initialization
- LCD_clear(); // LCD clear screen
- Circle p(100, 100, color_red, 50);
- p.draw();
- Line l(0, 80, 100, 100, color_green);
- Line l1(0,100, 100, color_white, 0);
- Rectangle r1(50, 100, 60, 0, color_white, false);
- r1.draw();
- Char c0(150, 50, color_red, 'A', 8);
- l.draw();
- l1.draw();
- c0.draw();
- return 0;
- }
- *
- * graph_lib.h
- *
- * Created on: Oct 11, 2018
- * Author: siv0017
- */
- #ifndef GRAPH_LIB_H_
- #define GRAPH_LIB_H_
- #include "mbed.h"
- #include "lcd_lib.h"
- //Serial pc( USBTX, USBRX );
- extern uint8_t font8x8[256][8];
- int color_red = 0xF800;
- int color_green = 0x07E0;
- int color_blue = 0x001F;
- int color_white = 0xFFFF;
- int color_black = 0x0000;
- class Pixel
- {
- public:
- int x, y;
- int colour;
- public:
- int getY() { return y; }
- int getX() { return x; }
- Pixel(int xx, int yy, int colour) : x(xx), y(yy), colour(colour) {}
- };
- class Circle : Pixel
- {
- public:
- int radius;
- public:
- Circle(int x, int y, int colour, int radius) : Pixel(x, y, colour)
- {
- this->radius = radius;
- }
- virtual void draw()
- {
- int xx = radius - 1;
- int yy = 0;
- int dx = 1;
- int dy = 1;
- int err = dx - (radius << 1);
- while (xx >= yy)
- {
- LCD_put_pixel(x + xx, y + yy, colour);
- LCD_put_pixel(x + yy, y + xx, colour);
- LCD_put_pixel(x - yy, y + xx, colour);
- LCD_put_pixel(x - xx, y + yy, colour);
- LCD_put_pixel(x - xx, y - yy, colour);
- LCD_put_pixel(x - yy, y - xx, colour);
- LCD_put_pixel(x + yy, y - xx, colour);
- LCD_put_pixel(x + xx, y - yy, colour);
- if (err <= 0)
- {
- yy++;
- err += dy;
- dy += 2;
- }
- if (err > 0)
- {
- xx--;
- dx += 2;
- err += dx - (radius << 1);
- }
- }
- }
- };
- class Line : Pixel
- {
- public:
- int x1, y1;
- public:
- Line(int x, int y, int x1, int y1, int colour) : Pixel(x, y, colour)
- {
- this->x1 = x1;
- this->y1 = y1;
- }
- virtual void draw()
- {
- int dx = x1 - x;
- int dy = y1 - y;
- int yi = 1;
- if (dy < 0)
- {
- yi = -1;
- dy = -dy;
- }
- int D = 2 * dy - dx;
- int _y = y;
- for (int _x = x; _x <= x1; _x++)
- {
- LCD_put_pixel(_x, _y, colour);
- if (D > 0)
- {
- _y = _y + yi;
- D = D - 2 * dx;
- }
- D = D + 2 * dy;
- }
- }
- };
- // two dimensional array with fixed size font
- class Char : public Pixel
- {
- public:
- char ch;
- int offset;
- public:
- Char(int x, int y, int colour, char ch, int offset) : Pixel(x, y, colour)
- {
- this->ch = ch;
- this->offset = offset;
- }
- virtual void draw()
- {
- int size = offset *8;
- for (int yy = 0; yy < 8; yy++)
- {
- int xx = x;
- for (int jj = 0; jj < 8; jj++) {
- if (font8x8[ch][yy] & (1 << jj)) {
- for(int k = 0; k < offset; k++) {
- for(int l = 0; l < offset; l++){
- LCD_put_pixel((yy*offset)+k + x, (offset*jj)+l +y, colour);
- }
- }
- LCD_put_pixel((yy*offset) + x, (offset*jj) +y, colour);
- }
- else {
- LCD_put_pixel((yy*offset) + x, (offset*jj)+y, color_black);
- }
- }
- }
- }
- };
- class Rectangle : Pixel
- {
- public:
- int xx, yy;
- bool fill;
- public:
- Rectangle(int x, int y, int xx, int yy, int colour,bool fill ) : Pixel(x, y, colour)
- {
- this->xx = xx;
- this->yy = yy;
- this-> fill = fill;
- }
- virtual void draw(){
- if (x > xx) {
- int tmp = x;
- x = xx;
- xx = tmp;
- }
- if (y > yy) {
- int tmp = y;
- y = yy;
- yy = tmp;
- }
- for (int i = x; i <= xx; i++) {
- LCD_put_pixel(i, y, colour);
- LCD_put_pixel(i, yy, colour);
- }
- for (int i = y; i <= yy; i++) {
- LCD_put_pixel(x, i, colour);
- LCD_put_pixel(xx, i, colour);
- }
- if(fill == true)
- {
- for (int i = y; i <yy; i++) {
- for (int j = x; j <xx; j++) {
- LCD_put_pixel(j, i, colour);
- }
- }
- }
- }
- };
- #endif /* GRAPH_LIB_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement