Arduino loop

From dftwiki3
Revision as of 11:44, 1 November 2008 by Thiebaut (talk | contribs) (New page: <code><pre> #include <stdio.h> #include <string.h> char buffer[256]; int len; char *operation; char *mode; char *pin; char *state; void setup(); void loop(); void resetAll(); int proce...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

#include <stdio.h>
#include <string.h>


char buffer[256];
int len;
char *operation;
char *mode;
char *pin;
char *state;

void setup();
void loop();
void resetAll();
int process();
int writePin( char mode, int pin, int state );
int readPins( char mode );

int main() {
    setup();
    while (1) {
        loop();
    }
    return 0;
}

void setup() {
    resetAll();
}

void resetAll() {
    buffer[0] = '\0';
    len       = 0;
    operation = mode = pin = state = 0;
}

void loop() {
    char c= getchar();
    printf( " c='%c'", c );
    if ( c=='\n' ) {
        if ( process() ) printf( "\n\nError!\n\n" );
        resetAll();
        return;
    }
     
    buffer[ len++ ] = c;
    buffer[ len ] = '\0';
    printf( " len=%d buffer=[%s] ", len, buffer );
}

char* mystrtok( char* p, int skipNonBlank ) {
    char* q=p;
    if ( skipNonBlank ) {
        for ( ; *q != ' ' && *q != '\0'; q++ );
        if ( *q=='\0' ) return NULL;
    }
    for ( ; *q != '\0' && *q==' '; q++ );
    if ( *q == '\0' ) return NULL;
    if ( *q == ' ' ) return NULL;
    return q;
}

int process() {
    char *p;
    int len = strlen( buffer );
    int intPin;
    int intState;

    printf( "\n\nProcessing buffer =[%s]\n\n", buffer );
    
    if ( len < 3 ) {
        resetAll();
        return 1; //error
    }

    operation = mystrtok( buffer, 0 );

    if ( ( !operation || (*operation!='r' && *operation!='w') ) 
         || ( ( *operation=='r' && len < 3 ) ||( *operation=='w' && len < 7 ) ) ) {
        resetAll();
        return 1; // error
    }

    printf( "Operation = %c\n", *operation );

    mode = mystrtok( operation + 1, 0 );
    if (  !mode || ( *mode!='a' && *mode!='d' ) ) {
        resetAll();
        return 1; // success
    }

    printf( "Mode = %c\n", *mode );

    if ( *operation == 'r' ) {
        readPins( *mode );
        resetAll();
        return 0; // success
    }


    pin = mystrtok( mode + 1, 0 );
    if ( !pin ) {
        resetAll();
        return 1; // error
    }
    intPin = atoi( pin );
    if ( intPin < 2 || intPin > 13 ) {
        resetAll();
        return 1; // error
    }
    
    state = mystrtok( pin+1, 1 );
    if ( !state ) {
        resetAll();
        return 1; // error 
    }
    intState = atoi( state );
    if ( intState < 0 || intState > 1 ) {
        resetAll();
        return 1;
    }
    
    writePin( *mode, intPin, intState );

    resetAll();
    return 0;
}

int writePin( char mode, int  intPin, int intState ) {
    if ( mode=='d' )
        printf( "digitalWrite( %d, %d )\n", intPin, intState );
    if ( mode=='a' )
        printf( "analogWrite( %d, %d )\n", intPin, intState );    
}

int readPins( char mode ) {
    int i;
    if ( mode=='d' )
        for ( i=2; i<14; i++ )
            printf( "digitalRead(%d) ", i );
    if ( mode=='a' )
        for ( i=0; i<6; i++ )
            printf( "analogRead(%d) ", i );
}