
Factory-written program for GR-PEACH
main.cpp@0:201064615590, 2017-08-02 (annotated)
- Committer:
- tuthai
- Date:
- Wed Aug 02 09:41:28 2017 +0000
- Revision:
- 0:201064615590
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tuthai | 0:201064615590 | 1 | #include "mbed.h" |
tuthai | 0:201064615590 | 2 | #include "SoftPWM.h" |
tuthai | 0:201064615590 | 3 | |
tuthai | 0:201064615590 | 4 | #define LED_ON 1 |
tuthai | 0:201064615590 | 5 | #define LED_OFF 0 |
tuthai | 0:201064615590 | 6 | |
tuthai | 0:201064615590 | 7 | #define TIME_10ms 1 |
tuthai | 0:201064615590 | 8 | #define TIME_20ms 2 |
tuthai | 0:201064615590 | 9 | #define TIME_30ms 3 |
tuthai | 0:201064615590 | 10 | #define TIME_40ms 4 |
tuthai | 0:201064615590 | 11 | #define TIME_50ms 5 |
tuthai | 0:201064615590 | 12 | #define TIME_100ms 10 |
tuthai | 0:201064615590 | 13 | #define TIME_200ms 20 |
tuthai | 0:201064615590 | 14 | #define TIME_500ms 50 |
tuthai | 0:201064615590 | 15 | |
tuthai | 0:201064615590 | 16 | static Ticker flipper; // Tick Timer |
tuthai | 0:201064615590 | 17 | |
tuthai | 0:201064615590 | 18 | static DigitalOut ledu(LED_USER); // LED-User |
tuthai | 0:201064615590 | 19 | static SoftPWM ledr(LED_RED); // LED-Red |
tuthai | 0:201064615590 | 20 | static SoftPWM ledg(LED_GREEN); // LED-Green |
tuthai | 0:201064615590 | 21 | static SoftPWM ledb(LED_BLUE); // LED-Blue |
tuthai | 0:201064615590 | 22 | |
tuthai | 0:201064615590 | 23 | static unsigned int syscnt_u; // SystemCounter for LED-User |
tuthai | 0:201064615590 | 24 | static unsigned int syscnt_r; // SystemCounter for LED-Red |
tuthai | 0:201064615590 | 25 | static unsigned int syscnt_g; // SystemCounter for LED-Green |
tuthai | 0:201064615590 | 26 | static unsigned int syscnt_b; // SystemCounter for LED-Blue |
tuthai | 0:201064615590 | 27 | |
tuthai | 0:201064615590 | 28 | volatile static unsigned int flg_ledu; // LED-User Flag |
tuthai | 0:201064615590 | 29 | volatile static unsigned int flg_ledr; // LED-Red Flag |
tuthai | 0:201064615590 | 30 | volatile static unsigned int flg_ledg; // LED-Green Flag |
tuthai | 0:201064615590 | 31 | volatile static unsigned int flg_ledb; // LED-Blue Flag |
tuthai | 0:201064615590 | 32 | |
tuthai | 0:201064615590 | 33 | void flip() { |
tuthai | 0:201064615590 | 34 | // Check 500ms for LED-User |
tuthai | 0:201064615590 | 35 | syscnt_u++; // increment SystemCounter for LED-User |
tuthai | 0:201064615590 | 36 | if( syscnt_u >= TIME_500ms ){ |
tuthai | 0:201064615590 | 37 | flg_ledu++; |
tuthai | 0:201064615590 | 38 | syscnt_u = 0; |
tuthai | 0:201064615590 | 39 | } |
tuthai | 0:201064615590 | 40 | |
tuthai | 0:201064615590 | 41 | // Check 10ms for LED-Red |
tuthai | 0:201064615590 | 42 | syscnt_r++; // increment SystemCounter for LED-Red |
tuthai | 0:201064615590 | 43 | if( syscnt_r >= TIME_10ms ){ |
tuthai | 0:201064615590 | 44 | flg_ledr++; |
tuthai | 0:201064615590 | 45 | syscnt_r = 0; |
tuthai | 0:201064615590 | 46 | } |
tuthai | 0:201064615590 | 47 | |
tuthai | 0:201064615590 | 48 | // Check 20ms for LED-Green |
tuthai | 0:201064615590 | 49 | syscnt_g++; // increment SystemCounter for LED-Green |
tuthai | 0:201064615590 | 50 | if( syscnt_g >= TIME_20ms ){ |
tuthai | 0:201064615590 | 51 | flg_ledg++; |
tuthai | 0:201064615590 | 52 | syscnt_g = 0; |
tuthai | 0:201064615590 | 53 | } |
tuthai | 0:201064615590 | 54 | |
tuthai | 0:201064615590 | 55 | // Check 30ms for LED-Blue |
tuthai | 0:201064615590 | 56 | syscnt_b++; // increment SystemCounter for LED-Blue |
tuthai | 0:201064615590 | 57 | if( syscnt_b >= TIME_30ms ){ |
tuthai | 0:201064615590 | 58 | flg_ledb++; |
tuthai | 0:201064615590 | 59 | syscnt_b = 0; |
tuthai | 0:201064615590 | 60 | } |
tuthai | 0:201064615590 | 61 | } |
tuthai | 0:201064615590 | 62 | |
tuthai | 0:201064615590 | 63 | int main() { |
tuthai | 0:201064615590 | 64 | |
tuthai | 0:201064615590 | 65 | ledu = LED_OFF; // LED-User Off |
tuthai | 0:201064615590 | 66 | |
tuthai | 0:201064615590 | 67 | ledr.period_ms(10); // Set PWM Period 10ms |
tuthai | 0:201064615590 | 68 | ledr = 0.0f; // Set LED-Red Duty |
tuthai | 0:201064615590 | 69 | |
tuthai | 0:201064615590 | 70 | ledg.period_ms(10); // Set PWM Period 10ms |
tuthai | 0:201064615590 | 71 | ledg = 0.0f; // Set LED-Green Duty |
tuthai | 0:201064615590 | 72 | |
tuthai | 0:201064615590 | 73 | ledb.period_ms(10); // Set PWM Period 10ms |
tuthai | 0:201064615590 | 74 | ledb = 0.0f; // Set LED-Blue Duty |
tuthai | 0:201064615590 | 75 | |
tuthai | 0:201064615590 | 76 | flg_ledu = 0; // Initialize LED-User Flag |
tuthai | 0:201064615590 | 77 | flg_ledr = 0; // Initialize LED-Red Flag |
tuthai | 0:201064615590 | 78 | flg_ledg = 0; // Initialize LED-Green Flag |
tuthai | 0:201064615590 | 79 | flg_ledb = 0; // Initialize LED-Blue Flag |
tuthai | 0:201064615590 | 80 | |
tuthai | 0:201064615590 | 81 | syscnt_u = 0; // Initialize System Counter for LED-User |
tuthai | 0:201064615590 | 82 | syscnt_r = 0; // Initialize System Counter for LED-Red |
tuthai | 0:201064615590 | 83 | syscnt_g = 0; // Initialize System Counter for LED-Green |
tuthai | 0:201064615590 | 84 | syscnt_b = 0; // Initialize System Counter for LED-Blue |
tuthai | 0:201064615590 | 85 | |
tuthai | 0:201064615590 | 86 | unsigned int cntr = 0; // Initialize LED-Red Counter |
tuthai | 0:201064615590 | 87 | unsigned int cntg = 0; // Initialize LED-Green Counter |
tuthai | 0:201064615590 | 88 | unsigned int cntb = 0; // Initialize LED-Blue Counter |
tuthai | 0:201064615590 | 89 | |
tuthai | 0:201064615590 | 90 | int cntrd = 1; // Set LED-Red Counter Direction +1 |
tuthai | 0:201064615590 | 91 | int cntgd = 1; // Set LED-Green Counter Direction +1 |
tuthai | 0:201064615590 | 92 | int cntbd = 1; // Set LED-Blue Counter Direction +1 |
tuthai | 0:201064615590 | 93 | |
tuthai | 0:201064615590 | 94 | unsigned int flg_ledu_last = 0; // Initialize LED-User Flag (last value) |
tuthai | 0:201064615590 | 95 | unsigned int flg_ledr_last = 0; // Initialize LED-Red Flag (last value) |
tuthai | 0:201064615590 | 96 | unsigned int flg_ledg_last = 0; // Initialize LED-Green Flag (last value) |
tuthai | 0:201064615590 | 97 | unsigned int flg_ledb_last = 0; // Initialize LED-Blue Flag (last value) |
tuthai | 0:201064615590 | 98 | |
tuthai | 0:201064615590 | 99 | flipper.attach_us(&flip, 10000); // TickerTime Set 10ms |
tuthai | 0:201064615590 | 100 | |
tuthai | 0:201064615590 | 101 | while(1) { |
tuthai | 0:201064615590 | 102 | |
tuthai | 0:201064615590 | 103 | //----- LED User ----- |
tuthai | 0:201064615590 | 104 | if(flg_ledu_last != flg_ledu) { // Has LED-User Flag been Changed? |
tuthai | 0:201064615590 | 105 | flg_ledu_last = flg_ledu; // Save current value |
tuthai | 0:201064615590 | 106 | ledu =!ledu; // Invert LED-User |
tuthai | 0:201064615590 | 107 | } |
tuthai | 0:201064615590 | 108 | |
tuthai | 0:201064615590 | 109 | //----- LED Red ----- |
tuthai | 0:201064615590 | 110 | if(flg_ledr_last != flg_ledr) { // Has LED-Red Flag been Changed? |
tuthai | 0:201064615590 | 111 | flg_ledr_last = flg_ledr; // Save current value |
tuthai | 0:201064615590 | 112 | |
tuthai | 0:201064615590 | 113 | if(cntr == 0) cntrd = 1; // Set Direction(+1) |
tuthai | 0:201064615590 | 114 | if(cntr >= 127) cntrd = -1; // Set Direction(-1) |
tuthai | 0:201064615590 | 115 | cntr += cntrd; // Increment/Decrement Counter |
tuthai | 0:201064615590 | 116 | ledr = (float)cntr / 128; // Set LED-Red Duty |
tuthai | 0:201064615590 | 117 | } |
tuthai | 0:201064615590 | 118 | |
tuthai | 0:201064615590 | 119 | //----- LED Green ----- |
tuthai | 0:201064615590 | 120 | if(flg_ledg_last != flg_ledg) { // Has LED-Green Flag been Changed? |
tuthai | 0:201064615590 | 121 | flg_ledg_last = flg_ledg; // Save current value |
tuthai | 0:201064615590 | 122 | |
tuthai | 0:201064615590 | 123 | if(cntg == 0) cntgd = 1; // Set Direction(+1) |
tuthai | 0:201064615590 | 124 | if(cntg >= 127) cntgd = -1; // Set Direction(-1) |
tuthai | 0:201064615590 | 125 | cntg += cntgd; // Increment/Decrement Counter |
tuthai | 0:201064615590 | 126 | ledg = (float)cntg / 128; // Set LED-Green Duty |
tuthai | 0:201064615590 | 127 | } |
tuthai | 0:201064615590 | 128 | |
tuthai | 0:201064615590 | 129 | //----- LED Blue ----- |
tuthai | 0:201064615590 | 130 | if(flg_ledb_last != flg_ledb) { // Has LED-Blue Flag been Changed? |
tuthai | 0:201064615590 | 131 | flg_ledb_last = flg_ledb; // Save current value |
tuthai | 0:201064615590 | 132 | |
tuthai | 0:201064615590 | 133 | if(cntb == 0) cntbd = 1; // Set Direction(+1) |
tuthai | 0:201064615590 | 134 | if(cntb >= 127) cntbd = -1; // Set Direction(-1) |
tuthai | 0:201064615590 | 135 | cntb += cntbd; // Increment/Decrement Counter |
tuthai | 0:201064615590 | 136 | ledb = (float)cntb / 128; // Set LED-Blue Duty |
tuthai | 0:201064615590 | 137 | } |
tuthai | 0:201064615590 | 138 | |
tuthai | 0:201064615590 | 139 | wait_us(1); |
tuthai | 0:201064615590 | 140 | } |
tuthai | 0:201064615590 | 141 | } |