Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
LEDFunctions.cpp@0:e4f37b52019a, 2012-06-20 (annotated)
- Committer:
- PA
- Date:
- Wed Jun 20 06:35:17 2012 +0000
- Revision:
- 0:e4f37b52019a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PA | 0:e4f37b52019a | 1 | /***************************************************************************** |
PA | 0:e4f37b52019a | 2 | ** @brief controlling ** |
PA | 0:e4f37b52019a | 3 | ** @detail ** |
PA | 0:e4f37b52019a | 4 | ** @argument: ipwd: pwd class ** |
PA | 0:e4f37b52019a | 5 | *****************************************************************************/ |
PA | 0:e4f37b52019a | 6 | |
PA | 0:e4f37b52019a | 7 | #include "mbed.h" |
PA | 0:e4f37b52019a | 8 | #include "LEDFunctions.h" |
PA | 0:e4f37b52019a | 9 | #include "UniPWM.h" |
PA | 0:e4f37b52019a | 10 | //#include "rtos.h" |
PA | 0:e4f37b52019a | 11 | |
PA | 0:e4f37b52019a | 12 | //Serial pc(USBTX,USBRX); |
PA | 0:e4f37b52019a | 13 | |
PA | 0:e4f37b52019a | 14 | LEDFunctions::LEDFunctions() |
PA | 0:e4f37b52019a | 15 | { |
PA | 0:e4f37b52019a | 16 | m_pwm.push_back(new UniPWM(p5)); |
PA | 0:e4f37b52019a | 17 | m_pwm[0]->duty(0); |
PA | 0:e4f37b52019a | 18 | m_map.insert(std::pair<std::string, int>("p5", 0)); |
PA | 0:e4f37b52019a | 19 | m_pwm.push_back(new UniPWM(p6)); |
PA | 0:e4f37b52019a | 20 | m_pwm[1]->duty(0); |
PA | 0:e4f37b52019a | 21 | m_map.insert(std::pair<std::string, int>("p6", 1)); |
PA | 0:e4f37b52019a | 22 | m_pwm.push_back(new UniPWM(p7)); |
PA | 0:e4f37b52019a | 23 | m_pwm[2]->duty(0); |
PA | 0:e4f37b52019a | 24 | m_map.insert(std::pair<std::string, int>("p7", 2)); |
PA | 0:e4f37b52019a | 25 | m_pwm.push_back(new UniPWM(p8)); |
PA | 0:e4f37b52019a | 26 | m_pwm[3]->duty(0); |
PA | 0:e4f37b52019a | 27 | m_map.insert(std::pair<std::string, int>("p8", 3)); |
PA | 0:e4f37b52019a | 28 | m_pwm.push_back(new UniPWM(p9)); |
PA | 0:e4f37b52019a | 29 | m_pwm[4]->duty(0); |
PA | 0:e4f37b52019a | 30 | m_map.insert(std::pair<std::string, int>("p9", 4)); |
PA | 0:e4f37b52019a | 31 | m_pwm.push_back(new UniPWM(p10)); |
PA | 0:e4f37b52019a | 32 | m_pwm[5]->duty(0); |
PA | 0:e4f37b52019a | 33 | m_map.insert(std::pair<std::string, int>("p10", 5)); |
PA | 0:e4f37b52019a | 34 | m_pwm.push_back(new UniPWM(p11)); |
PA | 0:e4f37b52019a | 35 | m_pwm[6]->duty(0); |
PA | 0:e4f37b52019a | 36 | m_map.insert(std::pair<std::string, int>("p11", 6)); |
PA | 0:e4f37b52019a | 37 | m_pwm.push_back(new UniPWM(p12)); |
PA | 0:e4f37b52019a | 38 | m_pwm[7]->duty(0); |
PA | 0:e4f37b52019a | 39 | m_map.insert(std::pair<std::string, int>("p12", 7)); |
PA | 0:e4f37b52019a | 40 | m_pwm.push_back(new UniPWM(p13)); |
PA | 0:e4f37b52019a | 41 | m_pwm[8]->duty(0); |
PA | 0:e4f37b52019a | 42 | m_map.insert(std::pair<std::string, int>("p13", 8)); |
PA | 0:e4f37b52019a | 43 | m_pwm.push_back(new UniPWM(p14)); |
PA | 0:e4f37b52019a | 44 | m_pwm[9]->duty(0); |
PA | 0:e4f37b52019a | 45 | m_map.insert(std::pair<std::string, int>("p14", 9)); |
PA | 0:e4f37b52019a | 46 | m_pwm.push_back(new UniPWM(p15)); // UniPWM(p15) |
PA | 0:e4f37b52019a | 47 | m_pwm[10]->duty(0); |
PA | 0:e4f37b52019a | 48 | m_map.insert(std::pair<std::string, int>("p15", 10)); |
PA | 0:e4f37b52019a | 49 | m_pwm.push_back(new UniPWM(p16)); |
PA | 0:e4f37b52019a | 50 | m_pwm[11]->duty(0); |
PA | 0:e4f37b52019a | 51 | m_map.insert(std::pair<std::string, int>("p16", 11)); |
PA | 0:e4f37b52019a | 52 | m_pwm.push_back(new UniPWM(p17)); |
PA | 0:e4f37b52019a | 53 | m_pwm[12]->duty(0); |
PA | 0:e4f37b52019a | 54 | m_map.insert(std::pair<std::string, int>("p17", 12)); |
PA | 0:e4f37b52019a | 55 | m_pwm.push_back(new UniPWM(p18)); |
PA | 0:e4f37b52019a | 56 | m_pwm[13]->duty(0); |
PA | 0:e4f37b52019a | 57 | m_map.insert(std::pair<std::string, int>("p18", 13)); |
PA | 0:e4f37b52019a | 58 | m_pwm.push_back(new UniPWM(p19)); |
PA | 0:e4f37b52019a | 59 | m_pwm[14]->duty(0); |
PA | 0:e4f37b52019a | 60 | m_map.insert(std::pair<std::string, int>("p19", 14)); |
PA | 0:e4f37b52019a | 61 | m_pwm.push_back(new UniPWM(p20)); |
PA | 0:e4f37b52019a | 62 | m_pwm[15]->duty(0); |
PA | 0:e4f37b52019a | 63 | m_map.insert(std::pair<std::string, int>("p20", 15)); |
PA | 0:e4f37b52019a | 64 | m_pwm.push_back(new UniPWM(p21)); |
PA | 0:e4f37b52019a | 65 | m_pwm[16]->duty(0); |
PA | 0:e4f37b52019a | 66 | m_map.insert(std::pair<std::string, int>("p21", 16)); |
PA | 0:e4f37b52019a | 67 | m_pwm.push_back(new UniPWM(p22)); |
PA | 0:e4f37b52019a | 68 | m_pwm[17]->duty(0); |
PA | 0:e4f37b52019a | 69 | m_map.insert(std::pair<std::string, int>("p22", 17)); |
PA | 0:e4f37b52019a | 70 | m_pwm.push_back(new UniPWM(p23)); |
PA | 0:e4f37b52019a | 71 | m_pwm[18]->duty(0); |
PA | 0:e4f37b52019a | 72 | m_map.insert(std::pair<std::string, int>("p23", 18)); |
PA | 0:e4f37b52019a | 73 | m_pwm.push_back(new UniPWM(p24)); |
PA | 0:e4f37b52019a | 74 | m_pwm[19]->duty(0); |
PA | 0:e4f37b52019a | 75 | m_map.insert(std::pair<std::string, int>("p24", 19)); |
PA | 0:e4f37b52019a | 76 | m_pwm.push_back(new UniPWM(p25)); |
PA | 0:e4f37b52019a | 77 | m_pwm[20]->duty(0); |
PA | 0:e4f37b52019a | 78 | m_map.insert(std::pair<std::string, int>("p25", 20)); |
PA | 0:e4f37b52019a | 79 | m_pwm.push_back(new UniPWM(p26)); |
PA | 0:e4f37b52019a | 80 | m_pwm[21]->duty(0); |
PA | 0:e4f37b52019a | 81 | m_map.insert(std::pair<std::string, int>("p26", 21)); |
PA | 0:e4f37b52019a | 82 | m_pwm.push_back(new UniPWM(p27)); |
PA | 0:e4f37b52019a | 83 | m_pwm[22]->duty(0); |
PA | 0:e4f37b52019a | 84 | m_map.insert(std::pair<std::string, int>("p27", 22)); |
PA | 0:e4f37b52019a | 85 | m_pwm.push_back(new UniPWM(p28)); |
PA | 0:e4f37b52019a | 86 | m_pwm[23]->duty(0); |
PA | 0:e4f37b52019a | 87 | m_map.insert(std::pair<std::string, int>("p28", 23)); |
PA | 0:e4f37b52019a | 88 | m_pwm.push_back(new UniPWM(p29)); |
PA | 0:e4f37b52019a | 89 | m_pwm[24]->duty(0); |
PA | 0:e4f37b52019a | 90 | m_map.insert(std::pair<std::string, int>("p29", 24)); |
PA | 0:e4f37b52019a | 91 | m_pwm.push_back(new UniPWM(p30)); |
PA | 0:e4f37b52019a | 92 | m_pwm[25]->duty(0); |
PA | 0:e4f37b52019a | 93 | m_map.insert(std::pair<std::string, int>("p30", 25)); |
PA | 0:e4f37b52019a | 94 | m_pwm.push_back(new UniPWM(LED1)); |
PA | 0:e4f37b52019a | 95 | m_pwm[26]->duty(0); |
PA | 0:e4f37b52019a | 96 | m_map.insert(std::pair<std::string, int>("LED1", 26)); |
PA | 0:e4f37b52019a | 97 | m_pwm.push_back(new UniPWM(LED2)); |
PA | 0:e4f37b52019a | 98 | m_pwm[27]->duty(0); |
PA | 0:e4f37b52019a | 99 | m_map.insert(std::pair<std::string, int>("LED2", 27)); |
PA | 0:e4f37b52019a | 100 | m_pwm.push_back(new UniPWM(LED3)); |
PA | 0:e4f37b52019a | 101 | m_pwm[28]->duty(0); |
PA | 0:e4f37b52019a | 102 | m_map.insert(std::pair<std::string, int>("LED3", 28)); |
PA | 0:e4f37b52019a | 103 | m_pwm.push_back(new UniPWM(LED4)); |
PA | 0:e4f37b52019a | 104 | m_pwm[29]->duty(0); |
PA | 0:e4f37b52019a | 105 | m_map.insert(std::pair<std::string, int>("LED4", 29)); |
PA | 0:e4f37b52019a | 106 | } |
PA | 0:e4f37b52019a | 107 | |
PA | 0:e4f37b52019a | 108 | void LEDFunctions::on(int n, int d, float b, float s){ |
PA | 0:e4f37b52019a | 109 | if(s > 1){ |
PA | 0:e4f37b52019a | 110 | for(float k = b/s; k < b; k += b/s){ |
PA | 0:e4f37b52019a | 111 | p += b/s; |
PA | 0:e4f37b52019a | 112 | m_pwm[n]->duty(p); |
PA | 0:e4f37b52019a | 113 | //Thread::wait(100); |
PA | 0:e4f37b52019a | 114 | wait(0.1); |
PA | 0:e4f37b52019a | 115 | } |
PA | 0:e4f37b52019a | 116 | for(float i = s*0.1; i < d; i += 0.1){ |
PA | 0:e4f37b52019a | 117 | m_pwm[n]->duty(b); |
PA | 0:e4f37b52019a | 118 | //Thread::wait(100); |
PA | 0:e4f37b52019a | 119 | wait(0.1); |
PA | 0:e4f37b52019a | 120 | } |
PA | 0:e4f37b52019a | 121 | } |
PA | 0:e4f37b52019a | 122 | else{ |
PA | 0:e4f37b52019a | 123 | for(float i = 0; i < d; i += 1){ |
PA | 0:e4f37b52019a | 124 | m_pwm[n]->duty(b); |
PA | 0:e4f37b52019a | 125 | //Thread::wait(1000); |
PA | 0:e4f37b52019a | 126 | wait(1); |
PA | 0:e4f37b52019a | 127 | } |
PA | 0:e4f37b52019a | 128 | } |
PA | 0:e4f37b52019a | 129 | m_pwm[n]->duty(0); |
PA | 0:e4f37b52019a | 130 | } |
PA | 0:e4f37b52019a | 131 | |
PA | 0:e4f37b52019a | 132 | /* |
PA | 0:e4f37b52019a | 133 | void LEDFunctions::on_abst(int n, int starttime, int endtime, float b, float s){ |
PA | 0:e4f37b52019a | 134 | id(n); |
PA | 0:e4f37b52019a | 135 | if(time==starttime){ |
PA | 0:e4f37b52019a | 136 | if(s > 1){ |
PA | 0:e4f37b52019a | 137 | for(k = 0; k = b; k = b/s){ |
PA | 0:e4f37b52019a | 138 | p = p + b/s; |
PA | 0:e4f37b52019a | 139 | _pin.duty(p); |
PA | 0:e4f37b52019a | 140 | wait(0.1); |
PA | 0:e4f37b52019a | 141 | } |
PA | 0:e4f37b52019a | 142 | for(i = starttime + b; i < endtime; i += 0.1){ |
PA | 0:e4f37b52019a | 143 | _pin.duty(b); |
PA | 0:e4f37b52019a | 144 | wait(0.1); |
PA | 0:e4f37b52019a | 145 | } |
PA | 0:e4f37b52019a | 146 | } |
PA | 0:e4f37b52019a | 147 | else{ |
PA | 0:e4f37b52019a | 148 | for(i = starttime; i = endtime; i += 0.1){ |
PA | 0:e4f37b52019a | 149 | _pin.duty(b); |
PA | 0:e4f37b52019a | 150 | wait(0.1); |
PA | 0:e4f37b52019a | 151 | } |
PA | 0:e4f37b52019a | 152 | } |
PA | 0:e4f37b52019a | 153 | } |
PA | 0:e4f37b52019a | 154 | } |
PA | 0:e4f37b52019a | 155 | */ |
PA | 0:e4f37b52019a | 156 | void LEDFunctions::on_forever(int n, float b, float s){ |
PA | 0:e4f37b52019a | 157 | if(s > 1){ |
PA | 0:e4f37b52019a | 158 | for(float k = b/s; k < b; k += b/s){ |
PA | 0:e4f37b52019a | 159 | p += b/s; |
PA | 0:e4f37b52019a | 160 | m_pwm[n]->duty(p); |
PA | 0:e4f37b52019a | 161 | //Thread::wait(100); |
PA | 0:e4f37b52019a | 162 | wait(0.1); |
PA | 0:e4f37b52019a | 163 | } |
PA | 0:e4f37b52019a | 164 | } |
PA | 0:e4f37b52019a | 165 | else{ |
PA | 0:e4f37b52019a | 166 | m_pwm[n]->duty(b); |
PA | 0:e4f37b52019a | 167 | } |
PA | 0:e4f37b52019a | 168 | } |
PA | 0:e4f37b52019a | 169 | |
PA | 0:e4f37b52019a | 170 | void LEDFunctions::off_forever(int n, float b, float s){ |
PA | 0:e4f37b52019a | 171 | if(s > 1){ |
PA | 0:e4f37b52019a | 172 | for(float k = 1; k > b; k -= 1/s){ |
PA | 0:e4f37b52019a | 173 | p = k; |
PA | 0:e4f37b52019a | 174 | m_pwm[n]->duty(p); |
PA | 0:e4f37b52019a | 175 | //Thread::wait(100); |
PA | 0:e4f37b52019a | 176 | wait(0.1); |
PA | 0:e4f37b52019a | 177 | } |
PA | 0:e4f37b52019a | 178 | m_pwm[n]->duty(b); |
PA | 0:e4f37b52019a | 179 | } |
PA | 0:e4f37b52019a | 180 | else{ |
PA | 0:e4f37b52019a | 181 | m_pwm[n]->duty(b); |
PA | 0:e4f37b52019a | 182 | } |
PA | 0:e4f37b52019a | 183 | } |
PA | 0:e4f37b52019a | 184 | |
PA | 0:e4f37b52019a | 185 | void LEDFunctions::blink(int n, int f, int d, float lb, float ub, float s){ |
PA | 0:e4f37b52019a | 186 | float time = 1.0 / (2.0*f); |
PA | 0:e4f37b52019a | 187 | float inc = 1.0/f; |
PA | 0:e4f37b52019a | 188 | for(float i = 0.0; i < d; i += inc){ |
PA | 0:e4f37b52019a | 189 | if(s > 1){ |
PA | 0:e4f37b52019a | 190 | for(float k = lb; k < ub; k += (ub-lb)/s){ |
PA | 0:e4f37b52019a | 191 | p += (ub-lb)/s; |
PA | 0:e4f37b52019a | 192 | m_pwm[n]->duty(p); |
PA | 0:e4f37b52019a | 193 | //Thread::wait(time*1000/s) |
PA | 0:e4f37b52019a | 194 | wait(time/s); |
PA | 0:e4f37b52019a | 195 | } |
PA | 0:e4f37b52019a | 196 | for(float k = ub; k > lb; k -= (ub-lb)/s){ |
PA | 0:e4f37b52019a | 197 | p = k; |
PA | 0:e4f37b52019a | 198 | m_pwm[n]->duty(k); |
PA | 0:e4f37b52019a | 199 | //Thread::wait(time*1000/s); |
PA | 0:e4f37b52019a | 200 | wait(time/s); |
PA | 0:e4f37b52019a | 201 | } |
PA | 0:e4f37b52019a | 202 | m_pwm[n]->duty(lb); |
PA | 0:e4f37b52019a | 203 | //Thread::wait(time*1000/s); |
PA | 0:e4f37b52019a | 204 | wait(time/s); |
PA | 0:e4f37b52019a | 205 | } |
PA | 0:e4f37b52019a | 206 | else { |
PA | 0:e4f37b52019a | 207 | m_pwm[n]->duty(ub); |
PA | 0:e4f37b52019a | 208 | //Thread::wait(time*1000); |
PA | 0:e4f37b52019a | 209 | wait(1); |
PA | 0:e4f37b52019a | 210 | m_pwm[n]->duty(lb); |
PA | 0:e4f37b52019a | 211 | //Thread::wait(time*1000); |
PA | 0:e4f37b52019a | 212 | wait(1); |
PA | 0:e4f37b52019a | 213 | } |
PA | 0:e4f37b52019a | 214 | } |
PA | 0:e4f37b52019a | 215 | } |
PA | 0:e4f37b52019a | 216 | /* |
PA | 0:e4f37b52019a | 217 | void LEDFunctions::blink_abst(int n, int f, int startime, int endtime, float ub, float lb, float s){ |
PA | 0:e4f37b52019a | 218 | id(n); |
PA | 0:e4f37b52019a | 219 | for(i = starttime; i = endtime; i += 1/frequency){ |
PA | 0:e4f37b52019a | 220 | if(s > 1){ |
PA | 0:e4f37b52019a | 221 | for(k = lb; k = ub; k += (ub-lb)/s){ |
PA | 0:e4f37b52019a | 222 | lb = lb + (up-lb)/s; |
PA | 0:e4f37b52019a | 223 | _pin.duty(lb); |
PA | 0:e4f37b52019a | 224 | wait((1/2*f)/((ub-lb)/s)); |
PA | 0:e4f37b52019a | 225 | } |
PA | 0:e4f37b52019a | 226 | for(k = ub; k = lb; k -= ub/s){ |
PA | 0:e4f37b52019a | 227 | ub = ub - (ub-lb)/s; |
PA | 0:e4f37b52019a | 228 | _pin.duty(ub); |
PA | 0:e4f37b52019a | 229 | wait((1/2*f)/((ub-lb)/s)); |
PA | 0:e4f37b52019a | 230 | } |
PA | 0:e4f37b52019a | 231 | } |
PA | 0:e4f37b52019a | 232 | else{ |
PA | 0:e4f37b52019a | 233 | _pin.duty(ub); |
PA | 0:e4f37b52019a | 234 | wait(1/2*f); |
PA | 0:e4f37b52019a | 235 | _pin.duty(lb); |
PA | 0:e4f37b52019a | 236 | wait(1/2*f); |
PA | 0:e4f37b52019a | 237 | } |
PA | 0:e4f37b52019a | 238 | } |
PA | 0:e4f37b52019a | 239 | } |
PA | 0:e4f37b52019a | 240 | */ |