Kiko Ishimoto / Mbed 2 deprecated Nucleo_Propo

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 PwmOut mypwm(PWM_OUT);
00004 
00005 DigitalOut myled(LED1);
00006 //InterruptIn Propo(A5);
00007 
00008 class propoRead
00009 {
00010     public:
00011 
00012     float High , Sum;
00013     float Max,Min,Normal,outMax,outMin;
00014     propoRead(PinName Pin , Timer *T) : Inter(Pin)
00015     {
00016         t = T;
00017         Inter.rise(this,&propoRead::Rise);
00018         Inter.fall(this,&propoRead::Fall);
00019         t->start();
00020         propoCalibration();
00021         outMax = 1;
00022         outMin = -1;
00023     }
00024     void propoCalibration(float max = 1900,float min = 1100,int normal = 1500)
00025     {
00026         Max = max-normal;
00027         Min = min-normal;
00028         Normal = normal;
00029     }
00030     operator float()
00031     {
00032         float width = Max-Min;
00033         float outWidth = outMax - outMin;
00034         float ratio = outWidth / width;
00035         
00036         float ans = ratio * (High - Normal);// outMin;
00037         if(fabs((double)ans)<0.1)    ans=0;
00038         return ans;
00039     }
00040     private:
00041     
00042     Timer *t;
00043     float prevTime;
00044     float readTime()
00045     {
00046         float ans = t->read_us() - prevTime;
00047         prevTime = t->read_us();
00048         return ans;
00049     }
00050     void Fall()
00051     {
00052         High = readTime();
00053     }
00054     void Rise()
00055     {
00056         prevTime = t->read_us();
00057     }
00058     InterruptIn Inter;
00059 };
00060 int main() {
00061     Timer T;
00062     propoRead p1(A5,&T);
00063     /*
00064     mypwm.period_ms(10);
00065     mypwm.pulsewidth_ms(1);
00066     */
00067     //printf("pwm set to %.2f %%\n", mypwm.read() * 100);
00068 
00069     //t.start();
00070     while(1) {  
00071         printf("%f\r\n",(float)p1);
00072         //wait(1);
00073     }
00074 }