Xiaoyuan Yu / Mbed 2 deprecated Ex_FileSystem

Dependencies:   SDFileSystem mbed

Fork of wmx_laser by w mx

Files at this revision

API Documentation at this revision

Comitter:
Dennis_Yu
Date:
Fri Sep 07 13:06:09 2018 +0000
Parent:
7:4c80e598d08c
Child:
9:32f2dcaabdae
Commit message:
to write a file and read one

Changed in this revision

MQTT.lib Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
WIZnet_Library.lib Show diff for this revision Revisions of this file
main.cpp 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
networking.cpp Show diff for this revision Revisions of this file
networking.h Show diff for this revision Revisions of this file
--- a/MQTT.lib	Mon Jul 23 06:28:17 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/users/zhangyx/code/MQTT/#6de46e8293d9
--- a/SDFileSystem.lib	Mon Jul 23 06:28:17 2018 +0000
+++ b/SDFileSystem.lib	Fri Sep 07 13:06:09 2018 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/neilt6/code/SDFileSystem/#3fa5eaf48e81
+http://developer.mbed.org/users/neilt6/code/SDFileSystem/#fd198df2322c
--- a/WIZnet_Library.lib	Mon Jul 23 06:28:17 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/zhangyx/code/WIZnet_Library/#f390679a0468
--- a/main.cpp	Mon Jul 23 06:28:17 2018 +0000
+++ b/main.cpp	Fri Sep 07 13:06:09 2018 +0000
@@ -1,226 +1,66 @@
-/************************************************************************************************
-File name:          main.cpp
-Description:        项目激光部分客户端代码
-Author:             秦智
-Date:               June 4, 2018
-Others:             本程序编写在mbed平台(https://os.mbed.com/)上
-
-*************************************************************************************************/
-
 #include "mbed.h"
 #include <math.h>
 #include <cstring>
 #include <stdlib.h>
 #include "SDFileSystem.h"
-#include "networking.h"
 
-
-/////调试
 Serial pc(PA_9, PA_10);
-DigitalOut LED(PB_8);
-/////
+//Serial serial(PA_2, PA_3);
 
-//硬件接口
+char fileName[64];
+char buff[1024];
+
+FILE * fp_open;
+
 // mosi, miso, sclk, cs, name
 SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd");
-//步进电机
-DigitalOut step[2] = {DigitalOut(PC_5), DigitalOut(PA_5)}; //0--x P9,1--y P17
-DigitalOut dir[2] = {DigitalOut(PC_4), DigitalOut(PA_4)}; //0--x,1--y
-DigitalOut en[2] = {DigitalOut(PD_2), DigitalOut(PA_2)}; //0--x,1--y
-//电子开关,激光开关
-DigitalOut switch_GS(PC_15);
-//行程开关
-// DigitalIn switch_pos1(PB_1);   //P25
-// DigitalIn switch_pos2(PA_15);  //P26
-// DigitalIn switch_pos3(PA_11);  //P27
 
-//运行中的全局变量
-volatile bool Working, Getawork, Isend, Dataused, Getdata;
-FILE *fp_drawing; // 存储图案的文件
-int status = 0; //0: 初始化;  1:建立通信;  2:等候任务;  3:等待数据    4:正在执行一个任务
-int now_x, now_y;
-int Endoffile = 0;
-
-// float thedata[50][3];
-// char sdata[1024];
-int Nofdata;
-bool ifreceive;
-
-//与实际有关参数
-float Lenoflattice = 1 ;//mm  取1mm为xy单元  unit_xy/Lenoflattice=100
-int unit_xy = 100;  //单位长度(xy移动一格)对应unit_xy转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
-int unit_x = 103;  //单位长度(xy移动一格)对应unit_x转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
-int unit_y = 103;  //单位长度(xy移动一格)对应unit_y转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
-///Ticker ticker_step;
-float step_halfperiod = 0.002;//0.02;
-int max_x, max_y;
-int dir_x = 1;///调试时调整
-int dir_y = 1;///调试时调整
-int dot_max = 10;      //灰度最大值对应多少次激光点击
-float dot_last = 0.5;    //每次激光点击持续多久
-int H_max = 255;  //灰度值最大值
-
-
-
-
-
-void report()
+void getFileName ()
 {
-    // ser2usb.printf("-1- status: %d \r\n", status);
-    // ser2usb.printf("-2- now_x: %d now_y: %d \r\n", now_x,now_y);
-    //ser2usb.printf("-3- switch_GS: %i \r\n", switch_GS.read());
-    //ser2usb.printf("-4- switch_pos1: %i switch_pos2: %i switch_pos3: %i \r\n", switch_pos1.read(),switch_pos2.read(),switch_pos3.read());
-    //ser2usb.printf("-5- en[0]: %i en[1]: %i \r\n", en[0].read(),en[1].read());
-    //ser2usb.printf("-6- dir[0]: %i dir[1]: %i \r\n", dir[0].read(),dir[1].read());
-    //ser2usb.printf("-7- step[0]: %i step[1]: %i \r\n", step[0].read(),step[1].read());
+    pc.printf("type in a file name:\r\n");
+    pc.scanf("%s", fileName);
+    pc.printf("got file name:%s\r\n", fileName);
 }
 
-void init()
-{
-    status = 0;
-    now_x = 0;
-    now_y = 0;
-    step[0] = 0;
-    step[1] = 0;
-}
-void rotate(int id, int pix) //id= 0--x,1--y  pix=3200为一圈
+void readFile(char * name)
 {
-    if (pix >= 0) {
-        dir[0] = dir_x;
-        dir[1] = dir_y;
-    } else {
-        pix = -pix;
-        dir[0] = 1 - dir_x;
-        dir[1] = 1 - dir_y;
+    fp_open = fopen(name, "r");
+    pc.printf("reading\r\n");
+    for (int i = 0; fscanf(fp_open, "(%s)", buff) == 1; i++) 
+    {
+        pc.printf("(%s)\r\n", buff);
     }
-    for (int i = 0; i < pix; i++) {
-        step[id] = 1;
-        wait(step_halfperiod);
-        step[id] = 0;
-        wait(step_halfperiod);
-    }
+    pc.printf("read done.\r\n");
 }
 
-void moveto(float x, float y)
-{
-    rotate(0, (x - now_x)*unit_xy); //电机旋转
-    rotate(1, (y - now_y)*unit_xy);
-    now_x = x;
-    now_y = y;
-}
-void markdot(float value)
-{
-    //switch_GS=1;
-    int Ndot = floor(value * dot_max / H_max);
-    for (int i = 0; i < Ndot; i++) {
-        switch_GS = 1; // 开激光
-        wait(dot_last); // 等待
-        switch_GS = 0; // 关激光
-    }
-}
-
-void draw()
+void writeFile(char * name)
 {
-    float x, y, v;
-    fp_drawing = fopen("/sd/write.txt", "r"); // 打开文件
-    for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) {
-        pc.printf("(%f,%f,%f)\r\n", x, y, v);
-        moveto(x, y); // 移动到坐标位置
-        markdot(v); // 打点
-    }
-    fclose(fp_drawing); // 关闭文件
-}
-void fuwei()
-{
-    moveto(0, 0);
-    status = 1;
-}
-
-void on_control_cmd(const char* actuator_name, const char* control_value)
-{
-    pc.printf("Received [%s] [%s]\r\n", actuator_name, control_value);
-    if (strcmp(actuator_name, "dat") == 0) {
-        //接受到的坐标数据写入存储卡
-        fprintf(fp_drawing, "%s", control_value);
-    } else if (strcmp(actuator_name, "cmd") == 0) {
-        int the_command = atoi(control_value);
-        if (the_command == 1) {
-            Getawork = 1;
-        } else if (the_command == 2) {
-            Getdata = 1;
+    int a = 0,
+        b = 0,
+        c = 0;
+    fp_open = fopen(name, "w");
+    pc.printf("writing\r\n");
+    pc.printf("type in data(use '0 0 0' to end)\r\n");
+    while(1)
+    {
+        pc.scanf("%d %d %d", &a, &b, &c);
+        if (a == 0 && b == 0 && c == 0)
+            break;
+        else
+        {
+            pc.printf("writing data: (%d,%d,%d)\r\n", a, b, c);
+            fprintf(fp_open, "(%d,%d,%d)", a, b, c);
         }
     }
+    fclose(fp_open);
+    pc.printf("write done.\r\n");
 }
 
-int main()
+int main ()
 {
-    init();     //初始化
-
-    //建立通信
-    status = 1;
-
-    pc.printf("starting\r\n");
-
-    MQTTSocket sock;
-    MClient client(sock);
-
-    //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
-    const char* sensors[][2] = {
-        "report", "",
-        NULL, NULL //最后一行以空指针作为结束标记
-    };
-
-    //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
-    const char* actuators[][2] = {
-        "dat", "",
-        "cmd", "",
-        NULL, NULL //最后一行以空指针作为结束标记
-    };
-    pc.printf("connecting...\r\n");
-
-    networking_init(sock, client, "tdxls-iot.xicp.net", sensors, actuators, on_control_cmd);
-
-    pc.printf("Initialization done.\r\n");
-
-    status = 2;
-
-    publish_value(client, "report", "ready.");
-    pc.printf("laser printer send ready.\r\n");
-
-    Getawork = 0;
-    Getdata = 0;
-
-    while (true) {
-        if (status == 2) {
-            if (Getawork) { //收到新任务
-                Getawork = 0;
-                pc.printf("Begin receiving...\r\n");
-                // 打开文件,准备接受坐标数据
-                fp_drawing = fopen("/sd/write.txt", "w");
-                if (fp_drawing) {
-                    pc.printf("File opened\r\n");
-                    publish_value(client, "report", "wait data.");
-                    status = 3;
-                }
-            }
-        } else if (status == 3) {
-            if (Getdata) { //数据接收完
-                Getdata = 0;
-                pc.printf("End receiving...\r\n");
-                fclose(fp_drawing);
-                publish_value(client, "report", "received.");
-                status = 4;
-            }
-        } else if (status == 4) {
-            // pc.printf("debug4 \r\n");
-            // dealdata();
-            pc.printf("Drawing.. \r\n");
-            // usbprintdata();
-            draw();
-            pc.printf("Finished\r\n");
-            publish_value(client, "report", "done.");
-            status = 2;
-        }
-        client.yield(1000);
-    }
+    pc.baud(115200);
+    pc.printf("hello\r\n");
+    getFileName();
+    writeFile(fileName);
+    readFile(fileName);
 }
--- a/mbed.bld	Mon Jul 23 06:28:17 2018 +0000
+++ b/mbed.bld	Fri Sep 07 13:06:09 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/c0f6e94411f5
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/c0f6e94411f5
\ No newline at end of file
--- a/networking.cpp	Mon Jul 23 06:28:17 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,165 +0,0 @@
-#include "mbed.h"
-#include "WIZnetInterface.h"
-#include "MQTTSocket.h"
-#include "MQTTClient.h"
-#include "networking.h"
-
-extern Serial pc;
- //W5500接线 mosi,miso,sclk,cs,reset
-WIZnetInterface wiz(PB_5,PB_4,PB_3,PC_14,NC);
-//节点名称任取
-#define NODE_NAME "laser"
- //接在同一子网下的设备MAC地址必须不同
-uint8_t mac_addr[6]={0x50,0x51,0x50,0x00,0x00,0x01};
-
-recv_ctl_cb g_recv_cb;
-const char* (*g_actuators)[2];
-static Timer g_timer;
-
-void disable_LSE() //调用此函数将 PC_14, PC_15 用作普通I/O
-{
-    RCC_OscInitTypeDef OscInitStruct;
-    HAL_RCC_GetOscConfig(&OscInitStruct);
-//    pc.printf("%u %u %u %u\r\n",OscInitStruct.HSEState,OscInitStruct.LSEState,OscInitStruct.HSIState,OscInitStruct.LSIState);
-    
-    // Enable access to Backup domain
-    HAL_PWR_EnableBkUpAccess();
-    // Reset Backup domain
-    __HAL_RCC_BACKUPRESET_FORCE();
-    __HAL_RCC_BACKUPRESET_RELEASE();
-    // Disable access to Backup domain
-    HAL_PWR_DisableBkUpAccess();
-    
-    OscInitStruct.LSEState=RCC_LSE_OFF;
-    HAL_RCC_OscConfig(&OscInitStruct);
-    
-    HAL_RCC_GetOscConfig(&OscInitStruct);
-//    pc.printf("%u %u %u %u\r\n",OscInitStruct.HSEState,OscInitStruct.LSEState,OscInitStruct.HSIState,OscInitStruct.LSIState);
-}
-
-
-void meta_report(MClient& client, const char* ns, const char* type, 
-                    const char* payload = NULL, size_t payload_len = 0, 
-                    bool retain = false, MQTT::QoS qos = MQTT::QOS1){
-    char topic[64];
-    sprintf(topic, "/%s/" NODE_NAME "/%s", ns, type);
-    int ret = client.publish(topic, (void*)payload, payload_len, qos, retain);
-    //pc.printf("client.publish()=%d\r\n",ret);
-}
-void messageArrived(MQTT::MessageData& md)
-{
-    MQTT::Message &message = md.message;
-    pc.printf("messageArrived %d,%d\r\n", md.topicName.lenstring.len, message.payloadlen);
-
-//    char buf[64];
-//    int value, len = sizeof(buf)-1;
-//    if(message.payloadlen < len)
-//        len = message.payloadlen;
-//    memcpy(buf, message.payload, len);
-//    buf[len] = '\0';
-//    sscanf(buf, "%d", &value);
-//    pc.printf("received %d\r\n", value);
-
-    char* payload = new char[message.payloadlen+1];
-    if(!payload)
-        return;
-    memcpy(payload, message.payload, message.payloadlen);
-    payload[message.payloadlen]='\0';
-    
-    char* topic = new char[md.topicName.lenstring.len+1];
-    if(!topic){
-        delete[] payload;
-        return;
-    }
-    memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len);
-    topic[md.topicName.lenstring.len]='\0';
-    
-    char *pch = strtok (topic,"/");
-    for (int tok=0; tok<2 && pch != NULL; tok++)
-    {
-//        pc.printf ("%s\r\n",pch);
-        pch = strtok (NULL, "/");
-    }
-    if(pch)
-        g_recv_cb(pch, payload);
-    delete[] topic;
-    delete[] payload;
-}
-
-void publish_value(MClient &client, const char *topic, const char *buf)
-{
-    meta_report(client, "values",topic,buf,strlen(buf),false);
-}
-
-void buildCapability(char *out, const char* infoList[][2])
-{
-    out[0] = '\0';
-    for (int i = 0; infoList[i][0]; ++i)
-    {
-        strcat(out, infoList[i][0]);
-        strcat(out, ",");
-        strcat(out, infoList[i][1]);
-        strcat(out, "\\n");
-    }
-}
- 
-int networking_init(MQTTSocket &sock, MClient &client, const char *broker,const char* sensors[][2], const char* actuators[][2], recv_ctl_cb cb) {
-    int ret;
-    g_timer.start();
-    disable_LSE(); //free LSE pins
-    wiz.init(mac_addr);
-    pc.printf("DHCP...\r\n");
-    wiz.connect();
-    pc.printf("IP: %s\r\n", wiz.getIPAddress());
-    
-    srand(rand()^g_timer.read_us());
-    
-    ret = sock.connect((char*)broker,1883);
-    if(ret != 0){
-        pc.printf("failed to connect to TCP server\r\n");
-        return 1;
-    }
-    pc.printf("sock.connect()=%d\r\n",ret);
-    
-    srand(rand()^g_timer.read_us());
-    
-    ret = client.connect();
-    if(ret != 0){
-        pc.printf("MQTT connect failed\r\n");
-        return 1;
-    }
-    pc.printf("client.connect()=%d\r\n",ret);
-    
-    
-    ret = client.subscribe("/control/" NODE_NAME "/+", MQTT::QOS1, messageArrived);    
-    pc.printf("sock.subscribe()=%d\r\n", ret);
-
-    g_recv_cb = cb;
-    g_actuators = actuators;
-
-    char * capabilities = new char[128];
-    if(!capabilities){
-        pc.printf("failed to alloc memory\r\n");
-        return 1;
-    }
-    //for (int i = 0; actuators[i][0]; ++i){
-//        sprintf(capabilities,"/control/" NODE_NAME "/+",actuators[i][0]);
-//        ret = client.subscribe(capabilities, MQTT::QOS1, messageArrived);    
-//        pc.printf("sock.subscribe(%s)=%d\r\n", capabilities, ret);
-//    }
-
-    //节点上线消息
-    meta_report(client, "events","online");
-    
-    //报告所有可接受的控制指令
-    buildCapability(capabilities, actuators);
-    meta_report(client, "capability","control", capabilities, strlen(capabilities), true);
-    //报告所有的传感器
-    buildCapability(capabilities, sensors);
-    meta_report(client, "capability","values", capabilities, strlen(capabilities), true);
-    delete[] capabilities;
-
-    pc.printf("Initialization done.\r\n");
-    
-    return 0;
-}
--- a/networking.h	Mon Jul 23 06:28:17 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-#pragma once
-
-#include "MQTTSocket.h"
-#include "MQTTClient.h"
-
-typedef MQTT::Client<MQTTSocket,Countdown> MClient;
-typedef void (*recv_ctl_cb)(const char*, const char*);
-
-int networking_init(MQTTSocket &sock, MClient &client, const char *broker,const char* sensors[][2], const char* actuators[][2], recv_ctl_cb cb);
-void publish_value(MClient &client, const char *topic, const char *buf);