Emulation of LocalFileSystem with virtual COM.

Dependencies:   USBDevice

Dependents:   KL46Z-lpc81isp lpcterm2

#include "USBLocalFileSystem.h"

int main() {
    USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)

    while(1) {
        usb_local->lock(true);
        usb_local->remount();
        char filename[32];
        if (usb_local->find(filename, sizeof(filename), "*.TXT")) {
            FILE* fp = fopen(filename, "r");
            if (fp) {
                int c;
                while((c = fgetc(fp)) != EOF) {
                    usb_local->putc(c);
                }
                fclose(fp);
            }
        }    
        usb_local->lock(false);

        wait_ms(1000*5);
    }
}



Sample application:

Import programKL46Z-lpc81isp

ISP example program.

Import programlpcterm2

semihost server example program

src/mystring.h

Committer:
va009039
Date:
2014-06-21
Revision:
6:528036abfb02
Parent:
0:39eb4d5b97df

File content as of revision 6:528036abfb02:

#include <stdlib.h>
#include <string.h>
#pragma once
class mystring {
public:
    mystring(){
        _init();
    }
    mystring(const char* s) {
        _init();
        append(s);
    }
    
    ~mystring() {
        if (_buf) {
            free(_buf);
        }
    }
    void clear() {
        if (_buf) {
            free(_buf);
        }
        _init();
    }
    bool empty() {
        return _len == 0;
    }
    size_t size() {
        return _len;
    }
    void append(const char* s, int len) {
        if (_buf == NULL) {
            return;
        }
        int new_len = _len + len;
        char* new_buf = (char*)malloc(new_len + 1);
        if (new_buf == NULL) {
            return;
        }
        memcpy(new_buf, _buf, _len);
        memcpy(new_buf+_len, s, len);
        new_buf[_len+len] = '\0';
        free(_buf);
        _buf = new_buf;
        _len += new_len;
    }
    void append(const char* s) {
        append(s, strlen(s));
    }
    void append(int c) {
        char buf[1];
        buf[0] = c;
        append(buf, sizeof(buf));
    }
    char* c_str() {
        if (_buf) {
            return _buf;
        }
        return "";
    }
    mystring& operator= (const char* s) {
        _init();
        append(s);
        return *this;
    }
    mystring& operator+= (const char* s) {
        append(s);
        return *this;
    }
    char& operator[] (size_t pos) {
        return _buf[pos];
    }
private:
    void _init() {
        _len = 0;
        _buf = (char*)malloc(1);
        if (_buf) {
            _buf[0] = '\0';
        }
    }
    char* _buf;
    int _len;
};