Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Sun Aug 21 19:42:48 2011 +0000
Parent:
2:1dab1089c332
Child:
4:745fbbd5e4e5
Commit message:
Test version for breadboard

Changed in this revision

HDSP253X_Display.cpp Show annotated file Show diff for this revision Revisions of this file
HDSP253X_Display.h Show annotated file Show diff for this revision Revisions of this file
Testloop.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
--- a/HDSP253X_Display.cpp	Sat Aug 20 12:49:44 2011 +0000
+++ b/HDSP253X_Display.cpp	Sun Aug 21 19:42:48 2011 +0000
@@ -127,7 +127,8 @@
 
     // Write data to the databus
     _databus.write(data);        
- 
+    wait_ms(HDSP253X_1TCY_WAIT_MS);
+     
     // Set WR low, wait, then set high and wait
     _controlbus.WR(LOW);
     wait_ms(HDSP253X_1TCY_WAIT_MS);
@@ -216,7 +217,6 @@
 /*****************************************************************************/
 
 #if(0)
-
 /*---------------------------------------------------------------------------*\
  |
  |  Function:       HDSP253X_display_ascii_char
@@ -293,8 +293,8 @@
     string_len = strlen(disp_string);   // obtain length of string
     for (i = start_pos; (i < HDSP253X_NUM_CHARS) && ((i - start_pos) < string_len) && (i <= end_pos); i++)
     {
-    // write selected character to display at selected position
-    _write(HDSP253X_ADDR_CHAR_BASE + i, disp_string[i - start_pos]);
+      // write selected character to display at selected position
+      _write(HDSP253X_ADDR_CHAR_BASE + i, disp_string[i - start_pos]);
     }
 }
 
@@ -310,7 +310,7 @@
  |            must NOT be used with read-only strings in program memory.
  |
  |  Parameters:     disp_string - fixed character string to display
- |            start_pos - starting position on the display (0 to 7)
+ |                  start_pos - starting position on the display (0 to 7)
  |
  |  Returns:        Nothing
  |
@@ -324,22 +324,19 @@
 
     // loop round, writing characters or spaces to whole display from start
     string_len = strlen(disp_string);   // obtain length of string
-    for (i = 0; i < HDSP253X_NUM_CHARS; i++)
-    {
-    // work out whether to display a character or space
-    if ((i < start_pos) || ((i - start_pos) >= string_len))
-    {
+    for (i = 0; i < HDSP253X_NUM_CHARS; i++) {
+      // work out whether to display a character or space
+      if ((i < start_pos) || ((i - start_pos) >= string_len)) {
         // out of string range, so select space character
         char_value = ' ';
-    }
-    else
-    {
+      }
+      else {
         // within string, so select string character
         char_value = disp_string[i - start_pos];
-    }
+      }
 
-    // write selected character to display at selected position
-    _write(HDSP253X_ADDR_CHAR_BASE + i, char_value);
+      // write selected character to display at selected position
+      _write(HDSP253X_ADDR_CHAR_BASE + i, char_value);
     }
 }
 
@@ -359,15 +356,23 @@
 \*---------------------------------------------------------------------------*/
 
 void HDSP253X_Display::printf (char * format, ...) {
-   char buffer[40];
+   char display_string[64];
+   int rv, string_len, i;   
    va_list args;
    va_start (args, format);
-   int rv=vsprintf (buffer, format, args);
-//    ::printf("printing:'%s'\n", buffer);
+
+   rv=vsprintf (display_string, format, args);
+   va_end (args);   
+// printf("printing:'%s'\n", display_string);
 //   writeString (buffer);
-   va_end (args);
 
-// return rv;
+   // loop round, writing characters
+   string_len = strlen(display_string);   // obtain length of string
+   for (i = 0; i < string_len; i++) {
+     putc(display_string[i]);
+   };
+
+   // return rv;
 }
 
 
@@ -387,13 +392,12 @@
  |  Returns:        Nothing
  |
 \*---------------------------------------------------------------------------*/
+void HDSP253X_Display::putc(char disp_char) {
 
-void HDSP253X_Display::putc(char disp_char)
-{
     // Write selected character to display at current position
    
     if ((disp_char & HDSP253X_UDC_SEL) == HDSP253X_UDC_SEL) {      
-        // Write UDC character to display, code between 128-142
+        // Write UDC character to display, code between 128-143
         disp_char &= HDSP253X_UDC_MASK;        // mask off unused bits
         disp_char |= HDSP253X_UDC_SEL;         // add in top bit to specify UDC        
         _write(HDSP253X_ADDR_CHAR_BASE + _column, disp_char);
@@ -405,33 +409,31 @@
     }
     
     // Incr and wrap around cursorposition
-    _column = (_column++) % HDSP253X_NUM_CHARS;    
+    _column++; 
+    _column = _column % HDSP253X_NUM_CHARS;    
 }
 
 
 #if(0)
-     if (disp_char == '\n') {
-       _column = 0;
-       _row = (_row++) % HDSP253X_NUM_LINES;
-     }
-     else {
-       _putc(_column, _row, disp_char);
-       _column = (_column++) % HDSP253X_NUM_CHARS;    
-       if (_column == 0) {
-         _row = (_row++) % HDSP253X_NUM_LINES;
-       }
+//Testversion
+void HDSP253X_Display::putc(char disp_char) {
 
-
-char HDSP253X_Display::getc() {
-  return -1;
+    // Write ASCII character, code between 0-127
+    _write(HDSP253X_ADDR_CHAR_BASE + _column, disp_char);        
+   
+    // Incr and wrap around cursorposition
+    _column++;
+    if (_column==HDSP253X_NUM_CHARS) _column=0;        
 }
-
 #endif
 
 
 
-
-
+#if(0)
+char HDSP253X_Display::getc() {
+  return -1;
+}
+#endif
 
 /*---------------------------------------------------------------------------*\
  |
@@ -445,8 +447,8 @@
  |
 \*---------------------------------------------------------------------------*/
 
-void HDSP253X_Display::locate(uint8_t column)
-{
+void HDSP253X_Display::locate(uint8_t column) {
+
 //    _row = row % HDSP253X_NUM_ROWS;
     _column = column % HDSP253X_NUM_CHARS;
 }
@@ -467,8 +469,8 @@
  |
 \*---------------------------------------------------------------------------*/
 
-void HDSP253X_Display::cls(void)
-{
+void HDSP253X_Display::cls(void) {
+
     uint8_t disp_data;
 
     // Read in control word, modify and write back out
@@ -497,8 +499,8 @@
  |
 \*---------------------------------------------------------------------------*/
 
-void HDSP253X_Display::set_brightness(uint8_t brightness)
-{
+void HDSP253X_Display::set_brightness(uint8_t brightness) {
+
     uint8_t ctrl_data;
 
     // Read in control word, modify and write back out
@@ -536,8 +538,8 @@
  |
 \*---------------------------------------------------------------------------*/
 
-void HDSP253X_Display::start_self_test(void)
-{
+void HDSP253X_Display::start_self_test(void) {
+
     // Reset the display to ensure it is ready for the self test
     reset();
 
@@ -564,8 +566,7 @@
  |
 \*---------------------------------------------------------------------------*/
 
-bool HDSP253X_Display::finish_self_test(void)
-{
+bool HDSP253X_Display::finish_self_test(void) {
     uint8_t ctrl_data;
     bool result;
     
@@ -597,18 +598,15 @@
  |
 \*---------------------------------------------------------------------------*/
 
-void HDSP253X_Display::set_blink_mode(bool enable)
-{
+void HDSP253X_Display::set_blink_mode(bool enable) {
     uint8_t ctrl_data;
 
     // read in control word, modify and write back out
     ctrl_data = _read(HDSP253X_ADDR_CTRL_WORD);
-    if (enable)
-    {
+    if (enable) {
       ctrl_data |= HDSP253X_CTRL_BLINK_MASK;
     }
-    else
-    {
+    else {
       ctrl_data &= ~HDSP253X_CTRL_BLINK_MASK;
     }
     _write(HDSP253X_ADDR_CTRL_WORD, ctrl_data);
@@ -628,18 +626,15 @@
  |
 \*---------------------------------------------------------------------------*/
 
-void HDSP253X_Display::set_flash_mode(bool enable)
-{
+void HDSP253X_Display::set_flash_mode(bool enable) {
     uint8_t ctrl_data;
 
     // read in control word, modify and write back out
     ctrl_data = _read(HDSP253X_ADDR_CTRL_WORD);
-    if (enable)
-    {
+    if (enable) {
       ctrl_data |= HDSP253X_CTRL_FLASH_MASK;
     }
-    else
-    {
+    else {
       ctrl_data &= ~HDSP253X_CTRL_FLASH_MASK;
     }
     _write(HDSP253X_ADDR_CTRL_WORD, ctrl_data);
@@ -696,8 +691,7 @@
  |
 \*---------------------------------------------------------------------------*/
 
-void HDSP253X_Display::set_char_flash_state(bool flash_state, uint8_t char_pos)
-{
+void HDSP253X_Display::set_char_flash_state(bool flash_state, uint8_t char_pos) {
     // Write out the new flash state to the flash RAM
     _write(HDSP253X_ADDR_FLASH_BASE + char_pos, flash_state);
 }
--- a/HDSP253X_Display.h	Sat Aug 20 12:49:44 2011 +0000
+++ b/HDSP253X_Display.h	Sun Aug 21 19:42:48 2011 +0000
@@ -120,6 +120,7 @@
 // Specific delays for display operation, assuming internal clocking
 // This may need to be lengthened if a significantly slower external clock is used
 #define HDSP253X_1TCY_WAIT_MS       1
+//#define HDSP253X_1TCY_WAIT_MS       10
 #define HDSP253X_RST_CLR_DELAY_MS   2       // delay AFTER issuing reset or clear
 #define HDSP253X_SELFTEST_WAIT_MS   6000    // duration of self test operation
 
--- a/Testloop.h	Sat Aug 20 12:49:44 2011 +0000
+++ b/Testloop.h	Sun Aug 21 19:42:48 2011 +0000
@@ -42,6 +42,7 @@
     pc.printf("B: Test I2C Enablebus\r");        
     pc.printf("C: Test Status LEDs\r");            
     pc.printf("D: Test Brightness LEDs\r");                
+    pc.printf("E: Test Display\r");                    
     pc.printf("\r");                
 }
 
@@ -54,7 +55,7 @@
     pc.printf("Start Test!\r");
     show_LEDS();
     show_menu();
-    myled4=1;
+
     while(running) {
     
        if(pc.readable()) {
@@ -81,7 +82,7 @@
                        heartbeat_stop();
                      }
                      else {
-                       heartbeat.start();
+                       heartbeat_start();
                      };            
                      break;
 
@@ -327,7 +328,66 @@
                       command = pc.getc(); 
                       pc.printf("..Done\r");                      
                       
-                      break;   
+                      break;  
+                      
+          case 'E' :
+                      pc.printf("Test Display\r"); 
+                      pc.printf("Press any key to continue...");
+
+                      LF28A_display.cls();                      
+                      LF28A_display.putc('H');
+                      LF28A_display.putc('i');
+                      LF28A_display.putc('-');
+                      LF28A_display.putc('W');
+                      LF28A_display.putc('i');
+                      LF28A_display.putc('m');                      
+                      LF28A_display.putc('-');                      
+
+                      while(! pc.readable()) {
+                      }                      
+                      command = pc.getc(); 
+                      
+                      pc.printf("Test printf\r\n");                      
+                      
+                      LF28A_display.locate (0);                      
+                      LF28A_display.printf ("%s=%d", "Rng", 2);                      
+                      
+                      while(! pc.readable()) {
+                      }                      
+                      command = pc.getc(); 
+
+                      LF28A_display.locate (4);                      
+                      LF28A_display.printf ("    ");                      
+
+                      LF28A_display.locate (4);                      
+                      LF28A_display.printf ("%d", 123);                      
+                      
+                      while(! pc.readable()) {
+                      }                      
+                      command = pc.getc(); 
+                      
+                      LF28A_display.cls();                      
+                      LF28A_display.printf ("%s", "Hello...");                      
+
+                      while(! pc.readable()) {
+                      }                      
+                      command = pc.getc(); 
+                                            
+                      pc.printf("Test Flash\r\n");     
+                      LF28A_display.set_flash_mode(true);                                      
+                      for (int i=0; i<8; i++) {
+                        LF28A_display.set_char_flash_state(true, i);                      
+                        while(! pc.readable()) {
+                        }                      
+                        command = pc.getc(); 
+                        LF28A_display.set_char_flash_state(false, i);                                              
+                      }
+
+                      LF28A_display.set_flash_mode(false);
+                                            
+                      pc.printf("..Done\r");                      
+                      
+                      break;                                           
                               
         } //switch
       }//if
--- a/main.cpp	Sat Aug 20 12:49:44 2011 +0000
+++ b/main.cpp	Sun Aug 21 19:42:48 2011 +0000
@@ -63,7 +63,7 @@
 
 enum RangeSelect { RNG_F, RNG_L };
 
-enum MessageToHost { LFPON, LFRDY, LFSTA };
+enum MessageToHost { LFPON, LFRDY_0, LFRDY_1, LFSTA, LFIDL };
 enum MessageFromHost { LFRES, LFRNG };
 
 //Typedef for Laser Range finder data
@@ -180,7 +180,8 @@
     LF28A_status.set_brightness(brightness);          
    
     // Init Alphanumeric Display
-    LF28A_display.cls(); 
+    LF28A_display.cls();                    // Clear display and Cursor home
+    LF28A_display.set_flash_mode(true);     // Enable flashing digits                                     
     
     //Done, Tell me about it 
     DBG("Init Done, Originator = %d\r", hostResetCmd);               
@@ -288,18 +289,20 @@
     case LFPON   : pc.printf("$LFPON*00\r\n");
                    break;
                    
-    case LFRDY   : if (!hostResetCmd) {
-                     pc.printf("$LFRDY,0*00\r\n");
-                   }
-                   else {
-                     pc.printf("$LFRDY,1*00\r\n");
-                     hostResetCmd = false;
-                   }       
+    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;
@@ -321,6 +324,7 @@
     clear_screen(); 
     version_string();
         
+#ifndef __TESTCODE
     lamp_test();
     
     BITE();
@@ -330,9 +334,7 @@
     // Prepare main State Machine 
     mode = INIT_ENTRY;    // Start with Init
     hostResetCmd = false; // Start with regular PowerOn reset   
-    
-    
-#ifndef __TESTCODE
+
     // Main Controlloop:
     //   Check keyboard input and respond as required in the current device mode
     //   Check Host commands and respond as required
@@ -350,8 +352,16 @@
                             init_state(); 
 
                            // Inform Host that Init has completed
-                            SendHostMessage(LFRDY);  
-                            
+                            if (!hostResetCmd) {
+                              // Regular PowerOn Reset                                                        
+                              SendHostMessage(LFRDY_0)
+                            }
+                            else {
+                              // Host initiated a Reset
+                              SendHostMessage(LFRDY_1);
+                              hostResetCmd = false;
+                            }
+                                                          
                             mode = INIT_EXIT;                            
                             break;                            
         
@@ -362,7 +372,11 @@
           // DESIG MODEs - Laser Designator
           case DESIG_ENTRY:  // Transitional state
 
+                            // Status LEDs
+                            LF28A_status.LED(LED_DESIG, LED_ON);
+
                             //Display current STANAG Code
+                            LF28A_display.printf("Designat");
                           
                             // Inform Host of change
                             SendHostMessage(LFSTA);  
@@ -398,13 +412,20 @@
 
           case DESIG_EXIT:  // Transitional state
           
+                            // Status LEDs
+                            LF28A_status.LED(LED_DESIG, LED_OFF);        
+          
                             mode = RANGE_ENTRY;
                             break;                              
 
           // RANGE MODEs
           case RANGE_ENTRY: // Transitional state
           
+                            // Status LEDs
+                            LF28A_status.LED(LED_RANGE, LED_ON);
+                  
                             //Display current STANAG Code
+                            LF28A_display.printf("Range   ");                            
                             
                             // Inform Host of change
                             SendHostMessage(LFSTA);  
@@ -443,6 +464,10 @@
                             break;                              
 
           case RANGE_EXIT:  // Transitional state
+          
+                            // Status LEDs
+                            LF28A_status.LED(LED_RANGE, LED_OFF);
+          
 
                             mode = CODE_ENTRY;
                             break;                              
@@ -450,9 +475,13 @@
 
           // CODE MODEs
           case CODE_ENTRY:  // Transitional state
-          
+
+                            // Status LEDs
+                            LF28A_status.LED(LED_CODE, LED_ON);          
+                    
                             //Display current STANAG Code
-                            
+                            LF28A_display.printf("Code    "); 
+                                                        
                             // Inform Host of change
                             SendHostMessage(LFSTA);  
                             
@@ -529,7 +558,7 @@
                                 case KEY_F_L_UP : //Incr current digit
                                                   STANAG_code.incDigitIdx();
                                 
-                                                  // Inform Host of change ro wait untip done editing ??
+                                                  // Inform Host of change or wait until done editing ??
                                                   //SendHostMessage(LFSTA);  
                                                                                   
                                                   break;                              
@@ -542,7 +571,10 @@
                             break;                              
 
           case CODE_EXIT:  // Transitional state
-
+          
+                            // Status LEDs
+                            LF28A_status.LED(LED_CODE, LED_OFF);          
+          
                             mode = DESIG_ENTRY;
                             break;