SMS message display on LED Matrix board with printer option

Dependencies:   AdafruitThermalPrinter HT1632_LedMatrix VodafoneUSBModem mbed-rtos mbed

Revision:
3:59038ad536ac
Parent:
2:787cbb491f8f
Child:
4:a364da55b42e
--- a/main.cpp	Mon Feb 25 22:22:20 2013 +0000
+++ b/main.cpp	Tue Feb 26 16:53:02 2013 +0000
@@ -60,9 +60,8 @@
 #define DEBUG 1
 
 #ifdef USE_LED
-// Cound try to get own number usine USSD request
+// Default scrolling message includes own number obtained using USSD request
 #define INFO_MSG "       Send a message to "
-//#define INFO_MSG "       Send a message to 07765946942        "
 #endif
 
 // Vodafone USSD commands
@@ -70,7 +69,6 @@
 #define USSD_COMMAND_BALANCE "*#134#"
 #define USSD_COMMAND_TIME "*#103#"
 
-
 #ifdef DEBUG
 Serial debug_pc(USBTX, USBRX); // tx, rx
 #endif
@@ -101,6 +99,7 @@
 static char msgBuf[MAX_NUM_MSGS][MAX_MSG_LENGTH];
 static int currentMsg = 0;
 static int numberOfMsgs = 0;
+static boolean getNextMessage = true;
 
 #ifdef USE_LED
 static char ownNumber[20];
@@ -243,7 +242,6 @@
     }
 }
 
-#ifdef USE_LED
 void getOwnNumber(VodafoneUSBModem *_modem, char *numPtr  )
 {
     int retryCount = 3;
@@ -262,7 +260,7 @@
             retryCount--;
     }
 }
-#endif
+
 
 char linkStateStr[6][13] = { "Unknown     ", "Registering ", "Denied      ", "No Signal   ", "Home Network", "Roaming     " };
 char bearerStr[6][5] = {"Unkn", "GSM ", "EDGE", "3G  ", "HSPA", "LTE " };
@@ -271,36 +269,40 @@
 {
     VodafoneUSBModem modem;
     time_t seconds = time(NULL);
+    int pRssi = 0;
+    LinkMonitor::REGISTRATION_STATE pRegistrationState;
+    LinkMonitor::BEARER pBearer;
 
     threadRestartCount++;
 
-    int pRssi = 0;
-//    addNewMessage( "Starting" );
-
-    LinkMonitor::REGISTRATION_STATE pRegistrationState;
-    LinkMonitor::BEARER pBearer;
-
+    // Getting the link state seems to speed up startup of the library
     modem.getLinkState( &pRssi,&pRegistrationState, &pBearer);
 #ifdef DEBUG
     debug_pc.printf("Link state Rssi: %d, Registration state %x Bearer %x\n",pRssi,pRegistrationState, pBearer);
 #endif
     sprintf(newMsgBuf, "Link State: %s on %s          ", linkStateStr[pRegistrationState], bearerStr[pBearer] );
+
+#ifdef USE_LED
     addNewMessage( newMsgBuf );
+#endif
+#ifdef USE_PRINTER
+    timestampMessage( newMsgBuf );
+#endif
 
     Thread::wait(3000);
 
+    // Get own number from the  network
+    getOwnNumber( &modem, ownNumber );
 #ifdef USE_LED
-    // Get own number fromt he  network
-    getOwnNumber( &modem, ownNumber );
+    sprintf(newMsgBuf, "Own number %s", ownNumber );
+    timestampMessage( newMsgBuf );
 #endif
 
-//    sprintf(msgBuf[&msgBuf[currentMsg][0], "Link state Rssi: %d, Registration state %x Bearer %x      ",pRssi,pRegistrationState, pBearer);
 #ifdef USE_PRINTER
     // Check if time already set, if not then get it. Use year = 0 as test
     struct tm *t = localtime(&seconds);
-    if( t->tm_year == 0 ) {
+    if( t->tm_year == 0 )
         setTime( &modem );
-    }
 
     sprintf(newMsgBuf, "Thread Start %d\r\n", threadRestartCount );
     timestampMessage( newMsgBuf );
@@ -313,23 +315,29 @@
     led.displayOn();
 #endif
     char num[17];
-    size_t smsCount;
+    size_t smsCount;    
+    getNextMessage = true;
 
     while(true) {
-        if( modem.getSMCount(&smsCount) ==OK ) {
-            if( smsCount > 0) {
+        if( modem.getSMCount(&smsCount) == OK ) {
+            if( smsCount > 0 && getNextMessage ) {
                 led3 = 1;
+                getNextMessage = false;
 #ifdef DEBUG
                 debug_pc.printf("%d SMS to read\n", smsCount);
 #endif
                 if( modem.getSM(num, newMsgBuf, MAX_MSG_LENGTH) == OK ) {
-
 #ifdef DEBUG
                     debug_pc.printf("%s : %s\n", num, newMsgBuf);
 #endif
 #ifdef USE_PRINTER
 // Print date/time, then message
                     timestampMessage( newMsgBuf );
+#ifndef USE_LED
+                    // Force processing of next messages if only printer being used
+                    // If LED display then update when displayed once
+                    getNextMessage = true;
+#endif
 #endif
 #ifdef USE_LED
                     resetMessage = true;
@@ -344,7 +352,6 @@
 #ifdef USE_LED
                         resetMessage = true;
                     } else if ( strncmp( cmdBuf, "INFO", 4 ) == 0 ) {
-//                        addNewMessage( INFO_MSG );
                         sprintf(newMsgBuf, "%s %s          ", INFO_MSG, ownNumber );
                         addNewMessage( newMsgBuf );
 #endif
@@ -370,6 +377,7 @@
 {
     int y,xmax,ymax;
     led.getXYMax(&xmax,&ymax);
+    boolean msgFinished = false;
 
     while(true) {
         // shift the whole screen 6 times, one column at a time; making 1 character
@@ -377,6 +385,11 @@
             for (int x=0; x < 6; x++) {
                 led.scrollLeft(1, 1);
                 msgx--;
+                if( msgx <= 0 ) {
+                    msgFinished = true;
+                    getNextMessage = true;
+                }
+
                 // fit as much as we can on the available display space
 
                 while (!led.putChar(msgx,0,msgBuf[currentMsg][crtPos]))  { // zero return if it all fitted
@@ -390,12 +403,14 @@
                 Thread::wait(DISPDELAY);
             }
         }
-        // Look for a new message being available
+        // Look for a new message being available and current message has been displayed
+        
         // TODO add minimum interval between message changes, e.g. 60s
-        if( resetMessage ) {
+        if( resetMessage && msgFinished ) {
             led.clear();
             crtPos = 0;
             msgx = 1;
+            msgFinished = false;
 
             strncpy( &msgBuf[currentMsg][0], newMsgBuf, MAX_MSG_LENGTH );
             if( strlen( &msgBuf[currentMsg][0] ) > 10 ) {
@@ -409,6 +424,7 @@
 }
 
 
+/*
 void matrixDemo( void )
 {
     int xmax,ymax;
@@ -440,7 +456,7 @@
     led.clear();
     led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y);
 }
-
+*/
 
 void showTime()
 {
@@ -480,7 +496,7 @@
     led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y);    // Use all displays as 128x8 display
     led.clear();
     led.setBrightness(2);
-    //led.displayOff(); // Turn off display for now until receiver tast has started
+    //led.displayOff(); // Turn off display for now until receiver task has started
     bool twoLineDisplay = (LED_MAX_DISPLAY_X > 1);
     led.putString( 0, 0, "SMS to LED Display" );
 
@@ -498,7 +514,7 @@
     // Show we are still working by alternitively flashing LED1 adn LED2, once a second
     led1 = 0;       // Working
     led2 = 1;       // Alternate with led1
-    led3 = 0;       // Received and processign SMS
+    led3 = 0;       // Received and processing SMS
     led4 = 0;       // Processing USSD message queue
     while(1) {
 #ifdef USE_LED
@@ -520,6 +536,4 @@
         led2=!led2;
         Thread::wait(1000);
     }
-
-    //return 0;
 }
\ No newline at end of file