Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. #include "mbed.h"
  2.  
  3. Serial pc( USBTX, USBRX );
  4.  
  5. #include "lcd_lib.h"
  6.  
  7. #include "graph_lib.h"
  8.  
  9. // two dimensional array with fixed size font
  10. extern uint8_t font8x8[ 256 ][ 8 ];
  11.  
  12. int main()
  13. {
  14.  
  15. LCD_init(); // LCD initialization
  16.  
  17. LCD_clear(); // LCD clear screen
  18.  
  19.  
  20. Circle p(100, 100, color_red, 50);
  21. p.draw();
  22.  
  23. Line l(0, 80, 100, 100, color_green);
  24. Line l1(0,100, 100, color_white, 0);
  25.  
  26. Rectangle r1(50, 100, 60, 0, color_white, false);
  27.  
  28. r1.draw();
  29.  
  30. Char c0(150, 50, color_red, 'A', 8);
  31.  
  32. l.draw();
  33.  
  34. l1.draw();
  35.  
  36. c0.draw();
  37.  
  38. return 0;
  39. }
  40.  
  41. *
  42. * graph_lib.h
  43. *
  44. * Created on: Oct 11, 2018
  45. * Author: siv0017
  46. */
  47.  
  48. #ifndef GRAPH_LIB_H_
  49. #define GRAPH_LIB_H_
  50. #include "mbed.h"
  51. #include "lcd_lib.h"
  52.  
  53. //Serial pc( USBTX, USBRX );
  54.  
  55.  
  56. extern uint8_t font8x8[256][8];
  57.  
  58.  
  59.  
  60. int color_red = 0xF800;
  61. int color_green = 0x07E0;
  62. int color_blue = 0x001F;
  63. int color_white = 0xFFFF;
  64. int color_black = 0x0000;
  65.  
  66.  
  67. class Pixel
  68. {
  69. public:
  70. int x, y;
  71. int colour;
  72.  
  73.  
  74. public:
  75. int getY() { return y; }
  76. int getX() { return x; }
  77.  
  78. Pixel(int xx, int yy, int colour) : x(xx), y(yy), colour(colour) {}
  79.  
  80. };
  81.  
  82.  
  83.  
  84. class Circle : Pixel
  85. {
  86. public:
  87. int radius;
  88.  
  89. public:
  90. Circle(int x, int y, int colour, int radius) : Pixel(x, y, colour)
  91. {
  92. this->radius = radius;
  93. }
  94.  
  95.  
  96.  
  97. virtual void draw()
  98. {
  99. int xx = radius - 1;
  100. int yy = 0;
  101. int dx = 1;
  102. int dy = 1;
  103. int err = dx - (radius << 1);
  104.  
  105. while (xx >= yy)
  106. {
  107. LCD_put_pixel(x + xx, y + yy, colour);
  108. LCD_put_pixel(x + yy, y + xx, colour);
  109. LCD_put_pixel(x - yy, y + xx, colour);
  110. LCD_put_pixel(x - xx, y + yy, colour);
  111. LCD_put_pixel(x - xx, y - yy, colour);
  112. LCD_put_pixel(x - yy, y - xx, colour);
  113. LCD_put_pixel(x + yy, y - xx, colour);
  114. LCD_put_pixel(x + xx, y - yy, colour);
  115.  
  116. if (err <= 0)
  117. {
  118. yy++;
  119. err += dy;
  120. dy += 2;
  121. }
  122.  
  123.  
  124.  
  125. if (err > 0)
  126. {
  127. xx--;
  128. dx += 2;
  129. err += dx - (radius << 1);
  130. }
  131.  
  132. }
  133.  
  134. }
  135.  
  136. };
  137.  
  138.  
  139.  
  140. class Line : Pixel
  141. {
  142. public:
  143. int x1, y1;
  144.  
  145. public:
  146. Line(int x, int y, int x1, int y1, int colour) : Pixel(x, y, colour)
  147. {
  148. this->x1 = x1;
  149. this->y1 = y1;
  150. }
  151.  
  152.  
  153.  
  154. virtual void draw()
  155. {
  156. int dx = x1 - x;
  157. int dy = y1 - y;
  158. int yi = 1;
  159.  
  160. if (dy < 0)
  161. {
  162. yi = -1;
  163. dy = -dy;
  164. }
  165.  
  166. int D = 2 * dy - dx;
  167. int _y = y;
  168.  
  169. for (int _x = x; _x <= x1; _x++)
  170. {
  171. LCD_put_pixel(_x, _y, colour);
  172. if (D > 0)
  173. {
  174. _y = _y + yi;
  175. D = D - 2 * dx;
  176. }
  177.  
  178. D = D + 2 * dy;
  179. }
  180.  
  181. }
  182.  
  183.  
  184. };
  185.  
  186.  
  187.  
  188.  
  189.  
  190. // two dimensional array with fixed size font
  191.  
  192.  
  193. class Char : public Pixel
  194. {
  195. public:
  196. char ch;
  197. int offset;
  198.  
  199. public:
  200. Char(int x, int y, int colour, char ch, int offset) : Pixel(x, y, colour)
  201. {
  202. this->ch = ch;
  203. this->offset = offset;
  204. }
  205.  
  206. virtual void draw()
  207. {
  208. int size = offset *8;
  209.  
  210. for (int yy = 0; yy < 8; yy++)
  211. {
  212. int xx = x;
  213. for (int jj = 0; jj < 8; jj++) {
  214. if (font8x8[ch][yy] & (1 << jj)) {
  215. for(int k = 0; k < offset; k++) {
  216. for(int l = 0; l < offset; l++){
  217.  
  218. LCD_put_pixel((yy*offset)+k + x, (offset*jj)+l +y, colour);
  219. }
  220.  
  221.  
  222. }
  223.  
  224. LCD_put_pixel((yy*offset) + x, (offset*jj) +y, colour);
  225.  
  226. }
  227. else {
  228. LCD_put_pixel((yy*offset) + x, (offset*jj)+y, color_black);
  229. }
  230. }
  231.  
  232. }
  233. }
  234.  
  235. };
  236.  
  237. class Rectangle : Pixel
  238. {
  239. public:
  240. int xx, yy;
  241. bool fill;
  242.  
  243. public:
  244. Rectangle(int x, int y, int xx, int yy, int colour,bool fill ) : Pixel(x, y, colour)
  245. {
  246. this->xx = xx;
  247. this->yy = yy;
  248. this-> fill = fill;
  249. }
  250. virtual void draw(){
  251.  
  252. if (x > xx) {
  253. int tmp = x;
  254. x = xx;
  255. xx = tmp;
  256. }
  257.  
  258. if (y > yy) {
  259. int tmp = y;
  260. y = yy;
  261. yy = tmp;
  262. }
  263.  
  264. for (int i = x; i <= xx; i++) {
  265. LCD_put_pixel(i, y, colour);
  266. LCD_put_pixel(i, yy, colour);
  267.  
  268. }
  269.  
  270. for (int i = y; i <= yy; i++) {
  271. LCD_put_pixel(x, i, colour);
  272. LCD_put_pixel(xx, i, colour);
  273.  
  274. }
  275.  
  276. if(fill == true)
  277. {
  278. for (int i = y; i <yy; i++) {
  279. for (int j = x; j <xx; j++) {
  280. LCD_put_pixel(j, i, colour);
  281. }
  282.  
  283.  
  284. }
  285.  
  286. }
  287. }
  288.  
  289.  
  290.  
  291. };
  292.  
  293. #endif /* GRAPH_LIB_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement