SMS message display on LED Matrix board with printer option
Dependencies: AdafruitThermalPrinter HT1632_LedMatrix VodafoneUSBModem mbed-rtos mbed
Diff: main.cpp
- Revision:
- 2:787cbb491f8f
- Parent:
- 1:243371cb92c8
- Child:
- 3:59038ad536ac
--- a/main.cpp Wed Jan 30 16:58:33 2013 +0000 +++ b/main.cpp Mon Feb 25 22:22:20 2013 +0000 @@ -37,11 +37,12 @@ * * TODO: Still have issue with restarts when using printer. * mbed-rtos and serial problem? + * Incoming queue for multiple messages, give each at least 60 to be displayed * */ #define USE_LED -#define USE_PRINTER +#undef USE_PRINTER #include <ctype.h> @@ -94,9 +95,12 @@ #endif // Message buffers. New message waiting to be displayed and current message being displayed +#define MAX_NUM_MSGS 10 static char cmdBuf[12]; static char newMsgBuf[MAX_MSG_LENGTH]; -static char msgBuf[MAX_MSG_LENGTH]; +static char msgBuf[MAX_NUM_MSGS][MAX_MSG_LENGTH]; +static int currentMsg = 0; +static int numberOfMsgs = 0; #ifdef USE_LED static char ownNumber[20]; @@ -112,7 +116,7 @@ DigitalOut led1(LED1); // Activity DigitalOut led2(LED2); // Activity, alternates with led2 DigitalOut led3(LED3); // SMS received, turns off after processed -DigitalOut led4(LED4); // USSD requested +DigitalOut led4(LED4); // USSD request in progress int threadRestartCount = 0; @@ -141,11 +145,28 @@ #ifdef USE_LED // Load a new message -void setNewMessage( char *newMsgStart ) +void addNewMessage( char *newMsgStart ) { strncpy( newMsgBuf, newMsgStart, MAX_MSG_LENGTH ); resetMessage = true; } + + +void resetMessageBuffers( ) +{ + for( int i=0; i<MAX_NUM_MSGS; i++ ) { + strcpy( &msgBuf[i][0], " " ); + } + currentMsg = 0; + numberOfMsgs = 0; +} + +void checkAndSetNextMessage() +{ + numberOfMsgs++; + +} + #endif void sendUSSDCommand( VodafoneUSBModem *_modem, char *ussdCommand, bool setScrolling ) @@ -225,10 +246,6 @@ #ifdef USE_LED void getOwnNumber(VodafoneUSBModem *_modem, char *numPtr ) { - char numBuf[10]; - // Set RTC using received message, format is DD-MMM-YYYY HH:MM - // Month is in text name, e.g. NOV, time is using 24 hour clock. - struct tm t; int retryCount = 3; while( retryCount ) { @@ -237,47 +254,18 @@ #ifdef DEBUG debug_pc.printf("Own Number received %s\n", newMsgBuf); #endif - - // Save number in array pointed to be numPtr - - // Terminator - *numPtr = '\0'; - -/* - - - t.tm_sec = 0; // 0-59 - strncpy(numBuf, &newMsgBuf[15], 2 ); - t.tm_min = atoi(numBuf); // 0-59 - strncpy(numBuf, &newMsgBuf[12], 2 ); - t.tm_hour = atoi(numBuf); // 0-23 - strncpy(numBuf, &newMsgBuf[0], 2 ); - t.tm_mday = atoi(numBuf); // 1-31 - strncpy(numBuf, &newMsgBuf[3], 3 ); - t.tm_mon = 0; // 0-11 - for( int i=0; i<12; i++ ) { - if( strncmp( months[i], numBuf, 3 ) == 0 ) { - t.tm_mon = i; // 0-11 - break; - } - } - strncpy(numBuf, &newMsgBuf[9], 2 ); - t.tm_year = 100 + atoi( numBuf ); // year since 1900 - - if( t.tm_year >110 ) { - // convert to timestamp and display - time_t seconds = mktime(&t); - set_time( seconds ); - retryCount = 0; // No more retries, terminate while - } else { - // Failed to set time, decrement tries + if( strlen(newMsgBuf) > 0 ) { + // Save number in array pointed to be numPtr + strncpy(ownNumber, newMsgBuf, strlen(newMsgBuf) ); + retryCount = 0; + } else retryCount--; - } -*/ } } #endif +char linkStateStr[6][13] = { "Unknown ", "Registering ", "Denied ", "No Signal ", "Home Network", "Roaming " }; +char bearerStr[6][5] = {"Unkn", "GSM ", "EDGE", "3G ", "HSPA", "LTE " }; void receiveSMS(void const*) { @@ -286,24 +274,27 @@ threadRestartCount++; -// int pRssi = 0; -// setNewMessage( "Starting" ); + int pRssi = 0; +// addNewMessage( "Starting" ); + LinkMonitor::REGISTRATION_STATE pRegistrationState; + LinkMonitor::BEARER pBearer; -// LinkMonitor::REGISTRATION_STATE pRegistrationState; -// LinkMonitor::BEARER pBearer; + 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] ); + addNewMessage( newMsgBuf ); -// modem.getLinkState( &pRssi,&pRegistrationState, &pBearer); -#ifdef DEBUG -// debug_pc.printf("Link state Rssi: %d, Registration state %x Bearer %x\n",pRssi,pRegistrationState, pBearer); -#endif + Thread::wait(3000); #ifdef USE_LED // Get own number fromt he network getOwnNumber( &modem, ownNumber ); #endif -// sprintf(msgBuf, "Link state Rssi: %d, Registration state %x Bearer %x ",pRssi,pRegistrationState, pBearer); +// 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); @@ -315,10 +306,10 @@ timestampMessage( newMsgBuf ); #endif #ifdef USE_LED - //strcpy( msgBuf, INFO_MSG ); + //strcpy( &msgBuf[currentMsg][0], INFO_MSG ); sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber ); - setNewMessage( newMsgBuf ); - + addNewMessage( newMsgBuf ); + led.displayOn(); #endif char num[17]; @@ -353,17 +344,17 @@ #ifdef USE_LED resetMessage = true; } else if ( strncmp( cmdBuf, "INFO", 4 ) == 0 ) { -// setNewMessage( INFO_MSG ); +// addNewMessage( INFO_MSG ); sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber ); - setNewMessage( newMsgBuf ); + addNewMessage( newMsgBuf ); #endif // } else if ( strncmp( cmdBuf, "DEMO", 4 ) == 0 ) { // matrixDemo(); // sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber ); -// setNewMessage( newMsgBuf ); +// addNewMessage( newMsgBuf ); #ifdef USE_LED } else if ( strncmp( cmdBuf, "CLEAR", 5 ) == 0 ) { - setNewMessage( " " ); + addNewMessage( " " ); #endif } } @@ -382,16 +373,16 @@ while(true) { // shift the whole screen 6 times, one column at a time; making 1 character - if( strlen( msgBuf ) > 10 ) { + if( strlen( &msgBuf[currentMsg][0] ) > 10 ) { for (int x=0; x < 6; x++) { led.scrollLeft(1, 1); msgx--; // fit as much as we can on the available display space - while (!led.putChar(msgx,0,msgBuf[crtPos])) { // zero return if it all fitted + while (!led.putChar(msgx,0,msgBuf[currentMsg][crtPos])) { // zero return if it all fitted led.getXY(&msgx,&y); crtPos++; // we got all of the character on!! - if (crtPos >= strlen(msgBuf)) { + if (crtPos >= strlen(&msgBuf[currentMsg][0])) { crtPos = 0; } } @@ -405,11 +396,12 @@ led.clear(); crtPos = 0; msgx = 1; - strncpy( msgBuf, newMsgBuf, MAX_MSG_LENGTH ); - if( strlen( msgBuf ) > 10 ) { - strcat( msgBuf, " "); + + strncpy( &msgBuf[currentMsg][0], newMsgBuf, MAX_MSG_LENGTH ); + if( strlen( &msgBuf[currentMsg][0] ) > 10 ) { + strcat( &msgBuf[currentMsg][0], " "); } else { - led.putString(0,0, msgBuf); + led.putString(0,0, &msgBuf[currentMsg][0]); } resetMessage = false; } @@ -446,7 +438,7 @@ wait(2); led.clear(); - led.init(2,1); // Use displays 1 and 2 as 64x8 display + led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y); } @@ -485,7 +477,7 @@ int displayCount = 10; bool displayTime = true; - led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y); // Use displays 1 and 2 as 64x8 display + 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 @@ -496,7 +488,7 @@ #endif // Set initial blank message - strcpy( msgBuf, " " ); + resetMessageBuffers(); Thread receiveTask(receiveSMS, NULL, osPriorityNormal, 1024 * 8); // try 6 next #ifdef USE_LED @@ -529,5 +521,5 @@ Thread::wait(1000); } - return 0; + //return 0; } \ No newline at end of file