dsfa

Dependencies:   dsf SDFileSystem TextLCD mbed stepper_moter

Fork of my_example_Nucleo_Ex05_SD_zhangyuxiang2 by liang brain

Files at this revision

API Documentation at this revision

Comitter:
brainliang
Date:
Tue May 08 01:50:44 2018 +0000
Parent:
3:1e51c10aa4f2
Commit message:
sdfa

Changed in this revision

LCDShow/LCDShow.cpp Show annotated file Show diff for this revision Revisions of this file
LCDShow/LCDShow.h Show annotated file Show diff for this revision Revisions of this file
QEI.lib Show annotated file Show diff for this revision Revisions of this file
SDRead/SDRead.cpp Show annotated file Show diff for this revision Revisions of this file
SDRead/SDRead.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
stepper_moter.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCDShow/LCDShow.cpp	Tue May 08 01:50:44 2018 +0000
@@ -0,0 +1,116 @@
+#include "LCDShow.h"
+
+LCDShow::LCDShow(PinName rs,PinName e,PinName d4,PinName d5,PinName d6,PinName d7,LCDType type):TextLCD(rs,e,d4,d5,d6,d7,type)
+{
+    Max_pos = PrePos = now_pos = 0;
+    start = true;
+    Pos_fresh = false;
+    Neg_fresh = false;
+    Now_fresh = false;
+    Read_file = false;
+}
+void LCDShow::CurrPos()     
+{
+    int i = Choose.pulses_ % 4;
+    if(i < 0)
+    {
+        Choose.pulses_ = 0;
+        i = 0;
+    }
+    if(PrePos == 3 && i == 0)
+    {
+        Pos_fresh = true;
+    }
+    else if(PrePos == 0 && i == 3)
+    {
+        Neg_fresh = true;
+    }
+    else if(PrePos != i)
+    {
+        Now_fresh = true;
+    }
+    
+    if(!Read_file)
+    {
+        PrePos = i;
+        this->locate(18,i);
+        this->printf("<-");   
+    }
+}
+
+void LCDShow::ReadFile()
+{
+    if(!btn3.read())
+    {
+        file_name = "/sd/";
+        file_name += File_dir[now_pos + (PrePos - 4)];
+        Read_file = true;
+    }
+}
+void LCDShow::getDir(vector<string> _dir)
+{
+    File_dir = _dir;
+}
+void LCDShow::showdir()
+{
+    if(start)
+    {
+        t1.attach(this,&LCDShow::showdir,0.2);
+        t2.attach(this,&LCDShow::ReadFile,0.5);
+        Max_pos = File_dir.size();
+        for(;now_pos < 4; now_pos ++)
+        {
+            this->printf("%s\n",File_dir[now_pos]);
+        }
+        start = false;
+        return;
+    }
+    CurrPos();
+    if(!Read_file)
+        {
+            if(Pos_fresh == true)
+            {
+                this->cls();
+                for(int i = 0;i < 4; i ++)
+                {
+                    if(now_pos >= Max_pos)
+                    {
+                        continue;
+                    }
+                    this->printf("%s\n",File_dir[now_pos]);
+                    now_pos ++;      
+                }
+                Pos_fresh = false;
+            }
+            else if(Neg_fresh == true)
+            {
+                this->cls();
+                now_pos -= 8;
+                if(now_pos < 0)
+                    now_pos = 0;
+                for(int i = 0;i < 4; i ++)
+                {
+                    this->printf("%s\n",File_dir[now_pos]);
+                    now_pos ++;  
+                }
+                Neg_fresh = false;
+            }
+            else if(Now_fresh == true)
+            {
+                this->cls();
+                now_pos -= 4;
+                for(int i = 0;i < 4; i ++)
+                {
+                    this->printf("%s\n",File_dir[now_pos]);
+                    now_pos ++;  
+                }
+                Now_fresh = false;
+            }
+        }
+        else if(Read_file)
+        {
+            this->cls();
+            this->printf("OPEN %s\n",file_name);
+        }
+    return;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCDShow/LCDShow.h	Tue May 08 01:50:44 2018 +0000
@@ -0,0 +1,37 @@
+#ifndef LCDSHOW_h
+#define LCDSHOW_h
+#include "mbed.h"
+#include "TextLCD.h"
+#include <vector>
+#include <string>
+#include "QEI.h"
+
+
+extern QEI Choose;
+extern InterruptIn btn3;
+
+class LCDShow:public TextLCD
+{
+public:
+    LCDShow(PinName rs,PinName e,PinName d4,PinName d5,PinName d6,PinName d7,LCDType type = LCD16x2);
+    void showdir();
+    void getDir(vector<string> _dir);
+    void CurrPos();
+    void ReadFile();
+    string file_name;
+    bool Read_file;
+private:
+    vector<string> File_dir;
+
+    int PrePos;
+    int now_pos;
+    int Max_pos;
+    Ticker t1;
+    Ticker t2;
+    bool start;
+    bool Pos_fresh;
+    bool Neg_fresh;
+    bool Now_fresh;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI.lib	Tue May 08 01:50:44 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/brainliang/code/dsf/#3753789f4288
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDRead/SDRead.cpp	Tue May 08 01:50:44 2018 +0000
@@ -0,0 +1,40 @@
+#include "SDRead.h"
+
+SDRead::SDRead(PinName mosi,PinName miso,PinName sclk,PinName cs,const char *name):SDFileSystem(mosi,miso,sclk,cs,name)
+{}
+bool SDRead:: Get_FileDir()
+{
+    this -> mount();
+    DirHandle *dh = opendir("/sd/"); 
+    if(!dh){
+        return false;
+    }
+    dirent *entry;
+    for(;;)
+    {
+        entry = dh -> readdir();
+        if(!entry)
+            {break;}
+        string dir_name = entry -> d_name;
+        if(dir_name == "System Volume Information")
+            continue;
+        File_dir.push_back(dir_name);
+    }
+    dh -> closedir();
+    return true;
+}
+bool SDRead::Read_File(const char* FileName)
+{
+    FILE *fp1 = fopen(FileName,"r");
+    if(fp1 == NULL)
+    {
+        return false;
+    }
+    char buf[200];
+    while(fgets(buf,sizeof(buf),fp1) != NULL)
+    {
+        string info = buf;
+        Info.push_back(info);
+    }
+    return true;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDRead/SDRead.h	Tue May 08 01:50:44 2018 +0000
@@ -0,0 +1,17 @@
+#ifndef SDREAD_H
+#define SDREAD_H
+#include "SDFileSystem.h"
+#include "mbed.h"
+#include <vector>
+#include <string>
+
+class SDRead:public SDFileSystem
+{
+public:
+    SDRead(PinName mosi,PinName miso,PinName sclk,PinName cs,const char *name);
+    vector<string> File_dir;
+    vector<string> Info;
+    bool Get_FileDir();
+    bool Read_File(const char* FileName);
+};
+#endif
\ No newline at end of file
--- a/main.cpp	Thu May 03 10:53:02 2018 +0000
+++ b/main.cpp	Tue May 08 01:50:44 2018 +0000
@@ -1,86 +1,33 @@
 #include "mbed.h"
-#include "TextLCD.h"
-#include "SDFileSystem.h"
+#include "SDRead.h"
+#include "LCDShow.h"
+
+QEI Choose(PB_5,PB_8, NC, 11, QEI::X2_ENCODING);
+InterruptIn btn3(PA_5);
 
 //LCD显示屏  rs,     e,    d4-d7,                    model
-TextLCD lcd(PC_13, PC_14, PC_15, PA_0, PA_1, PA_4, TextLCD::LCD20x4); // rs, e, d4-d7, model  V4版本
-    
-//SD卡          mosi, miso, sclk, cs,    name     用SPI端口
-SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd");                                 
-Serial pc(PA_9, PA_10);                                                         //与电脑通讯的串口
+LCDShow lcd(PC_13, PC_14, PC_15, PA_0, PA_1, PA_4, TextLCD::LCD20x4); // rs, e, d4-d7, model  V4版本  屏幕为4行,每行20字符(列)
+SDRead sd(PB_15, PB_14, PB_13, PB_12, "sd");
 
-int main() {
-    DirHandle *dh = opendir("/sd/");    //读取SD卡中的文件列表
-    if(!dh){
-        pc.printf("opendir error\n");
-        return 1;
-    }
-    dirent *entry;
-    for(;;){
-        entry = dh->readdir();
-        if(!entry)
-            break;
-        pc.printf("%s\r\n", entry->d_name);
-    }
-    dh->closedir();
-    
-    // 写文件例子
-    FILE *fp2 = fopen("/sd/write.txt", "w");
-    if (fp2 == NULL)
+int main()
+{
+    sd.mount();             //加注释!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+    while(1)
     {
-        pc.printf("open error2!!\r\n");
-        return 1;
-    }
-    pc.printf("file opened for write\r\n");
-    fprintf(fp2, "hello\r\n");
-    fprintf(fp2, "%d", 23333);
-    fclose(fp2);  //写完文件要记得关闭,不然可能没保存上
-    
-    // 读文件例子
-    FILE *fp = fopen("/sd/test.txt", "r"); //打开文件,路径以“/sd/”开头
-
-    if (fp == NULL) //打开失败,原因可能是文件不存在,或卡没有连接好
-    {
-        pc.printf("open error!!\r\n");
-        return 1;
+        if(sd.Get_FileDir())        //加注释!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+        {
+            lcd.getDir(sd.File_dir);    //加注释!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+            lcd.showdir();//LCD显示文件目录
+        }
+        if(lcd.Read_file)       //加注释!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+        {
+            if(sd.Read_File(lcd.file_name.data()))
+            {
+                for(int i = 0; i < sd.Info.size(); i++)//sd.Info 就是读取到的文件信息向量
+                {
+                    printf("%s\n",sd.Info[i]);
+                }
+            }
+        }
     }
-    pc.printf("file opened for read\r\n");
-    char buf[64];
-    while (fgets(buf, sizeof(buf), fp) != NULL) //读入一行的C函数
-    {    
-        pc.printf("%s", buf);
-    }
-    fclose(fp);  //关闭文件,释放资源
-    
-    //pc.printf("card type is 0x%x\r\n" , sd.card_type());
-
-    return 0;
-
-
-    lcd.printf("Hello World!");
-    wait_ms(1000); //等待1秒
-    lcd.cls(); //清屏
-    
-    for(int i=0; i<10000; i+=4){
-        lcd.locate(0,0); //分别控制起始的列和行,从0开始
-        lcd.printf("%d", i);
-        wait_ms(1000); //等待1秒
-        
-        lcd.locate(0,1);
-        lcd.printf("%d", i+1);
-        wait_ms(1000);
-        
-        lcd.locate(0,2);
-        lcd.printf("%d", i+2);
-        wait_ms(1000);
-        
-        lcd.locate(0,3);
-        lcd.printf("%d", i+3);
-        wait_ms(1000);
-        
-        lcd.cls(); //清屏
-    }
-
-    
-
-}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepper_moter.lib	Tue May 08 01:50:44 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/brainliang/code/stepper_moter/#ab0f997c2d9c