Adam Clarkson / Mbed 2 deprecated Bluetooth_Car_ECE595

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
awclarks
Date:
Sat Mar 04 23:04:02 2017 +0000
Parent:
1:72d5896dd1f1
Child:
3:70b72839154f
Commit message:
Collects input using queue. Numerical data stored in uint16. Extra printing turned off in Queue.h

Changed in this revision

Queue.c Show annotated file Show diff for this revision Revisions of this file
Queue.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Queue.c	Sat Mar 04 23:04:02 2017 +0000
@@ -0,0 +1,167 @@
+/*
+Adam Clarkson
+ECE 359
+HW2 part 3
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "Queue.h"
+
+//#include "Serial.h"
+
+
+
+/*creates an empty Queue*/
+void InitQ(Q_t *pQ)
+{
+	pQ->head = NULL;
+	pQ->tail = NULL;
+	pQ->n = 0;
+	return;
+}
+
+//removes all items in Queue
+void FreeQ(Q_t *pQ)
+{
+	Item_t *pItem = pQ->head;
+	Item_t *pX = NULL;
+	
+	while (NULL != pItem)
+	{
+		pX = pItem;
+		pItem = pItem->next;
+		free(pX);
+	}
+	pQ->head = NULL;
+	pQ->tail = NULL;
+	pQ->n = NULL;
+
+	return;
+}
+
+//Return 1 if full
+int IsFull(Q_t *pQ)
+{
+	return(pQ->n >= MAX_QUEUE_SIZE);
+}
+
+//Return 1 if empty 
+int IsEmpty(Q_t *pQ)
+{
+	return(0==pQ->n);
+
+}
+
+//Insert item into Q. If succesfull return 1
+int Enqueue(Item_t item, Q_t *pQ)
+{
+	Item_t *pItem=NULL;
+	int returnVal = 0;
+
+	if (IsFull(pQ))
+	{
+		returnVal = 0;
+		//printf("Max Queue Reached!\n\r");
+	}
+	else
+	{
+		
+		Item_t *pItem = (Item_t*)calloc(1, sizeof(Item_t));
+		*pItem = item;
+		returnVal = 1;
+		//if only one item in Q
+		if (NULL == pQ->head)
+		{
+			pQ->head = pItem;
+			pQ->tail = pItem;
+			pItem->next = NULL;
+			pItem->prev = NULL;
+		}
+		//if multiple items in Q
+		else
+		{
+			pQ->tail->next = pItem;
+			pItem->next = NULL;
+			pItem->prev = pQ->tail;
+			pQ->tail=pItem;
+		}
+		pQ->n += 1;
+	}
+	PrintQ(pQ);
+	return returnVal;
+}
+
+//Remove item from Q. If succesfull return 1
+int Dequeue(Item_t *pItem, Q_t *pQ)
+{
+	int j = 0;
+	int returnVal = 1;
+	if (IsEmpty(pQ))
+	{
+		returnVal = 0;
+		//printf("Empty Queue");
+	}
+	//if only one item in Q
+	else if (pQ->head==pQ->tail)
+	{
+		*pItem = *(pQ->head);
+		free(pQ->head);
+		pQ->head = NULL;
+		pQ->tail = NULL;
+		pQ->n -= 1;
+	}
+	//if multiple items in Q
+	else
+	{
+		*pItem = *(pQ->head);
+		pQ->head = pQ->head->next;
+		free(pQ->head->prev);
+		pQ->head->prev = NULL;
+		pQ->n -= 1;
+	}
+	
+#ifdef EXTRA_PRINT
+	//if Dequeue is succesfull display removed item
+	if (returnVal)
+	{
+		
+		printf("Item Removed...raw data=");
+		for (j = 0; j < INPUT_LEN; j++)
+		{
+			printf("%c_", pItem->data[j]);
+		}
+		printf("\n\r");
+	}
+	PrintQ(pQ);
+#endif
+	return returnVal;
+}
+
+void PrintQ(Q_t *pQ)
+{
+#ifdef EXTRA_PRINT
+
+	Item_t *pItem=pQ->head;
+	int i = 1;
+	int j = 0;
+	
+	printf("QUEUE LIST\n\r***************************************\n\r");
+	if (NULL == pItem)
+	{
+		printf("Empty Queue\n\r\n\r");
+	}
+	else
+	{
+		for (; NULL != pItem; pItem = pItem->next, i++)
+		{
+			printf("Item #%d\traw data=",i);
+			for (j = 0; j < INPUT_LEN; j++)
+			{
+				printf("%c_", pItem->data[j]);
+			}
+			printf("\n\r\n\r");
+		}
+	}
+#endif
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Queue.h	Sat Mar 04 23:04:02 2017 +0000
@@ -0,0 +1,60 @@
+/*
+Adam Clarkson
+3/3/17
+*/
+
+#ifndef __QUEUE__
+#define __QUEUE__
+
+
+
+/*
+************************************************************
+*********************DEFINITIONS****************************
+************************************************************/
+//Maximum number of elements in Dict array
+#define MAX_QUEUE_SIZE 10
+#define INPUT_LEN	7
+
+//#define EXTRA_PRINT
+
+
+typedef struct qItem{
+	char data[INPUT_LEN];
+	struct qItem *next, *prev;
+}Item_t;
+
+//NOTE: tail points to last item inserted
+typedef struct{
+	Item_t *head, *tail;
+	int n;
+}Q_t;
+
+
+/*
+************************************************************
+***************FUNCTION PROTOTYPES**************************
+************************************************************/
+
+/*creates an empty Queue*/
+void InitQ(Q_t *pQ);
+
+//removes all items in Queue
+void FreeQ(Q_t *pQ);
+
+//Insert item into Q. If succesfull return 1
+int Enqueue(Item_t item, Q_t *pQ);
+
+//Remove item from Q. If succesfull return 1
+int Dequeue(Item_t *pItem, Q_t *pQ);
+
+//Displays all items in Queue
+void PrintQ(Q_t *pQ);
+
+//Return 1 if full
+int IsFull(Q_t *pQ);
+
+//Return 1 if empty 
+int IsEmpty(Q_t *pQ);
+
+#endif // !__QUEUE__
--- a/main.cpp	Wed Mar 01 06:57:51 2017 +0000
+++ b/main.cpp	Sat Mar 04 23:04:02 2017 +0000
@@ -1,4 +1,7 @@
 #include "mbed.h"
+#include "Queue.c"
+#include "Queue.h"
+//#include "Serial.h"
 #define KEY_WORD_LEN    5
 #define DATA_LEN        2
 
@@ -9,75 +12,92 @@
 DigitalOut  ledR(PTB22);
 DigitalOut  ledB(PTB21);
 
+typedef struct RawData{
+    char keyWord[KEY_WORD_LEN+1];
+    uint16_t rawNum[DATA_LEN];
+    uint16_t num;
+    }Data_t;
+
 void rxInterrupt();
+Data_t convertData(Item_t item);
 
 const char *KEY_WORD="BEGIN";
 
-char keyWord[KEY_WORD_LEN+1]={0};
-int flag=0;
-uint16_t xNum[DATA_LEN] = {0};
+Q_t Q;
+
+
+
 
+/*
+************************************************************
+*************************MAIN*******************************
+************************************************************/
+    
 int main()
 {
-    uint16_t data=0;
-    int i = 0;
-    int j=0;
-    pc.printf("Hello World!\n");
+    Data_t newData;
+    Item_t item;
+    
+    /*Initilize Program*/
+    InitQ(&Q);
+    pc.printf("Hello World!\n\r");
     bluetooth.attach(&rxInterrupt);
+    
     while (1) 
     {   
         ledR=1;
         ledB=0;
-        if(flag)
+        if(!IsEmpty(&Q))
         {   
-            //convert from char to uint16_t
-            //for(i=0,J=0;i<2;i++,j=j+2)
+            if(Dequeue(&item,&Q))    
             {
-                data=xNum[0]|(xNum[1]<<8);     
+                newData=convertData(item);
+        
+                //output results
+                pc.printf("CONVERTED DATA\n\r********************************\n\r");
+                pc.printf("string= %s\n\rDATA\t%d\t%d\tnum=%d\n\r\n\r"
+                    ,newData.keyWord,newData.rawNum[0],newData.rawNum[1],newData.num);
             }
-            
-            //output results
-            pc.printf("\n\rstring= %s\n\rDATA\t%d\t%d\tnum=%d\n\r"
-                ,keyWord,xNum[0],xNum[1],data);
-
-/*            for(i=0;i<2;i++)
-            {
-                pc.printf("\n\rstring= %s\n\rDATA\t%d\t%d\tnum=%d\n\r"
-                    ,keyWord[i],xNum[0+i*2],xNum[1+i*2],data[i]);
-            }
-            pc.printf("*******************************************\n\r\n\r");
-*/
-            flag=0;
         }
     }
 }
 void rxInterrupt()
 {
   int i;
+  Item_t item;
+  
   ledB=0;
   //ledR=1;
+  
+    item.next=NULL;
+    item.prev=NULL;
+    for(i=0;i<INPUT_LEN;i++)
+    {
+        item.data[i]=bluetooth.getc();
+    }
+    Enqueue(item,&Q);
+}
+    
+Data_t convertData(Item_t item)
+{
+    int i=0;
+    int j=0;
+    Data_t data;
+    
     //parse 1st keyword
     for(i=0;i<KEY_WORD_LEN;i++)
     {
-            keyWord[i]=bluetooth.getc();
+        data.keyWord[i]=item.data[i];
     }
     //convert to string
-    keyWord[i]='\0';  
+    data.keyWord[i]='\0';  
     
     //parse data
-    for(i=0;i<DATA_LEN;i++)
-    {
-        xNum[i]=bluetooth.getc();    
-    }
-    
-/*    //parse 2nd keyword
-    for(i=0;i<KEY_WORD_LEN;i++)
+    for(i=KEY_WORD_LEN,j=0;i< INPUT_LEN;i++,j++)
     {
-        keyWord[1][i]=bluetooth.getc();
+        data.rawNum[j]=item.data[i];    
     }
-    //convert to string
-    keyWord[1][i]='\0';  
-*/
-        
-    flag=1;
-}
\ No newline at end of file
+    data.num=data.rawNum[0]|(data.rawNum[1]<<8); 
+    
+    return data;
+}