Anh Tran / Mbed OS GR-Boards_WebCamera

Dependencies:   HttpServer_snapshot_mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SdUsbConnect.h Source File

SdUsbConnect.h

Go to the documentation of this file.
00001 /* Copyright (c) 2017 dkato
00002  * SPDX-License-Identifier: Apache-2.0
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 /**************************************************************************//**
00017 * @file          SdUsbConnect.h
00018 * @brief         
00019 ******************************************************************************/
00020 #ifndef SD_USB_CONNECT_H
00021 #define SD_USB_CONNECT_H
00022 
00023 #include "mbed.h"
00024 #include "FATFileSystem.h"
00025 #include "SDBlockDevice_GRBoard.h"
00026 #include "USBHostConf.h"
00027 #if USBHOST_MSD
00028 #include "USBHostMSD.h"
00029 #endif
00030 
00031 /** A class to communicate a StorageConnect
00032  *
00033  */
00034 class SdUsbConnect {
00035 public:
00036 
00037     typedef enum {
00038         STORAGE_NON  = 0,           /*!< NON         */
00039         STORAGE_SD ,                /*!< SD          */
00040         STORAGE_USB                 /*!< USB         */
00041     } storage_type_t ;
00042 
00043     SdUsbConnect(const char *name) : fs(name), storage_type(STORAGE_NON ) {}
00044 
00045     bool connected(storage_type_t  type = STORAGE_NON ) {
00046         if (type == STORAGE_SD ) {
00047             return sd.connected();
00048         }
00049       #if USBHOST_MSD
00050         if (type == STORAGE_USB ) {
00051             return usb.connected();
00052         }
00053       #endif
00054         if (storage_type == STORAGE_SD ) {
00055             return sd.connected();
00056         }
00057       #if USBHOST_MSD
00058         if (storage_type == STORAGE_USB ) {
00059             return usb.connected();
00060         }
00061       #endif
00062 
00063         return false;
00064     }
00065 
00066     storage_type_t  connect(storage_type_t  type = STORAGE_NON ) {
00067         if ((storage_type == STORAGE_SD ) && (!sd.connected())) {
00068             fs.unmount();
00069             storage_type = STORAGE_NON ;
00070         }
00071       #if USBHOST_MSD
00072         if ((storage_type == STORAGE_USB ) && (!usb.connected())) {
00073             fs.unmount();
00074             storage_type = STORAGE_NON ;
00075         }
00076       #endif
00077         if ((type == STORAGE_SD ) && (storage_type != STORAGE_SD )) {
00078             if (sd.connect()) {
00079                 if (storage_type != STORAGE_NON ) {
00080                     fs.unmount();
00081                 }
00082                 fs.mount(&sd);
00083                 storage_type = STORAGE_SD ;
00084             }
00085       #if USBHOST_MSD
00086         } else if ((type == STORAGE_USB ) && (storage_type != STORAGE_USB )) {
00087             if (usb.connect()) {
00088                 if (storage_type != STORAGE_NON ) {
00089                     fs.unmount();
00090                 }
00091                 fs.mount(&usb);
00092                 storage_type = STORAGE_USB ;
00093             }
00094       #endif
00095         } else if (storage_type == STORAGE_NON ) {
00096             if (sd.connect()) {
00097                 fs.mount(&sd);
00098                 storage_type = STORAGE_SD ;
00099           #if USBHOST_MSD
00100             } else if (usb.connect()) {
00101                 fs.mount(&usb);
00102                 storage_type = STORAGE_USB ;
00103           #endif
00104             } else {
00105                 // do nothing
00106             }
00107         } else {
00108             // do nothing
00109         }
00110 
00111         return storage_type;
00112     }
00113 
00114     storage_type_t  wait_connect(storage_type_t  type = STORAGE_NON ) {
00115         while (connect(type) == STORAGE_NON ) {
00116             ThisThread::sleep_for(100);
00117         }
00118         return storage_type;
00119     }
00120 
00121     bool format(storage_type_t  type = STORAGE_NON , int allocation_unit = 0) {
00122         if ((storage_type == STORAGE_SD ) && (sd.connected())) {
00123             fs.unmount();
00124             fs.format(&sd, allocation_unit);
00125             fs.mount(&sd);
00126             return true;
00127         }
00128       #if USBHOST_MSD
00129         if ((storage_type == STORAGE_USB ) && (usb.connected())) {
00130             fs.unmount();
00131             fs.format(&usb, allocation_unit);
00132             fs.mount(&usb);
00133             return true;
00134         }
00135       #endif
00136         return false;
00137     }
00138 
00139     FATFileSystem * get_fs() {
00140         return &fs;
00141     }
00142 
00143 private:
00144     FATFileSystem fs;
00145     SDBlockDevice_GRBoard sd;
00146   #if USBHOST_MSD
00147     USBHostMSD usb;
00148   #endif
00149     storage_type_t  storage_type;
00150 };
00151 
00152 #endif