BoardOrange TestProgram 2013/08/13 SD => SDHC 対応

Dependencies:   EthernetNetIf FATFileSystem HTTPServer MSCFileSystem_Lib TextLCD mbed

Fork of SDHCFileSystem by Klaus Bu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "string"
00003 
00004 ////////////////////////////////////////
00005 ////////    general setting     ////////
00006 ////////////////////////////////////////
00007 //#define     USE_TextLCD_20x4
00008 #define     USE_FIXED_IP
00009 
00010 ////////////////////////////////////////
00011 ////////    For TextLCD         ////////
00012 ////////////////////////////////////////
00013 #include "TextLCD.h"
00014 #ifdef  USE_TextLCD_20x4
00015 //TextLCD     lcd( p24, p26, p27, p28, p29, p30, TextLCD::LCD20x4 ); // rs, e, d0-d3
00016 #else
00017 TextLCD     lcd( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3
00018 #endif
00019 
00020 ////////////////////////////////////////
00021 ////////    For SD_card         ////////
00022 ////////////////////////////////////////
00023 #include "SDHCFileSystem.h"
00024  
00025  SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs
00026 
00027 ////////////////////////////////////////
00028 ////////    For USB storage     ////////
00029 ////////////////////////////////////////
00030 #include "MSCFileSystem.h"
00031 MSCFileSystem   usb("usb");
00032 
00033 ////////////////////////////////////////
00034 ////////    For Ethernet test   ////////
00035 ////////////////////////////////////////
00036 #include "EthernetNetIf.h"
00037 #include "HTTPServer.h"
00038 #ifdef      USE_FIXED_IP
00039 EthernetNetIf   eth(
00040     IpAddr(192,168,0,7), //IP Address
00041     IpAddr(255,255,255,0), //Network Mask
00042     IpAddr(192,168,0,1), //Gateway
00043     IpAddr(192,168,0,1)  //DNS
00044 );
00045 #else
00046 EthernetNetIf   eth;
00047 #endif
00048 HTTPServer svr;
00049 LocalFileSystem web("local");
00050 
00051 
00052 void test_TextLCD( void );
00053 void test_file_write( char *title, char *path );
00054 void test_httpserver( void );
00055 int  position( void );
00056 
00057 int main() {
00058     test_TextLCD();
00059     wait( 1 );
00060     test_file_write( "SD card", "/sd/star_bd.txt" );
00061     wait( 1 );
00062     test_file_write( "USB storage", "/usb/star_bd.txt" );
00063     wait( 1 );
00064     test_httpserver();
00065 }
00066 
00067 
00068 void test_TextLCD( void ) {
00069     //  TextLCD test
00070 
00071 #ifdef  USE_TextLCD_20x4
00072     lcd.locate( 0, 0 );
00073     for ( int i = 0, c = '0'; i < 20; i++, c++ )
00074         lcd.putc( c );
00075 
00076     for ( int i = 0, c = 'A'; i < 20; i++, c++ )
00077         lcd.putc( c );
00078             
00079     for ( int i = 0, c = 'a'; i < 20; i++, c++ )
00080         lcd.putc( c );
00081     for ( int i = 0, c = '0' - 10; i < 20; i++, c++ )
00082         lcd.putc( c );
00083     exit( 0 );
00084     wait( 300 );
00085     lcd.cls();
00086 #endif  //  USE_TextLCD_20x4
00087 
00088     lcd.locate( 0, 0 );
00089     lcd.printf( "TextLCD: OK?" );
00090     lcd.locate( 0, 1 );
00091     lcd.printf( "" );
00092 
00093 }
00094 
00095 void test_file_write( char *title, char *path ) {
00096     //  SD card test
00097     lcd.locate( 0, position() );
00098     lcd.printf( "%s: ", title );
00099 
00100     FILE *fp = fopen( path, "w" );
00101     if ( fp == NULL ) {
00102         lcd.printf( "error" );
00103         error( "Could not open file for write\n" );
00104     }
00105     fprintf( fp, "The mbed writing a file through the star board orange (%s)!", title );
00106     fclose( fp );
00107 
00108     lcd.printf( "OK." );
00109 }
00110 
00111 
00112 void test_httpserver( void ) {
00113     DigitalOut  led1( LED1 );
00114 
00115     lcd.locate( 0, position() );
00116     lcd.printf( "HTTP srv: " );
00117 
00118     Base::add_rpc_class<DigitalOut>();
00119 
00120     printf("Setting up...n");
00121     EthernetErr ethErr = eth.setup();
00122     if ( ethErr ) {
00123         lcd.printf( "error" );
00124         error( "error @ eth.setup()\n" );
00125     }
00126     lcd.printf("OK ");
00127 
00128     FSHandler::mount("/local", "/");  //Mount /webfs path on web root path
00129     FSHandler::mount("/sd", "/sd");   //Mount /webfs path on web sd path
00130     FSHandler::mount("/usb", "/usb"); //Mount /webfs path on web usb path
00131 
00132     svr.addHandler<FSHandler>("/");      //Default handler
00133     svr.addHandler<FSHandler>("/sd");  
00134     svr.addHandler<FSHandler>("/usb"); 
00135     //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
00136 
00137     svr.bind(80);
00138 
00139     lcd.locate( 5, position() -1 );
00140     lcd.printf("Listening");
00141 
00142     Timer tm;
00143     tm.start();
00144     //Listen indefinitely
00145     while (true) {
00146         Net::poll();
00147         if (tm.read()>.5) {
00148             led1=!led1; //Show that we are alive
00149             tm.start();
00150         }
00151     }
00152 }
00153 
00154 int position( void ) {
00155     static int  p   = 0;
00156 
00157 #ifdef  USE_TextLCD_20x4
00158     return( ++p % 4 );
00159 #else
00160     return( ++p % 2 );
00161 #endif
00162 }