Export for Dan

Dependencies:   MODSERIAL mbed

Revision:
0:6861a3fd2ef0
Child:
1:26499ae2eda9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DC-4E.cpp	Tue Nov 03 22:17:02 2015 +0000
@@ -0,0 +1,115 @@
+
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+
+#define MESSAGE_BUFFER_SIZE 100
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+MODSERIAL Compass (p9, p10);
+
+MODSERIAL messageSystem(p13, p14);
+
+unsigned char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
+unsigned int hexBufferIncoming[MESSAGE_BUFFER_SIZE];
+unsigned char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
+unsigned int compassMessage[3];
+int count = 0;
+int i = 0;
+bool messageReceived;
+
+
+void messageReceive(MODSERIAL_IRQ_INFO *q) {
+    // Get the pointer to MODSERIAL object that invoked this callback.
+    led1 = !led1;
+    MODSERIAL *sys = q->serial;
+    
+    //dereference rxGetLasChar() of sys object 
+    unsigned int c = sys->rxGetLastChar(); // Returns the last byte to pass through the RX interrupt handler.
+    
+    //add the character that triggered the interrupt to the incoming buffers
+    //I'm adding to an in buffer and a char buffer, but using the int buffer to determine which message it is
+        //if I want to use the char buffer to determine, then I need to use strcmp instead of =
+    if(i <=MESSAGE_BUFFER_SIZE){
+        messageBufferIncoming[i] = c;
+        hexBufferIncoming[i] = c;
+        messageSystem.printf("%x", hexBufferIncoming[i]);
+        i++;
+        }     
+    count++;
+   
+  /*if (count == 3){ //send to compass and read the compasses response
+       led3 = !led3;
+            if (hexBufferIncoming[0] == 0xA4){              //Start Calibration
+                if (hexBufferIncoming[1] == 0x01){
+                    if (hexBufferIncoming[2] == 0xA0){
+                            led1 = !led1;
+                            messageSystem.printf("%c", hexBufferIncoming[0]);              
+                            messageSystem.printf("%c", hexBufferIncoming[1]);
+                            messageSystem.printf("%c", hexBufferIncoming[2]);
+                            
+                        
+                        }
+                    }
+               }
+           }
+         
+       */
+         
+             
+         memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
+         memset(hexBufferIncoming, '\0', sizeof(hexBufferIncoming));
+         memset(messageBufferOutgoing, '\0', sizeof(messageBufferOutgoing));
+         i = 0;
+         messageReceived = true;
+         count = 0;
+         //} //end of if count == 4
+
+
+    return;
+}
+
+void messageProcess(void) { 
+     //  led1 = !led1;
+    messageReceived = false;
+}
+
+
+
+
+
+int main() {
+
+    messageReceived = false;
+    memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
+    memset(hexBufferIncoming, '\0', sizeof(hexBufferIncoming));
+    memset(messageBufferOutgoing, '\0', sizeof(messageBufferOutgoing));
+    messageSystem.baud(9600);
+    Compass.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) {
+        //led1 = !led1;
+        wait(.05);
+        compassMessage[0] = 0xA3;
+      //  compassMessage[1] = 0x02;
+        compassMessage[2] = 0xA1;
+        Compass.printf("%x", compassMessage[0]);
+        Compass.printf("%x", compassMessage[1]);
+        Compass.printf("%x", compassMessage[2]);
+    
+        if (messageReceived)
+        {
+        //led2 = !led2;
+        messageProcess();
+         }
+    }
+}