Advertisement
Guest User

CODE

a guest
Jun 27th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.04 KB | None | 0 0
  1. //Programming Package
  2.  
  3. //Inputs package (for sensors, excluding the touchscreen)
  4. #include <SD.h>
  5.  
  6. //Touchscreen packages
  7. #include <Adafruit_STMPE610.h>
  8. #include <Adafruit_ILI9341.h>
  9.  
  10. // This is calibration data for the raw touch data to the screen coordinates
  11. #define TS_MINX 150
  12. #define TS_MINY 130
  13. #define TS_MAXX 3800
  14. #define TS_MAXY 4000
  15.  
  16. //Declaring Touchscreen variables
  17. #define STMPE_CS 8
  18. Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
  19.  
  20. //The display
  21. #define TFT_CS 10
  22. #define TFT_DC 9
  23. #define SD_CS 4
  24. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  25.  
  26. //Receiving Stuff
  27. #include <SoftwareSerial.h>
  28. SoftwareSerial mySerial(0, 2);
  29.  
  30. byte counter = 0;
  31. short counter2 = 0;
  32. bool Analizer = false;
  33.  
  34. String* names;
  35. String* ports;
  36. String* mins;
  37. String* maxs;
  38.  
  39. bool Autorisation = false;
  40.  
  41. #define BUFFPIXEL 20
  42.  
  43. void bmpDraw(char *filename, uint16_t x, uint16_t y) {
  44.  
  45.   File     bmpFile;
  46.   int      bmpWidth, bmpHeight;   // W+H in pixels
  47.   uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  48.   uint32_t bmpImageoffset;        // Start of image data in file
  49.   uint32_t rowSize;               // Not always = bmpWidth; may have padding
  50.   uint8_t  sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  51.   uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  52.   boolean  goodBmp = false;       // Set to true on valid header parse
  53.   boolean  flip    = true;        // BMP is stored bottom-to-top
  54.   int      w, h, row, col;
  55.   uint8_t  r, g, b;
  56.   uint32_t pos = 0, startTime = millis();
  57.  
  58.   if((x >= tft.width()) || (y >= tft.height())) return;
  59.  
  60.   // Open requested file on SD card
  61.   bmpFile = SD.open(filename);
  62.   if (bmpFile == NULL) {
  63.     return;
  64.   }
  65.  
  66.   // Parse BMP header
  67.   if(read16(bmpFile) == 0x4D42) { // BMP signature
  68.     (void)read32(bmpFile); // Read & ignore creator bytes
  69.     bmpImageoffset = read32(bmpFile); // Start of image data
  70.     // Read DIB header
  71.     bmpWidth  = read32(bmpFile);
  72.     bmpHeight = read32(bmpFile);
  73.     if(read16(bmpFile) == 1) { // # planes -- must be '1'
  74.       bmpDepth = read16(bmpFile); // bits per pixel
  75.       if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  76.  
  77.         goodBmp = true; // Supported BMP format -- proceed!
  78.  
  79.         // BMP rows are padded (if needed) to 4-byte boundary
  80.         rowSize = (bmpWidth * 3 + 3) & ~3;
  81.  
  82.         // If bmpHeight is negative, image is in top-down order.
  83.         // This is not canon but has been observed in the wild.
  84.         if(bmpHeight < 0) {
  85.           bmpHeight = -bmpHeight;
  86.           flip      = false;
  87.         }
  88.  
  89.         // Crop area to be loaded
  90.         w = bmpWidth;
  91.         h = bmpHeight;
  92.         if((x+w-1) >= tft.width())  w = tft.width()  - x;
  93.         if((y+h-1) >= tft.height()) h = tft.height() - y;
  94.  
  95.         // Set TFT address window to clipped image bounds
  96.         tft.setAddrWindow(x, y, x+w-1, y+h-1);
  97.  
  98.         for (row=0; row<h; row++) { // For each scanline...
  99.  
  100.           // Seek to start of scan line.  It might seem labor-
  101.           // intensive to be doing this on every line, but this
  102.           // method covers a lot of gritty details like cropping
  103.           // and scanline padding.  Also, the seek only takes
  104.           // place if the file position actually needs to change
  105.           // (avoids a lot of cluster math in SD library).
  106.           if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  107.             pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  108.           else     // Bitmap is stored top-to-bottom
  109.             pos = bmpImageoffset + row * rowSize;
  110.           if(bmpFile.position() != pos) { // Need seek?
  111.             bmpFile.seek(pos);
  112.             buffidx = sizeof(sdbuffer); // Force buffer reload
  113.           }
  114.  
  115.           for (col=0; col<w; col++) { // For each pixel...
  116.             // Time to read more pixel data?
  117.             if (buffidx >= sizeof(sdbuffer)) { // Indeed
  118.               bmpFile.read(sdbuffer, sizeof(sdbuffer));
  119.               buffidx = 0; // Set index to beginning
  120.             }
  121.  
  122.             // Convert pixel from BMP to TFT format, push to display
  123.             b = sdbuffer[buffidx++];
  124.             g = sdbuffer[buffidx++];
  125.             r = sdbuffer[buffidx++];
  126.             tft.pushColor(tft.color565(r,g,b));
  127.           } // end pixel
  128.         } // end scanline
  129.       } // end goodBmp
  130.     }
  131.   }
  132.  
  133.   bmpFile.close();
  134. }
  135.  
  136. // These read 16- and 32-bit types from the SD card file.
  137. // BMP data is stored little-endian, Arduino is little-endian too.
  138. // May need to reverse subscript order if porting elsewhere.
  139.  
  140. uint16_t read16(File &f) {
  141.   uint16_t result;
  142.   ((uint8_t *)&result)[0] = f.read(); // LSB
  143.   ((uint8_t *)&result)[1] = f.read(); // MSB
  144.   return result;
  145. }
  146.  
  147. uint32_t read32(File &f) {
  148.   uint32_t result;
  149.   ((uint8_t *)&result)[0] = f.read(); // LSB
  150.   ((uint8_t *)&result)[1] = f.read();
  151.   ((uint8_t *)&result)[2] = f.read();
  152.   ((uint8_t *)&result)[3] = f.read(); // MSB
  153.   return result;
  154. }
  155.  
  156. void setup() {
  157.   Serial.begin(9600);
  158.   mySerial.begin(9600);
  159.  
  160.   tft.begin();
  161.   ts.begin();
  162.  
  163.   tft.setRotation(1);
  164.   tft.fillScreen(ILI9341_BLACK);
  165.   tft.setCursor(10,10);
  166.   tft.setTextSize(2);
  167.   tft.setTextColor(ILI9341_WHITE);
  168.   tft.print(F("GreenOS2... Loading"));
  169.  
  170.   tft.setTextSize(1);
  171.   tft.setCursor(10,28);
  172.  
  173.   if(!SD.begin(SD_CS)) {
  174.     tft.print(F("SD Card Error"));
  175.     tft.setCursor(10,41);
  176.     tft.print(F("Please, insert the SD card and try again"));
  177.     return;
  178.   }
  179.  
  180.   tft.print(F("SD Card Found"));
  181.  
  182.   tft.setCursor(10,41);
  183.  
  184.   if(!SD.exists("/F/")) {
  185.     tft.print(F("Main File (F) folder does not exist"));
  186.     tft.setCursor(10,54);
  187.     tft.print(F("Create a folder named \"F\" and try again"));
  188.     return;
  189.   }
  190.   tft.print(F("Main File (F) found"));
  191.   tft.setCursor(10,54);
  192.  
  193.   Analizer = false;
  194.  
  195.   counter = 0;
  196.   while(!Analizer) {
  197.       if(!SD.exists("/F/DATA" + String(counter))) {
  198.         Analizer = false;
  199.         break;
  200.       } else {
  201.         ClearFile(counter, 0);
  202.         ClearFile(counter, 1);
  203.         ClearFile(counter, 2);
  204.         ClearFile(counter, 3);
  205.         ClearFile(counter, 4);
  206.       }
  207.       counter++;
  208.   }
  209.  
  210.   names = new String[counter];
  211.   ports = new String[counter];
  212.   mins = new String[counter];
  213.   maxs = new String[counter];
  214.  
  215.   for(byte i = 0; i < counter; i++) {
  216.     File file = SD.open("/F/DATA" + String(i) + "/DAT.txt");
  217.     String FileCode;
  218.     while (file.available()) {
  219.       FileCode = (FileCode + char(file.read()));
  220.       //tft.print(char(file.read()));
  221.     }
  222.    
  223.     char stringconv[FileCode.length()]/* = FileCode.toCharArray(stringconv)*/;
  224.     FileCode.toCharArray(stringconv,FileCode.length());
  225.  
  226.     String Value;
  227.     counter2 = 0;
  228.    
  229.     for(int x = 0; x < FileCode.length(); x++) {
  230.       if(stringconv[x] == '\n') {
  231.         counter2=x+1;
  232.         break;
  233.       }
  234.       if(x > 4) { //4 : N0, A1, M2, E3, :4
  235.         Value = (Value+stringconv[x]);
  236.       }
  237.     }
  238.     names[i] = Value;
  239.  
  240.     Value = "";
  241.     for(int x = counter2; x < FileCode.length(); x++) {
  242.       if(stringconv[x] == '\n') {
  243.         counter2=x+1;
  244.         break;
  245.       }
  246.       if(x > counter2+9) {
  247.         Value = (Value+stringconv[x]);
  248.       }
  249.     }
  250.     ports[i] = Value.toInt();
  251.  
  252.     Value = "";
  253.     for(int x = counter2; x < FileCode.length(); x++) {
  254.       if(stringconv[x] == '\n') {
  255.         counter2=x+1;
  256.         break;
  257.       }
  258.       if(x > counter2+3) {
  259.         Value = (Value+stringconv[x]);
  260.       }
  261.     }
  262.     mins[i] = Value.toInt();
  263.  
  264.     Value = "";
  265.     for(int x = counter2; x < FileCode.length(); x++) {
  266.       if(stringconv[x] == '\n') {
  267.         counter2=x+1;
  268.         break;
  269.       }
  270.       if(x > counter2+3) {
  271.         Value = (Value+stringconv[x]);
  272.       }
  273.     }
  274.     maxs[i] = Value.toInt();
  275.     file.close();
  276.   }
  277.   tft.print("Number of DataFolder found: " + String(counter));
  278.   tft.setCursor(10,67);
  279.   tft.print("Name: " + String(names[0]) + ", ...");
  280.   tft.setCursor(10,80);
  281.   tft.print("Port: " + String(ports[0]) + ", ...");
  282.   tft.setCursor(10,93);
  283.   tft.print("Min: " + String(mins[0]) + ", ...");
  284.   tft.setCursor(10,106);
  285.   tft.print("Max: " + String(maxs[0]) + ", ...");
  286.  
  287.  
  288.   bmpDraw("logo.bmp",271, 191);
  289.   Autorisation = true;
  290.   tft.setCursor(10,10);
  291.  
  292.   delay(800);
  293.  
  294.   while(true) {
  295.     if(!ts.bufferEmpty()) {
  296.       break;
  297.     }
  298.   }
  299.  
  300. }
  301.  
  302. bool ResetScreen = true;
  303.  
  304. float Value0;
  305. float Value1;
  306. float Value2;
  307. float Value3;
  308.  
  309. #define PORTCOUNT 4
  310.  
  311. byte Cursor0 = 0;
  312. byte Cursor1 = 0;
  313. byte Cursor2 = 0;
  314. byte Cursor3 = 0;
  315. byte Cursor4 = 0;
  316.  
  317. byte TCursor0 = 0; //5s >x12
  318. byte TCursor1 = 0; //1m >x5
  319. byte TCursor2 = 0; //5m >x12
  320. byte TCursor3 = 0; //1h >x12
  321. byte TCursor4 = 0; //12h >x0
  322.  
  323. String InputValue;
  324. bool LastAvailable = false;
  325.  
  326. void loop() {
  327.   if(Autorisation) {
  328.     if(ResetScreen) {
  329.       bmpDraw("main.bmp",0,0);
  330.       ResetScreen = false;
  331.     }
  332.  
  333.     if(mySerial.available() && !LastAvailable) {
  334.       InputValue = "";
  335.     }
  336.     if(mySerial.available()) {
  337.       InputValue = (InputValue + char(mySerial.read()));
  338.     }
  339.     if(!mySerial.available() && LastAvailable) {
  340.       char stringconv[InputValue.length()];
  341.       InputValue.toCharArray(stringconv,InputValue.length());
  342.  
  343.       counter2 = 0;
  344.       String Value;
  345.  
  346.       Value = "";
  347.       for(short x = 0; x < InputValue.length(); x++) {
  348.         if(stringconv[x] == ',') {
  349.           counter2=x+1;
  350.           break;
  351.         }
  352.         Value = (Value+stringconv[x]);
  353.       }
  354.       Value0 = Value.toFloat();
  355.  
  356.       Value = "";
  357.       for(short x = 0; x < InputValue.length(); x++) {
  358.         if(stringconv[x] == ',') {
  359.           counter2=x+1;
  360.           break;
  361.         }
  362.         Value = (Value+stringconv[x]);
  363.       }
  364.       Value1 = Value.toFloat();
  365.  
  366.       Value = "";
  367.       for(short x = 0; x < InputValue.length(); x++) {
  368.         if(stringconv[x] == '\n') {
  369.           counter2=x+1;
  370.           break;
  371.         }
  372.         Value = (Value+stringconv[x]);
  373.       }
  374.       Value3 = Value.toFloat();
  375.      
  376.     }
  377.     LastAvailable = mySerial.available();
  378.   }
  379.   delay(5000);
  380.  
  381.   if(TCursor0+1 == 12) {
  382.     TCursor0 = 0;
  383.     TCursor1++;
  384.     SaveData(1);
  385.   } else {
  386.     TCursor0++;
  387.     if(Cursor0 == 255) {
  388.       Cursor0 = 0;
  389.       for(byte i = 0; i < counter; i++) {
  390.         ClearFile(i, 0);
  391.       }
  392.     } else {
  393.       Cursor0++;
  394.       SaveData(0);
  395.     }
  396.   }
  397.  
  398.   if(TCursor1+1 == 5) {
  399.     TCursor1 = 0;
  400.     TCursor2++;
  401.   } else {
  402.     TCursor1++;
  403.     if(Cursor1 == 255) {
  404.       Cursor1 = 0;
  405.       for(byte i = 0; i < counter; i++) {
  406.         ClearFile(i, 1);
  407.       }
  408.     } else {
  409.       Cursor1++;
  410.     }
  411.   }
  412.  
  413.   if(TCursor2+1 == 12) {
  414.     TCursor2 = 0;
  415.     TCursor3++;
  416.     SaveData(3);
  417.   } else {
  418.     TCursor2++;
  419.     if(Cursor2 == 255) {
  420.       Cursor2 = 0;
  421.       for(byte i = 0; i < counter; i++) {
  422.         ClearFile(i, 2);
  423.       }
  424.     } else {
  425.       Cursor2++;
  426.     }
  427.   }
  428.  
  429.   if(TCursor3+1 == 12) {
  430.     TCursor3 = 0;
  431.     TCursor4++;
  432.     SaveData(4);
  433.   } else {
  434.     TCursor3++;
  435.     if(Cursor3 == 255) {
  436.       Cursor3 = 0;
  437.       for(byte i = 0; i < counter; i++) {
  438.         ClearFile(i, 3);
  439.       }
  440.     } else {
  441.       Cursor3++;
  442.     }
  443.   }
  444.  
  445.   if(TCursor4+1 == 12) {
  446.     TCursor4 = 0;
  447.   } else {
  448.     TCursor4++;
  449.     if(Cursor4 == 255) {
  450.       Cursor4 = 0;
  451.       for(byte i = 0; i < counter; i++) {
  452.         ClearFile(i, 4);
  453.       }
  454.     } else {
  455.       Cursor4++;
  456.     }
  457.   }
  458.  
  459. }
  460.  
  461. void SaveData (byte DFCode) {
  462.   for(int i = 0; i < counter; i++) {
  463.     File file = SD.open("/F/DATA" + String(i) + "/DF" + String(DFCode) +".txt",FILE_WRITE);
  464.     if(ports[i] == 0) {
  465.       file.println(String(Value0));
  466.     } else if(ports[i] == 1) {
  467.       file.println(String(Value1));
  468.     } else if(ports[i] == 2) {
  469.       file.println(String(Value2));
  470.     } else if(ports[i] == 3) {
  471.       file.println(String(Value3));
  472.     }
  473.     file.close();
  474.   }
  475. }
  476.  
  477. void ClearFile (byte DATACode, byte DFCode) {
  478.   File file;
  479.   SD.remove("/F/DATA" + String(DATACode) + "/DF" + String(DFCode) + ".txt");
  480.   file = SD.open("/F/DATA" + String(DATACode) + "/DF" + String(DFCode) + ".txt", FILE_WRITE);
  481.   file.close();
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement