Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.26 KB | None | 0 0
  1. // **************************************************************************
  2. //
  3. //               Demo program for labs
  4. //
  5. // Subject:      Computer Architectures and Parallel systems
  6. // Author:       Petr Olivka, petr.olivka@vsb.cz, 08/2016
  7. // Organization: Department of Computer Science, FEECS,
  8. //               VSB-Technical University of Ostrava, CZ
  9. //
  10. // File:         Main program for LCD module
  11. //
  12. // **************************************************************************
  13.  
  14. #include "mbed.h"
  15. #include "lcd_lib.h"
  16. #include <font12x20_lsb.h>
  17. #include "FXOS8700Q.h"
  18. I2C i2c(PTE25,PTE24);
  19. //#include "lcd-simulator.hpp"
  20. //#include "fonts/font10x16_lsb.hpp"
  21.  
  22. // Serial line for printf output
  23. //Serial pc(USBTX, USBRX);
  24.  
  25. // two dimensional array with fixed size font
  26. extern uint8_t g_font8x8[256][8];
  27.  
  28. #define width 10
  29. #define height 16
  30.  
  31. struct Point2D
  32. {
  33.     int32_t x, y;
  34.     Point2D(int32_t x, int32_t y) : x(x), y(y) {
  35.     }
  36. };
  37.  
  38. struct RGB
  39. {
  40.     uint8_t r, g, b;
  41.     RGB(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {
  42.     }
  43. };
  44.  
  45. class GraphElement
  46. {
  47. public:
  48.     // foreground and background color
  49.     RGB fg_color, bg_color;
  50.  
  51.     // constructor
  52.     GraphElement(RGB t_fg_color, RGB t_bg_color) :
  53.         fg_color(t_fg_color), bg_color(t_bg_color) {}
  54.  
  55.     // ONLY ONE INTERFACE WITH LCD HARDWARE!!!
  56.     void drawPixel(int32_t t_x, int32_t t_y) { lcd_put_pixel(t_x, t_y, convert_RGB888_to_RGB565(fg_color)); }
  57.  
  58.     // Draw graphics element
  59.     virtual void draw() = 0;
  60.  
  61.     // Hide graphics element
  62.     virtual void hide() { swap_fg_bg_color(); draw(); swap_fg_bg_color(); }
  63.     void setColor(RGB c) { fg_color = c; }
  64. private:
  65.     // swap foreground and backgroud colors
  66.     void swap_fg_bg_color() { RGB l_tmp = fg_color; fg_color = bg_color; bg_color = l_tmp; }
  67.  
  68.     // conversion of 24-bit RGB color into 16-bit color format
  69.     int convert_RGB888_to_RGB565(RGB t_color) {
  70.         int red = (t_color.r >> 3) << 11;
  71.         int green = (t_color.g >> 2) << 5;
  72.         int blue = t_color.b >> 3;
  73.         return red | green | blue;
  74.     }
  75. };
  76.  
  77. class Pixel : public GraphElement
  78. {
  79. public:
  80.     // constructor
  81.     Pixel(Point2D t_pos, RGB t_fg_color, RGB t_bg_color) : pos(t_pos), GraphElement(t_fg_color, t_bg_color) {}
  82.     // Draw method implementation
  83.     virtual void draw() { drawPixel(pos.x, pos.y); }
  84.     // Position of Pixel
  85.     Point2D pos;
  86. };
  87.  
  88. class Character : public GraphElement
  89. {
  90. public:
  91.     // position of character
  92.     Point2D pos;
  93.     // character
  94.     char character;
  95.  
  96.     Character(Point2D t_pos, char t_char, RGB t_fg, RGB t_bg) :
  97.         pos(t_pos), character(t_char), GraphElement(t_fg, t_bg) {};
  98.  
  99.     void draw() {
  100.         for (int i = 0; i < height; i++) {
  101.             for (int j = 0; j < width; j++) {
  102.                 if (font[character][i] & (1 << j))
  103.                     drawPixel(j + pos.x, i + pos.y);
  104.                 //else
  105.                     //lcd_put_pixel(j + pos.x, i + pos.y, 0x001F);
  106.             }
  107.         }
  108.     }
  109.  
  110. };
  111.  
  112. class Circle : public GraphElement
  113. {
  114. public:
  115.     // Center of circle
  116.     Point2D center;
  117.     // Radius of circle
  118.     int32_t radius;
  119.  
  120.     Circle(Point2D t_center, int32_t t_radius, RGB t_fg, RGB t_bg) :
  121.         center(t_center), radius(t_radius), GraphElement(t_fg, t_bg) {};
  122.  
  123.     void draw() {
  124.         int f = 1 - radius;
  125.  
  126.         int x = 0;
  127.         int y = radius;
  128.  
  129.         int ddF_x = 0;
  130.         int ddF_y = -2 * radius;
  131.  
  132.         drawPixel(center.x, center.y + radius);
  133.         drawPixel(center.x, center.y - radius);
  134.         drawPixel(center.x + radius, center.y);
  135.         drawPixel(center.x - radius, center.y);
  136.  
  137.         while (x < y) {
  138.             if (f >= 0) {
  139.                 y--;
  140.                 ddF_y += 2;
  141.                 f += ddF_y;
  142.             }
  143.  
  144.             x++;
  145.             ddF_x += 2;
  146.             f += ddF_x + 1;
  147.             drawPixel(center.x + x, center.y + y);
  148.             drawPixel(center.x - x, center.y + y);
  149.             drawPixel(center.x + x, center.y - y);
  150.             drawPixel(center.x - x, center.y - y);
  151.             drawPixel(center.x + y, center.y + x);
  152.             drawPixel(center.x - y, center.y + x);
  153.             drawPixel(center.x + y, center.y - x);
  154.             drawPixel(center.x - y, center.y - x);
  155.         }
  156.     }
  157. };
  158.  
  159. class Line : public GraphElement
  160. {
  161. public:
  162.     // the first and the last point of line
  163.     Point2D pos1, pos2;
  164.  
  165.     Line(Point2D t_pos1, Point2D t_pos2, RGB t_fg, RGB t_bg) :
  166.         pos1(t_pos1), pos2(t_pos2), GraphElement(t_fg, t_bg) {}
  167.  
  168.     void draw() {
  169.         drawLine(pos1.x,pos1.y, pos2.x, pos2.y);
  170.     };
  171.  
  172. private:
  173.    void drawLine(int x1, int y1, int x2, int y2) {
  174.     int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i;
  175.     dx = x2-x1;
  176.     dy = y2-y1;
  177.  
  178.     dx1 = fabs(dx);
  179.     dy1 = fabs(dy);
  180.    
  181.     px=2 * dy1 - dx1;
  182.     py=2 * dx1 - dy1;
  183.  
  184.     if(dy1<=dx1) {
  185.         if(dx>=0) {
  186.             x=x1;
  187.             y=y1;
  188.             xe=x2;
  189.         } else {
  190.             x=x2;
  191.             y=y2;
  192.             xe=x1;
  193.         }
  194.         drawPixel(x, y);
  195.         for(i=0;x<xe;i++) {
  196.             x=x+1;
  197.             if(px<0) {
  198.                 px=px+2*dy1;
  199.             } else {
  200.                 if((dx<0 && dy<0) || (dx>0 && dy>0)) {
  201.                     y=y+1;
  202.                 } else {
  203.                     y=y-1;
  204.                 }
  205.                 px=px+2*(dy1-dx1);
  206.             }
  207.             drawPixel(x, y);
  208.         }
  209.      } else {
  210.          if(dy>=0) {
  211.              x=x1;
  212.              y=y1;
  213.              ye=y2;
  214.          } else {
  215.              x=x2;
  216.              y=y2;
  217.              ye=y1;
  218.          }
  219.          drawPixel(x, y);
  220.          for(i=0;y<ye;i++) {
  221.              y=y+1;
  222.              if(py<=0) {
  223.                  py=py+2*dx1;
  224.              } else {
  225.                  if((dx<0 && dy<0) || (dx>0 && dy>0)) {
  226.                      x=x+1;
  227.                  } else {
  228.                      x=x-1;
  229.                  }
  230.                  py=py+2*(dx1-dy1);
  231.              }
  232.              drawPixel(x, y);
  233.          }
  234.      }
  235. }
  236. };
  237.  
  238. #define LCD_WIDTH 320
  239. #define LCD_HEIGHT 240
  240.  
  241. Serial pc(USBTX, USBRX);
  242. int main(){
  243.     lcd_init();
  244.     lcd_clear();
  245.     pc.baud(115200);
  246.     FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
  247.  
  248.     motion_data_units_t acc_data;
  249.     float faX, faY, faZ;
  250.     acc.enable();
  251.  
  252.     struct Point2D p1(10, 10);
  253.     struct Point2D p2(310, 10);
  254.  
  255.     struct Point2D p3(310, 230);
  256.     struct Point2D p4(10, 230);
  257.  
  258.     struct Point2D p5(10, 120);
  259.     struct Point2D p6(310, 120);
  260.  
  261.     struct RGB fgColor(0, 255, 0);
  262.     struct RGB fgColor2(255, 0, 0);
  263.     struct RGB fgColor3(0, 0, 255);
  264.  
  265.     struct RGB bgColor(0, 0, 0);
  266.  
  267.     Line l1(p1, p2, fgColor, bgColor);
  268.     l1.draw();
  269.     Line l2(p2, p3, fgColor, bgColor);
  270.     l2.draw();
  271.  
  272.     Line l3(p4, p3, fgColor, bgColor);
  273.     l3.draw();
  274.     Line l4(p1, p4, fgColor, bgColor);
  275.     l4.draw();
  276.  
  277.     Line l5(p5, p6, fgColor, bgColor);
  278.     l5.draw();
  279.  
  280.     struct Point2D mid(10, 120);
  281.     struct Point2D endOfx(10, 120);
  282.  
  283.     struct Point2D mid2(10, 120);
  284.     struct Point2D endOfy(10, 120);
  285.  
  286.     struct Point2D mid3(10, 120);
  287.     struct Point2D endOfz(10, 120);
  288.  
  289.     while (1) {
  290.         Line lx(mid, endOfx, fgColor, bgColor);
  291.         Line lx2(mid2, endOfy, fgColor2, bgColor);
  292.         Line lx3(mid3, endOfz, fgColor3, bgColor);
  293.  
  294.         acc.getAxis(acc_data);
  295.         acc.getX(faX);
  296.         acc.getY(faY);
  297.         acc.getZ(faZ);
  298.  
  299.         endOfx.y += 100 * faX;
  300.         //endOfx.x += 10;
  301.  
  302.         endOfy.y += 100 * faY;
  303.         //endOfy.x += 10;
  304.  
  305.         endOfz.y += 100 * faZ;
  306.         //endOfz.x += 10;
  307.  
  308.         lx.draw();
  309.         lx2.draw();
  310.         //lx3.draw();
  311.  
  312.         mid = endOfx;
  313.         mid2 = endOfy;
  314.         mid3 = endOfz;
  315.  
  316.         endOfx.y += 100 * faX;
  317.         endOfx.x += 10;
  318.  
  319.         endOfy.y += 100 * faY;
  320.         endOfy.x += 10;
  321.  
  322.         endOfz.y += 100 * faZ;
  323.         endOfz.x += 10;
  324.  
  325.         wait(1);
  326.         //lx.hide();
  327.         //pc.printf("acc: %5.2f,%5.2f,%5.2f          \r",faX,faY,faZ);
  328.  
  329.  
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement