テキストファイルからLEDを光らせるプログラムです。

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut led1(LED1);
00004 DigitalOut led2(LED2);
00005 DigitalOut led3(LED3);
00006 DigitalOut led4(LED4);
00007 
00008 LocalFileSystem local("local");
00009 
00010 char LedPtn[256][4];
00011 int LedPtn_num = 0;
00012 int WaitTime = 0;
00013 
00014 unsigned char GetFileLine( FILE *stm , char *str ) {
00015     char count = 0;
00016 
00017     if(fread(&str[count], 1,1,stm) > 0) {
00018         count++;
00019         while( (fread(&str[count], 1,1,stm) > 0)
00020               &&(str[count] != '\n')
00021              &&(count < 512)) {
00022 
00023             count++;
00024 
00025         }
00026     }
00027     str[count] = '\0';
00028 
00029     return(count);
00030 }
00031 
00032 int main() {
00033     FILE *stm;
00034     char tmp[512];
00035     int count = 0;
00036     
00037     // Get File
00038     stm = fopen("/local/led.txt","r");
00039     
00040     if( stm == NULL ) {
00041         // File read err
00042         return(-1);
00043     }
00044     
00045     // Wait time read
00046     if( GetFileLine( stm , tmp ) == 0 ) {
00047         // read err
00048         return(-1);
00049     }
00050     if( sscanf( tmp , "%d",&WaitTime ) == 0 ) {
00051         //read err
00052         return(-1);
00053     }
00054     printf( "Get! Wait time : %d[ms]\r\n", WaitTime );
00055     
00056     // LED flash pattern read
00057     while( (GetFileLine( stm , tmp ) != 0) && (LedPtn_num < 256) ) {
00058         char *pled = LedPtn[LedPtn_num];
00059         if( sscanf( tmp , "%d,%d,%d,%d",&pled[0],&pled[1],&pled[2],&pled[3]) == 0)
00060         {
00061             break;
00062         }
00063         printf( "Get!Led flash pattern : [%d][%d][%d][%d]\r\n", pled[0],pled[1],pled[2],pled[3]);
00064         LedPtn_num++;
00065     }
00066     fclose(stm);
00067     
00068     while(1) {
00069         led1 = LedPtn[count][0];
00070         led2 = LedPtn[count][1];
00071         led3 = LedPtn[count][2];
00072         led4 = LedPtn[count][3];
00073         wait_ms(WaitTime);
00074         count++;
00075         if( count >= LedPtn_num ) {
00076             count = 0;
00077         }
00078     }
00079 }