Michał Biolik / Mbed 2 deprecated SD_TUT

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
micbio
Date:
Fri Dec 09 11:22:47 2016 +0000
Parent:
2:e699312248f3
Commit message:
dupa

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
application/cli.cpp Show annotated file Show diff for this revision Revisions of this file
application/cli.hpp Show annotated file Show diff for this revision Revisions of this file
application/file_manager.cpp Show annotated file Show diff for this revision Revisions of this file
application/file_manager.hpp Show annotated file Show diff for this revision Revisions of this file
application/main.cpp Show annotated file Show diff for this revision Revisions of this file
cli.cpp Show diff for this revision Revisions of this file
cli.hpp Show diff for this revision Revisions of this file
file_manager.cpp Show diff for this revision Revisions of this file
file_manager.hpp Show diff for this revision Revisions of this file
main.cpp Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Fri Dec 09 11:22:47 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#183490eb1b4a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/cli.cpp	Fri Dec 09 11:22:47 2016 +0000
@@ -0,0 +1,44 @@
+#include "cli.hpp"
+
+#define CMD_LENGTH      15
+
+extern Serial pc;
+
+void cli_sd::welcome(void)
+{
+    printf("***********************************************\r\n");
+    printf("** Welcome to an example application showing **\r\n");
+    printf("** basic file operations on sd card.         **\r\n");
+    printf("***********************************************\r\n");
+}
+
+void cli_sd::print_help(void)
+{
+    pc.printf("List of commands:\r\n");
+    pc.printf("m : mounts a sd_card\r\n");
+    pc.printf("p : changes the file to write to\r\n");
+    pc.printf("w : write to example file\r\n");
+//    pc.printf("a : append to example file\r\n");
+    pc.printf("r : read text from example file\r\n");
+    pc.printf("h : this help\r\n");
+}
+
+char cli_sd::get_cmd(void)
+{
+    char c;
+    while(true)
+    {
+        c = pc.getc();
+        if (c == 'm' || c == 'p' || c == 'w' || c == 'a' || c == 'r' || c == 'h')
+        {
+            break;
+        }
+        else
+        {
+            pc.printf("Unkown command.\r\n");
+            this->print_help();
+        }
+    }
+    
+    return c;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/cli.hpp	Fri Dec 09 11:22:47 2016 +0000
@@ -0,0 +1,9 @@
+#include "mbed.h"
+
+class cli_sd
+{
+public:
+    void welcome(void);
+    void print_help(void);
+    char get_cmd(void);
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/file_manager.cpp	Fri Dec 09 11:22:47 2016 +0000
@@ -0,0 +1,167 @@
+#include "file_manager.hpp"
+
+extern Serial pc;
+
+void file_manager::print_files(void)
+{
+    pc.printf("1. %s\r\n", &path_sisk[4]);
+    pc.printf("2. %s\r\n", &path_agh[4]);
+    pc.printf("3. %s\r\n", &path_krk[4]);
+}
+
+void file_manager::get_text(void)
+{
+   uint8_t char_counter = 0;
+   static char byte_received;
+   
+   do
+   {
+       byte_received = pc.getc();
+       buffer[char_counter] = byte_received;
+       char_counter++;
+       if (char_counter >= MAX_TEXT_LEN)
+       {
+           pc.printf("Text too long for buffer.\r\n");
+           break;      // Text length reached max length.
+       }        
+       pc.putc(byte_received);
+   }while(byte_received != '\r');
+   
+   // Add string end sign
+   buffer[char_counter] = '\0';
+   
+   pc.printf("\r\n");
+}
+
+void file_manager::get_path(void)
+{
+    pc.printf("Current file to write to is: %s.\r\n", current_path);    
+}
+
+void file_manager::set_path(void)
+{
+    char number;
+    bool correct_number = false;
+
+    if (fp != NULL)
+    {
+        pc.printf("First close the file!\r\n");
+        return;
+    }
+
+    this->get_path();
+    pc.printf("Write the number of new file to write to. \r\n");    
+    this->print_files();
+
+    do
+    {
+        correct_number = true;
+        number = pc.getc();
+        switch(number)
+        {
+            case '1':
+                current_path = path_sisk;
+                break;
+            case '2':
+                current_path = path_agh;
+                break;
+            case '3':
+                current_path = path_krk;
+                break;
+            default:
+                pc.printf("Incorrect number, try again.\r\n");
+                correct_number = false;
+        }
+    }while(!correct_number);
+
+    pc.printf("Setting path to: %s.\r\n", current_path);
+}
+
+bool file_manager::open_file(char option)
+{
+    if (fp != NULL)
+    {
+        pc.printf("File already opened!\r\n");
+        return false;
+    }
+
+    fp = fopen(this->current_path, &option);
+
+    if (fp != NULL)
+    {
+        pc.printf("Opened file: %s.\r\n", this->current_path);
+    }
+
+    return true;
+}
+
+bool file_manager::close_file(void)
+{
+    if (fp == NULL)
+    {
+        pc.printf("File already closed!\r\n");
+        return false;
+    }
+
+    fclose(fp);
+
+    fp = NULL;
+    
+    
+    if (fp != NULL)
+    {
+        pc.printf("Closed file: %s.\r\n", this->current_path);
+    }
+
+    return true;
+}
+
+void file_manager::write_to_file(void)
+{
+    this->open_file('w');
+
+    get_text();
+    fprintf(fp, "%s", buffer);
+    //fwrite (buffer , sizeof(char), sizeof(buffer), fp); 
+
+    this->close_file();
+}
+
+
+/*void file_manager::append_to_file(void)
+{
+    //this->open_file('a');
+    char * a = "a";
+    fp = fopen(this->current_path, a);
+    get_text();
+    fprintf(fp, "%s", buffer);  
+   // fwrite (buffer , sizeof(char), sizeof(buffer), fp);  
+    this->close_file();
+}*/
+
+void file_manager::read_file(void)
+{
+    long lSize;
+    size_t result;
+
+    this->open_file('r');
+
+    fseek(fp , 0 , SEEK_END);
+    lSize = ftell (fp);
+    rewind(fp);
+    
+    // copy the file into the buffer:
+    result = fread(buffer, 1, lSize, fp);
+    if (result != lSize) 
+    {
+        pc.printf("Reading error");
+    }
+    else
+    {
+        pc.printf("File: %s.\r\r\n", this->current_path);
+        pc.printf("%s\r\r\n", buffer);
+    }
+    
+
+    this->close_file();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/file_manager.hpp	Fri Dec 09 11:22:47 2016 +0000
@@ -0,0 +1,38 @@
+#include "mbed.h"
+#include <string>
+
+#define MAX_TEXT_LEN        400
+
+
+class file_manager
+{
+private:
+    FILE *fp;
+    char * path_sisk;
+    char * path_agh;
+    char * path_krk;
+    char * current_path;
+    char buffer[MAX_TEXT_LEN];
+    
+    void print_files(void);
+    void get_string(void);
+    bool open_file(char option);
+    bool close_file(void);
+    void get_text(void);
+    
+public:
+    file_manager(void) :
+        path_sisk("/sd/sisk.txt"), 
+        path_agh("/sd/agh.txt"), 
+        path_krk("/sd/krakow.txt"),
+        current_path(path_sisk)
+    {
+        fp = NULL;
+    }
+
+    void get_path(void);
+    void set_path(void);
+    void write_to_file(void);
+    void append_to_file(void);
+    void read_file(void);
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/main.cpp	Fri Dec 09 11:22:47 2016 +0000
@@ -0,0 +1,84 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "SDFileSystem.h"
+#include "cli.hpp"
+#include "file_manager.hpp"
+
+#define SD      1
+#define MII     0
+ 
+#if SD + MII == 2
+    #error "Wybierz tylko jeden przyklad."
+#endif
+
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
+Serial pc(USBTX, USBRX);
+DigitalOut led(LED1);
+EthernetInterface eth;
+
+int main() {
+#if MII
+    pc.printf("Ethernet example\r\n");
+    eth.init(); //Use DHCP
+    eth.connect();
+    pc.printf("IP Address is %s\n", eth.getIPAddress());
+    
+    TCPSocketConnection sock;
+    sock.connect("mbed.org", 80);
+    
+    char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+    
+    char buffer[300];
+    int ret;
+    while (true) {
+        ret = sock.receive(buffer, sizeof(buffer)-1);
+        if (ret <= 0)
+            break;
+        buffer[ret] = '\0';
+        pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
+    }
+      
+    sock.close();
+    
+    eth.disconnect();
+    
+    while(1) {}
+#elif SD
+    cli_sd cli;
+    file_manager fm;
+    char cmd;
+    
+    cli.welcome();
+    cli.print_help();
+    
+    while(1) 
+    {
+        cmd = cli.get_cmd();
+        
+        switch(cmd)
+        {
+//            case 'm':
+//                break;
+            case 'p':
+                fm.set_path();
+                break;
+            case 'w':
+                fm.write_to_file();
+                break;
+//            case 'a':
+//                fm.append_to_file();
+//                break;
+            case 'r':
+                fm.read_file();
+                break;
+            case 'h':
+                cli.print_help();
+                break;
+            default:
+                pc.printf("Error.\r\n");         // should never enter this
+                break;
+        }
+    }
+#endif
+}
\ No newline at end of file
--- a/cli.cpp	Mon Dec 05 22:46:14 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-#include "cli.hpp"
-
-#define CMD_LENGTH      15
-
-extern Serial pc;
-
-void cli_sd::welcome(void)
-{
-    printf("***********************************************\r\n");
-    printf("** Welcome to an example application showing **\r\n");
-    printf("** basic file operations on sd card.         **\r\n");
-    printf("***********************************************\r\n");
-}
-
-void cli_sd::print_help(void)
-{
-    pc.printf("List of commands:\r\n");
-    pc.printf("m : mounts a sd_card\r\n");
-    pc.printf("p : changes the file to write to\r\n");
-    pc.printf("w : write to example file\r\n");
-    pc.printf("a : append to example file\r\n");
-    pc.printf("r : read text from example file\r\n");
-}
-
-char cli_sd::get_cmd(void)
-{
-    char c;
-    while(true)
-    {
-        c = pc.getc();
-        if (c == 'm' || c == 'p' || c == 'w' || c == 'a' || c == 'r')
-        {
-            break;
-        }
-        else
-        {
-            pc.printf("Unkown command.\r\n");
-            this->print_help();
-        }
-    }
-    
-    return c;
-}
--- a/cli.hpp	Mon Dec 05 22:46:14 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-#include "mbed.h"
-
-class cli_sd
-{
-public:
-    void welcome(void);
-    void print_help(void);
-    char get_cmd(void);
-};
\ No newline at end of file
--- a/file_manager.cpp	Mon Dec 05 22:46:14 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,166 +0,0 @@
-#include "file_manager.hpp"
-
-#define MAX_TEXT_LEN        400
-
-extern Serial pc;
-
-char buffer[MAX_TEXT_LEN];
-
-void file_manager::print_files(void)
-{
-    pc.printf("1. %s\r\n", path_sisk);
-    pc.printf("2. %s\r\n", path_agh);
-    pc.printf("3. %s\r\n", path_krk);
-}
-
-void file_manager::get_string(void)
-{
-//    char * p_my_//cmd = p_cmd;
-//    uint8_t char_counter = 0;
-//    static char byte_received;
-//    
-//    do
-//    {
-//        byte_received = pc.getc();
-//        *p_my_cmd = byte_received;
-//        p_my_cmd++;
-//        char_counter++;
-//        if (char_counter >= CMD_LENGTH)
-//        {
-//            break;      // CMD length reached max length.
-//        }        
-//        pc.putc(byte_received);
-//        for (int i = 0; i < len; i++)
-//        {
-//            pc.putc(*(p_cmd + i));
-//        }
-//    }while(byte_received != '\r');
-//    
-//    len = char_counter;
-//    
-//    pc.printf("\r\r\n");
-//    pc.putc('Y');    
-}
-
-void file_manager::get_path(void)
-{
-    pc.printf("Current file to write to is: %s.\r\n", current_path);    
-}
-
-void file_manager::set_path(void)
-{
-    char number;
-    bool correct_number = false;
-
-    if (fp != NULL)
-    {
-        pc.printf("First close the file!\r\n");
-        return;
-    }
-
-    this->get_path();
-    pc.printf("Write the number of new file to write to. \r\n");    
-    this->print_files();
-
-    do
-    {
-        correct_number = true;
-        number = pc.getc();
-        switch(number)
-        {
-            case '1':
-                current_path = path_sisk;
-                break;
-            case '2':
-                current_path = path_agh;
-                break;
-            case '3':
-                current_path = path_krk;
-                break;
-            default:
-                pc.printf("Incorrect number, try again.\r\n");
-                correct_number = false;
-        }
-    }while(!correct_number);
-
-    pc.printf("Setting path to: %s.\r\n", current_path);
-}
-
-bool file_manager::open_file(char option)
-{
-    if (fp != NULL)
-    {
-        pc.printf("File already opened!\r\n");
-        return false;
-    }
-
-    fp = fopen(this->current_path, &option);
-
-    if (fp != NULL)
-    {
-        pc.printf("Opened file: %s.\r\n", this->current_path);
-    }
-
-    return true;
-}
-
-bool file_manager::close_file(void)
-{
-    if (fp == NULL)
-    {
-        pc.printf("File already closed!\r\n");
-        return false;
-    }
-
-    fclose(fp);
-
-    fp = NULL;
-
-    return true;
-}
-
-void file_manager::write_to_file(void)
-{
-    this->open_file('w');
-
-    
-
-    this->close_file();
-}
-
-
-void file_manager::append_to_file(void)
-{
-    this->open_file('a');
-
-    
-
-    this->close_file();
-}
-
-void file_manager::read_file(void)
-{
-    long lSize;
-    size_t result;
-
-    this->open_file('r');
-
-    fseek(fp , 0 , SEEK_END);
-    lSize = ftell (fp);
-    rewind(fp);
-    
-    // copy the file into the buffer:
-    result = fread(buffer, 1, lSize, fp);
-    if (result != lSize) 
-    {
-        pc.printf("Reading error");
-    }
-    else
-    {
-        pc.printf("File: %s.\r\r\n", this->current_path);
-    }
-    
-
-    this->close_file();
-    
-}
\ No newline at end of file
--- a/file_manager.hpp	Mon Dec 05 22:46:14 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-#include "mbed.h"
-#include <string>
-
-class file_manager
-{
-private:
-    FILE *fp;
-    char * current_path;
-    char * path_sisk;
-    char * path_agh;
-    char * path_krk;
-
-    void print_files(void);
-    void get_string(void);
-    bool open_file(char option);
-    bool close_file(void);
-public:
-    file_manager(void) :
-        path_sisk("/sisk.txt"), 
-        path_agh("/agh.txt"), 
-        path_krk("/krakow.txt"),
-        current_path("/sisk.txt")
-    {
-        fp = NULL;
-    }
-
-    void get_path(void);
-    void set_path(void);
-    void write_to_file(void);
-    void append_to_file(void);
-    void read_file(void);
-};
--- a/main.cpp	Mon Dec 05 22:46:14 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,64 +0,0 @@
-#include "mbed.h"
-#include "SDFileSystem.h"
-#include "cli.hpp"
-#include "file_manager.hpp"
-
-SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
-Serial pc(USBTX, USBRX);
-// DigitalIn enable(SW2);
-DigitalOut led(LED1);
-
-int main() 
-{    
-    cli_sd cli;
-    file_manager fm;
-    char cmd;
-    
-    cli.welcome();
-    cli.print_help();
-
-    while(1) 
-    {
-        cmd = cli.get_cmd();
-        
-        switch(cmd)
-        {
-            case 'm':
-                break;
-            case 'p':
-                fm.set_path();
-                break;
-            case 'w':
-                fm.write_to_file();
-                break;
-            case 'a':
-                fm.append_to_file();
-                break;
-            case 'r':
-                fm.read_file();
-                break;
-            default:
-                pc.printf("Error.\n");         // should never enter this
-                break;
-        }
-    }
-    
-    while(1) {
-        if(!true) {
-            //pc.printf("\r\nThis is: %c", cmd);
-            //pc.scanf(cmd);
-            if (pc.writeable())
-            {
-                led = !led;
-            }
-        }
-        wait(0.25);
-    }
-    
-    while(1) {
-        FILE *fp = fopen("/sd/testfile.txt", "a");
-        fprintf(fp, "hello world!\r\n");
-        fclose(fp);
-        wait(1);
-    }
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Fri Dec 09 11:22:47 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#031a41d65add
--- a/mbed.bld	Mon Dec 05 22:46:14 2016 +0000
+++ b/mbed.bld	Fri Dec 09 11:22:47 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/d75b3fe1f5cb
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/0ab6a29f35bf
\ No newline at end of file