Dependencies:   mbed TextLCD

Revision:
4:28134b3439bc
Parent:
1:3ae60fd51e2b
Child:
5:b354825e5372
--- a/functions.cpp	Fri Oct 26 17:52:40 2018 +0000
+++ b/functions.cpp	Mon Nov 12 00:12:11 2018 +0000
@@ -1,149 +1,382 @@
 #include "stdafx.h"
 #include "functions.h"
 
-float temp_cal(AnalogIn temp_sen)
+void modeProcess()
 {
-    float a[10], tempC;
-    float sum = 0;
+    temp_measure = temp_cal();
+    if (modeStatus) {
+        fanLevel_dis = fan_cal(temp_measure);
+        lightLevel_dis = light_cal();
+    } else {
+        fanLevel_dis = fanLevel_man;
+        lightLevel_dis = lightLevel_man;
+    }
+    setPWM(fanLevel_dis);
+}
+float temp_cal()
+{
+    float a[10], tempC, sum = 0;
     int i;
     for(i = 0; i < 10; i++) {
         a[i] = temp_sen.read();
         wait(.05);
         sum += a[i];
     }
-    tempC=(sum*3.3*10); //3.685503686
+    tempC=sum*3.3*10;
     //tempF=(9.0*tempC)/5.0 + 32.0;
     return tempC;
 }
 
-float light_cal (AnalogIn light_sen)
+int light_cal()
 {
-    float vl[10], vol, sum1 = 0;
-    int i;
+    float vl[10], vol, sum = 0;
+    int i, lvl;
     for (i = 0; i < 10; i++) {
         vl[i] = light_sen.read();
         wait(.05);
-        sum1 += vl[i];
+        sum += vl[i];
     }
     //output voltage of light sensor
-    vol = sum1*3.3*10;
-    return vol;
+    vol = sum*3.3*10;
+    if ((vol > 0) && (vol <= 66)) {
+        lvl = 4;
+    } else if ((vol > 66) && (vol <= 132)) {
+        lvl = 3;
+    } else if ((vol > 132) && (vol <= 198)) {
+        lvl = 2;
+    } else if ((vol > 198) && (vol <= 264)) {
+        lvl = 1;
+    } else {
+        lvl = 0;
+    }
+    return lvl;
+}
+
+int fan_cal(int &temp)
+{
+    int speed;
+    int range = (high - low)/3;
+    int mid1 = low + range;
+    int mid2 = high - range;
+    if (temp <= low) {
+        speed = 0;
+    } else if ((temp > low) && (temp <= mid1)) {
+        speed = 1;
+    } else if ((temp > mid1) && (temp <= mid2)) {
+        speed = 2;
+    } else if ((temp > mid2) && (temp <= high)) {
+        speed = 3;
+    } else {
+        speed = 4;
+    }
+    return speed;
+}
+
+void setPWM(int &speed)
+{
+    if (speed == 0) {
+        pwm.period(0);
+        pwm.write(0);
+    } else if (speed == 1) {
+        pwm.period(1);
+        pwm.write(0.25);
+    } else if (speed == 2) {
+        pwm.period(1);
+        pwm.write(0.5);
+    } else if (speed == 3) {
+        pwm.period(1);
+        pwm.write(0.75);
+    } else {
+        pwm.period(1);
+        pwm.write(1);
+    }
 }
 
-void buttonMode(int &state, DigitalIn buttonMode)
+void buttonEnter()
+{
+    if ((enter == 0) && (state == 0)) {
+        state = 1;
+        wait(0.2);
+    }
+    if ((enter == 0) && (state == 1) && (type == 0)) {
+        type = 1;
+        wait(0.2);
+    }
+    if ((enter == 0) && (state == 1) && (type == 1)) {
+        state = 2;
+        type = 0;
+        wait(0.2);
+    }
+    if ((enter == 0) && (state == 1) && (type == 2)) {
+        state = 3;
+    }
+    if ((enter == 0) && (state == 1) && (type == 3)) {
+        state = 4;
+    }
+    if ((enter == 0) && (state == 1) && (type == 4)) {
+        state = 5;
+        type = 0;
+    }
+    if ((enter == 0) && (state == 2) && (type == 0)) {
+        type = 1;
+        wait(0.2);
+    }
+    if ((enter == 0) && (state == 2) && (type == 1)) {
+        type = 2;
+        wait(0.2);
+    }
+    if ((enter == 0) && (state == 2) && (type == 2)) {
+        type = 1;
+        wait(0.2);
+    }
+    if ((enter == 0) && (state == 5) && (type == 0)) {
+        type = 1;
+        modeStatus = !modeStatus;
+    }
+    if ((enter == 0) && (state == 5) && (type == 1)) {
+        modeStatus = !modeStatus;
+    }
+}
+
+void buttonBack()
 {
-    if (buttonMode == 0) {
-        if (state == 0) {
-            state = 1;
-        } else if (state == 1) {
-            state = 2;
-        } else {
-            state = 0;
+    if ((back == 0) && (state == 1) && (type == 0)) {
+        state = 0;
+        type = 0;
+        wait(0.2);
+    }
+    if ((back == 0) && (state == 1) && ((type == 1) || (type == 2) || (type == 3) || (type == 4))) {
+        state = 1;
+        type = 0;
+        wait(0.2);
+    }
+    if ((back == 0) && (state == 2) && (type == 0)) {
+        state = 1;
+        // type = 1;
+        wait(0.2);
+    }
+    if ((back == 0) && (state == 2) && ((type == 1) || (type == 2))) {
+        state = 2;
+        type = 0;
+        wait(0.2);
+    }
+    if ((back == 0) && (state == 3)) {
+        state = 1;
+        type = 2;
+        wait(0.2);
+    }
+    if ((back == 0) && (state == 4)) {
+        state = 1;
+        type = 3;
+        wait(0.2);
+    }
+    if ((back == 0) && (state == 5)) {
+        state = 1;
+        type = 4;
+        wait(0.2);
+    }
+}
+
+void buttonUp()
+{
+    if ((up == 0) && (state == 1) && (type == 0)) {
+        type = 1;
+        wait(0.2);
+    }
+    if ((up == 0) && (state == 1) && (type == 1)) {
+        type = 2;
+        wait(0.2);
+    }
+    if ((up == 0) && (state == 1) && (type == 2)) {
+        type = 3;
+        wait(0.2);
+    }
+    if ((up == 0) && (state == 1) && (type == 3)) {
+        type = 4;
+        wait(0.2);
+    }
+    if ((up == 0) && (state == 2) && (type == 1)) {
+        low++;
+    }
+    if ((up == 0) && (state == 2) && (type == 2)) {
+        high++;
+    }
+    if ((up == 0) && (state == 3)) {
+        if (lightLevel_man < 4) {
+            lightLevel_man++;
+        }
+    }
+    if ((up == 0) && (state == 4)) {
+        if (fanLevel_man < 4) {
+            fanLevel_man++;
         }
     }
 }
 
-int buttonUp(DigitalIn buttonUp, int &value)
+void buttonDown()
 {
-    if (buttonUp == 0) {
-        value++;
+    if ((down == 0) && (state == 1) && (type == 4)) {
+        type = 3;
+        wait(0.2);
+    }
+    if ((down == 0) && (state == 1) && (type == 3)) {
+        type = 2;
+        wait(0.2);
+    }
+    if ((down == 0) && (state == 1) && (type == 2)) {
+        type = 1;
+        wait(0.2);
     }
-    return value;
-}
-
-int buttonDown(DigitalIn buttonDown, int &value)
-{
-    if (buttonUp == 0) {
-        value--;
+    if ((down == 0) && (state == 1) && (type == 1)) {
+        type = 0;
+        wait(0.2);
+    }
+    if ((down == 0) && (state == 2) && (type == 1)) {
+        low--;
+    }
+    if ((down == 0) && (state == 2) && (type == 2)) {
+        high--;
     }
-    return value;
+    if ((down == 0) && (state == 3)) {
+        if (lightLevel_man > 0) {
+            lightLevel_man--;
+        }
+    }
+    if ((down == 0) && (state == 4)) {
+        if (fanLevel_man > 0) {
+            fanLevel_man--;
+        }
+    }
 }
 
-void mainScreen(TextLCD &lcd, AnalogIn &temp_sen, AnalogIn &light_sen, int &state, int &temp_val, int &light_val, int &fan_val)
+void mainScreen()
 {
-    int vl_temp = 0;
-    float vl_light = 0;
-    if (state == 0) {
-        vl_temp = temp_cal(temp_sen);
-        vl_light = light_cal(light_sen);
-        if (temp_val != vl_temp) {
-            lcd.cls();
-            lcd.printf("Temp: %i\n",vl_temp);
-            wait(0.1);
-            temp_val = vl_temp;
-        } else {
-            lcd.cls();
-            lcd.printf("Temp: %i\n",temp_val);
-            //lcd.printf("Light: %.2f \n", light_val);
-            wait(0.1);
-        }
-        lcd.printf("Light: %.2f \n", vl_light);
+    modeProcess();
+    buttonEnter();
+    lcd.cls();
+    lcd.printf("T %i  L %i  F %i \n",temp_measure, lightLevel_dis, fanLevel_dis);
+    if (modeStatus) {
+        lcd.printf("Auto mode: ON\n");
+    } else {
+        lcd.printf("Auto mode: OFF\n");
     }
-    if (state == 1) {
-
-
+    wait(0.2);
+}
+void optionScreen()
+{
+    if(type == 0) {
+        lcd.cls();
+        lcd.printf("  Set Temp\n");
+        lcd.printf("  Set Light");
+        wait(0.2);
+        buttonBack();
+        buttonUp();
     }
-}
-void optionScreen(TextLCD &lcd, int &state, int &pointer, DigitalIn &up,  DigitalIn &down)
-{
-    if(pointer == 0) {
+    if (type == 1) {
+        buttonEnter();
+        buttonUp();
+        buttonDown();
+        buttonBack();
         lcd.cls();
         lcd.printf("o Set Temp\n");
         lcd.printf("  Set Light");
         wait(0.2);
     }
-    if (up == 0 && down == 1 && pointer == 0) {
-        pointer++; // pointer = 1
+    if (type == 2) {
         lcd.cls();
         lcd.printf("  Set Temp\n");
         lcd.printf("o Set Light");
         wait(0.2);
-    }
-    if (up == 0 && down == 1 && pointer == 1) {
-        pointer++; // pointer = 2
-        lcd.cls();
-        lcd.printf("o Set Fan\n");
-        lcd.printf("  Set Bluetooth");
-        wait(0.2);
+        buttonEnter();
+        buttonUp();
+        buttonDown();
+        buttonBack();
     }
-    if (up == 0 && down == 1 && pointer == 2) {
-        pointer++; // pointer = 3
-        lcd.cls();
-        lcd.printf("  Set Fan\n");
-        lcd.printf("o Set Bluetooth");
-        wait(0.2);
-    }
-    if (up == 0 && down == 1 && pointer == 3) {
-        lcd.cls();
-        lcd.printf("  Set Fan\n");
-        lcd.printf("o Set Bluetooth");
-        wait(0.2);
-    }
-    if (up == 1 && down == 0 &&  pointer == 3) {
-        pointer--; // pointer = 2
+    if (type == 3) {
         lcd.cls();
         lcd.printf("o Set Fan\n");
-        lcd.printf("  Set Bluetooth");
+        lcd.printf("  Set Mode");
         wait(0.2);
+        buttonEnter();
+        buttonUp();
+        buttonDown();
+        buttonBack();
+    }
+    if (type == 4) {
+        lcd.cls();
+        lcd.printf("  Set Fan\n");
+        lcd.printf("o Set Mode");
+        wait(0.2);
+        buttonEnter();
+        buttonUp();
+        buttonDown();
+        buttonBack();
     }
-    if (up == 1 && down == 0 &&  pointer == 2) {
-        pointer--; // pointer = 1
+}
+
+void temperatureScreen()
+{
+    if (type == 0) {
         lcd.cls();
-        lcd.printf("  Set Temp\n");
-        lcd.printf("o Set Light");
+        lcd.printf("Temp setup\n");
+        lcd.printf(" Low:%i  High:%i", low, high);
         wait(0.2);
+        buttonEnter();
+        buttonBack();
+    }
+    if (type == 1)  {
+        lcd.cls();
+        lcd.printf("Temp setup\n");
+        lcd.printf("oLow:%i  High:%i", low, high);
+        wait(0.2);
+        buttonEnter();
+        buttonUp();
+        buttonDown();
+        buttonBack();
     }
-    if (up == 1 && down == 0 &&  pointer == 1) {
-        pointer--; // pointer = 1
+    if (type == 2) {
         lcd.cls();
-        lcd.printf("o Set Temp\n");
-        lcd.printf("  Set Light");
+        lcd.printf("Temp setup\n");
+        lcd.printf(" Low:%i oHigh:%i", low, high);
         wait(0.2);
+        buttonEnter();
+        buttonUp();
+        buttonDown();
+        buttonBack();
     }
-    if (up == 1 && down == 0 &&  pointer == 0) {
-        lcd.cls();
-        lcd.printf("o Set Temp\n");
-        lcd.printf("  Set Light");
-        wait(0.2);
+}
+
+void lightScreen()
+{
+    lcd.cls();
+    lcd.printf("Light setup\n");
+    lcd.printf("Brightness: %i", lightLevel_man);
+    wait(0.2);
+    buttonUp();
+    buttonDown();
+    buttonBack();
+}
+
+void fanScreen()
+{
+    lcd.cls();
+    lcd.printf("Fan setup\n");
+    lcd.printf("Speed: %i", fanLevel_man);
+    wait(0.2);
+    buttonUp();
+    buttonDown();
+    buttonBack();
+}
+void modeScreen()
+{
+    lcd.cls();
+    lcd.printf("Mode setup\n");
+    if (modeStatus) {
+        lcd.printf("Auto: ON");
+    } else {
+        lcd.printf("Auto: OFF");
     }
-}
\ No newline at end of file
+    wait(0.2);
+    buttonEnter();
+    buttonBack();
+}