ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18gs

Dependencies:   mbed

Revision:
8:4220d116f17c
Parent:
7:220d3ebf74cf
Child:
16:3b298bea3a70
--- a/Inventory/Ghost/Ghost.cpp	Mon May 11 18:28:37 2020 +0000
+++ b/Inventory/Ghost/Ghost.cpp	Mon May 11 18:58:07 2020 +0000
@@ -4,7 +4,7 @@
 const string Ghost::_suffix = ".ghost";
 
 
-Ghost::Ghost(const std::string path, const std::string root)
+Ghost::Ghost(const std::string path, const std::string root, SDFileSystem &sd)
 {
    string names[20] = {"Saul", "Airon", "Aiden", "Delic", "Airian",
               "Guya", "Tormey", "Muse", "Shayepe", "Lite",
@@ -18,7 +18,7 @@
     _root = root;
 
     // Connections to SD card holder on K64F (SPI interface)
-    SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
+    //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
 
     FILE *fp;
 
@@ -29,7 +29,7 @@
 
     if (fp == NULL) {  // if it can't open the file then print error message
         printf("Error! Unable to open file: %s\n", path.c_str());
-        listdir(path);
+        listdir(path, sd);
     } else {
         //Since we may not know the
         // number of lines in the files ahead of time, we'll first count them
@@ -65,7 +65,7 @@
     }
 }
 
-Ghost::Ghost(int num, int nameNumber, const std::string root)
+Ghost::Ghost(int num, int nameNumber, const std::string root, SDFileSystem &sd)
 {
     string names[20] = {"Saul", "Airon", "Aiden", "Delic", "Airian",
               "Guya", "Tormey", "Muse", "Shayepe", "Lite",
@@ -77,7 +77,7 @@
         _names.push_back(names[i]);
     }
     _root = root;
-    _uid = gen_uid();
+    _uid = gen_uid(sd);
     int type = 0;
     if ((num > 0) && (num < 50)) {
         type = 0;
@@ -141,11 +141,8 @@
     _name = _names[nameNumber];
 }
 
-void Ghost::listdir(std::string path)
+void Ghost::listdir(std::string path, SDFileSystem &sd)
 {
-    // Connections to SD card holder on K64F (SPI interface)
-    SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
-
     printf("Listing directory: %s\n", path.c_str());
     DIR *d;
     struct dirent *p;
@@ -161,9 +158,8 @@
     closedir(d);
 }
 
-void Ghost::save(void)
+void Ghost::save(SDFileSystem &sd)
 {
-    SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
     FILE *fp; // this is our file pointer
 
     std::string filepath = "/sd";
@@ -205,7 +201,7 @@
     }
 }
 
-int Ghost::sell(void)
+int Ghost::sell(SDFileSystem &sd)
 {
 
     std::string filepath = "";
@@ -218,12 +214,12 @@
     filepath.append(buf);
     filepath.append(_suffix);
 
-    delete_file(filepath.c_str());
+    delete_file(filepath.c_str(), sd);
 
     return _value;
 };
 
-void Ghost::feed(int ammount)
+void Ghost::feed(int ammount, SDFileSystem &sd)
 {
     _xp = _xp + ammount;
 
@@ -263,7 +259,7 @@
         }
     }
 
-    save();
+    save(sd);
 }
 
 void Ghost::print_all(void)
@@ -337,10 +333,8 @@
     return _uid;
 }
 
-int Ghost::gen_uid(void)
+int Ghost::gen_uid(SDFileSystem &sd)
 {
-    // Connections to SD card holder on K64F (SPI interface)
-    SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
     std::string path = "/sd/ghosts";
     printf("Listing directory: %s\n", path.c_str());
     DIR *d;
@@ -430,10 +424,8 @@
 }
 
 
-void Ghost::delete_file(const char filename[])
+void Ghost::delete_file(const char filename[], SDFileSystem &sd)
 {
-    // Connections to SD card holder on K64F (SPI interface)
-    SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
     printf("Deleting file '%s'...\n", filename);
     FILE *fp = fopen(filename, "r");  // try and open file
     if (fp != NULL) {  // if it does open...