Código para o menu no LCD

Dependencies:   mbed SDFileSystemSD TextLCD

Fork of TextLCD_HelloWorld2 by Wim Huiskamp

Files at this revision

API Documentation at this revision

Comitter:
lucasfontenla
Date:
Mon May 21 17:11:55 2018 +0000
Parent:
27:83d2a710cc2f
Child:
29:558eccdfc079
Commit message:
Enviando GCODE para machine

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
classes.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon May 21 17:11:55 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/neilt6/code/SDFileSystem/#4a0e35f3f0fe
--- a/classes.h	Thu May 17 22:38:03 2018 +0000
+++ b/classes.h	Mon May 21 17:11:55 2018 +0000
@@ -2,7 +2,7 @@
 #define rx PC_11
 
 Serial Mach(tx, rx);
-//SDFileSystem sd(PB_5, PB_4, PB_3, PB_10, "sd"); //MOSI - D4, MISO - D5, SCLK - D3, CS - D6
+Timer t;
 
 class Machine { 
     int n;
@@ -93,6 +93,13 @@
             send("r");
         }
         
+        void upload_gcode(string gcode){
+            buffer = "";
+            buffer.append("u");
+            buffer.append(gcode);
+            
+            send(buffer);
+        }
         
         private:
             
@@ -108,10 +115,156 @@
 };
 
 
+Machine machine;
+
+SDFileSystem sd(PB_15, PB_14, PB_13, PB_1, "sd"); //MOSI - D4, MISO - D5, SCLK - D3, CS - D7
+DIR *dp;
+
+string savedString = "Programas Salvos";
+string gcodeString = "Programas GCODE";
+
+class SDCard{
+    public:        
+        int save_GCODE(string name, string gcode){
+            string file_path = "/sd/" + savedString + "/" + name; 
+            char * path = str_to_char(file_path);
+            
+            printf(path);
+            
+            printf("going...\n\r");
+            
+            FILE *fp = fopen(path, "w");
+            
+            if(fp == NULL) {
+                error("Could not open file for write\n\r");
+                return 0;
+            }
+            
+            char * write_file = str_to_char(gcode);
+            
+            fprintf(fp, write_file);
+            fclose(fp);
+            
+            printf("Cool\n\r");
+            
+            return 1;
+        }
+        
+        int send_gcode(string name){
+            string file_path = "/sd/" + gcodeString + "/" + name; 
+            char * path = str_to_char(file_path);
+            unsigned char c;
+            string buffer;
+            
+            printf(path);
+            
+            printf("going...\n\r");
+            
+            FILE *fp = fopen(path, "r");
+            
+            if(fp == NULL) {
+                error("Could not open file for write\n\r");
+                return 0;
+            }  
+            
+            t.start();
+            
+            while(1){
+                if(t.read() > 4){
+                    fclose(fp);
+                    return 0;    
+                }
+                c = fgetc(fp);
+                printf("%c\n\r", c);
+                buffer.push_back(c);
+                
+                if(c == ';'){
+                    fclose(fp);
+                    break;    
+                }
+            }
+            fclose(fp);
+            
+            printf("%s\n\r", buffer);
+            
+            machine.upload_gcode(buffer);
+            return 1;
+        }
+        
+        int check_name_available(string name){
+            string file_path = "/sd/" + savedString;
+            char * dir = str_to_char(file_path);
+            struct dirent *dirp;
+            string filename;
+            
+            printf(dir);
+            
+            dp = opendir(dir);
+        
+            while((dirp = readdir(dp)) != NULL) {
+                filename = (dirp->d_name);
+                printf("%s\n\r", filename);
+                if(filename == name){
+                    closedir(dp);
+                    return 0;  
+                }                  
+            } 
+            closedir(dp);
+            return 1; 
+        }
+        
+        int create_folders(void){
+            int gotSaved = 0;
+            int gotFiles = 0;
+            string filename;
+            struct dirent *dirp;
+            
+            dp = opendir("/sd");
+            
+            t.start();
+        
+            while((dirp = readdir(dp)) != NULL) {
+                if(t.read() > 2){
+                    t.stop();
+                    return 0;    
+                }
+                filename = (dirp->d_name);
+                printf("%s\n\r", filename);
+                if(filename == savedString){
+                    printf("Saved OK\n\r");
+                    gotSaved = 1;
+                } else if(filename == gcodeString){
+                    printf("GCODE OK\n\r");
+                    gotFiles = 1;   
+                }
+            }
+            
+            if(!gotSaved){
+                char * path = str_to_char(("/sd/" + savedString));
+                mkdir(path, 0777);
+            }
+            if(!gotFiles){
+                char * path2 = str_to_char(("/sd/" + gcodeString));
+                mkdir(path2, 0777);
+            }
+            
+            closedir(dp);
+            return 1;
+        }    
+        
+        private:
+            char * str_to_char(string str){
+                char * path = new char[str.length() + 1];
+                std::strcpy(path, str.c_str());
+                return path;
+            } 
+};
+
+SDCard sdcard;
+
 //criar classe "menu" (funções para as telas)
 class Menu{
     
-    Machine machine;
     public:
     int linha;
     int func;
@@ -243,13 +396,33 @@
                        
     //func 0
     void inicializar(){ //função tela incial de boas-vindas
+        int status;
+        
+        printf("Starting...\n\r");
+        
         machine.reset();
         lcd.cls();
         lcd.locate(4,0);
         lcd.printf("Bem-vindo(a)");
         lcd.locate(0,1);
         lcd.printf("Utilize seu EPI para operar a maquina.");
+        
         wait(1);
+        
+        status = sdcard.create_folders();
+        if(!status){
+            printf("SD Error: creating files\n\r");    
+        }
+        
+        wait(0.25);
+        
+        status = sdcard.send_gcode("New.txt");
+        if(!status){
+            printf("SD Error: sending files\n\r");    
+        }
+        
+        wait(0.5);
+        
         func = 1;
         }
     
--- a/main.cpp	Thu May 17 22:38:03 2018 +0000
+++ b/main.cpp	Mon May 21 17:11:55 2018 +0000
@@ -2,6 +2,7 @@
 
 #include "mbed.h"
 #include "TextLCD.h"
+#include "SDFileSystem.h"
 #include "string"
 
 // Host PC Communication channels