Export for Dan

Dependencies:   mbed MODSERIAL1

main.cpp

Committer:
atravieso
Date:
2015-10-27
Revision:
3:4039b6c8da2e
Parent:
2:d6cadd28a68e
Child:
4:810c3971bb3e

File content as of revision 3:4039b6c8da2e:



#include "mbed.h"
#include "MODSERIAL.h"

#define MESSAGE_BUFFER_SIZE 100

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

MODSERIAL messageSystem(USBTX, USBRX);

char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
int hexBufferIncoming[MESSAGE_BUFFER_SIZE];
char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
int count = 0;
int i = 0;
bool messageReceived;


void messageReceive(MODSERIAL_IRQ_INFO *q) {
    // Get the pointer to MODSERIAL object that invoked this callback.
    MODSERIAL *sys = q->serial;
    
    //dereference rxGetLasChar() of sys object 
    int c = sys->rxGetLastChar(); // Returns the last byte to pass through the RX interrupt handler.
    
    //add char c to messageBufferIncoming
    if(i <=MESSAGE_BUFFER_SIZE){
        messageBufferIncoming[i] = c;
        hexBufferIncoming[i] = c;

      // if (c == 0xAA) led4 = !led4;
      //  led3 = !led3;
        i++;
       
        }
        
    count++;
    if (count == 4){ //Need to be able to find the string anywhere in the buffer
    //need to change to HEX.  When I change to Hex, I can just use '==' instead of strncmp
        // led4 = !led4;

            if (hexBufferIncoming[0] == 0xAA){
                if (hexBufferIncoming[1] == 0x55){
                    if (hexBufferIncoming[2] == 0xFF){
                        if (hexBufferIncoming[3] == 0x00){
                            led1 = !led1;
                            }
                        }
                    }
                }
            
         else if (hexBufferIncoming[0] == 0xBB){
                if (hexBufferIncoming[1] == 0x44){
                    if (hexBufferIncoming[2] == 0xFF){
                        if (hexBufferIncoming[3] == 0x00){
                            led2 = !led2;
                            }
                        }
                    }
                }
                
        else if (hexBufferIncoming[0] == 0xCC){
                if (hexBufferIncoming[1] == 0x33){
                    if (hexBufferIncoming[2] == 0xFF){
                        if (hexBufferIncoming[3] == 0x00){
                            led3 = !led3;
                            }
                        }
                    }
                }     
        else if (hexBufferIncoming[0] == 0xDD){
                if (hexBufferIncoming[1] == 0x22){
                    if (hexBufferIncoming[2] == 0xFF){
                        if (hexBufferIncoming[3] == 0x00){
                            led4 = !led4;
                            }
                        }
                    }
                }  
        else if (hexBufferIncoming[0] == 0xDB){             //Get Heading
                if (hexBufferIncoming[1] == 0x24){
                    if (hexBufferIncoming[2] == 0xFF){
                        if (hexBufferIncoming[3] == 0x00){
                            led1 = !led1;
                            }
                        }
                    }
                }    
        else if (hexBufferIncoming[0] == 0xD9){             //Get Temperature
                if (hexBufferIncoming[1] == 0x26){
                    if (hexBufferIncoming[2] == 0xFF){
                        if (hexBufferIncoming[3] == 0x00){
                            led2 = !led2;
                            }
                        }
                    }
                }     
        else if (hexBufferIncoming[0] == 0xEE){             //Get Elapsed Time On
                if (hexBufferIncoming[1] == 0x11){
                    if (hexBufferIncoming[2] == 0x00){
                        if (hexBufferIncoming[3] == 0x01){
                            led3 = !led3;
                            }
                        }
                    }
                }      
         memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
         i = 0;
         messageReceived = true;
         count = 0;
         }
    return;
}

void messageProcess(void) { 
     //  led1 = !led1;

    messageReceived = false;
}


int main() {

    messageReceived = false;
    memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
    messageSystem.baud(9600);
    messageSystem.attach(&messageReceive, MODSERIAL::RxIrq); //Attach a C++ type object/method pointer as the callback.
   
    // Fix Mbed library bug, see http://mbed.org/forum/bugs-suggestions/topic/1498
    LPC_GPIOINT->IO2IntClr = (1UL << 5) | (1UL << 4) | (1UL << 3) | (1UL << 2); 
 \
    while(1) {
    
        if (messageReceived)
        {
       // led1 = !led1;
        messageProcess();
         }
    }
}