sd file lib updated

Dependencies:   FATFileSystem

Dependents:   grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3

sdcardJPA.cpp

Committer:
38domo
Date:
2021-01-13
Revision:
6:75d0098c7522
Parent:
4:03a26c21cdaf

File content as of revision 6:75d0098c7522:

//sd card file IPdatas.txt contents
/*
IP address:         192.168.1.30
Ethernet actif      0;Ethernet
Wifi actif          1;WiFi
Can actif           1;can
Serial actif        1;serial
Option function     F;Fonction
Touche 1 status     0;Touche1 status : off
Touche1 BackColor   0xFF00FF00;Touche1 BackColor
Touche1 TextColor   0xFF000000;Touche1 TextColor
Touche1 text lign1  Pompe  ; Touche1 text
Touche1 text1lign2  Piscine ; Touche1 text1
Touche2 Status      1;Touche2 Status : off
Touche2 BackColor   0xFFFF0000; Touche2 BackColor
Touche2 TextColor   0xFFFFFFFF; Touche2 TextColor
Touche2 text L1     Eclair ;Touche2 text 
Touche2 text1 L2    Piscine ;Touche2 text1
Touche3 Status      0; Touche3 Status : off
Touche3 BackColor   0xFF00FF00; Touche3 BackColor
Touche3 TextColor   0xFFFFFF00; Touche3 TextColor
Touche3 text L1     Leds Bl ;Touche3 text
Touche3 text1 L2    Piscine ; Touche3 text1
Touche4 Status      1; Touche4 Status : off
Touche4 BackColor   0xFF800000; Touche4 BackColor
Touche4 TextColor   0xFFFFFF00; Touche4 TextColor
Touche4 text L1     Porche ; Touche4 text
Touche4 text1 L2    entree ; Touche4 text1
Display1 text       Temp Piscine ; Temp1 text
Display1 Backcolor  0xFF404040; backColorTemp1
Display1 text color 0xFFFFFF00;  txtColorTemp1   
Display2 text       Temp Exterieur ; Temp2 text 
Display2 backcolor  0xFF404040; backColorTemp2
Display2 text color 0xFFFFFF00; txtColorTemp2
Can Id Touche1 On   0x100130CA;Can Id Touche1 On
Can Data Touche1 On 0x01;Can Data Touche1 On
Can Id Touche1 Off  0x100130CA;Can Id Touche1 Off
Can Data Touche1 Off0x01;Can Data Touche1 Off
Can Id Touche2 On   0x100130CA;Can Id Touche2 On
Can Data Touche2 On 0x01;Can Data Touche2 On
Can Id Touche2 Off  0x100130CA;Can Id Touche2 Off
Can Data Touche2 Off0x01;Can Data Touche2 Off
Can Id Touche3 On   0x100130CA;Can Id Touche3 On
Can Data Touche3 On 0x01;Can Data Touche3 On
Can Id Touche3 Off  0x100130CA;Can Id Touche3 Off
Can Data Touche3 Off0x01;Can Data Touche3 Off
Can Id Touche4 On   0x100130CA;Can Id Touche4 On
Can Data Touche4 On 0x01;Can Data Touche4 On
Can Id Touche4 Off  0x100130CA;Can Id Touche4 Off
Can Data Touche4 Off0x01;Can Data Touche4 Off
*/

#include "mbed.h"
#include "SDFileSystem.h"
 
//SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
//SDFileSystem sd(PA_7, PA_6, PA_5, PD_15, "sd"); // mosi, miso, sclk, cs
SDFileSystem sd("sd"); // mosi, miso, sclk, cs
//gpio_init_structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;

extern char gsStreamIPdatas[]; // IP address ethernet
extern int ethernet_actif ;  // use ethernet connection
extern int wifi_actif;
extern int can_actif;
extern int serial_actif;
extern char function_mod[];
extern char read_sd[];

extern char touch1_status[];
extern char touch1_back_color[];
extern char touch1_text_color[];
extern char touch1_text[];
extern char touch1_text1[];

extern char touch2_status[];
extern char touch2_back_color[];
extern char touch2_text_color[];
extern char touch2_text[];
extern char touch2_text1[];

extern char touch3_status[];
extern char touch3_back_color[];
extern char touch3_text_color[];
extern char touch3_text[];
extern char touch3_text1[];

extern char touch4_status[];
extern char touch4_back_color[];
extern char touch4_text_color[];
extern char touch4_text[];
extern char touch4_text1[];

extern char temp1_text[];
extern char temp1_back_color[];
extern char temp1_text_color[];

extern char temp2_text[];
extern char temp2_back_color[];
extern char temp2_text_color[];

extern int len_line_sd;

extern char textButton[5][8];
extern char text1Button[5][8];
extern char textTempTitle[3][30];
extern uint32_t backColorTemp1;
extern uint32_t txtColorTemp1;
extern uint32_t backColorTemp2;
extern uint32_t txtColorTemp2;


// routine lecture sd card -----------------------------------------------------------------------
void  readline( char rp[], FILE fr[])  // readline SD line (text to displaay, file to read)
{   // SD read -----------------------------
   printf(rp);
    int xxx = 0;   
    while (true)
    {   int chr = fgetc(fr);
        if ((chr == EOF) || (chr == 10) || (chr == 13) ||(xxx > 30)||(xxx ==59) ) 
        {   if (xxx == 0)
           {    // on passe au caractere suivant = ligne suivante        
           }
           else
           {  break;  // on sort
           }
        }
        else
        {   read_sd[xxx]=chr;
            xxx = xxx +1;
        }
        //printf("%c", chr);       
    }
    len_line_sd = xxx;
    printf(" %s\n",read_sd);  
}    

// ------------------------------------------------------------------------------------
int mainSDCardJPA() {
    //printf("\r\nSD Text file write/read test: started\r\n\n");
    sd.mount(); 
    FILE *fp = fopen("/sd/WriteReadTest.txt", "w");
    if (fp == NULL)
    {   fprintf(stderr, "Open error for writing!!\r\n");
        while (true) {}
    }
    fprintf(fp, "Example of writing and reading of text file.\n");
    fclose(fp);
    
    // SD read IP address --------------------------
    //printf("IP to get datas from file IPdatas.txt : ");
    fp = fopen("/sd/IPdatas.txt", "r");
    if (fp == NULL)
    {  fprintf(stderr, "Open error for reading!!\r\n");
        while (true) {}
    }
    //printf("String in read file is as follows:\r\n");  
    int x = 0;
    printf("IPdatas : ");
    while (true)
    {   int chr = fgetc(fp);
        if ((chr == EOF) || (chr == 10) || (chr == 13) ||(x > 14)) 
        {   break;
        }
        gsStreamIPdatas[x]=chr;
        x=x+1;
        //printf("%c", chr);
    }
    printf(" %s\n",gsStreamIPdatas);
    
    // SD read ethernet connectivity -----------------------------
    readline( "Ethernet status: ",fp);  
    if (read_sd[0]=='0')
    {  ethernet_actif = 0;
       printf("Ethernet inactif: %i\n", ethernet_actif);}
    else
    {  ethernet_actif = 1;
       printf("Ethernet actif: %i\n", ethernet_actif);}
    
    // SD read wifi connectivity -----------------------------
    readline( "WiFi actif : ",fp);  
    //wifi_actif[0] = read_sd[0];
    if (read_sd[0]=='0')
    { wifi_actif = 0;}
    else
    { wifi_actif = 1;}
    
    // SD read CAN connectivity -----------------------------
    readline( "Can actif : ",fp);  
    //can_actif[0] = read_sd[0];
    if (read_sd[0]=='0')
    { can_actif = 0;}
    else
    { can_actif = 1;}   
    //printf("Can value saved %c\n", can_actif);
      
    // SD read serial 2 connectivity -----------------------------
    readline( "Serial actif : ",fp);  
    //serial_actif[0] = read_sd[0];
    if (read_sd[0]=='0')
    { serial_actif = 0;}
    else
    { serial_actif = 1;}
    //printf("Serial value saved %c\n", serial_actif);

    // SD read card function -----------------------------
    readline( "Function : ",fp);  
    function_mod[0] = read_sd[0];
    //printf("Function mod saved %c\n", function_mod[0]);
// ------------- touche  1 parameters    
    // SD read -----------------------------
    readline( "Touch1 status : ",fp);  
    touch1_status[0] = read_sd[0];
    printf("Touch1 status: %c\n", touch1_status[0]);
    // SD read -----------------------------
    readline( "Touch1 back color : ",fp);  
    printf("Touch1 back color: "); 
    for ( x=0; x<10;x++)
    {    touch1_back_color[x]=read_sd[x];
         printf("%c", touch1_back_color[x]);        
    }
    //printf("Touch1 back color: %s\n",touch1_back_color); 
    printf("\n");       
    // SD read -----------------------------
    readline( "Touch1 text color : ",fp);  
    printf("Touch1 text color: ");
    for ( x=0; x<10;x++)
    {    touch1_text_color[x]=read_sd[x];  
    printf("%c", touch1_text_color[x]);    
    }
    //printf("Touch1 text color: %s\n",touch1_text_color);
    printf("\n");      
    // SD read -----------------------------
    readline( "Touch1 text : ",fp);  
    printf("Touch1 text: ");   
    for ( x=0; x<7;x++) //len_line_sd
    {   if (  read_sd[x] == ';')
        {break;}
        touch1_text[x] = read_sd[x];
        textButton[1][x]=read_sd[x];
        printf("%c", touch1_text[x]);
    }
    printf("\n"); 
    //sprintf(textButton[1],touch1_text);
    printf("TextButton1 : %s\n",textButton[1]);      
    // SD read -----------------------------
    readline( "Touch1 text1 : ",fp);  
    for ( x=0; x<7;x++)
    {    touch1_text1[x] = read_sd[x];
         text1Button[1][x]=read_sd[x];
         printf("%c", touch1_text1[x]);  
    }
    //sprintf(text1Button[1],touch1_text1);
    printf("Touch1 text1: %s\n",touch1_text1);
    
       
 // --------------- touche 2  datas -
    // SD read -----------------------------
    readline( "Touch2 status : ",fp);  
    touch2_status[0] = read_sd[0];
    printf("Touch2 status: %c\n", touch2_status[0]);

    // SD read -----------------------------
    readline( "Touch2 back color : ",fp); 
    printf("Touch2 back color:  ");  
    for ( x=0; x<10;x++)
    {    touch2_back_color[x]=read_sd[x];
         printf("%c", touch2_back_color[x]);  
    }
   // printf("Touch2 back color:  %s\n",touch2_back_color); 
    printf("\n");   
    
    // SD read -----------------------------
    readline( "Touch2 text color : ",fp); 
    printf("Touch2 text color: "); 
    for ( x=0; x<10;x++)
    {    touch2_text_color[x]=read_sd[x];
         printf("%c", touch2_text_color[x]);  
    }
    //printf("Touch2 text color: %s\n",touch2_text_color);
    printf("\n");       
    
    // SD read -----------------------------
    readline( "Touch2 text : ",fp);  
    printf("Touch2 text: "); 
    for ( x=0; x<7;x++)
    {  touch2_text[x] = read_sd[x]; 
       textButton[2][x]=read_sd[x];
       printf("%c", touch2_text[x]);  
    } 
    //printf("Touch2 text: %s\n",touch2_text);    
    printf("\n"); 
    //sprintf(textButton[2],touch2_text);   
    
    // SD read -----------------------------
    readline( "Touch2 text1 : ",fp); 
    printf("Touch2 text1 ");
    for ( x=0; x<7;x++)
    {  touch2_text1[x] = read_sd[x]; 
       text1Button[2][x]=read_sd[x];
       printf("%c", touch2_text1[x]);  
    }  
    //printf("Touch2 text1 %c\n", touch2_text1);
    printf("\n");   
    //sprintf(text1Button[2],touch2_text1);
    
 // --------------------- touche 3       
     // SD read -----------------------------
    readline( "Touch3 status : ",fp);  
    touch3_status[0] = read_sd[0];
    printf("Touch3 status %c\n", touch3_status[0]);

    // SD read -----------------------------
    readline( "Touch3 back color : ",fp); 
    printf("Touch3 back color ");
    for ( x=0; x<10;x++)
    {    touch3_back_color[x]=read_sd[x];
         printf("%c", touch3_back_color[x]);  
    } 
    //printf("Touch3 back color %c\n", touch3_back_color);
    printf("\n");
    
    // SD read -----------------------------
    readline( "Touch3 text color : ",fp); 
    printf("Touch3 text color ");
    for ( x=0; x<10;x++)
    {    touch3_text_color[x]=read_sd[x];
         printf("%c", touch3_text_color[x]);  
    } 
    //printf("Touch3 text color %c\n", touch3_text_color);
    printf("\n");
    
    // SD read -----------------------------
    readline( "Touch3 text : ",fp);
    printf("Touch3 text "); 
    for ( x=0; x<7;x++)
    {  touch3_text[x] = read_sd[x];
       textButton[3][x]=read_sd[x]; 
       printf("%c", touch3_text[x]);  
    }     
    //printf("Touch3 text %c\n", touch3_text);
    printf("\n");
    //sprintf(textButton[3],touch3_text);
    
    // SD read -----------------------------
    readline( "Touch3 text1 : ",fp); 
    printf("Touch3 text1 ");
    for ( x=0; x<7;x++)
    {  touch3_text1[x] = read_sd[x];
       text1Button[3][x]=read_sd[x]; 
       printf("%c", touch3_text1[x]);  
    }     
    //printf("Touch3 text1 %c\n", touch3_text1);
    printf("\n");
   // sprintf(text1Button[3],touch3_text1);
    
 // -------------------- touche 4   
     // SD read -----------------------------
    readline( "Touch4 status : ",fp);  
    touch4_status[0] = read_sd[0];
    printf("Touch4 status %c\n", touch4_status[0]);

    // SD read -----------------------------
    readline( "Touch4 back color : ",fp);
   printf("Touch4 back color "); 
   for ( x=0; x<10;x++)
    {    touch4_back_color[x]=read_sd[x];
         printf("%c", touch4_back_color[x]);  
    } 
    //printf("Touch4 back color %c\n", touch4_back_color);
    printf("\n");
    
    // SD read -----------------------------
    readline( "Touch4 text color : ",fp);
    printf("Touch4 text color ");  
    for ( x=0; x<10;x++)
    {    touch4_text_color[x]=read_sd[x];
         printf("%c", touch4_text_color[x]);  
    } 
    //printf("Touch4 text color %c\n", touch4_text_color);
    printf("\n");
    
    // SD read -----------------------------
    readline( "Touch4 text : ",fp);  
    printf("Touch4 text ");
    for ( x=0; x<7;x++)
    {  touch4_text[x] = read_sd[x]; 
       textButton[4][x]=read_sd[x]; 
       printf("%c", touch4_text[x]);  
    }     
    //printf("Touch4 text %c\n", touch4_text);
    printf("\n");
    //sprintf(textButton[4],touch4_text);
    
    // SD read -----------------------------
    readline( "Touch4 text1 : ",fp); 
    printf("Touch4 text1 "); 
    for ( x=0; x<7;x++)
    {  touch4_text1[x] = read_sd[x];
       text1Button[4][x]=read_sd[x]; 
       printf("%c", touch4_text1[x]);  
    }     
    //printf("Touch4 text1 %c\n", touch4_text1); 
    printf("\n");  
   // sprintf(text1Button[4],touch4_text1);
    
 // -------------------- Display1  pad  -------------------------------------------------------------- 
     // SD read -----------------------------
    readline( "Temp1 text : ",fp); 
    printf("Temp1 text  ");
    for ( x=0; x<15;x++)
    {  temp1_text[x] = read_sd[x]; 
       textTempTitle[1][x] = read_sd[x]; 
       printf("%c", temp1_text[x]);  
    }     
    //printf("Temp1 text %c\n", temp1_text);
    printf("\n");

    // SD read -----------------------------
    readline( "Temp1 back color : ",fp); 
    printf("Temp1 back color ");
    for ( x=0; x<10;x++)
    {  temp1_back_color[x] = read_sd[x];    
       printf("%c", temp1_back_color[x]);  
    }     
    //printf("Temp1 back color %c\n", temp1_back_color);
    printf("\n");
    backColorTemp1 = int(touch1_back_color);
    
    // SD read -----------------------------
    readline( "Temp1 text color : ",fp);
    printf("Temp1 text color "); 
    for ( x=0; x<10;x++)
    {  temp1_text_color[x] = read_sd[x]; 
       printf("%c", temp1_text_color[x]);  
    }          
    //printf("Temp1 text color %c\n", temp1_text_color);
    printf("\n");
    txtColorTemp1 = int(temp1_text_color);
    
// -------------------- display2 pad -------------------------------------------------
    // SD read -----------------------------
    readline( "Temp2 text : ",fp);
    printf("Temp2 text ");  
    for ( x=0; x<15;x++)
    {  temp2_text[x] = read_sd[x]; 
       textTempTitle[2][x] = read_sd[x]; 
       printf("%c", temp2_text[x]);  
    }         
    //printf("Temp2 text %c\n", temp2_text);
    printf("\n");

    // SD read -----------------------------
    readline( "Temp2 back color : ",fp); 
    printf("Temp2 back color ");
    for ( x=0; x<10;x++)
    {  temp2_back_color[x] = read_sd[x]; 
       printf("%c", temp2_back_color[x]);  
    }          
    //printf("Temp2 back color %c\n", temp2_back_color);
    printf("\n");
    backColorTemp2 = int(temp2_back_color);
    
    // SD read -----------------------------
    readline( "Temp2 text color : ",fp); 
    printf("Temp2 text color ");
     for ( x=0; x<10;x++)
    {  temp2_text_color[x] = read_sd[x]; 
       printf("%c", temp2_text_color[x]);  
    }         
    //printf("Temp2 text color %c\n", temp2_text_color);
    printf("\n");
    txtColorTemp2 = int(temp2_text_color);
          
 // --------------------------------              
    fclose(fp);
    sd.unmount();    
    printf("\r\n SD Text file write/read : completed\r\n");
    return 1;
    }