Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

Revision:
5:38b853bb1afa
Parent:
4:745fbbd5e4e5
Child:
6:aaefa04f06be
--- a/main.cpp	Tue Aug 23 20:26:05 2011 +0000
+++ b/main.cpp	Fri Aug 26 20:35:11 2011 +0000
@@ -15,12 +15,13 @@
 #include "Status_Display.h"
 #include "Keyboard.h"
 #include "STANAG_Codes.h"
+#include "Host_Coms.h"
   
 // Debug stuff
 #define __DEBUG  
 #include "Dbg.h"
 
-#define __TESTCODE  
+//#define __TESTCODE  
 
 
 // Software version
@@ -29,14 +30,24 @@
 #define LF28A_SW_VERSION_MINOR       2
 #define LF28A_COPYRIGHT_DESCRIPTOR   "(C)TNO 2011"
 
+// Laser Time-out in Seconds
+//#define LF28A_RANGE_TIME_OUT_S           1      
+//#define LF28A_DESIG_TIME_OUT_S  (3*60 + 20)      
+//Test only:
+#define LF28A_RANGE_TIME_OUT_S           5      
+#define LF28A_DESIG_TIME_OUT_S          10      
+ 
 // mbed Interface Hardware definitions
 DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
 DigitalOut myled3(LED3);
 DigitalOut heartbeatLED(LED4);
 
+// Host PC Communication channels
 Serial pc(USBTX, USBRX);
+Host_Coms host_coms(pc);
 
+//I2C Bus
 I2C i2c(D_SDA, D_SCL);
 
 // CDA Interface Hardware definitions
@@ -63,7 +74,8 @@
 
 enum RangeSelect { RNG_F, RNG_L };
 
-enum MessageToHost { LFPON, LFRDY_0, LFRDY_1, LFSTA, LFIDL };
+enum MessageToHost { LFPON, LFRDY_0, LFRDY_1, LFSTA_D, LFSTA_R, LFSTA_C, LFSTA_A, LFSTA_F, LFSTA_P, LFIDL };
+//enum MessageToHost { LFPON, LFRDY_0, LFRDY_1, LFSTA, LFIDL };
 enum MessageFromHost { LFRES, LFRNG };
 
 //Typedef for Laser Range finder data
@@ -78,7 +90,7 @@
 Brightness brightness, graticule; 
 Key_Code keycode;
 Range_t range;
-bool laser;
+bool laser;                           // Laser On/Off
 bool hostResetCmd;
 MessageToHost messageToHost;
 STANAG_Codes STANAG_codes;
@@ -87,6 +99,13 @@
 Ticker heartbeat;
 bool heartbeatflag=false;
 
+// Laser Time-out
+Timer timer;
+
+// Cycle Timer
+Timer cycletimer;
+int cyclecount = 0;
+const int maxcount = 1000000;
 
 // Local functions
 void clear_screen() {
@@ -125,7 +144,38 @@
     myled1 = 1;
     DBG("Init Interfaces Done, Main, Step = %d\r", 10);    
 }
+      
+void BITE() {
+  int count;
+  
+    LF28A_status.set_brightness(BRT_MED);           
+    LF28A_status.NoGo(LED_ON);
+    
+    for (count=0; count<5; count++) {
+      LF28A_display.locate(0);
+      LF28A_display.printf("BITE  - ");    
+      LF28A_display.locate(0);    
+      LF28A_display.printf("BITE  \ ");    
+      LF28A_display.locate(0);        
+      LF28A_display.printf("BITE  | ");        
+      LF28A_display.locate(0);    
+      LF28A_display.printf("BITE  / ");    
+      LF28A_display.locate(0);    
+      LF28A_display.printf("BITE  - ");                
+    };
 
+    LF28A_display.locate(0);    
+    LF28A_display.printf("BITE  OK");    
+    LF28A_status.NoGo(LED_OFF);  
+    
+    wait(2.0);          
+    
+    LF28A_display.cls();  
+//    LF28A_status.set_brightness(D_STATUS_LED_BRIGHT_OFF); 
+
+    //Done, Tell me about it                        
+    DBG("BITE Done, Main, Step = %d\r", 30);    
+}
 
 void lamp_test() {
     LF28A_status.set_brightness(BRT_LOW);       
@@ -142,24 +192,9 @@
 //    LF28A_status.set_brightness(D_STATUS_LED_BRIGHT_OFF); 
 
     //Done, Tell me about it          
-    DBG("Lamp Test Done, Main, Step = %d\r", 20);    
+    //DBG("Lamp Test Done, Main, Step = %d\r", 20);    
 }
-      
-void BITE() {
-    LF28A_status.set_brightness(BRT_LOW);           
-    LF28A_status.NoGo(LED_OFF);
-    
-    LF28A_display.locate(0);
-    LF28A_display.printf("--------");
-    
-    wait(0.5);  
-    
-    LF28A_display.cls();  
-//    LF28A_status.set_brightness(D_STATUS_LED_BRIGHT_OFF); 
 
-    //Done, Tell me about it                        
-    DBG("BITE Done, Main, Step = %d\r", 30);    
-}
 
 void init_state() {
     // Default modes and settings after Power On or Host Reset
@@ -171,6 +206,8 @@
     range.select = RNG_F;
 
     laser = false;
+    timer.stop();
+    timer.reset();
 
 // Read Config File if needed..
 // Reload STANAG Codes ?
@@ -184,10 +221,10 @@
    
     // Init Alphanumeric Display
     LF28A_display.cls();                    // Clear display and Cursor home
-    LF28A_display.set_flash_mode(true);     // Enable flashing digits                                     
+    LF28A_display.set_flash_mode(false);    // Disable flashing digits                                     
     
     //Done, Tell me about it 
-    DBG("Init Done, Originator = %d\r", hostResetCmd);               
+    //DBG("Init Done, Originator = %d\r", hostResetCmd);               
 }
 
 
@@ -210,7 +247,7 @@
   } //end switch              
      
   //Done, Tell me about it 
-  DBG("Graticule Brightness Change, current val = %d\r", graticule);
+  //DBG("Grat Bright Chnge, val = %d\r", graticule);
 }
 
 void disp_bright_selector() {
@@ -237,7 +274,7 @@
   } //end switch              
         
   //Done, Tell me about it 
-  DBG("Display Brightness Change, current val = %d\r", brightness);
+  //DBG("Disp Bright Chng, val = %d\r", brightness);
 }
 
 void range_selector() {
@@ -255,7 +292,7 @@
   } //end switch              
      
   //Done, Tell me about it   
-  DBG("Range Change, current val = %d\r", range.select);
+  //DBG("Rng Chng, val = %d\r", range.select);
 }
 
 // Heartbeat monitor
@@ -273,6 +310,56 @@
   heartbeatflag = false;
 }
 
+// Construct and Send messages to Host PC. Need to complete this !!
+void SendHostMessage(MessageToHost messageToHost) {
+  int result;
+
+  switch (messageToHost) {
+    case LFPON:    host_coms.sendMessage("$LFPON");
+                   break;
+                   
+    case LFRDY_0:  // No hostResetCmd
+                   host_coms.sendMessage("$LFRDY,0");
+                   //pc.printf("$LFRDY,0*00\r\n");
+                   break;
+                   
+    case LFRDY_1:  // hostResetCmd
+                   host_coms.sendMessage("$LFRDY,1");
+//                   pc.printf("$LFRDY,1*00\r\n");
+                   break;
+
+    case LFSTA_D:  // Designate
+                   host_coms.sendMessage("$LFSTA,D,%1d,%04d,%1d", laser, STANAG_codes.getCode(), graticule);    
+//                   pc.printf("$LFSTA,D,0,1234,1*00\r\n");
+                   break;
+
+    case LFSTA_R:  // Range
+                   host_coms.sendMessage("$LFSTA,R,%1d,%04d,%1d", laser, STANAG_codes.getCode(), graticule);        
+//                   pc.printf("$LFSTA,D,0,1234,1*00\r\n");
+                   break;
+
+    case LFSTA_C:  // Code
+                   host_coms.sendMessage("$LFSTA,C,%1d,%04d,%1d", laser, STANAG_codes.getCode(), graticule);    
+//                   pc.printf("$LFSTA,D,0,1234,1*00\r\n");
+                   break;
+
+    case LFSTA_A:
+    case LFSTA_F:
+    case LFSTA_P:        
+    case LFIDL:    // No Message
+                   break;
+
+    default:     // Oops, we should never end up here....
+
+                   result = -1;
+                   break;                 
+  } //end switch              
+     
+  //Done, Tell me about it  
+  //DBG("To Host Msg, type = %d\r", messageToHost);
+}
+
+
 //------------------------------------------------------------//
 // Testing Stuff                                              //   
 //------------------------------------------------------------//
@@ -284,39 +371,6 @@
 //------------------------------------------------------------//
 
 
-// Construct and Send messages to Host PC. Need to complete this !!
-void SendHostMessage(MessageToHost messageToHost) {
-  int result;
-
-  switch (messageToHost) {
-    case LFPON   : pc.printf("$LFPON*00\r\n");
-                   break;
-                   
-    case LFRDY_0 : // No hostResetCmd
-                   pc.printf("$LFRDY,0*00\r\n");
-                   break;
-                   
-    case LFRDY_1 : // hostResetCmd
-                   pc.printf("$LFRDY,1*00\r\n");
-                   break;
-
-    case LFSTA   : pc.printf("$LFSTA,D,0,1234,1*00\r\n");
-                   break;
-
-    case LFIDL   : // No Message
-                   break;
-
-    default:     // Oops, we should never end up here....
-
-                   result = -1;
-                   break;                 
-  } //end switch              
-     
-  //Done, Tell me about it  
-  DBG("Message, current val = %d\r", messageToHost);
-}
-
-
 int main() {
     int address, result;
    
@@ -325,7 +379,7 @@
     heartbeat_start();
           
     clear_screen(); 
-    version_string();
+    version_string();    
         
 #ifndef __TESTCODE
     lamp_test();
@@ -338,6 +392,10 @@
     mode = INIT_ENTRY;    // Start with Init
     hostResetCmd = false; // Start with regular PowerOn reset   
 
+//Test cycletime
+    cycletimer.start();      
+    cycletimer.reset();
+          
     // Main Controlloop:
     //   Check keyboard input and respond as required in the current device mode
     //   Check Host commands and respond as required
@@ -355,14 +413,14 @@
                             init_state(); 
 
                            // Inform Host that Init has completed
-                            if (!hostResetCmd) {
-                              // Regular PowerOn Reset                                                        
-                              SendHostMessage(LFRDY_0);
+                            if (hostResetCmd) {
+                              // Host initiated a Reset
+                              SendHostMessage(LFRDY_1);
+                              hostResetCmd = false;                            
                             }
                             else {
-                              // Host initiated a Reset
-                              SendHostMessage(LFRDY_1);
-                              hostResetCmd = false;
+                              // Regular PowerOn Reset                                                        
+                              SendHostMessage(LFRDY_0);                            
                             }
                                                           
                             mode = INIT_EXIT;                            
@@ -383,7 +441,7 @@
                             LF28A_display.printf ("    %04d",STANAG_codes.getCode() );                      
                           
                             // Inform Host of change
-                            SendHostMessage(LFSTA);  
+                            SendHostMessage(LFSTA_D);  
                           
                             mode = DESIG_DISP;
                             break;
@@ -396,25 +454,77 @@
                               switch (keycode) {
                                 case KEY_GRAT_RT: grat_bright_selector();
                                                   // Inform Host of change
-                                                  SendHostMessage(LFSTA);  
+                                                  SendHostMessage(LFSTA_D);  
                                                   break;
 
                                 case KEY_BRIGHT:  disp_bright_selector();
                                                   break;                              
 
                                 case KEY_MODE:    mode = DESIG_EXIT;
+                                                  break;
+                                                  
+                                case KEY_FIRE:    // Check and handle Fire key
+                                                  
+                                                  // Toggle Laser On/Off
+                                                  if (laser) {
+                                                    // Laser is On, Switch Off
+                                                    laser=false;
+                                                    timer.stop();  // Stop time-out timer
+                                                    timer.reset();
+                                
+                                                    // Status LEDs
+                                                    LF28A_status.LED(LED_LASER, LED_OFF);                                                    
+                                                  }
+                                                  else {
+                                                    // Laser is Off, Switch On
+                                                    laser=true;
+                                                    timer.start(); // Start time-out timer
+
+                                                    // Status LEDs
+                                                    LF28A_status.LED(LED_LASER, LED_ON);                                                                                                         
+                                                  };
+                                                  
+                                                  // Inform Host of change (laser on/off)
+                                                  SendHostMessage(LFSTA_D);  
+                                
                                                   break;                              
+                                                                                
 
                                 default:          // Ignore other keys
                                                   break;                                                            
                               }; //End Keyswitch            
                             }; // End Keyread
                               
-                            // Check and handle Fire key
+                            // Check Automatic Time-out for Laser
+                            if (laser) {
+                              // Laser is On, Test Time-out to Switch Off
+                              if (timer.read() > LF28A_DESIG_TIME_OUT_S) {
+                                laser=false;   // Laser Off
+                                timer.stop();  // Stop time-out timer
+                                timer.reset();
 
+                                // Status LEDs
+                                LF28A_status.LED(LED_LASER, LED_OFF); 
+
+                                // Inform Host of change (laser off)
+                                SendHostMessage(LFSTA_D);
+                              }                                                                                     
+                            };
+                                                       
                             break;                              
 
           case DESIG_EXIT:  // Transitional state
+
+                            // Check Laser
+                            if (laser) {
+                              // Laser is On, Switch Off
+                              laser=false;   // Laser Off
+                              timer.stop();  // Stop time-out timer
+                              timer.reset();
+                                
+                              // Status LEDs
+                              LF28A_status.LED(LED_LASER, LED_OFF); 
+                            };
           
                             // Status LEDs
                             LF28A_status.LED(LED_DESIG, LED_OFF);        
@@ -433,7 +543,7 @@
                             LF28A_display.printf ("    %04d",STANAG_codes.getCode() );                      
                             
                             // Inform Host of change
-                            SendHostMessage(LFSTA);  
+                            SendHostMessage(LFSTA_R);  
                            
                             mode = RANGE_DISP;
                             break;
@@ -447,7 +557,7 @@
                                 case KEY_GRAT_RT: grat_bright_selector();
 
                                                   // Inform Host of change
-                                                  SendHostMessage(LFSTA);  
+                                                  SendHostMessage(LFSTA_R);  
 
                                                   break;
 
@@ -467,7 +577,7 @@
                                                   }
                                                   else {
                                                     LF28A_display.locate (0);                      
-                                                    LF28A_display.printf ("F   %04d", range.last);                                                     
+                                                    LF28A_display.printf ("L   %04d", range.last);                                                     
                                                   };
                                                                                                  
                                                   // Inform Host of change ??
@@ -475,29 +585,29 @@
                                                   
                                                   break;
 
-
                                 case KEY_FIRE:    // Check and handle Fire key
                                                   
                                                   // Toggle Laser On/Off
                                                   if (laser) {
                                                     // Laser is On, Switch Off
+                                                    laser=false;
+                                                    timer.stop();  // Stop time-out timer
+                                                    timer.reset();                                                    
 
                                                     // Status LEDs
-                                                    LF28A_status.LED(LED_LASER, LED_ON);
-                                                    
+                                                    LF28A_status.LED(LED_LASER, LED_OFF);                                                    
                                                   }
                                                   else {
-                                                    // Laser is Off, Switch On                                                  
+                                                    // Laser is Off, Switch On
+                                                    laser=true;
+                                                    timer.start(); // Start time-out timer
 
                                                     // Status LEDs
-                                                    LF28A_status.LED(LED_LASER, LED_ON);
-                                                     
-                                                    // Automatic Time-out needed
-                                                     
+                                                    LF28A_status.LED(LED_LASER, LED_ON);                                                                                                         
                                                   };
                                                   
                                                   // Inform Host of change (laser on/off)
-                                                  SendHostMessage(LFSTA);  
+                                                  SendHostMessage(LFSTA_R);  
                                 
                                                   break;                              
   
@@ -506,26 +616,37 @@
                               }; //End Keyswitch            
                             }; // End Keyread          
 
-#if (0)
-                            // tbd Check Automatic Time-out for Laser
-                            Timer.stop()
-                            laser = false;
-                            // Status LEDs
-                            LF28A_status.LED(LED_LASER, LED_ON);
-#endif
+                            // Check Automatic Time-out for Laser
+                            if (laser) {
+                              // Laser is On, Test Time-out to Switch Off
+                              if (timer.read() > LF28A_RANGE_TIME_OUT_S) {
+                                laser=false;   // Laser Off
+                                timer.stop();  // Stop time-out timer
+                                timer.reset();                                
+
+                                // Status LEDs
+                                LF28A_status.LED(LED_LASER, LED_OFF); 
+
+                                // Inform Host of change (laser off)
+                                SendHostMessage(LFSTA_R);
+                              }                                                                                     
+                            };
 
                             break;                              
 
           case RANGE_EXIT:  // Transitional state
 
-#if (0)
-                            // tbd Check Automatic Time-out for Laser
-                            Timer.stop()
-                            laser = false;
-                            // Status LEDs
-                            LF28A_status.LED(LED_LASER, LED_ON);
-#endif
-          
+                            // Check Laser
+                            if (laser) {
+                              // Laser is On, Switch Off
+                              laser=false;   // Laser Off
+                              timer.stop();  // Stop time-out timer
+                              timer.reset();                              
+
+                              // Status LEDs
+                              LF28A_status.LED(LED_LASER, LED_OFF); 
+                            };
+        
                             // Status LEDs
                             LF28A_status.LED(LED_RANGE, LED_OFF);
 
@@ -547,7 +668,7 @@
                             LF28A_display.printf ("    %04d",STANAG_codes.getCode() );                      
  
                             // Inform Host of change
-                            SendHostMessage(LFSTA);  
+                            SendHostMessage(LFSTA_C);  
                             
                             mode = CODE_DISP;
                             break;
@@ -561,7 +682,7 @@
                                 case KEY_GRAT_RT: grat_bright_selector();
 
                                                   // Inform Host of change
-                                                  SendHostMessage(LFSTA);  
+                                                  SendHostMessage(LFSTA_C);  
                                 
                                                   break;
 
@@ -626,7 +747,7 @@
                                                   LF28A_display.set_char_flash_state(false, 4 + STANAG_codes.getDigitIdx());                          
                                 
                                                   // Inform Host of change
-                                                  SendHostMessage(LFSTA);  
+                                                  SendHostMessage(LFSTA_C);  
                                                   
                                                   mode = CODE_EXIT;                                                                                  
                                                   
@@ -638,14 +759,14 @@
                                                   LF28A_display.set_char_flash_state(false, 4 + STANAG_codes.getDigitIdx());                          
                                  
                                                   // Inform Host of change
-                                                  SendHostMessage(LFSTA);  
+                                                  SendHostMessage(LFSTA_C);  
                                 
                                                   mode = CODE_DISP;
                                                   
                                                   break;                              
 
                                 case KEY_F_L_UP : //Incr current digit
-                                                  STANAG_codes.incDigitIdx();
+                                                  STANAG_codes.incDigit();
 
                                                   // Display Current STANAG Code
                                                   LF28A_display.locate (0);                      
@@ -701,8 +822,16 @@
         //  Reset command 
         //  Range data         
 
-// Just for Info, lets see how fast this cycle is...
-//
+#if(0)
+        // Just for Info, lets see how fast this cycle is...
+        cyclecount++;
+        if (cyclecount == maxcount) {
+          pc.printf("Freq = %d Hz\r", (cyclecount * 1000) / cycletimer.read_ms());
+          cyclecount = 0;          
+          cycletimer.reset();      
+        }
+#endif
+      
     } // end while(1)