Factory-written program for GR-PEACH

Dependencies:   SoftPWM mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SoftPWM.h"
00003 
00004 #define LED_ON      1
00005 #define LED_OFF     0
00006 
00007 #define TIME_10ms   1
00008 #define TIME_20ms   2
00009 #define TIME_30ms   3
00010 #define TIME_40ms   4
00011 #define TIME_50ms   5
00012 #define TIME_100ms  10
00013 #define TIME_200ms  20
00014 #define TIME_500ms  50
00015 
00016 static Ticker flipper;                                          // Tick Timer
00017 
00018 static DigitalOut ledu(LED_USER);                               // LED-User
00019 static SoftPWM ledr(LED_RED);                                   // LED-Red
00020 static SoftPWM ledg(LED_GREEN);                                 // LED-Green
00021 static SoftPWM ledb(LED_BLUE);                                  // LED-Blue
00022 
00023 static unsigned int syscnt_u;                                   // SystemCounter for LED-User
00024 static unsigned int syscnt_r;                                   // SystemCounter for LED-Red
00025 static unsigned int syscnt_g;                                   // SystemCounter for LED-Green
00026 static unsigned int syscnt_b;                                   // SystemCounter for LED-Blue
00027 
00028 volatile static unsigned int flg_ledu;                                   // LED-User Flag
00029 volatile static unsigned int flg_ledr;                                   // LED-Red Flag
00030 volatile static unsigned int flg_ledg;                                   // LED-Green Flag
00031 volatile static unsigned int flg_ledb;                                   // LED-Blue Flag
00032 
00033 void flip() {
00034     // Check 500ms for LED-User
00035     syscnt_u++;                                                 // increment SystemCounter for LED-User
00036     if( syscnt_u >= TIME_500ms ){
00037         flg_ledu++;
00038         syscnt_u = 0;
00039     }
00040 
00041     // Check 10ms for LED-Red
00042     syscnt_r++;                                                 // increment SystemCounter for LED-Red
00043     if( syscnt_r >= TIME_10ms ){
00044         flg_ledr++;
00045         syscnt_r = 0;
00046     }
00047 
00048     // Check 20ms for LED-Green
00049     syscnt_g++;                                                 // increment SystemCounter for LED-Green
00050     if( syscnt_g >= TIME_20ms ){
00051         flg_ledg++;
00052         syscnt_g = 0;
00053     }
00054 
00055     // Check 30ms for LED-Blue
00056     syscnt_b++;                                                 // increment SystemCounter for LED-Blue
00057     if( syscnt_b >= TIME_30ms ){
00058         flg_ledb++;
00059         syscnt_b = 0;
00060     }
00061 }
00062 
00063 int main() {
00064 
00065     ledu = LED_OFF;                                             // LED-User Off
00066 
00067     ledr.period_ms(10);                                         // Set PWM Period 10ms
00068     ledr = 0.0f;                                                // Set LED-Red Duty
00069 
00070     ledg.period_ms(10);                                         // Set PWM Period 10ms
00071     ledg = 0.0f;                                                // Set LED-Green Duty
00072 
00073     ledb.period_ms(10);                                         // Set PWM Period 10ms
00074     ledb = 0.0f;                                                // Set LED-Blue Duty
00075 
00076     flg_ledu = 0;                                               // Initialize LED-User Flag
00077     flg_ledr = 0;                                               // Initialize LED-Red Flag
00078     flg_ledg = 0;                                               // Initialize LED-Green Flag
00079     flg_ledb = 0;                                               // Initialize LED-Blue Flag
00080 
00081     syscnt_u = 0;                                               // Initialize System Counter for LED-User
00082     syscnt_r = 0;                                               // Initialize System Counter for LED-Red
00083     syscnt_g = 0;                                               // Initialize System Counter for LED-Green
00084     syscnt_b = 0;                                               // Initialize System Counter for LED-Blue
00085 
00086     unsigned int cntr = 0;                                      // Initialize LED-Red Counter
00087     unsigned int cntg = 0;                                      // Initialize LED-Green Counter
00088     unsigned int cntb = 0;                                      // Initialize LED-Blue Counter
00089 
00090     int cntrd = 1;                                              // Set LED-Red Counter Direction +1
00091     int cntgd = 1;                                              // Set LED-Green Counter Direction +1
00092     int cntbd = 1;                                              // Set LED-Blue Counter Direction +1
00093 
00094     unsigned int flg_ledu_last = 0;                             // Initialize LED-User Flag  (last value)
00095     unsigned int flg_ledr_last = 0;                             // Initialize LED-Red Flag   (last value)
00096     unsigned int flg_ledg_last = 0;                             // Initialize LED-Green Flag (last value)
00097     unsigned int flg_ledb_last = 0;                             // Initialize LED-Blue Flag  (last value)
00098 
00099     flipper.attach_us(&flip, 10000);                            // TickerTime Set 10ms
00100 
00101     while(1) {
00102 
00103 //----- LED User -----
00104         if(flg_ledu_last != flg_ledu) {                         // Has LED-User Flag been Changed?
00105             flg_ledu_last = flg_ledu;                           // Save current value
00106             ledu =!ledu;                                        // Invert LED-User
00107         }
00108 
00109 //----- LED Red -----
00110         if(flg_ledr_last != flg_ledr) {                         // Has LED-Red Flag been Changed?
00111             flg_ledr_last = flg_ledr;                           // Save current value
00112  
00113             if(cntr ==   0) cntrd =  1;                         // Set Direction(+1)
00114             if(cntr >= 127) cntrd = -1;                         // Set Direction(-1)
00115             cntr += cntrd;                                      // Increment/Decrement Counter
00116             ledr = (float)cntr / 128;                           // Set LED-Red Duty
00117         }
00118 
00119 //----- LED Green -----
00120         if(flg_ledg_last != flg_ledg) {                         // Has LED-Green Flag been Changed?
00121             flg_ledg_last = flg_ledg;                           // Save current value
00122  
00123             if(cntg ==   0) cntgd =  1;                         // Set Direction(+1)
00124             if(cntg >= 127) cntgd = -1;                         // Set Direction(-1)
00125             cntg += cntgd;                                      // Increment/Decrement Counter
00126             ledg = (float)cntg / 128;                           // Set LED-Green Duty
00127         }
00128 
00129 //----- LED Blue -----
00130         if(flg_ledb_last != flg_ledb) {                         // Has LED-Blue Flag been Changed?
00131             flg_ledb_last = flg_ledb;                           // Save current value
00132  
00133             if(cntb ==   0) cntbd =  1;                         // Set Direction(+1)
00134             if(cntb >= 127) cntbd = -1;                         // Set Direction(-1)
00135             cntb += cntbd;                                      // Increment/Decrement Counter
00136             ledb = (float)cntb / 128;                           // Set LED-Blue Duty
00137         }
00138 
00139         wait_us(1);
00140     }
00141 }