Guest User

Untitled

a guest
Oct 11th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.27 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SdFat.h>
  3. #include <SPIflash.h>
  4. #include <UTFT.h>
  5. #include <UTFT_SPIflash.h>
  6. #include <CapacitiveSensor.h>
  7. #include <DCF77.h>
  8. #include <Time.h>
  9. #include <UTFT_SdRaw.h>
  10. #include <UTouch.h>
  11.  
  12. #ifndef __time_t_defined
  13. typedef unsigned long time_t;
  14. #endif
  15.  
  16. #define DCF_PIN 23
  17. #define DCF_INTERRUPT 23
  18.  
  19. #define SD_CHIP_SELECT  53
  20. SdFat sd;
  21.  
  22. time_t prevDisplay = 0;
  23. time_t time;
  24.  
  25. extern uint8_t SmallFont[];
  26.  
  27. int count = 0 ;
  28. const int maxpin = 8;
  29. const int minpin = 9;
  30.  
  31. int maxpinstate = 0;
  32. int minpinstate = 0;
  33. long minlvl = 0;
  34. long maxlvl = 0;
  35. long offset = 0;
  36. long prozent1 = 0;
  37. long prozent2 = 0;
  38. int firststart = 1;
  39. byte dispScreen = 1;
  40. byte mistScreen = 1;
  41. byte fanScreen = 1;
  42. byte fogScreen = 1;
  43. uint8_t  fileid;
  44. uint16_t filetype;
  45. byte zz = 0;
  46.  
  47. DCF77 DCF = DCF77(DCF_PIN, DCF_INTERRUPT);
  48. CapacitiveSensor   liquidLevel = CapacitiveSensor(12, 13);
  49. UTFT myGLCD(CTE50, 25, 26, 27, 28);
  50. UTouch  myTouch( 6, 5, 4, 3, 2);
  51. SPIflash      myFlash(52);
  52. UTFT_SPIflash myFlashFiles(&myGLCD, &myFlash);
  53. UTFT_SdRaw myFiles(&myGLCD);
  54.  
  55. void setup()
  56. {
  57.   Serial.begin(9600);
  58.   DCF.Start();
  59.   setSyncInterval(30);
  60.   setSyncProvider(DCF.getTime);
  61.   Serial.println("Start!");
  62.   pinMode(maxpin, INPUT);
  63.   pinMode(minpin, INPUT);
  64.   myGLCD.InitLCD();
  65.   myGLCD.setFont(SmallFont);
  66.   myGLCD.clrScr();
  67.   myGLCD.setDisplayPage(1);
  68.   myFlash.begin();
  69.   myTouch.InitTouch();
  70.   myTouch.setPrecision(PREC_MEDIUM);
  71.  
  72.   bool mysd = 0;
  73.   // see if the card is present and can be initialized:
  74.   while (!mysd)
  75.   {
  76.     if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) {
  77.       Serial.println(F("Card failed, or not present"));
  78.       Serial.println(F("Retrying...."));
  79.     }
  80.     else
  81.     {
  82.       mysd = 1;
  83.       Serial.println(F("Card initialised."));
  84.     }
  85.   }
  86.   Serial.println(F("Initialising LCD."));
  87.  
  88.  
  89.   if (myFlash.ID_device == 0)
  90.   {
  91.     myGLCD.print("Unknown flash device!", 0, 0);
  92.     Serial.println("Unknown flash device!");
  93.     while (true) {};
  94.   }
  95.   
  96.   Serial.println("Waiting for DCF77 time ... ");
  97.   Serial.println("It will take at least 2 minutes until a first update can be processed.");
  98.  
  99.   myGLCD.setWritePage(0);
  100.   myGLCD.clrScr();
  101.   myFiles.loadcpld(230, 40, 378, 268, "Logo.raw", 0, 4);
  102.   myFiles.loadcpld(340, 360, 149, 47, "Copy.raw", 0, 4);
  103.   myGLCD.setDisplayPage(0);
  104.   myFiles.loadcpld(0, 0, 800, 480, "1Home.raw", 1, 2);
  105.   myFiles.loadcpld(0, 0, 800, 480, "2Lights.raw", 2, 2);
  106.   myFiles.loadcpld(0, 0, 800, 480, "3TempGHL.raw", 3, 2);
  107.   myFiles.loadcpld(124, 365, 73, 75, "DegC.raw", 3, 6);
  108.   myFiles.loadcpld(0, 0, 800, 480, "46SchedG.raw", 4, 2);
  109.   myFiles.loadcpld(0, 0, 800, 480, "5MistG.raw", 7, 2);
  110.   myFiles.loadcpld(0, 0, 800, 480, "7Clock.raw", 5, 2);
  111.   myFiles.loadcpld(0, 0, 800, 480, "8Screen.raw", 6, 2);
  112.   
  113.   screenHome();
  114. }
  115.  
  116. void loop()
  117. {
  118.   time_t DCFtime = DCF.getTime(); // Check if new DCF77 time is available
  119.   fileid = 0;
  120.   filetype = 0;
  121.   count++ ;
  122.  
  123.   maxpinstate = digitalRead(maxpin);
  124.   minpinstate = digitalRead(minpin);
  125.   long total =  liquidLevel.capacitiveSensor(50);
  126.  
  127.   myGLCD.print("    ", 280 , 10);
  128.   myGLCD.printNumI(total, 280 , 10);
  129.  
  130.   if (firststart == 1) {
  131.  
  132.  
  133.     Serial.println("Warte auf MIN Wert...");
  134.     myGLCD.print("Warte auf MIN Wert...", 30, 20);
  135.     Serial.println("Warte auf MAX Wert...");
  136.     myGLCD.print("Warte auf MAX Wert...", 30, 50);
  137.  
  138.  
  139.     // if (minpinstate == HIGH) {
  140.     if (count == 3) {
  141.       Serial.println("MIN WERT GESETZT auf:");
  142.       myGLCD.print("MIN WERT GESETZT auf:", 30, 35);
  143.       minlvl = total;
  144.       Serial.print(minlvl);
  145.       myGLCD.printNumI(minlvl, 200, 35);
  146.       delay(500);
  147.     }
  148.  
  149.     // if (maxpinstate == HIGH) {
  150.     if (count == 5) {
  151.       Serial.println("MAX WERT GESETZT auf:");
  152.       myGLCD.print("MAX WERT GESETZT auf:", 30, 65);
  153.       maxlvl = total;
  154.       Serial.print(maxlvl);
  155.       myGLCD.printNumI(maxlvl, 200, 65);
  156.       delay(500);
  157.       firststart = 0;
  158.       myGLCD.clrScr();
  159.     }
  160.  
  161.   }
  162.   
  163.   if (firststart == 0) {
  164.     myGLCD.setDisplayPage(1);
  165.     screenHome();
  166.     processMyTouch();
  167.  
  168.     prozent1 = maxlvl / 100;
  169.     prozent2 = total / prozent1;
  170.  
  171.     myGLCD.print("           ", 40, 10);
  172.     myGLCD.print("           ", 40, 30);
  173.     myGLCD.print("           ", 40, 50);
  174.     myGLCD.print("           ", 40, 70);
  175.     myGLCD.print("           ", 40, 90);
  176.     myGLCD.print("           ", 40, 110);
  177.  
  178.     myGLCD.print("Max:", 10, 10);
  179.     myGLCD.print("Min:", 10, 30);
  180.     myGLCD.print("LVL:", 10, 50);
  181.     myGLCD.print("Hour:", 10, 70);
  182.     myGLCD.print("Min:", 10, 90);
  183.     myGLCD.print("Sek:", 10, 110);
  184.  
  185.     myGLCD.printNumI(maxlvl, 40, 10);
  186.     myGLCD.printNumI(minlvl, 40, 30);
  187.     myGLCD.printNumI(prozent2, 40, 50);
  188.     myGLCD.printNumI(hour(), 45, 70);
  189.     myGLCD.printNumI(minute(), 45, 90);
  190.     myGLCD.printNumI(second(), 45, 110);
  191.  
  192.  
  193.  
  194.     /*
  195.         Serial.println("max:");
  196.         Serial.println(maxlvl);
  197.         Serial.println("min:");
  198.         Serial.println(minlvl);
  199.         Serial.println("prozent1:");
  200.         Serial.println(prozent1);
  201.         Serial.println("prozent2:");
  202.         Serial.println(prozent2);
  203.         */
  204.   }
  205.  
  206.  
  207.   // delay (400);
  208. }
  209.  
  210. void DoScreen(byte page, byte screen) {
  211.   myGLCD.setDisplayPage(page);
  212.   myGLCD.setWritePage(page);
  213.   dispScreen = screen;
  214. }
  215.  
  216. void screenHome()
  217. {
  218.   DoScreen(1, 1);
  219.   Serial.println(F("Home Screen"));
  220. }
  221.  
  222. void screenLights()
  223. {
  224.   DoScreen(2, 2);
  225.   Serial.println(F("Lights Screen"));
  226. }
  227.  
  228. void screenTemp() {
  229.   DoScreen(3, 3);
  230.   Serial.println(F("Temp Screen"));
  231. }
  232.  
  233. void screenFogger()
  234. {
  235.   myFiles.loadcpld(21, 6, 336, 74, "FogTitle.raw", 4, 6);
  236.   DoScreen(4, 4);
  237.   Serial.println(F("Fogger Screen"));
  238.   //
  239. }
  240.  
  241. void screenMist()
  242. {
  243.   DoScreen(7, 5);
  244.   Serial.println(F("Misting Screen"));
  245. }
  246.  
  247. void screenFan()
  248. {
  249.   myFiles.loadcpld(21, 6, 336, 74, "FanTitle.raw", 4, 6);
  250.   DoScreen(4, 6);
  251.   Serial.println(F("Fan Screen"));
  252. }
  253.  
  254. void screenClock()
  255. {
  256.   DoScreen(5, 7);
  257.   Serial.println(F("Clock Screen"));
  258. }
  259.  
  260. void screenScreen()
  261. {
  262.   DoScreen(6, 8);
  263.   Serial.println(F("Screen Screen"));
  264. }
  265.  
  266. void processMyTouch()
  267. {
  268.   myTouch.read();
  269.   word x = myTouch.getX();
  270.   word y = myTouch.getY();
  271.   switch (dispScreen)
  272.   {
  273.     case 1:
  274.       if ((y >= 64) && (y < 114))
  275.       {
  276.         if ((x >= 371) && ( x < 421)) // 'Secret button'
  277.         {
  278.           Serial.println(F("SECRET MENU"));
  279.         }
  280.       }
  281.       if ((y >= 117) && (y < 235))
  282.       {
  283.  
  284.         if ((x >= 59) && (x <= 134)) // Light Settings Page
  285.         {
  286.           screenLights();
  287.         }
  288.  
  289.         if ((x >= 175) && (x <= 240)) // Temp Settings Page
  290.         {
  291.           screenTemp();
  292.         }
  293.  
  294.         if ((x >= 270) && (x <= 362)) // Fogger Settings Page
  295.         {
  296.           screenFogger();
  297.         }
  298.       }
  299.       if (( y >= 285) && (y < 411)) {
  300.         if ((x >= 46) && (x <= 141))
  301.         {
  302.           screenMist();
  303.         }
  304.         if ((x >= 171) && (x <= 237))
  305.         {
  306.           screenFan();
  307.         }
  308.         if ((x >= 270) && (x <= 370))
  309.         {
  310.           screenClock();
  311.           
  312.         }
  313.       }
  314.       if ((x >= 712) && (x <= 779) && (y >= 13) && (y <= 77))
  315.       {
  316.         screenScreen();
  317.       }
  318.       break;
  319.  
  320.  
  321.     case 2:
  322.       if ((x >= 363) && (x <= 444) && (y >= 422) && (y <= 479))
  323.       {          
  324.         //screenHome();
  325.       }
  326.       break;
  327.  
  328.  
  329.     case 3:
  330.       if ((x >= 363) && (x <= 444) && (y >= 422) && (y <= 479) || zz >= 20)
  331.       {
  332.         screenHome();
  333.       }
  334.       break;
  335.  
  336.     case 4:
  337.       if ((x >= 363) && (x < 445) && (y >= 422) && (y < 480))
  338.       {
  339.         screenHome();
  340.       }
  341.       break;
  342.  
  343.     case 5:
  344.       if ((x >= 363) && (x < 445) && (y >= 422) && (y < 480))
  345.       {
  346.         screenHome();
  347.       }
  348.       break;
  349.  
  350.     case 6:
  351.       if ((x >= 363) && (x < 445) && (y >= 422) && (y < 480))
  352.       {
  353.        screenHome();
  354.       }
  355.  
  356.       break;
  357.  
  358.     case 7:
  359.       if ((x >= 363) && (x <= 444) && (y >= 422) && (y < 480))
  360.       {
  361.         screenHome();
  362.       }
  363.       break;
  364.  
  365.     case 8:
  366.       if ((x >= 363) && (x <= 444) && (y >= 422) && (y < 480))
  367.       {
  368.         screenHome();
  369.       }
  370.  
  371.       break;
  372.     case 9:
  373.       if (((x >= 365) && (x <= 440)) && ((y >= 420) && (y < 480)))
  374.       {
  375.         screenHome();
  376.       }
  377.       break;
  378.   }
  379. }
  380.  
  381. void printDigits(int digits) {
  382.   // utility function for digital clock display: prints preceding colon and leading 0
  383.   Serial.print(":");
  384.   if (digits < 10)
  385.     Serial.print('0');
  386.   Serial.print(digits);
  387. }
  388.  
  389. unsigned long getDCFTime()
  390. {
  391.   time_t DCFtime = DCF.getTime();
  392.   // Indicator that a time check is done
  393.   if (DCFtime != 0) {
  394.     Serial.print("X");
  395.   }
  396.   return DCFtime;
  397. }
Advertisement
Add Comment
Please, Sign In to add comment