tim008 tim008 / Mbed 2 deprecated LV08Z1_Tim008_Z1

Dependencies:   TSI mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TSISensor.h"
00003 #include <string>
00004 
00005 DigitalOut myled(LED1);
00006 
00007 Serial pc(USBTX, USBRX);
00008 
00009 PwmOut led1(LED1);
00010 PwmOut led2(LED2);
00011 PwmOut led3(LED3);
00012 
00013 string input;
00014 
00015 Ticker ticker;
00016 
00017 TSISensor tsi;
00018 
00019 void displayTSI()
00020 {
00021     pc.printf("%f\n",tsi.readPercentage());
00022 }   
00023 
00024 void turnOnLed(char color, int ipct)
00025 {
00026     if(color!='R' && color!='G' && color!='B')
00027         return;
00028     pc.printf("%d\n",ipct);   
00029     led1=1;
00030     led2=1;
00031     led3=1;
00032     float pct=float(ipct)/100;
00033     
00034     if(color=='R')
00035         led1=1-pct;
00036     else if(color=='G')
00037         led2=1-pct;
00038     else
00039         led3=1-pct;
00040 }
00041 
00042 void readFromPC()
00043 {
00044     while(pc.readable())
00045     {
00046         input.push_back(pc.getc());
00047     }
00048 }
00049 
00050 void clearBuffer()
00051 {
00052     input="";
00053 }
00054 
00055 void executeInstruction()
00056 {
00057     int pct=0;
00058     if(input.length()>3)
00059     {
00060         clearBuffer();
00061         return;
00062     }
00063     if(input.length()>=1)
00064     if(input[input.length()-1]==13)
00065     {
00066         clearBuffer();
00067         printf("\r");
00068     }
00069     if(input.length()==3){
00070     if((input[0]=='R' || input[0]=='G' || input[0]=='B') && input[1]>='0'&&input[1]<='9' && input[2]>='0'&&input[2]<='9')
00071     {
00072         pct=(input[1]-'0')*10+(input[2]-'0');
00073         turnOnLed(input[0],pct);
00074         clearBuffer();
00075          pc.printf("\n");
00076     }
00077     }
00078     else if(input.length()==1){
00079         if(input[0]=='P')
00080         {
00081             ticker.attach(displayTSI,1.0);
00082             pc.printf("\n");
00083             clearBuffer();
00084         }
00085         else if(input[0]=='Z')
00086         {
00087              ticker.detach();
00088              pc.printf("\n");
00089              clearBuffer();
00090         }
00091 
00092    }         
00093 }
00094 
00095 
00096 int main(){
00097     led1=1;
00098     led2=2;
00099     led3=3;
00100     while(1) {
00101         readFromPC();
00102         executeInstruction();
00103     }
00104 }