smart-home / Mbed 2 deprecated smart-home-final-version

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers functions.cpp Source File

functions.cpp

00001 #include "stdafx.h"
00002 #include "functions.h"
00003 
00004 void modeProcess()
00005 {
00006     temp_measure = temp_cal();
00007     if (modeStatus) {
00008         fanLevel_dis = fan_cal(temp_measure);
00009         lightLevel_dis = light_cal();
00010     } else {
00011         fanLevel_dis = fanLevel_man;
00012         lightLevel_dis = lightLevel_man;
00013     }
00014     setPWM(fanLevel_dis);
00015     setLED(lightLevel_dis);
00016 }
00017 float temp_cal()
00018 {
00019     float a[10], tempC, sum = 0;
00020     int i;
00021     for(i = 0; i < 10; i++) {
00022         a[i] = temp_sen.read();
00023         wait(.05);
00024         sum += a[i];
00025     }
00026     tempC=sum*3.3*10;
00027     //tempF=(9.0*tempC)/5.0 + 32.0;
00028     return tempC;
00029 }
00030 
00031 int light_cal()
00032 {
00033     float vl[10], vol, sum = 0;
00034     int i, lvl;
00035     for (i = 0; i < 10; i++) {
00036         vl[i] = light_sen.read();
00037         wait(.05);
00038         sum += vl[i];
00039     }
00040     //output voltage of light sensor
00041     vol = sum*3.3*10;
00042     if ((vol > 0) && (vol <= 66)) {
00043         lvl = 0;
00044     } else if ((vol > 66) && (vol <= 132)) {
00045         lvl = 1;
00046     } else if ((vol > 132) && (vol <= 198)) {
00047         lvl = 2;
00048     } else if ((vol > 198) && (vol <= 264)) {
00049         lvl = 3;
00050     } else {
00051         lvl = 4;
00052     }
00053     return lvl;
00054 }
00055 
00056 int fan_cal(int &temp)
00057 {
00058     int speed;
00059     int range = (high - low)/3;
00060     int mid1 = low + range;
00061     int mid2 = high - range;
00062     if (temp <= low) {
00063         speed = 0;
00064     } else if ((temp > low) && (temp <= mid1)) {
00065         speed = 1;
00066     } else if ((temp > mid1) && (temp <= mid2)) {
00067         speed = 2;
00068     } else if ((temp > mid2) && (temp <= high)) {
00069         speed = 3;
00070     } else {
00071         speed = 4;
00072     }
00073     return speed;
00074 }
00075 
00076 void setLED(int &light_value)
00077 {
00078     if (light_value == 0) {
00079         led_o.period(0.01);
00080         led_o.write(1);
00081     } else if (light_value == 1) {
00082         led_o.period(0.01);
00083         led_o.write(0.75);
00084     } else if (light_value == 2) {
00085         led_o.period(0.01);
00086         led_o.write(0.5);
00087     } else if (light_value == 3) {
00088         led_o.period(0.01);
00089         led_o.write(0.25);
00090     } else {
00091         led_o.period(0.1);
00092         led_o.write(0);
00093     }
00094 }
00095 
00096 void setPWM(int &speed)
00097 {
00098     if (speed == 4) {
00099         pwm.period(0);
00100         pwm.write(0);
00101     } else if (speed == 3) {
00102         pwm.period(1);
00103         pwm.write(0.25);
00104     } else if (speed == 2) {
00105         pwm.period(1);
00106         pwm.write(0.5);
00107     } else if (speed == 1) {
00108         pwm.period(1);
00109         pwm.write(0.75);
00110     } else {
00111         pwm.period(1);
00112         pwm.write(1);
00113     }
00114 }
00115 
00116 void buttonEnter()
00117 {
00118     if ((enter == 0) && (state == 0)) {
00119         state = 1;
00120         wait(0.2);
00121     }
00122     if ((enter == 0) && (state == 1) && (type == 0)) {
00123         type = 1;
00124         wait(0.2);
00125     }
00126     if ((enter == 0) && (state == 1) && (type == 1)) {
00127         state = 2;
00128         type = 0;
00129         wait(0.2);
00130     }
00131     if ((enter == 0) && (state == 1) && (type == 2)) {
00132         state = 3;
00133     }
00134     if ((enter == 0) && (state == 1) && (type == 3)) {
00135         state = 4;
00136     }
00137     if ((enter == 0) && (state == 1) && (type == 4)) {
00138         state = 5;
00139         type = 0;
00140     }
00141     if ((enter == 0) && (state == 2) && (type == 0)) {
00142         type = 1;
00143         wait(0.2);
00144     }
00145     if ((enter == 0) && (state == 2) && (type == 1)) {
00146         type = 2;
00147         wait(0.2);
00148     }
00149     if ((enter == 0) && (state == 2) && (type == 2)) {
00150         type = 1;
00151         wait(0.2);
00152     }
00153     if ((enter == 0) && (state == 5) && (type == 0)) {
00154         type = 1;
00155         modeStatus = !modeStatus;
00156     }
00157     if ((enter == 0) && (state == 5) && (type == 1)) {
00158         modeStatus = !modeStatus;
00159     }
00160 }
00161 
00162 void buttonBack()
00163 {
00164     if ((back == 0) && (state == 1) && (type == 0)) {
00165         state = 0;
00166         type = 0;
00167         wait(0.2);
00168     }
00169     if ((back == 0) && (state == 1) && ((type == 1) || (type == 2) || (type == 3) || (type == 4))) {
00170         state = 1;
00171         type = 0;
00172         wait(0.2);
00173     }
00174     if ((back == 0) && (state == 2) && (type == 0)) {
00175         state = 1;
00176         // type = 1;
00177         wait(0.2);
00178     }
00179     if ((back == 0) && (state == 2) && ((type == 1) || (type == 2))) {
00180         state = 2;
00181         type = 0;
00182         wait(0.2);
00183     }
00184     if ((back == 0) && (state == 3)) {
00185         state = 1;
00186         type = 2;
00187         wait(0.2);
00188     }
00189     if ((back == 0) && (state == 4)) {
00190         state = 1;
00191         type = 3;
00192         wait(0.2);
00193     }
00194     if ((back == 0) && (state == 5)) {
00195         state = 1;
00196         type = 4;
00197         wait(0.2);
00198     }
00199 }
00200 
00201 void buttonUp()
00202 {
00203     if ((up == 0) && (state == 1) && (type == 0)) {
00204         type = 1;
00205         wait(0.2);
00206     }
00207     if ((up == 0) && (state == 1) && (type == 1)) {
00208         type = 2;
00209         wait(0.2);
00210     }
00211     if ((up == 0) && (state == 1) && (type == 2)) {
00212         type = 3;
00213         wait(0.2);
00214     }
00215     if ((up == 0) && (state == 1) && (type == 3)) {
00216         type = 4;
00217         wait(0.2);
00218     }
00219     if ((up == 0) && (state == 2) && (type == 1)) {
00220         low++;
00221     }
00222     if ((up == 0) && (state == 2) && (type == 2)) {
00223         high++;
00224     }
00225     if ((up == 0) && (state == 3)) {
00226         if (lightLevel_man < 4) {
00227             lightLevel_man++;
00228         }
00229     }
00230     if ((up == 0) && (state == 4)) {
00231         if (fanLevel_man < 4) {
00232             fanLevel_man++;
00233         }
00234     }
00235 }
00236 
00237 void buttonDown()
00238 {
00239     if ((down == 0) && (state == 1) && (type == 4)) {
00240         type = 3;
00241         wait(0.2);
00242     }
00243     if ((down == 0) && (state == 1) && (type == 3)) {
00244         type = 2;
00245         wait(0.2);
00246     }
00247     if ((down == 0) && (state == 1) && (type == 2)) {
00248         type = 1;
00249         wait(0.2);
00250     }
00251     if ((down == 0) && (state == 1) && (type == 1)) {
00252         type = 0;
00253         wait(0.2);
00254     }
00255     if ((down == 0) && (state == 2) && (type == 1)) {
00256         low--;
00257     }
00258     if ((down == 0) && (state == 2) && (type == 2)) {
00259         high--;
00260     }
00261     if ((down == 0) && (state == 3)) {
00262         if (lightLevel_man > 0) {
00263             lightLevel_man--;
00264         }
00265     }
00266     if ((down == 0) && (state == 4)) {
00267         if (fanLevel_man > 0) {
00268             fanLevel_man--;
00269         }
00270     }
00271 }
00272 
00273 void mainScreen()
00274 {
00275     modeProcess();
00276     buttonEnter();
00277     lcd.cls();
00278     lcd.printf("T %i  L %i  F %i \n",temp_measure, lightLevel_dis, fanLevel_dis);
00279     if (modeStatus) {
00280         lcd.printf("Auto mode: ON\n");
00281     } else {
00282         lcd.printf("Auto mode: OFF\n");
00283     }
00284     wait(0.2);
00285 }
00286 void optionScreen()
00287 {
00288     if(type == 0) {
00289         lcd.cls();
00290         lcd.printf("  Set Temp\n");
00291         lcd.printf("  Set Light");
00292         wait(0.2);
00293         buttonBack();
00294         buttonUp();
00295     }
00296     if (type == 1) {
00297         buttonEnter();
00298         buttonUp();
00299         buttonDown();
00300         buttonBack();
00301         lcd.cls();
00302         lcd.printf("o Set Temp\n");
00303         lcd.printf("  Set Light");
00304         wait(0.2);
00305     }
00306     if (type == 2) {
00307         lcd.cls();
00308         lcd.printf("  Set Temp\n");
00309         lcd.printf("o Set Light");
00310         wait(0.2);
00311         buttonEnter();
00312         buttonUp();
00313         buttonDown();
00314         buttonBack();
00315     }
00316     if (type == 3) {
00317         lcd.cls();
00318         lcd.printf("o Set Fan\n");
00319         lcd.printf("  Set Mode");
00320         wait(0.2);
00321         buttonEnter();
00322         buttonUp();
00323         buttonDown();
00324         buttonBack();
00325     }
00326     if (type == 4) {
00327         lcd.cls();
00328         lcd.printf("  Set Fan\n");
00329         lcd.printf("o Set Mode");
00330         wait(0.2);
00331         buttonEnter();
00332         buttonUp();
00333         buttonDown();
00334         buttonBack();
00335     }
00336 }
00337 
00338 void temperatureScreen()
00339 {
00340     if (type == 0) {
00341         lcd.cls();
00342         lcd.printf("Temp setup\n");
00343         lcd.printf(" Low:%i  High:%i", low, high);
00344         wait(0.2);
00345         buttonEnter();
00346         buttonBack();
00347     }
00348     if (type == 1)  {
00349         lcd.cls();
00350         lcd.printf("Temp setup\n");
00351         lcd.printf("oLow:%i  High:%i", low, high);
00352         wait(0.2);
00353         buttonEnter();
00354         buttonUp();
00355         buttonDown();
00356         buttonBack();
00357     }
00358     if (type == 2) {
00359         lcd.cls();
00360         lcd.printf("Temp setup\n");
00361         lcd.printf(" Low:%i oHigh:%i", low, high);
00362         wait(0.2);
00363         buttonEnter();
00364         buttonUp();
00365         buttonDown();
00366         buttonBack();
00367     }
00368 }
00369 
00370 void lightScreen()
00371 {
00372     lcd.cls();
00373     lcd.printf("Light setup\n");
00374     lcd.printf("Brightness: %i", lightLevel_man);
00375     wait(0.2);
00376     buttonUp();
00377     buttonDown();
00378     buttonBack();
00379 }
00380 
00381 void fanScreen()
00382 {
00383     lcd.cls();
00384     lcd.printf("Fan setup\n");
00385     lcd.printf("Speed: %i", fanLevel_man);
00386     wait(0.2);
00387     buttonUp();
00388     buttonDown();
00389     buttonBack();
00390 }
00391 void modeScreen()
00392 {
00393     lcd.cls();
00394     lcd.printf("Mode setup\n");
00395     if (modeStatus) {
00396         lcd.printf("Auto: ON");
00397     } else {
00398         lcd.printf("Auto: OFF");
00399     }
00400     wait(0.2);
00401     buttonEnter();
00402     buttonBack();
00403 }