Give water to plants if dry amd pla meanwhile music the music vou can define with note length bind and breake also select several instruments for the sound
main.cpp@0:5150b09127e3, 2012-09-19 (annotated)
- Committer:
- helmut
- Date:
- Wed Sep 19 14:16:56 2012 +0000
- Revision:
- 0:5150b09127e3
plays mostly music that you can do with notes length breakes and bind; Some already defined; Tool is to start a pump for water to give plant is too dry.; Meanwhie plays music the most part of all
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
helmut | 0:5150b09127e3 | 1 | #include "mbed.h" |
helmut | 0:5150b09127e3 | 2 | #include "waves.h" |
helmut | 0:5150b09127e3 | 3 | #include "EthernetNetIf.h" |
helmut | 0:5150b09127e3 | 4 | #include "HTTPServer.h" |
helmut | 0:5150b09127e3 | 5 | #include "DebugTrace.h" |
helmut | 0:5150b09127e3 | 6 | |
helmut | 0:5150b09127e3 | 7 | EthernetNetIf eth (IpAddr(192,168,1,101), IpAddr(255,255,255,0), IpAddr(192,168,1,1), IpAddr(195,50,140,246)); |
helmut | 0:5150b09127e3 | 8 | //EthernetNetIf eth; //for DHCP |
helmut | 0:5150b09127e3 | 9 | HTTPServer svr; |
helmut | 0:5150b09127e3 | 10 | |
helmut | 0:5150b09127e3 | 11 | DigitalOut myled1(LED1); |
helmut | 0:5150b09127e3 | 12 | DigitalOut myled2(LED2); |
helmut | 0:5150b09127e3 | 13 | DigitalOut myled3(LED3); |
helmut | 0:5150b09127e3 | 14 | DigitalOut myled4(LED4); |
helmut | 0:5150b09127e3 | 15 | LocalFileSystem fs("local"); |
helmut | 0:5150b09127e3 | 16 | DebugTrace file(ON, TO_FILE); // i.e. file(ON, TO_FILE, "log.txt", 1024) |
helmut | 0:5150b09127e3 | 17 | |
helmut | 0:5150b09127e3 | 18 | float combind; |
helmut | 0:5150b09127e3 | 19 | |
helmut | 0:5150b09127e3 | 20 | bool Check_earth(); |
helmut | 0:5150b09127e3 | 21 | void Motor_run(); |
helmut | 0:5150b09127e3 | 22 | void Play_music(int track); |
helmut | 0:5150b09127e3 | 23 | void Play_Eine_kleine_Nachtmusik(); |
helmut | 0:5150b09127e3 | 24 | void Play_5th_Symphonie(); |
helmut | 0:5150b09127e3 | 25 | // Play_Note(welche Note, welchen Länge, gebunden, Lautstärke, welches Instrument) |
helmut | 0:5150b09127e3 | 26 | void Play_Note(float note, float lengh, bool combine, float loud, int instrument); |
helmut | 0:5150b09127e3 | 27 | void Play_Pause(float lengh); |
helmut | 0:5150b09127e3 | 28 | void Play_ladder(); |
helmut | 0:5150b09127e3 | 29 | float Check_loud(); |
helmut | 0:5150b09127e3 | 30 | int val = 122; |
helmut | 0:5150b09127e3 | 31 | float fval = 1.414; |
helmut | 0:5150b09127e3 | 32 | |
helmut | 0:5150b09127e3 | 33 | |
helmut | 0:5150b09127e3 | 34 | int main() { |
helmut | 0:5150b09127e3 | 35 | bool too_low; |
helmut | 0:5150b09127e3 | 36 | Base::add_rpc_class<AnalogIn>(); |
helmut | 0:5150b09127e3 | 37 | Base::add_rpc_class<AnalogOut>(); |
helmut | 0:5150b09127e3 | 38 | Base::add_rpc_class<DigitalIn>(); |
helmut | 0:5150b09127e3 | 39 | Base::add_rpc_class<DigitalOut>(); |
helmut | 0:5150b09127e3 | 40 | Base::add_rpc_class<PwmOut>(); |
helmut | 0:5150b09127e3 | 41 | Base::add_rpc_class<Timer>(); |
helmut | 0:5150b09127e3 | 42 | Base::add_rpc_class<SPI>(); |
helmut | 0:5150b09127e3 | 43 | Base::add_rpc_class<BusOut>(); |
helmut | 0:5150b09127e3 | 44 | Base::add_rpc_class<BusIn>(); |
helmut | 0:5150b09127e3 | 45 | |
helmut | 0:5150b09127e3 | 46 | EthernetErr ethErr = eth.setup(); |
helmut | 0:5150b09127e3 | 47 | if (ethErr) { |
helmut | 0:5150b09127e3 | 48 | printf("Error %d in setup.\n", ethErr); |
helmut | 0:5150b09127e3 | 49 | return -1; |
helmut | 0:5150b09127e3 | 50 | } |
helmut | 0:5150b09127e3 | 51 | |
helmut | 0:5150b09127e3 | 52 | FSHandler::mount("/local", "/files"); //Mount /webfs path on /files web path |
helmut | 0:5150b09127e3 | 53 | FSHandler::mount("/local", "/"); //Mount /webfs path on web root path |
helmut | 0:5150b09127e3 | 54 | svr.addHandler<SimpleHandler>("/hello"); |
helmut | 0:5150b09127e3 | 55 | svr.addHandler<RPCHandler>("/rpc"); |
helmut | 0:5150b09127e3 | 56 | svr.addHandler<FSHandler>("/files"); |
helmut | 0:5150b09127e3 | 57 | svr.addHandler<FSHandler>("/"); //Default handler |
helmut | 0:5150b09127e3 | 58 | //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
helmut | 0:5150b09127e3 | 59 | IpAddr(192,168,1,101); |
helmut | 0:5150b09127e3 | 60 | |
helmut | 0:5150b09127e3 | 61 | svr.bind(80); |
helmut | 0:5150b09127e3 | 62 | |
helmut | 0:5150b09127e3 | 63 | while (1) { |
helmut | 0:5150b09127e3 | 64 | Net::poll(); |
helmut | 0:5150b09127e3 | 65 | too_low = Check_earth(); |
helmut | 0:5150b09127e3 | 66 | combind = 0; |
helmut | 0:5150b09127e3 | 67 | if (too_low == true) { |
helmut | 0:5150b09127e3 | 68 | Motor_run(); |
helmut | 0:5150b09127e3 | 69 | myled1 = true; |
helmut | 0:5150b09127e3 | 70 | Play_music(0); |
helmut | 0:5150b09127e3 | 71 | } else { |
helmut | 0:5150b09127e3 | 72 | myled1 = false; |
helmut | 0:5150b09127e3 | 73 | Play_music(1); |
helmut | 0:5150b09127e3 | 74 | |
helmut | 0:5150b09127e3 | 75 | } |
helmut | 0:5150b09127e3 | 76 | myled2 = false; |
helmut | 0:5150b09127e3 | 77 | file.traceOut("Test message\r\n"); |
helmut | 0:5150b09127e3 | 78 | file.traceOut("%x \r\n", val); |
helmut | 0:5150b09127e3 | 79 | file.traceOut("%d \r\n", val); |
helmut | 0:5150b09127e3 | 80 | file.traceOut("%f \r\n", fval); |
helmut | 0:5150b09127e3 | 81 | } |
helmut | 0:5150b09127e3 | 82 | } |
helmut | 0:5150b09127e3 | 83 | |
helmut | 0:5150b09127e3 | 84 | bool Check_earth() { |
helmut | 0:5150b09127e3 | 85 | float TRAY = 0.3; |
helmut | 0:5150b09127e3 | 86 | bool plant = false; |
helmut | 0:5150b09127e3 | 87 | AnalogIn plant_(p20); |
helmut | 0:5150b09127e3 | 88 | //Einlesen = der Messwert |
helmut | 0:5150b09127e3 | 89 | if (plant_ > TRAY | plant_ == 0) { |
helmut | 0:5150b09127e3 | 90 | plant = true; |
helmut | 0:5150b09127e3 | 91 | } |
helmut | 0:5150b09127e3 | 92 | //Sollte ein Messwert <= TRAY sein, Pflanze zu trocken, muss die Pumpe anlaufen |
helmut | 0:5150b09127e3 | 93 | if (plant == false) { |
helmut | 0:5150b09127e3 | 94 | myled3 = true; |
helmut | 0:5150b09127e3 | 95 | return true; |
helmut | 0:5150b09127e3 | 96 | } else { |
helmut | 0:5150b09127e3 | 97 | myled3 = false; |
helmut | 0:5150b09127e3 | 98 | return false; |
helmut | 0:5150b09127e3 | 99 | } |
helmut | 0:5150b09127e3 | 100 | } |
helmut | 0:5150b09127e3 | 101 | |
helmut | 0:5150b09127e3 | 102 | void Motor_run() { |
helmut | 0:5150b09127e3 | 103 | DigitalOut motor(p14); |
helmut | 0:5150b09127e3 | 104 | motor = true; |
helmut | 0:5150b09127e3 | 105 | wait(0.2); |
helmut | 0:5150b09127e3 | 106 | motor = false; |
helmut | 0:5150b09127e3 | 107 | } |
helmut | 0:5150b09127e3 | 108 | |
helmut | 0:5150b09127e3 | 109 | void Play_music(int track) { |
helmut | 0:5150b09127e3 | 110 | if (track == 0) { |
helmut | 0:5150b09127e3 | 111 | Play_Eine_kleine_Nachtmusik(); |
helmut | 0:5150b09127e3 | 112 | } |
helmut | 0:5150b09127e3 | 113 | if (track == 1) { |
helmut | 0:5150b09127e3 | 114 | Play_5th_Symphonie(); |
helmut | 0:5150b09127e3 | 115 | //Play_ladder(); |
helmut | 0:5150b09127e3 | 116 | } |
helmut | 0:5150b09127e3 | 117 | } |
helmut | 0:5150b09127e3 | 118 | |
helmut | 0:5150b09127e3 | 119 | void Play_Note(float note, float lengh, bool combine, float loud, int instrument) { |
helmut | 0:5150b09127e3 | 120 | int point_read; |
helmut | 0:5150b09127e3 | 121 | float lengh_index; |
helmut | 0:5150b09127e3 | 122 | long index; |
helmut | 0:5150b09127e3 | 123 | long index1; |
helmut | 0:5150b09127e3 | 124 | long index2; |
helmut | 0:5150b09127e3 | 125 | char* text; |
helmut | 0:5150b09127e3 | 126 | char* text2; |
helmut | 0:5150b09127e3 | 127 | float add; |
helmut | 0:5150b09127e3 | 128 | AnalogOut aout(p18); |
helmut | 0:5150b09127e3 | 129 | |
helmut | 0:5150b09127e3 | 130 | switch (instrument) { //what instrument, read number of points of Form of Instument, if not set 0 |
helmut | 0:5150b09127e3 | 131 | case 1: |
helmut | 0:5150b09127e3 | 132 | point_read = Instrument1_; |
helmut | 0:5150b09127e3 | 133 | text = "Instrument1_"; |
helmut | 0:5150b09127e3 | 134 | break; |
helmut | 0:5150b09127e3 | 135 | case 2: |
helmut | 0:5150b09127e3 | 136 | point_read = Instrument2_; |
helmut | 0:5150b09127e3 | 137 | text = "Instrument2_"; |
helmut | 0:5150b09127e3 | 138 | break; |
helmut | 0:5150b09127e3 | 139 | case 3: |
helmut | 0:5150b09127e3 | 140 | point_read = Instrument3_; |
helmut | 0:5150b09127e3 | 141 | text = "Instrument3_"; |
helmut | 0:5150b09127e3 | 142 | break; |
helmut | 0:5150b09127e3 | 143 | case 4: |
helmut | 0:5150b09127e3 | 144 | point_read = Instrument4_; |
helmut | 0:5150b09127e3 | 145 | text = "Instrument4_"; |
helmut | 0:5150b09127e3 | 146 | break; |
helmut | 0:5150b09127e3 | 147 | case 5: |
helmut | 0:5150b09127e3 | 148 | point_read = Instrument5_; |
helmut | 0:5150b09127e3 | 149 | text = "Instrument5_"; |
helmut | 0:5150b09127e3 | 150 | break; |
helmut | 0:5150b09127e3 | 151 | case 6: |
helmut | 0:5150b09127e3 | 152 | point_read = Instrument6_; |
helmut | 0:5150b09127e3 | 153 | text = "Instrument6_"; |
helmut | 0:5150b09127e3 | 154 | break; |
helmut | 0:5150b09127e3 | 155 | case 7: |
helmut | 0:5150b09127e3 | 156 | point_read = Instrument7_; |
helmut | 0:5150b09127e3 | 157 | text = "Instrument7_"; |
helmut | 0:5150b09127e3 | 158 | break; |
helmut | 0:5150b09127e3 | 159 | case 8: |
helmut | 0:5150b09127e3 | 160 | point_read = Instrument8_; |
helmut | 0:5150b09127e3 | 161 | text = "Instrument8_"; |
helmut | 0:5150b09127e3 | 162 | break; |
helmut | 0:5150b09127e3 | 163 | case 9: |
helmut | 0:5150b09127e3 | 164 | point_read = Instrument9_; |
helmut | 0:5150b09127e3 | 165 | text = "Instrument9_"; |
helmut | 0:5150b09127e3 | 166 | break; |
helmut | 0:5150b09127e3 | 167 | case 10: |
helmut | 0:5150b09127e3 | 168 | point_read = Instrument10_; |
helmut | 0:5150b09127e3 | 169 | text = "Instrument10_"; |
helmut | 0:5150b09127e3 | 170 | break; |
helmut | 0:5150b09127e3 | 171 | case 11: |
helmut | 0:5150b09127e3 | 172 | point_read = Instrument11_; |
helmut | 0:5150b09127e3 | 173 | text = "Instrument11_"; |
helmut | 0:5150b09127e3 | 174 | break; |
helmut | 0:5150b09127e3 | 175 | case 12: |
helmut | 0:5150b09127e3 | 176 | point_read = Instrument12_; |
helmut | 0:5150b09127e3 | 177 | text = "Instrument12_"; |
helmut | 0:5150b09127e3 | 178 | break; |
helmut | 0:5150b09127e3 | 179 | case 13: |
helmut | 0:5150b09127e3 | 180 | point_read = Instrument13_; |
helmut | 0:5150b09127e3 | 181 | text = "Instrument13_"; |
helmut | 0:5150b09127e3 | 182 | break; |
helmut | 0:5150b09127e3 | 183 | case 14: |
helmut | 0:5150b09127e3 | 184 | point_read = Instrument14_; |
helmut | 0:5150b09127e3 | 185 | text = "Instrument14_"; |
helmut | 0:5150b09127e3 | 186 | break; |
helmut | 0:5150b09127e3 | 187 | case 15: |
helmut | 0:5150b09127e3 | 188 | point_read = Instrument15_; |
helmut | 0:5150b09127e3 | 189 | text = "Instrument15_"; |
helmut | 0:5150b09127e3 | 190 | break; |
helmut | 0:5150b09127e3 | 191 | case 16: |
helmut | 0:5150b09127e3 | 192 | point_read = Instrument16_; |
helmut | 0:5150b09127e3 | 193 | text = "Instrument16_"; |
helmut | 0:5150b09127e3 | 194 | break; |
helmut | 0:5150b09127e3 | 195 | case 17: |
helmut | 0:5150b09127e3 | 196 | point_read = Instrument17_; |
helmut | 0:5150b09127e3 | 197 | text = "Instrument17_"; |
helmut | 0:5150b09127e3 | 198 | break; |
helmut | 0:5150b09127e3 | 199 | default: |
helmut | 0:5150b09127e3 | 200 | point_read = 0; |
helmut | 0:5150b09127e3 | 201 | text = ""; |
helmut | 0:5150b09127e3 | 202 | break; |
helmut | 0:5150b09127e3 | 203 | } //end switch |
helmut | 0:5150b09127e3 | 204 | |
helmut | 0:5150b09127e3 | 205 | lengh_index = (1/(note * point_read))*100; |
helmut | 0:5150b09127e3 | 206 | |
helmut | 0:5150b09127e3 | 207 | for (index = 0; index < lengh; ++index) { //as long the tone up to 1s |
helmut | 0:5150b09127e3 | 208 | add = Check_loud(); |
helmut | 0:5150b09127e3 | 209 | if (combind == 0) { //no combine |
helmut | 0:5150b09127e3 | 210 | myled2 = true; |
helmut | 0:5150b09127e3 | 211 | for (index1 = 0; index1 < point_read;++index1) { //upper wave |
helmut | 0:5150b09127e3 | 212 | text = text + char(index1); |
helmut | 0:5150b09127e3 | 213 | wait(lengh_index); |
helmut | 0:5150b09127e3 | 214 | aout = 0.5 + (*text * (loud - add) * 0.5); |
helmut | 0:5150b09127e3 | 215 | if (*text == 1) { |
helmut | 0:5150b09127e3 | 216 | } |
helmut | 0:5150b09127e3 | 217 | } |
helmut | 0:5150b09127e3 | 218 | for (index2 = 0; index2 < point_read; ++index2) { //lower wave |
helmut | 0:5150b09127e3 | 219 | text = text + char(index2); |
helmut | 0:5150b09127e3 | 220 | wait(lengh_index); |
helmut | 0:5150b09127e3 | 221 | aout = 0.5 - (*text * (loud - add) * 0.5); |
helmut | 0:5150b09127e3 | 222 | if (*text == 1) { |
helmut | 0:5150b09127e3 | 223 | } |
helmut | 0:5150b09127e3 | 224 | } |
helmut | 0:5150b09127e3 | 225 | myled2 = false; |
helmut | 0:5150b09127e3 | 226 | } else { |
helmut | 0:5150b09127e3 | 227 | myled3 = true; |
helmut | 0:5150b09127e3 | 228 | for (index1 = 0; index1 < point_read;++index1) { //upper wave |
helmut | 0:5150b09127e3 | 229 | text = text + char(index1); |
helmut | 0:5150b09127e3 | 230 | text2 = text + char(index1 - 1); |
helmut | 0:5150b09127e3 | 231 | wait(lengh_index); |
helmut | 0:5150b09127e3 | 232 | aout = 0.5 + ((*text + *text2) * 0.5 * (loud - add) * 0.5); |
helmut | 0:5150b09127e3 | 233 | if (*text == 1) { |
helmut | 0:5150b09127e3 | 234 | } |
helmut | 0:5150b09127e3 | 235 | } |
helmut | 0:5150b09127e3 | 236 | for (index2 = 0; index2 < point_read; ++index2) { //lower wave |
helmut | 0:5150b09127e3 | 237 | text = text + char(index2); |
helmut | 0:5150b09127e3 | 238 | text2 = text + char(index1 - 1); |
helmut | 0:5150b09127e3 | 239 | wait(lengh_index); |
helmut | 0:5150b09127e3 | 240 | aout = 0.5 - ((*text + *text2) * 0.5 * (loud - add) * 0.5); |
helmut | 0:5150b09127e3 | 241 | if (*text == 1) { |
helmut | 0:5150b09127e3 | 242 | } |
helmut | 0:5150b09127e3 | 243 | } |
helmut | 0:5150b09127e3 | 244 | myled3 = false; |
helmut | 0:5150b09127e3 | 245 | combind = 0; //combine clear |
helmut | 0:5150b09127e3 | 246 | } |
helmut | 0:5150b09127e3 | 247 | } |
helmut | 0:5150b09127e3 | 248 | if (combine == true) { // next combine set |
helmut | 0:5150b09127e3 | 249 | combind = note; |
helmut | 0:5150b09127e3 | 250 | } |
helmut | 0:5150b09127e3 | 251 | } |
helmut | 0:5150b09127e3 | 252 | |
helmut | 0:5150b09127e3 | 253 | void Play_Pause(float lengh) { |
helmut | 0:5150b09127e3 | 254 | AnalogOut aout(p18); |
helmut | 0:5150b09127e3 | 255 | |
helmut | 0:5150b09127e3 | 256 | myled4 = true; |
helmut | 0:5150b09127e3 | 257 | aout = 0.5; |
helmut | 0:5150b09127e3 | 258 | wait(lengh); |
helmut | 0:5150b09127e3 | 259 | myled4 = false; |
helmut | 0:5150b09127e3 | 260 | } |
helmut | 0:5150b09127e3 | 261 | |
helmut | 0:5150b09127e3 | 262 | float Check_loud() { |
helmut | 0:5150b09127e3 | 263 | DigitalIn up(p5); |
helmut | 0:5150b09127e3 | 264 | DigitalIn down(p6); |
helmut | 0:5150b09127e3 | 265 | /* while (up.read == 0 | index < 10000 */ |
helmut | 0:5150b09127e3 | 266 | |
helmut | 0:5150b09127e3 | 267 | return 0; |
helmut | 0:5150b09127e3 | 268 | } |