Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Programming Package
- //Inputs package (for sensors, excluding the touchscreen)
- #include <SD.h>
- //Touchscreen packages
- #include <Adafruit_STMPE610.h>
- #include <Adafruit_ILI9341.h>
- // This is calibration data for the raw touch data to the screen coordinates
- #define TS_MINX 150
- #define TS_MINY 130
- #define TS_MAXX 3800
- #define TS_MAXY 4000
- //Declaring Touchscreen variables
- #define STMPE_CS 8
- Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
- //The display
- #define TFT_CS 10
- #define TFT_DC 9
- #define SD_CS 4
- Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
- //Receiving Stuff
- #include <SoftwareSerial.h>
- SoftwareSerial mySerial(0, 2);
- byte counter = 0;
- short counter2 = 0;
- bool Analizer = false;
- String* names;
- String* ports;
- String* mins;
- String* maxs;
- bool Autorisation = false;
- #define BUFFPIXEL 20
- void bmpDraw(char *filename, uint16_t x, uint16_t y) {
- File bmpFile;
- int bmpWidth, bmpHeight; // W+H in pixels
- uint8_t bmpDepth; // Bit depth (currently must be 24)
- uint32_t bmpImageoffset; // Start of image data in file
- uint32_t rowSize; // Not always = bmpWidth; may have padding
- uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
- uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
- boolean goodBmp = false; // Set to true on valid header parse
- boolean flip = true; // BMP is stored bottom-to-top
- int w, h, row, col;
- uint8_t r, g, b;
- uint32_t pos = 0, startTime = millis();
- if((x >= tft.width()) || (y >= tft.height())) return;
- // Open requested file on SD card
- bmpFile = SD.open(filename);
- if (bmpFile == NULL) {
- return;
- }
- // Parse BMP header
- if(read16(bmpFile) == 0x4D42) { // BMP signature
- (void)read32(bmpFile); // Read & ignore creator bytes
- bmpImageoffset = read32(bmpFile); // Start of image data
- // Read DIB header
- bmpWidth = read32(bmpFile);
- bmpHeight = read32(bmpFile);
- if(read16(bmpFile) == 1) { // # planes -- must be '1'
- bmpDepth = read16(bmpFile); // bits per pixel
- if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
- goodBmp = true; // Supported BMP format -- proceed!
- // BMP rows are padded (if needed) to 4-byte boundary
- rowSize = (bmpWidth * 3 + 3) & ~3;
- // If bmpHeight is negative, image is in top-down order.
- // This is not canon but has been observed in the wild.
- if(bmpHeight < 0) {
- bmpHeight = -bmpHeight;
- flip = false;
- }
- // Crop area to be loaded
- w = bmpWidth;
- h = bmpHeight;
- if((x+w-1) >= tft.width()) w = tft.width() - x;
- if((y+h-1) >= tft.height()) h = tft.height() - y;
- // Set TFT address window to clipped image bounds
- tft.setAddrWindow(x, y, x+w-1, y+h-1);
- for (row=0; row<h; row++) { // For each scanline...
- // Seek to start of scan line. It might seem labor-
- // intensive to be doing this on every line, but this
- // method covers a lot of gritty details like cropping
- // and scanline padding. Also, the seek only takes
- // place if the file position actually needs to change
- // (avoids a lot of cluster math in SD library).
- if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
- pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
- else // Bitmap is stored top-to-bottom
- pos = bmpImageoffset + row * rowSize;
- if(bmpFile.position() != pos) { // Need seek?
- bmpFile.seek(pos);
- buffidx = sizeof(sdbuffer); // Force buffer reload
- }
- for (col=0; col<w; col++) { // For each pixel...
- // Time to read more pixel data?
- if (buffidx >= sizeof(sdbuffer)) { // Indeed
- bmpFile.read(sdbuffer, sizeof(sdbuffer));
- buffidx = 0; // Set index to beginning
- }
- // Convert pixel from BMP to TFT format, push to display
- b = sdbuffer[buffidx++];
- g = sdbuffer[buffidx++];
- r = sdbuffer[buffidx++];
- tft.pushColor(tft.color565(r,g,b));
- } // end pixel
- } // end scanline
- } // end goodBmp
- }
- }
- bmpFile.close();
- }
- // These read 16- and 32-bit types from the SD card file.
- // BMP data is stored little-endian, Arduino is little-endian too.
- // May need to reverse subscript order if porting elsewhere.
- uint16_t read16(File &f) {
- uint16_t result;
- ((uint8_t *)&result)[0] = f.read(); // LSB
- ((uint8_t *)&result)[1] = f.read(); // MSB
- return result;
- }
- uint32_t read32(File &f) {
- uint32_t result;
- ((uint8_t *)&result)[0] = f.read(); // LSB
- ((uint8_t *)&result)[1] = f.read();
- ((uint8_t *)&result)[2] = f.read();
- ((uint8_t *)&result)[3] = f.read(); // MSB
- return result;
- }
- void setup() {
- Serial.begin(9600);
- mySerial.begin(9600);
- tft.begin();
- ts.begin();
- tft.setRotation(1);
- tft.fillScreen(ILI9341_BLACK);
- tft.setCursor(10,10);
- tft.setTextSize(2);
- tft.setTextColor(ILI9341_WHITE);
- tft.print(F("GreenOS2... Loading"));
- tft.setTextSize(1);
- tft.setCursor(10,28);
- if(!SD.begin(SD_CS)) {
- tft.print(F("SD Card Error"));
- tft.setCursor(10,41);
- tft.print(F("Please, insert the SD card and try again"));
- return;
- }
- tft.print(F("SD Card Found"));
- tft.setCursor(10,41);
- if(!SD.exists("/F/")) {
- tft.print(F("Main File (F) folder does not exist"));
- tft.setCursor(10,54);
- tft.print(F("Create a folder named \"F\" and try again"));
- return;
- }
- tft.print(F("Main File (F) found"));
- tft.setCursor(10,54);
- Analizer = false;
- counter = 0;
- while(!Analizer) {
- if(!SD.exists("/F/DATA" + String(counter))) {
- Analizer = false;
- break;
- } else {
- ClearFile(counter, 0);
- ClearFile(counter, 1);
- ClearFile(counter, 2);
- ClearFile(counter, 3);
- ClearFile(counter, 4);
- }
- counter++;
- }
- names = new String[counter];
- ports = new String[counter];
- mins = new String[counter];
- maxs = new String[counter];
- for(byte i = 0; i < counter; i++) {
- File file = SD.open("/F/DATA" + String(i) + "/DAT.txt");
- String FileCode;
- while (file.available()) {
- FileCode = (FileCode + char(file.read()));
- //tft.print(char(file.read()));
- }
- char stringconv[FileCode.length()]/* = FileCode.toCharArray(stringconv)*/;
- FileCode.toCharArray(stringconv,FileCode.length());
- String Value;
- counter2 = 0;
- for(int x = 0; x < FileCode.length(); x++) {
- if(stringconv[x] == '\n') {
- counter2=x+1;
- break;
- }
- if(x > 4) { //4 : N0, A1, M2, E3, :4
- Value = (Value+stringconv[x]);
- }
- }
- names[i] = Value;
- Value = "";
- for(int x = counter2; x < FileCode.length(); x++) {
- if(stringconv[x] == '\n') {
- counter2=x+1;
- break;
- }
- if(x > counter2+9) {
- Value = (Value+stringconv[x]);
- }
- }
- ports[i] = Value.toInt();
- Value = "";
- for(int x = counter2; x < FileCode.length(); x++) {
- if(stringconv[x] == '\n') {
- counter2=x+1;
- break;
- }
- if(x > counter2+3) {
- Value = (Value+stringconv[x]);
- }
- }
- mins[i] = Value.toInt();
- Value = "";
- for(int x = counter2; x < FileCode.length(); x++) {
- if(stringconv[x] == '\n') {
- counter2=x+1;
- break;
- }
- if(x > counter2+3) {
- Value = (Value+stringconv[x]);
- }
- }
- maxs[i] = Value.toInt();
- file.close();
- }
- tft.print("Number of DataFolder found: " + String(counter));
- tft.setCursor(10,67);
- tft.print("Name: " + String(names[0]) + ", ...");
- tft.setCursor(10,80);
- tft.print("Port: " + String(ports[0]) + ", ...");
- tft.setCursor(10,93);
- tft.print("Min: " + String(mins[0]) + ", ...");
- tft.setCursor(10,106);
- tft.print("Max: " + String(maxs[0]) + ", ...");
- bmpDraw("logo.bmp",271, 191);
- Autorisation = true;
- tft.setCursor(10,10);
- delay(800);
- while(true) {
- if(!ts.bufferEmpty()) {
- break;
- }
- }
- }
- bool ResetScreen = true;
- float Value0;
- float Value1;
- float Value2;
- float Value3;
- #define PORTCOUNT 4
- byte Cursor0 = 0;
- byte Cursor1 = 0;
- byte Cursor2 = 0;
- byte Cursor3 = 0;
- byte Cursor4 = 0;
- byte TCursor0 = 0; //5s >x12
- byte TCursor1 = 0; //1m >x5
- byte TCursor2 = 0; //5m >x12
- byte TCursor3 = 0; //1h >x12
- byte TCursor4 = 0; //12h >x0
- String InputValue;
- bool LastAvailable = false;
- void loop() {
- if(Autorisation) {
- if(ResetScreen) {
- bmpDraw("main.bmp",0,0);
- ResetScreen = false;
- }
- if(mySerial.available() && !LastAvailable) {
- InputValue = "";
- }
- if(mySerial.available()) {
- InputValue = (InputValue + char(mySerial.read()));
- }
- if(!mySerial.available() && LastAvailable) {
- char stringconv[InputValue.length()];
- InputValue.toCharArray(stringconv,InputValue.length());
- counter2 = 0;
- String Value;
- Value = "";
- for(short x = 0; x < InputValue.length(); x++) {
- if(stringconv[x] == ',') {
- counter2=x+1;
- break;
- }
- Value = (Value+stringconv[x]);
- }
- Value0 = Value.toFloat();
- Value = "";
- for(short x = 0; x < InputValue.length(); x++) {
- if(stringconv[x] == ',') {
- counter2=x+1;
- break;
- }
- Value = (Value+stringconv[x]);
- }
- Value1 = Value.toFloat();
- Value = "";
- for(short x = 0; x < InputValue.length(); x++) {
- if(stringconv[x] == '\n') {
- counter2=x+1;
- break;
- }
- Value = (Value+stringconv[x]);
- }
- Value3 = Value.toFloat();
- }
- LastAvailable = mySerial.available();
- }
- delay(5000);
- if(TCursor0+1 == 12) {
- TCursor0 = 0;
- TCursor1++;
- SaveData(1);
- } else {
- TCursor0++;
- if(Cursor0 == 255) {
- Cursor0 = 0;
- for(byte i = 0; i < counter; i++) {
- ClearFile(i, 0);
- }
- } else {
- Cursor0++;
- SaveData(0);
- }
- }
- if(TCursor1+1 == 5) {
- TCursor1 = 0;
- TCursor2++;
- } else {
- TCursor1++;
- if(Cursor1 == 255) {
- Cursor1 = 0;
- for(byte i = 0; i < counter; i++) {
- ClearFile(i, 1);
- }
- } else {
- Cursor1++;
- }
- }
- if(TCursor2+1 == 12) {
- TCursor2 = 0;
- TCursor3++;
- SaveData(3);
- } else {
- TCursor2++;
- if(Cursor2 == 255) {
- Cursor2 = 0;
- for(byte i = 0; i < counter; i++) {
- ClearFile(i, 2);
- }
- } else {
- Cursor2++;
- }
- }
- if(TCursor3+1 == 12) {
- TCursor3 = 0;
- TCursor4++;
- SaveData(4);
- } else {
- TCursor3++;
- if(Cursor3 == 255) {
- Cursor3 = 0;
- for(byte i = 0; i < counter; i++) {
- ClearFile(i, 3);
- }
- } else {
- Cursor3++;
- }
- }
- if(TCursor4+1 == 12) {
- TCursor4 = 0;
- } else {
- TCursor4++;
- if(Cursor4 == 255) {
- Cursor4 = 0;
- for(byte i = 0; i < counter; i++) {
- ClearFile(i, 4);
- }
- } else {
- Cursor4++;
- }
- }
- }
- void SaveData (byte DFCode) {
- for(int i = 0; i < counter; i++) {
- File file = SD.open("/F/DATA" + String(i) + "/DF" + String(DFCode) +".txt",FILE_WRITE);
- if(ports[i] == 0) {
- file.println(String(Value0));
- } else if(ports[i] == 1) {
- file.println(String(Value1));
- } else if(ports[i] == 2) {
- file.println(String(Value2));
- } else if(ports[i] == 3) {
- file.println(String(Value3));
- }
- file.close();
- }
- }
- void ClearFile (byte DATACode, byte DFCode) {
- File file;
- SD.remove("/F/DATA" + String(DATACode) + "/DF" + String(DFCode) + ".txt");
- file = SD.open("/F/DATA" + String(DATACode) + "/DF" + String(DFCode) + ".txt", FILE_WRITE);
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement