Martin Deng / Mbed 2 deprecated mbed_shapedriver

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
martydd3
Date:
Fri May 01 03:26:50 2015 +0000
Parent:
3:7e568908f1c4
Child:
5:495d64d8934d
Commit message:
Moving drawing code to functions

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
shape.cpp Show annotated file Show diff for this revision Revisions of this file
shape.h Show annotated file Show diff for this revision Revisions of this file
shape_drawer.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri May 01 01:43:57 2015 +0000
+++ b/main.cpp	Fri May 01 03:26:50 2015 +0000
@@ -264,67 +264,49 @@
     } 
     else if(display_mode == 2)
     {
-        // Eight sided star
-        Line line1(15, 0, 0, 15, 90, 7, &work_buffer);
-        Line line2(15, 90, 7, 15, 180, 0, &work_buffer);
-        Line line3(15, 180, 0, 15, 270, 7, &work_buffer);
-        Line line4(15, 270, 7, 15, 0, 0, &work_buffer);
-        line1.draw();
-        line2.draw();
-        line3.draw();
-        line4.draw();
+        Line line1(10, 10, 0, 10, -10, 0, &work_buffer);  
+        Line line2(10, -10, 0, -10, -10, 0, &work_buffer); 
+        Line line3(-10, -10, 0, -10, 10, 0, &work_buffer); 
+        Line line4(-10, 10, 0, 10, 10, 0, &work_buffer); 
+        
+        line1.draw(); 
+        line2.draw(); 
+        line3.draw(); 
+        line4.draw(); 
+        
+        Line line5(10, 10, 7, 10, -10, 7, &work_buffer);  
+        Line line6(10, -10, 7, -10, -10, 7, &work_buffer); 
+        Line line7(-10, -10, 7, -10, 10, 7, &work_buffer); 
+        Line line8(-10, 10, 7, 10, 10, 7, &work_buffer);
         
-        Line line5(15, 45, 0, 15, 135, 7, &work_buffer);
-        Line line6(15, 135, 7, 15, 225, 0, &work_buffer);
-        Line line7(15, 225, 0, 15, 315, 7, &work_buffer);
-        Line line8(15, 315, 7, 15, 45, 0, &work_buffer);
-        line5.draw();
-        line6.draw();
-        line7.draw();
+        line5.draw(); 
+        line6.draw(); 
+        line7.draw(); 
         line8.draw(); 
+        
+        Line line9(10, 10, 0, 10, 10, 7, &work_buffer); 
+        Line line10(10, -10, 0, 10, -10, 7, &work_buffer); 
+        Line line11(-10, -10, 0, -10, -10, 7, &work_buffer); 
+        Line line12(-10, 10, 0, -10, 10, 7, &work_buffer); 
+        
+        line9.draw();   
+        line10.draw(); 
+        line11.draw(); 
+        line12.draw(); 
     } 
     else if(display_mode == 3)
     {
-        Line line1(1, 0, 0, 1, 0, 7, &work_buffer);
-        Line line2(9, 35, 0, 9, 35, 7, &work_buffer);
-        Line line3(9, 325, 0, 9, 325, 7, &work_buffer);
-        Line line4(13, 0, 0, 13, 0, 7, &work_buffer);
-        
-        line1.draw();
-        line2.draw();
-        line3.draw();
-        line4.draw();
-    
-        Line line5(M_PI * 320 / 180, 0.777030, 0, 0, 35, 0, &work_buffer);
-        Line line6(M_PI * 40 / 180, 0.777030, 0, 0, 325, 0, &work_buffer);
-        Line line7(M_PI * 320 / 180, 8.787681, 325, 0, 0, 0, &work_buffer);
-        Line line8(M_PI * 40 / 180, 8.787681, 35, 0, 0, 0, &work_buffer);
-        
-        line5.draw();
-        line6.draw();  
-        line7.draw();
-        line8.draw();
-    
-        Line line9(M_PI * 320 / 180, 0.777030, 0, 7, 35, 7, &work_buffer);
-        Line line10(M_PI * 40 / 180, 0.777030, 0, 7, 325, 7, &work_buffer);
-        Line line11(M_PI * 320 / 180, 8.787681, 325, 7, 0, 7, &work_buffer);
-        Line line12(M_PI * 40 / 180, 8.787681, 35, 7, 0, 7, &work_buffer);
-        
-        line9.draw();
-        line10.draw();
-        line11.draw();
-        line12.draw();    
+          
     } else if(display_mode == 4){
-        CartLine line1(0, 16, 0, 0, -16, 7, &work_buffer);
-        CartLine line2(4, 16, 0, 4, -16, 7, &work_buffer);   
-        CartLine line3(8, 16, 0, 8, -16, 7, &work_buffer);
-        CartLine line4(12, 16, 0, 12, -16, 7, &work_buffer);
-        CartLine line5(-4, 16, 0, -4, -16, 7, &work_buffer);   
-        CartLine line6(-8, 16, 0, -8, -16, 7, &work_buffer);
-        CartLine line7(-12, 16, 0, -12, -16, 7, &work_buffer);
 
-        
-        
+        Line line1(0, 16, 0, 0, -16, 7, &work_buffer);
+        Line line2(4, 16, 0, 4, -16, 7, &work_buffer);   
+        Line line3(8, 16, 0, 8, -16, 7, &work_buffer);
+        Line line4(12, 16, 0, 12, -16, 7, &work_buffer);
+        Line line5(-4, 16, 0, -4, -16, 7, &work_buffer);   
+        Line line6(-8, 16, 0, -8, -16, 7, &work_buffer);
+        Line line7(-12, 16, 0, -12, -16, 7, &work_buffer);
+  
         line1.draw();
         line2.draw();
         line3.draw();
@@ -332,6 +314,22 @@
         line5.draw();
         line6.draw();
         line7.draw();
+
+        Line line11(16, 4, 0, -16, 4, 7, &work_buffer);
+        Line line21(16, 8, 0, -16, 8, 7, &work_buffer);   
+        Line line31(16, 12, 0, -16, 12, 7, &work_buffer);
+        Line line41(16, 0, 0, -16, 0, 7, &work_buffer);
+        Line line51(16, -4, 0, -16, -4, 7, &work_buffer);   
+        Line line61(16, -8, 0, -16, -8, 7, &work_buffer);
+        Line line71(16, -12, 0, -16, -12, 7, &work_buffer);
+
+        line11.draw();
+        line21.draw();
+        line31.draw();
+        line41.draw();  
+        line51.draw();
+        line61.draw();
+        line71.draw();
     }
     
     convert_array(); 
@@ -344,7 +342,7 @@
 
 int main (void)
 {
-    display_mode = 4;
+    display_mode = 2;
         
     InterruptIn hall_pin(p25);
     hall_pin.fall(&rotate_sense);
--- a/shape.cpp	Fri May 01 01:43:57 2015 +0000
+++ b/shape.cpp	Fri May 01 03:26:50 2015 +0000
@@ -3,6 +3,8 @@
 #include <math.h>
 #include "mbed.h"
 
+
+
 void Circle::draw(){
     uint8_t s = (0x01 << height);
     for(int i = 0; i < SLICES; i++){
@@ -36,249 +38,9 @@
     return radius < 0 || radius >= WIDTH;   
 }
 
-/*
-*********************************************************************************
-*/
-
-Line::Line(double _y_, double r_per_, int d1_, int h1_, int d2_, int h2_, char(*b)[SLICES][WIDTH]):
-    Shape(b), d1(d1_), d2(d2_), h1(h1_), h2(h2_), _y(_y_), r_per(r_per_)
-{
-    calculated = true;
-    
-    d1 = d1 < 0 ? d1 + SLICES : d1;
-    d2 = d2 < 0 ? d2 + SLICES : d2;
-    
-    d1 = d1 > SLICES ? d1 % SLICES : d1;
-    d2 = d2 > SLICES ? d2 % SLICES : d2;
-    
-    //r1 should always be ccw of r2
-    if((abs(d1 - d2) > SLICES / 2 && d1 < d2) || (abs(d1 - d2) < SLICES / 2 && d1 > d2)){
-        //d1 ccw of 0 degrees ( d1 > 180 ), d2 cw of 0 degrees, d1 > d2
-        
-        //d1 and d2 cw of 0 degrees, d1 < d2
-        int h_temp = h1;
-        int d_temp = d1;
-        
-        h1 = h2;
-        d1 = d2;
-        
-        h2 = h_temp;
-        d2 = d_temp;
-    }
-}
-
-Line::Line(int r1_, int d1_, int h1_, int r2_, int d2_, int h2_, char(*b)[SLICES][WIDTH]): 
-    Shape(b), r1(r1_), r2(r2_), d1(d1_), d2(d2_), h1(h1_), h2(h2_)
-{
-    calculated = false;
-    // Adjust all variables to be within display limits
-    r1 = r1 >= WIDTH ? WIDTH - 1 : r1;
-    r2 = r2 >= WIDTH ? WIDTH - 1 : r2;
-    
-    if(r1 < 0){
-        r1 = -r1;
-        d1 += SLICES / 2;    
-    }
-    
-    if(r2 < 0){
-        r2 = -r2;
-        d2 += SLICES / 2;
-    }         
-    
-    d1 = d1 < 0 ? d1 + SLICES : d1;
-    d2 = d2 < 0 ? d2 + SLICES : d2;
-    
-    d1 = d1 > SLICES ? d1 % SLICES : d1;
-    d2 = d2 > SLICES ? d2 % SLICES : d2;
-    
-    h1 = h1 >= HEIGHT ? HEIGHT - 1 : h1;
-    h2 = h2 >= HEIGHT ? HEIGHT - 1 : h2;
-    
-    h1 = h1 < 0 ? 0 : h1;
-    h2 = h2 < 0 ? 0 : h2;
-    
-    //r1 should always be ccw of r2
-    if((abs(d1 - d2) > SLICES / 2 && d1 < d2) || (abs(d1 - d2) < SLICES / 2 && d1 > d2)){
-        //d1 ccw of 0 degrees ( d1 > 180 ), d2 cw of 0 degrees, d1 > d2
-        
-        //d1 and d2 cw of 0 degrees, d1 < d2
-
-        int r_temp = r1;
-        int h_temp = h1;
-        int d_temp = d1;
-        
-        r1 = r2;
-        h1 = h2;
-        d1 = d2;
-        
-        r2 = r_temp;
-        h2 = h_temp;
-        d2 = d_temp;
-    }
-}
-
-void Line::draw(){
-    
-    if(r1 == r2 && d1 == d2)
-    {
-        //Vertical Line
-        
-        int i = h1;
-        int di = (h1 < h2 ? 1 : -1);
-        
-        while(i != h2){
-            (*buffer)[d1][r1] |= (0x01 << i);
-            i += di;    
-        }    
-    }
-    else if(d1 == d2)
-    {
-        //Radial Line on same side of axle
-        double m = (1.0*h2 - 1.0*h1)/(1.0*r2 - 1.0*r1);
-        
-        int i = r1;
-        int di = (r1 < r2 ? 1 : -1);
-        
-        //loop from r1 to r2
-        while(i != r2){
-            int h = h1 + (int)rint(m*(i - r1));
-            (*buffer)[d1][i] |= (0x01 << h); 
-            i += di;   
-        }     
-    }
-    if((d2 + SLICES / 2) % SLICES == d1)
-    {
-        //Radial Line on opposite sides of axel
-        double m = (1.0*h2 - 1.0*h1)/(1.0*r2 + 1.0*r1);
-        
-        for(int i = -r1; i < r2 + 1; i++){
-            int h = h1 + (int)rint(m*(i + r1));
-            
-            if(i <= 0){
-                (*buffer)[d1][-i] |= (0x01 << h);  
-            } 
-            
-            if(i >= 0){
-                (*buffer)[d2][i] |= (0x01 << h);   
-            }
-        }
-    } 
-    else 
-    {
-        double m = 0;
-        double _d_r1 = 0;
-        if(!calculated){
-            //All variables precceded by _ are in radian measures
-            
-            //Convert all angles to radian measures
-            double _d1 = d1*M_PI/180;
-            double _d2 = d2*M_PI/180;
-            double _dp = abs(_d2 - _d1);
-            
-            _dp = _dp > M_PI ? abs(_dp - 2*M_PI) : _dp;
-            
-            //length of line opposite of central angle
-            double r_op = sqrt(1.0*r1*r1 + 1.0*r2*r2 - 2.0*r1*r2*cos(_dp));  
-            
-            //angle between rays and r_op
-            double _d_r1 = asin(1.0*r2*sin(_dp)/r_op);
-            double _d_r2 = asin(1.0*r1*sin(_dp)/r_op);
-    
-            _d_r1 = _d_r1 < 0 ? M_PI / 2 - _d_r1 : _d_r1;
-            _d_r2 = _d_r2 < 0 ? M_PI / 2 - _d_r2 : _d_r2;
-    
-            if((_d_r1 + _d_r2 + _dp) < M_PI/2){
-                if(r2 > r1)
-                    _d_r1 += M_PI / 2;
-                else
-                    _d_r2 += M_PI / 2;    
-            } 
-            
-            //length of ray perpendicular to opposite line
-            r_per = r1*sin(_d_r1);
-            
-            //angle of ray perpendicular to opposite line
-            // since r1 is ccw of r2, y is between d1 and d2 if d_r1 < 90 degrees and 
-            // to left of d1 if _d_r1 > 90 degrees
-            
-            //if d_r1 < 90 degrees, y = 90 - d_r1
-            //if d_r1 > 90 degrees, y = d_r1 - 90
-            _y = _d_r1 < M_PI / 2 ? (M_PI / 2 - _d_r1) : (_d_r1 - M_PI / 2);
-            
-            //calculate displacement from d1
-            //if d_r1 < 90, y = d1 + displacement
-            //if d_r1 > 90, y = d1 - displacement
-            _y = _d_r1 < M_PI / 2 ? _d1 + _y : _d1 - _y;
-            
-            //renormalize _y
-            _y = _y < 0 ? _y + 2 * M_PI : _y;
-            _y = _y > 2 * M_PI ? _y - 2 * M_PI : _y;     
-            
-            //vertical slope of line
-            m = (h2 - h1)/r_op;    
-        } else {
-            m = 0;
-        }
-         
-        
-        //decide which direction to iterate from d1
-        int i = d1;
-        int di;
-        if(abs(d1 - d2) > SLICES / 2){
-            di = d1 > d2 ? 1 : -1;
-        } else {
-            di = d1 > d2 ? -1 : 1;    
-        }
-        int j = 0;
-    
-        while(i != d2){
-            
-            //current angle
-            double _d_i = i * 2 * M_PI / SLICES;
-            
-            //angle tranveled
-            double _dd_i = j * 2 * M_PI / SLICES; 
-            
-            double r = (r_per / cos(abs(_d_i - _y))) ;
-            
-            //distance traveled along line
-            double h;
-            if(!calculated){
-                double r_par = abs(r * sin(_dd_i)/sin(_d_r1));
-                h = h1 + m * r_par;
-            } else {
-                h = h1;    
-            }
-            //if r < 0, actual line 180 degrees away
-            if(r > 0 && r < WIDTH)
-                (*buffer)[i][(int)rint(r)] |= (0x01 << (int)rint(h));
-            else if(r < 0 && r < WIDTH)
-                (*buffer)[(i + SLICES / 2)%SLICES][(int)rint(-r)] |= (0x01 << (int)rint(h));
-            
-            i += di;
-            if(i == SLICES)
-                i = 0;   
-                
-            if(i == -1)
-                i += SLICES;
-                
-            j++; 
-        }
-    }
-}
-
-bool Line::remove(){
-    return  r1 < 0 || r1 >= WIDTH   || 
-            r2 < 0 || r2 >= WIDTH   ||
-            h1 < 0 || h1 >= HEIGHT  ||
-            h2 < 0 || h2 >= HEIGHT  ||
-            d1 < 0 || d1 >= SLICES  ||
-            d2 < 0 || d2 >= SLICES;
-}
-
 /*********************************************************************/
 
-//coordinates in CartLine are cartesian
+//coordinates in Line are cartesian
 //on display, 0 - 90 degrees is 1st quadrant, 180 to 270 is 3rd
 
 //Lines in polar coordinates have 2 types, through the origin and displaced
@@ -287,7 +49,7 @@
 
 //For line y = mx + b, r sin p = m r cos p + b, r = b / (sin p - m cos p)
 
-void CartLine::draw(){
+void Line::draw(){
     //Perfectly vertical lines
     if(x1 == x2 && y1 == y2){
         float r = sqrt(1.0*x1*x1 + 1.0*y1*y1);
@@ -453,16 +215,21 @@
         float m_h = (z2 - z1)/d;
         float m, p1, p2, b, h;
         
+        //x = m_2 y + b is steeper than y = m_1 x + b
         if(abs(m_2) > abs(m_1)){
-            m = m_2;
+            m = m_1;
             p1 = atan2(y1, x1);
             p2 = atan2(y2, x2);
             
+            p1 = p1 < 0 ? p1 + 2 * M_PI : p1;
+            p2 = p2 < 0 ? p2 + 2 * M_PI : p2;
+            
             b = y1 - m * x1;
             h = z1;
             
-            int i1 = (int)rint(p1) * SLICES / (2 * M_PI);
-            int i2 = (int)rint(p2) * SLICES / (2 * M_PI);
+            int i1 = (int)rint(p1 * SLICES / (2 * M_PI));
+            int i2 = (int)rint(p2 * SLICES / (2 * M_PI));
+            
             while(true){
                 float p = i1 * 2 * M_PI / SLICES;
                 float r = b / (sin(p) - m * cos(p));
@@ -491,15 +258,19 @@
                     i1 = SLICES - 1;
             }
         } else {
-            m = m_1;
+            m = m_2;
             p1 = atan2(y1, x1);
             p2 = atan2(y2, x2);
             
+            p1 = p1 < 0 ? p1 + 2 * M_PI : p1;
+            p2 = p2 < 0 ? p2 + 2 * M_PI : p2;
+            
             b = x1 - m * y1;
             h = z1;
             
-            int i1 = (int)rint(p1) * SLICES / (2 * M_PI);
-            int i2 = (int)rint(p2) * SLICES / (2 * M_PI);
+            int i1 = (int)rint(p1 * SLICES / (2 * M_PI));
+            int i2 = (int)rint(p2 * SLICES / (2 * M_PI));
+            
             while(true){
                 float p = i1 * 2 * M_PI / SLICES;
                 float r = b / (cos(p) - m * sin(p));
@@ -531,6 +302,6 @@
     } 
 }
 
-bool CartLine::remove(){
+bool Line::remove(){
     return true;
 }
\ No newline at end of file
--- a/shape.h	Fri May 01 01:43:57 2015 +0000
+++ b/shape.h	Fri May 01 03:26:50 2015 +0000
@@ -34,21 +34,9 @@
 
 class Line: public Shape {
     protected:
-        int r1, d1, h1, r2, d2, h2;
-        double _y, r_per;
-        bool calculated;
-    public:
-        Line(int r1_, int d1_, int h1_, int r2_, int d2_, int h2_, char(*b)[SLICES][WIDTH]);
-        Line(double _y_, double r_per_, int d1_, int h1_, int d2_, int h2_, char(*b)[SLICES][WIDTH]);
-        void draw();
-        bool remove();
-};
-
-class CartLine: public Shape {
-    protected:
         float x1, y1, z1, x2, y2, z2;
     public:
-        CartLine(float x1, float y1, float z1, float x2, float y2, float z2, char(*b)[SLICES][WIDTH]):
+        Line(float x1, float y1, float z1, float x2, float y2, float z2, char(*b)[SLICES][WIDTH]):
             Shape(b), x1(x1), y1(y1), z1(z1), x2(x2), y2(y2), z2(z2) {};
         void draw();
         bool remove();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/shape_drawer.cpp	Fri May 01 03:26:50 2015 +0000
@@ -0,0 +1,3 @@
+#include "constants.h"
+
+static char 
\ No newline at end of file