Dependencies:   mbed

Fork of Serial_HelloWorld_Mbed by mbed official

Files at this revision

API Documentation at this revision

Comitter:
YuliangHao
Date:
Wed Nov 08 21:44:23 2017 +0000
Parent:
0:879aa9d0247b
Commit message:
Lab 6 Fortune Teller Sample Code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 879aa9d0247b -r aeff3dd6345d main.cpp
--- a/main.cpp	Tue Feb 12 17:39:05 2013 +0000
+++ b/main.cpp	Wed Nov 08 21:44:23 2017 +0000
@@ -1,10 +1,57 @@
 #include "mbed.h"
  
 Serial pc(USBTX, USBRX); // tx, rx
- 
+Serial mb(p9,p10);       //Serial Port on mbed
+char receive[200]="";    //Receive char array
+char pcRcv =0;      //Letter received from PC
+char mbRcv =0;      //Letter received from the mbed port
+
+ //Fortune string array    
+char *fortuneString[10] =  {  
+      "Today is your luck day. You will meet your lover.",
+      "You are cursed today.  Stay home."
+      //etc
+     };
+
 int main() {
-    pc.printf("Hello World!\n");
-    while(1) {
-        pc.putc(pc.getc() + 1);
+    
+    //Show prompt message to user    
+    pc.printf("Please input your fortune number:");       
+    
+    while(1)     
+    {           
+        if(pc.readable() )  //If there is letter on PC port
+        {
+            pcRcv=pc.getc();  //Read a char from the PC 
+            pc.printf("%c\n",pcRcv); //Bounce back user input to Tera Term
+        
+            if (pcRcv >= '0' && pcRcv <='9') //If you received a number, ignore any other letter
+            {
+                //Send the number to another side
+                mb.putc(pcRcv); 
+                
+                //Read the fortune string endded with a new line "\n"
+                mb.scanf("%[^\n]",receive);
+                pc.printf("%s\n\n,receive);  //Show the string to the Tera Term
+                
+                pc.printf("Please input your fortune number:");
+            }                
+            
+        }      
+          
+        if(mb.readable())  //If there is letter from another mbed
+        {   
+            mbRcv=mb.getc();  //Read a char from the mbed
+            pc.printf("\nmbed received letter: %c\n",mbRcv); //Debug message. Show char received from another mbed to Tera Term
+        
+            if (mbRcv >= '0' && mbRcv <='9') //If you received a number
+            {
+                mb.printf("%s\n", fortuneString[mbRcv-0x30]);  //Send the fortune string to another mbed                
+                pc.printf("Sent to another mbed %s\n", fortuneString[mbRcv-0x30]);  //Debug Message: Show what is sent to another mbed
+            }    
+            
+            pc.printf("Please input your fortune number:");                 
+        }     
+               
     }
 }
\ No newline at end of file