Functions and formatted printing of time and date for RTC8563

Dependencies:   mbed

Lösung ParsSerial

ParsSerial.cpp

#include <string.h>

// ---------------- Inherited ParsSerial Class  --------------------------
class ParsSerial : public SerialEventInh {
    public:
        ParsSerial() : SerialEventInh() { // create the Serial on the Default pins
        }

        ParsSerial(PinName tx, PinName rx) : SerialEventInh(tx, rx) { // create the Serial on the pin specified
        }
        
        void ParseFelder(char* inputBuffer, char** pFelder, int anzFelder, char* trennzeichen);
        void pc_recv(char str[]);
        char * fgetStr(char str[]);
};

// ---------------- Parse String Function --------------------------
#define ANZ_FELDER  (5) // Anzahl Komma getrennter Felder
#define TRENNZEICHEN "/"
 
// This function seperates the single input string in to anzFelder substrings

/** How to call: 
    char str[STRMAX];
    char* pFelder[ANZ_FELDER];
    
    ps.ParseFelder(str, pFelder, ANZ_FELDER, TRENNZEICHEN);
    printf("Str: %s  %s  %s\r\n", pFelder[0], pFelder[1], pFelder[2]);
    
    Maybe convert:
    int type = atoi(pFelder[0]);
    int value = atoi(pFelder[1]);
    float fl = atof(pFelder[2]);
    printf("Type: %d \tValue %d \t Float %f \r\n", type, value, fl);
*/

void ParsSerial :: ParseFelder(char inputBuffer[], char* pFelder[], int anzFelder, char* trennzeichen)
{
    char* pString = inputBuffer;
    char* pFeld;
    
    for(int i=0; i<anzFelder; i++)
    {
        pFeld = strtok(pString, trennzeichen);
 
        if(pFeld != NULL)
            pFelder[i] = pFeld;
        else
            pFelder[i] = "";
         pString = NULL; //to make strtok continue parsing the next field rather than start again on the original string (see strtok documentation for more details)
    }
}

DigitalOut led1(LED1, 0);
// ParsSerial ps(USBTX, USBRX);
ParsSerial ps;

int main() 
{
    char str[STRMAX];
    char* pFelder[ANZ_FELDER];

    led1 = 1; wait(0.2); led1=0;

    printf("Hello\n");
    while(1) {
        if(ps.checkFlag()) {
            ps.getString(str);
            printf("String: %s\n", str);
            ps.ParseFelder(str, pFelder, ANZ_FELDER, TRENNZEICHEN);
            printf("Str: %s  %s  %s\r\n", pFelder[0], pFelder[1], pFelder[2]);
        }
    }
}

All wikipages