SMS message display on LED Matrix board with printer option

Dependencies:   AdafruitThermalPrinter HT1632_LedMatrix VodafoneUSBModem mbed-rtos mbed

Committer:
SomeRandomBloke
Date:
Tue Feb 26 16:53:02 2013 +0000
Revision:
3:59038ad536ac
Parent:
2:787cbb491f8f
Child:
4:a364da55b42e
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:9d29b886d41b 1 /**
SomeRandomBloke 0:9d29b886d41b 2 * Mbed SMS to Printer and LED Matrix Displays
SomeRandomBloke 0:9d29b886d41b 3 * Based on SMS example from VodafoneUSBModem library and
SomeRandomBloke 0:9d29b886d41b 4 * 3GReceiptPrinter app from Ashley Mills.
SomeRandomBloke 0:9d29b886d41b 5 *
SomeRandomBloke 0:9d29b886d41b 6 * Requires libraries:
SomeRandomBloke 0:9d29b886d41b 7 * AdafruitThermalPrinter - Port of Arduino library by Ashley Mills
SomeRandomBloke 0:9d29b886d41b 8 * VodafoneUSBModem - Driver for Vodafone K3370 Mobile Broadband dongle
SomeRandomBloke 0:9d29b886d41b 9 * HT1632_LedMatrix - LED Matrix library by Andrew Lindsay, port of Arduino library by Andrew Lindsay
SomeRandomBloke 0:9d29b886d41b 10 *
SomeRandomBloke 0:9d29b886d41b 11 * @author Andrew Lindsay
SomeRandomBloke 0:9d29b886d41b 12 *
SomeRandomBloke 0:9d29b886d41b 13 * @section LICENSE
SomeRandomBloke 0:9d29b886d41b 14 *
SomeRandomBloke 0:9d29b886d41b 15 * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
SomeRandomBloke 0:9d29b886d41b 16 *
SomeRandomBloke 0:9d29b886d41b 17 * Permission is hereby granted, free of charge, to any person obtaining a copy
SomeRandomBloke 0:9d29b886d41b 18 * of this software and associated documentation files (the "Software"), to deal
SomeRandomBloke 0:9d29b886d41b 19 * in the Software without restriction, including without limitation the rights
SomeRandomBloke 0:9d29b886d41b 20 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
SomeRandomBloke 0:9d29b886d41b 21 * copies of the Software, and to permit persons to whom the Software is
SomeRandomBloke 0:9d29b886d41b 22 * furnished to do so, subject to the following conditions:
SomeRandomBloke 0:9d29b886d41b 23
SomeRandomBloke 0:9d29b886d41b 24 * The above copyright notice and this permission notice shall be included in
SomeRandomBloke 0:9d29b886d41b 25 * all copies or substantial portions of the Software.
SomeRandomBloke 0:9d29b886d41b 26 *
SomeRandomBloke 0:9d29b886d41b 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
SomeRandomBloke 0:9d29b886d41b 28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
SomeRandomBloke 0:9d29b886d41b 29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
SomeRandomBloke 0:9d29b886d41b 30 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
SomeRandomBloke 0:9d29b886d41b 31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SomeRandomBloke 0:9d29b886d41b 32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SomeRandomBloke 0:9d29b886d41b 33 * THE SOFTWARE.
SomeRandomBloke 0:9d29b886d41b 34 *
SomeRandomBloke 0:9d29b886d41b 35 * @section DESCRIPTION
SomeRandomBloke 0:9d29b886d41b 36 * Display received SMS on scrolling LED matrix with optional output to thermal printer.
SomeRandomBloke 0:9d29b886d41b 37 *
SomeRandomBloke 0:9d29b886d41b 38 * TODO: Still have issue with restarts when using printer.
SomeRandomBloke 0:9d29b886d41b 39 * mbed-rtos and serial problem?
SomeRandomBloke 2:787cbb491f8f 40 * Incoming queue for multiple messages, give each at least 60 to be displayed
SomeRandomBloke 0:9d29b886d41b 41 *
SomeRandomBloke 0:9d29b886d41b 42 */
SomeRandomBloke 0:9d29b886d41b 43
SomeRandomBloke 0:9d29b886d41b 44 #define USE_LED
SomeRandomBloke 2:787cbb491f8f 45 #undef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 46
SomeRandomBloke 0:9d29b886d41b 47 #include <ctype.h>
SomeRandomBloke 0:9d29b886d41b 48
SomeRandomBloke 0:9d29b886d41b 49 #include "mbed.h"
SomeRandomBloke 0:9d29b886d41b 50 #include "VodafoneUSBModem.h"
SomeRandomBloke 0:9d29b886d41b 51 //#include <LinkMonitor.h>
SomeRandomBloke 0:9d29b886d41b 52
SomeRandomBloke 0:9d29b886d41b 53 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 54 #include "HT1632_LedMatrix.h"
SomeRandomBloke 0:9d29b886d41b 55 #endif
SomeRandomBloke 0:9d29b886d41b 56 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 57 #include "AdafruitThermal.h"
SomeRandomBloke 0:9d29b886d41b 58 #endif
SomeRandomBloke 0:9d29b886d41b 59
SomeRandomBloke 0:9d29b886d41b 60 #define DEBUG 1
SomeRandomBloke 0:9d29b886d41b 61
SomeRandomBloke 0:9d29b886d41b 62 #ifdef USE_LED
SomeRandomBloke 3:59038ad536ac 63 // Default scrolling message includes own number obtained using USSD request
SomeRandomBloke 1:243371cb92c8 64 #define INFO_MSG " Send a message to "
SomeRandomBloke 0:9d29b886d41b 65 #endif
SomeRandomBloke 0:9d29b886d41b 66
SomeRandomBloke 0:9d29b886d41b 67 // Vodafone USSD commands
SomeRandomBloke 0:9d29b886d41b 68 #define USSD_COMMAND_OWN_NUMBER "*#100#"
SomeRandomBloke 0:9d29b886d41b 69 #define USSD_COMMAND_BALANCE "*#134#"
SomeRandomBloke 0:9d29b886d41b 70 #define USSD_COMMAND_TIME "*#103#"
SomeRandomBloke 0:9d29b886d41b 71
SomeRandomBloke 0:9d29b886d41b 72 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 73 Serial debug_pc(USBTX, USBRX); // tx, rx
SomeRandomBloke 0:9d29b886d41b 74 #endif
SomeRandomBloke 0:9d29b886d41b 75
SomeRandomBloke 0:9d29b886d41b 76 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 77 AdafruitThermal printer(p28,p27); // setup printer
SomeRandomBloke 0:9d29b886d41b 78 #endif
SomeRandomBloke 0:9d29b886d41b 79 // Define a maximum size for storage arrays, is 160 (SMS size) + a bit more.
SomeRandomBloke 0:9d29b886d41b 80 #define MAX_MSG_LENGTH 192
SomeRandomBloke 0:9d29b886d41b 81
SomeRandomBloke 0:9d29b886d41b 82 // Month list used in converting to integer for set_time
SomeRandomBloke 0:9d29b886d41b 83 char months[12][4] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
SomeRandomBloke 0:9d29b886d41b 84
SomeRandomBloke 0:9d29b886d41b 85 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 86
SomeRandomBloke 0:9d29b886d41b 87 #define LED_MAX_DISPLAY_X 4
SomeRandomBloke 0:9d29b886d41b 88 #define LED_MAX_DISPLAY_Y 1
SomeRandomBloke 0:9d29b886d41b 89
SomeRandomBloke 0:9d29b886d41b 90 // create object to control the LED Matrix
SomeRandomBloke 0:9d29b886d41b 91 HT1632_LedMatrix led = HT1632_LedMatrix();
SomeRandomBloke 0:9d29b886d41b 92 #define DISPDELAY 90
SomeRandomBloke 0:9d29b886d41b 93 #endif
SomeRandomBloke 0:9d29b886d41b 94
SomeRandomBloke 0:9d29b886d41b 95 // Message buffers. New message waiting to be displayed and current message being displayed
SomeRandomBloke 2:787cbb491f8f 96 #define MAX_NUM_MSGS 10
SomeRandomBloke 0:9d29b886d41b 97 static char cmdBuf[12];
SomeRandomBloke 0:9d29b886d41b 98 static char newMsgBuf[MAX_MSG_LENGTH];
SomeRandomBloke 2:787cbb491f8f 99 static char msgBuf[MAX_NUM_MSGS][MAX_MSG_LENGTH];
SomeRandomBloke 2:787cbb491f8f 100 static int currentMsg = 0;
SomeRandomBloke 2:787cbb491f8f 101 static int numberOfMsgs = 0;
SomeRandomBloke 3:59038ad536ac 102 static boolean getNextMessage = true;
SomeRandomBloke 0:9d29b886d41b 103
SomeRandomBloke 0:9d29b886d41b 104 #ifdef USE_LED
SomeRandomBloke 1:243371cb92c8 105 static char ownNumber[20];
SomeRandomBloke 1:243371cb92c8 106
SomeRandomBloke 0:9d29b886d41b 107 int crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 108 int msgx = 1; // position on message screen of current character, set to 1 to allow for first scroll
SomeRandomBloke 0:9d29b886d41b 109 bool resetMessage = false;
SomeRandomBloke 0:9d29b886d41b 110
SomeRandomBloke 0:9d29b886d41b 111 void matrixDemo( void );
SomeRandomBloke 0:9d29b886d41b 112 #endif
SomeRandomBloke 0:9d29b886d41b 113
SomeRandomBloke 0:9d29b886d41b 114 // Define the onboard LEDs to use as status indicators
SomeRandomBloke 0:9d29b886d41b 115 DigitalOut led1(LED1); // Activity
SomeRandomBloke 0:9d29b886d41b 116 DigitalOut led2(LED2); // Activity, alternates with led2
SomeRandomBloke 0:9d29b886d41b 117 DigitalOut led3(LED3); // SMS received, turns off after processed
SomeRandomBloke 2:787cbb491f8f 118 DigitalOut led4(LED4); // USSD request in progress
SomeRandomBloke 0:9d29b886d41b 119
SomeRandomBloke 0:9d29b886d41b 120 int threadRestartCount = 0;
SomeRandomBloke 0:9d29b886d41b 121
SomeRandomBloke 0:9d29b886d41b 122 // Convert string buffer to upper case, is destructive. Buffer will be changed.
SomeRandomBloke 0:9d29b886d41b 123 char* stoupper( char* s )
SomeRandomBloke 0:9d29b886d41b 124 {
SomeRandomBloke 0:9d29b886d41b 125 char* p = s;
SomeRandomBloke 0:9d29b886d41b 126 while (*p = toupper( *p )) p++;
SomeRandomBloke 0:9d29b886d41b 127 return s;
SomeRandomBloke 0:9d29b886d41b 128 }
SomeRandomBloke 0:9d29b886d41b 129
SomeRandomBloke 0:9d29b886d41b 130
SomeRandomBloke 0:9d29b886d41b 131 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 132 void timestampMessage( char *msg )
SomeRandomBloke 0:9d29b886d41b 133 {
SomeRandomBloke 0:9d29b886d41b 134 char timebuf[50];
SomeRandomBloke 0:9d29b886d41b 135 // Print date/time, then message
SomeRandomBloke 0:9d29b886d41b 136 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 137 strftime(timebuf, 50, "%d/%m/%Y %X\n", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 138 printer.print(timebuf);
SomeRandomBloke 0:9d29b886d41b 139 printer.print(msg);
SomeRandomBloke 0:9d29b886d41b 140 // linefeed a couple of times
SomeRandomBloke 0:9d29b886d41b 141 printer.feed(3);
SomeRandomBloke 0:9d29b886d41b 142 }
SomeRandomBloke 0:9d29b886d41b 143 #endif
SomeRandomBloke 0:9d29b886d41b 144
SomeRandomBloke 0:9d29b886d41b 145 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 146 // Load a new message
SomeRandomBloke 2:787cbb491f8f 147 void addNewMessage( char *newMsgStart )
SomeRandomBloke 0:9d29b886d41b 148 {
SomeRandomBloke 0:9d29b886d41b 149 strncpy( newMsgBuf, newMsgStart, MAX_MSG_LENGTH );
SomeRandomBloke 0:9d29b886d41b 150 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 151 }
SomeRandomBloke 2:787cbb491f8f 152
SomeRandomBloke 2:787cbb491f8f 153
SomeRandomBloke 2:787cbb491f8f 154 void resetMessageBuffers( )
SomeRandomBloke 2:787cbb491f8f 155 {
SomeRandomBloke 2:787cbb491f8f 156 for( int i=0; i<MAX_NUM_MSGS; i++ ) {
SomeRandomBloke 2:787cbb491f8f 157 strcpy( &msgBuf[i][0], " " );
SomeRandomBloke 2:787cbb491f8f 158 }
SomeRandomBloke 2:787cbb491f8f 159 currentMsg = 0;
SomeRandomBloke 2:787cbb491f8f 160 numberOfMsgs = 0;
SomeRandomBloke 2:787cbb491f8f 161 }
SomeRandomBloke 2:787cbb491f8f 162
SomeRandomBloke 2:787cbb491f8f 163 void checkAndSetNextMessage()
SomeRandomBloke 2:787cbb491f8f 164 {
SomeRandomBloke 2:787cbb491f8f 165 numberOfMsgs++;
SomeRandomBloke 2:787cbb491f8f 166
SomeRandomBloke 2:787cbb491f8f 167 }
SomeRandomBloke 2:787cbb491f8f 168
SomeRandomBloke 0:9d29b886d41b 169 #endif
SomeRandomBloke 0:9d29b886d41b 170
SomeRandomBloke 0:9d29b886d41b 171 void sendUSSDCommand( VodafoneUSBModem *_modem, char *ussdCommand, bool setScrolling )
SomeRandomBloke 0:9d29b886d41b 172 {
SomeRandomBloke 0:9d29b886d41b 173 led4 = 1;
SomeRandomBloke 0:9d29b886d41b 174 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 175 debug_pc.printf("Sending %s on USSD channel\n", ussdCommand);
SomeRandomBloke 0:9d29b886d41b 176 #endif
SomeRandomBloke 0:9d29b886d41b 177 int ret = _modem->sendUSSD(ussdCommand, newMsgBuf, MAX_MSG_LENGTH);
SomeRandomBloke 0:9d29b886d41b 178 // Check for correct response
SomeRandomBloke 0:9d29b886d41b 179 if(ret) {
SomeRandomBloke 0:9d29b886d41b 180 // Non 0 value indicates an error??
SomeRandomBloke 0:9d29b886d41b 181 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 182 debug_pc.printf("Send USSD command returned %d\n", ret);
SomeRandomBloke 0:9d29b886d41b 183 #endif
SomeRandomBloke 0:9d29b886d41b 184 led4 = 0;
SomeRandomBloke 0:9d29b886d41b 185 return;
SomeRandomBloke 0:9d29b886d41b 186 }
SomeRandomBloke 0:9d29b886d41b 187
SomeRandomBloke 0:9d29b886d41b 188 // Should only display message if one has been received.
SomeRandomBloke 0:9d29b886d41b 189 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 190 debug_pc.printf("Result of command: %s\n", newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 191 #endif
SomeRandomBloke 0:9d29b886d41b 192
SomeRandomBloke 0:9d29b886d41b 193 led4 = 0;
SomeRandomBloke 0:9d29b886d41b 194
SomeRandomBloke 0:9d29b886d41b 195 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 196 resetMessage = setScrolling;
SomeRandomBloke 0:9d29b886d41b 197 #endif
SomeRandomBloke 0:9d29b886d41b 198 }
SomeRandomBloke 0:9d29b886d41b 199
SomeRandomBloke 0:9d29b886d41b 200 void setTime(VodafoneUSBModem *_modem )
SomeRandomBloke 0:9d29b886d41b 201 {
SomeRandomBloke 0:9d29b886d41b 202 char numBuf[10];
SomeRandomBloke 0:9d29b886d41b 203 // Set RTC using received message, format is DD-MMM-YYYY HH:MM
SomeRandomBloke 0:9d29b886d41b 204 // Month is in text name, e.g. NOV, time is using 24 hour clock.
SomeRandomBloke 0:9d29b886d41b 205 struct tm t;
SomeRandomBloke 0:9d29b886d41b 206 int retryCount = 3;
SomeRandomBloke 0:9d29b886d41b 207
SomeRandomBloke 0:9d29b886d41b 208 while( retryCount ) {
SomeRandomBloke 0:9d29b886d41b 209 sendUSSDCommand( _modem, USSD_COMMAND_TIME, false );
SomeRandomBloke 0:9d29b886d41b 210
SomeRandomBloke 0:9d29b886d41b 211 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 212 debug_pc.printf("Time received is %s\n", newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 213 #endif
SomeRandomBloke 0:9d29b886d41b 214
SomeRandomBloke 0:9d29b886d41b 215 t.tm_sec = 0; // 0-59
SomeRandomBloke 0:9d29b886d41b 216 strncpy(numBuf, &newMsgBuf[15], 2 );
SomeRandomBloke 0:9d29b886d41b 217 t.tm_min = atoi(numBuf); // 0-59
SomeRandomBloke 0:9d29b886d41b 218 strncpy(numBuf, &newMsgBuf[12], 2 );
SomeRandomBloke 0:9d29b886d41b 219 t.tm_hour = atoi(numBuf); // 0-23
SomeRandomBloke 0:9d29b886d41b 220 strncpy(numBuf, &newMsgBuf[0], 2 );
SomeRandomBloke 0:9d29b886d41b 221 t.tm_mday = atoi(numBuf); // 1-31
SomeRandomBloke 0:9d29b886d41b 222 strncpy(numBuf, &newMsgBuf[3], 3 );
SomeRandomBloke 0:9d29b886d41b 223 t.tm_mon = 0; // 0-11
SomeRandomBloke 0:9d29b886d41b 224 for( int i=0; i<12; i++ ) {
SomeRandomBloke 0:9d29b886d41b 225 if( strncmp( months[i], numBuf, 3 ) == 0 ) {
SomeRandomBloke 0:9d29b886d41b 226 t.tm_mon = i; // 0-11
SomeRandomBloke 0:9d29b886d41b 227 break;
SomeRandomBloke 0:9d29b886d41b 228 }
SomeRandomBloke 0:9d29b886d41b 229 }
SomeRandomBloke 0:9d29b886d41b 230 strncpy(numBuf, &newMsgBuf[9], 2 );
SomeRandomBloke 0:9d29b886d41b 231 t.tm_year = 100 + atoi( numBuf ); // year since 1900
SomeRandomBloke 0:9d29b886d41b 232
SomeRandomBloke 0:9d29b886d41b 233 if( t.tm_year >110 ) {
SomeRandomBloke 0:9d29b886d41b 234 // convert to timestamp and display
SomeRandomBloke 0:9d29b886d41b 235 time_t seconds = mktime(&t);
SomeRandomBloke 0:9d29b886d41b 236 set_time( seconds );
SomeRandomBloke 0:9d29b886d41b 237 retryCount = 0; // No more retries, terminate while
SomeRandomBloke 0:9d29b886d41b 238 } else {
SomeRandomBloke 0:9d29b886d41b 239 // Failed to set time, decrement tries
SomeRandomBloke 0:9d29b886d41b 240 retryCount--;
SomeRandomBloke 0:9d29b886d41b 241 }
SomeRandomBloke 0:9d29b886d41b 242 }
SomeRandomBloke 0:9d29b886d41b 243 }
SomeRandomBloke 0:9d29b886d41b 244
SomeRandomBloke 1:243371cb92c8 245 void getOwnNumber(VodafoneUSBModem *_modem, char *numPtr )
SomeRandomBloke 1:243371cb92c8 246 {
SomeRandomBloke 1:243371cb92c8 247 int retryCount = 3;
SomeRandomBloke 1:243371cb92c8 248
SomeRandomBloke 1:243371cb92c8 249 while( retryCount ) {
SomeRandomBloke 1:243371cb92c8 250 sendUSSDCommand( _modem, USSD_COMMAND_OWN_NUMBER, false );
SomeRandomBloke 1:243371cb92c8 251
SomeRandomBloke 1:243371cb92c8 252 #ifdef DEBUG
SomeRandomBloke 1:243371cb92c8 253 debug_pc.printf("Own Number received %s\n", newMsgBuf);
SomeRandomBloke 1:243371cb92c8 254 #endif
SomeRandomBloke 2:787cbb491f8f 255 if( strlen(newMsgBuf) > 0 ) {
SomeRandomBloke 2:787cbb491f8f 256 // Save number in array pointed to be numPtr
SomeRandomBloke 2:787cbb491f8f 257 strncpy(ownNumber, newMsgBuf, strlen(newMsgBuf) );
SomeRandomBloke 2:787cbb491f8f 258 retryCount = 0;
SomeRandomBloke 2:787cbb491f8f 259 } else
SomeRandomBloke 1:243371cb92c8 260 retryCount--;
SomeRandomBloke 1:243371cb92c8 261 }
SomeRandomBloke 1:243371cb92c8 262 }
SomeRandomBloke 3:59038ad536ac 263
SomeRandomBloke 1:243371cb92c8 264
SomeRandomBloke 2:787cbb491f8f 265 char linkStateStr[6][13] = { "Unknown ", "Registering ", "Denied ", "No Signal ", "Home Network", "Roaming " };
SomeRandomBloke 2:787cbb491f8f 266 char bearerStr[6][5] = {"Unkn", "GSM ", "EDGE", "3G ", "HSPA", "LTE " };
SomeRandomBloke 0:9d29b886d41b 267
SomeRandomBloke 0:9d29b886d41b 268 void receiveSMS(void const*)
SomeRandomBloke 0:9d29b886d41b 269 {
SomeRandomBloke 0:9d29b886d41b 270 VodafoneUSBModem modem;
SomeRandomBloke 0:9d29b886d41b 271 time_t seconds = time(NULL);
SomeRandomBloke 3:59038ad536ac 272 int pRssi = 0;
SomeRandomBloke 3:59038ad536ac 273 LinkMonitor::REGISTRATION_STATE pRegistrationState;
SomeRandomBloke 3:59038ad536ac 274 LinkMonitor::BEARER pBearer;
SomeRandomBloke 0:9d29b886d41b 275
SomeRandomBloke 0:9d29b886d41b 276 threadRestartCount++;
SomeRandomBloke 0:9d29b886d41b 277
SomeRandomBloke 3:59038ad536ac 278 // Getting the link state seems to speed up startup of the library
SomeRandomBloke 2:787cbb491f8f 279 modem.getLinkState( &pRssi,&pRegistrationState, &pBearer);
SomeRandomBloke 2:787cbb491f8f 280 #ifdef DEBUG
SomeRandomBloke 2:787cbb491f8f 281 debug_pc.printf("Link state Rssi: %d, Registration state %x Bearer %x\n",pRssi,pRegistrationState, pBearer);
SomeRandomBloke 2:787cbb491f8f 282 #endif
SomeRandomBloke 2:787cbb491f8f 283 sprintf(newMsgBuf, "Link State: %s on %s ", linkStateStr[pRegistrationState], bearerStr[pBearer] );
SomeRandomBloke 3:59038ad536ac 284
SomeRandomBloke 3:59038ad536ac 285 #ifdef USE_LED
SomeRandomBloke 2:787cbb491f8f 286 addNewMessage( newMsgBuf );
SomeRandomBloke 3:59038ad536ac 287 #endif
SomeRandomBloke 3:59038ad536ac 288 #ifdef USE_PRINTER
SomeRandomBloke 3:59038ad536ac 289 timestampMessage( newMsgBuf );
SomeRandomBloke 3:59038ad536ac 290 #endif
SomeRandomBloke 0:9d29b886d41b 291
SomeRandomBloke 2:787cbb491f8f 292 Thread::wait(3000);
SomeRandomBloke 0:9d29b886d41b 293
SomeRandomBloke 3:59038ad536ac 294 // Get own number from the network
SomeRandomBloke 3:59038ad536ac 295 getOwnNumber( &modem, ownNumber );
SomeRandomBloke 1:243371cb92c8 296 #ifdef USE_LED
SomeRandomBloke 3:59038ad536ac 297 sprintf(newMsgBuf, "Own number %s", ownNumber );
SomeRandomBloke 3:59038ad536ac 298 timestampMessage( newMsgBuf );
SomeRandomBloke 1:243371cb92c8 299 #endif
SomeRandomBloke 1:243371cb92c8 300
SomeRandomBloke 0:9d29b886d41b 301 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 302 // Check if time already set, if not then get it. Use year = 0 as test
SomeRandomBloke 0:9d29b886d41b 303 struct tm *t = localtime(&seconds);
SomeRandomBloke 3:59038ad536ac 304 if( t->tm_year == 0 )
SomeRandomBloke 0:9d29b886d41b 305 setTime( &modem );
SomeRandomBloke 0:9d29b886d41b 306
SomeRandomBloke 0:9d29b886d41b 307 sprintf(newMsgBuf, "Thread Start %d\r\n", threadRestartCount );
SomeRandomBloke 0:9d29b886d41b 308 timestampMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 309 #endif
SomeRandomBloke 0:9d29b886d41b 310 #ifdef USE_LED
SomeRandomBloke 2:787cbb491f8f 311 //strcpy( &msgBuf[currentMsg][0], INFO_MSG );
SomeRandomBloke 1:243371cb92c8 312 sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 2:787cbb491f8f 313 addNewMessage( newMsgBuf );
SomeRandomBloke 2:787cbb491f8f 314
SomeRandomBloke 0:9d29b886d41b 315 led.displayOn();
SomeRandomBloke 0:9d29b886d41b 316 #endif
SomeRandomBloke 0:9d29b886d41b 317 char num[17];
SomeRandomBloke 3:59038ad536ac 318 size_t smsCount;
SomeRandomBloke 3:59038ad536ac 319 getNextMessage = true;
SomeRandomBloke 0:9d29b886d41b 320
SomeRandomBloke 0:9d29b886d41b 321 while(true) {
SomeRandomBloke 3:59038ad536ac 322 if( modem.getSMCount(&smsCount) == OK ) {
SomeRandomBloke 3:59038ad536ac 323 if( smsCount > 0 && getNextMessage ) {
SomeRandomBloke 0:9d29b886d41b 324 led3 = 1;
SomeRandomBloke 3:59038ad536ac 325 getNextMessage = false;
SomeRandomBloke 0:9d29b886d41b 326 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 327 debug_pc.printf("%d SMS to read\n", smsCount);
SomeRandomBloke 0:9d29b886d41b 328 #endif
SomeRandomBloke 0:9d29b886d41b 329 if( modem.getSM(num, newMsgBuf, MAX_MSG_LENGTH) == OK ) {
SomeRandomBloke 0:9d29b886d41b 330 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 331 debug_pc.printf("%s : %s\n", num, newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 332 #endif
SomeRandomBloke 0:9d29b886d41b 333 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 334 // Print date/time, then message
SomeRandomBloke 0:9d29b886d41b 335 timestampMessage( newMsgBuf );
SomeRandomBloke 3:59038ad536ac 336 #ifndef USE_LED
SomeRandomBloke 3:59038ad536ac 337 // Force processing of next messages if only printer being used
SomeRandomBloke 3:59038ad536ac 338 // If LED display then update when displayed once
SomeRandomBloke 3:59038ad536ac 339 getNextMessage = true;
SomeRandomBloke 3:59038ad536ac 340 #endif
SomeRandomBloke 0:9d29b886d41b 341 #endif
SomeRandomBloke 0:9d29b886d41b 342 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 343 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 344 #endif
SomeRandomBloke 0:9d29b886d41b 345 strncpy( cmdBuf, newMsgBuf, 10 );
SomeRandomBloke 0:9d29b886d41b 346 stoupper( cmdBuf ); // This is a destructive function, original characters are uppercased
SomeRandomBloke 0:9d29b886d41b 347 if( strncmp( cmdBuf, "BALANCE", 7 ) == 0 ) {
SomeRandomBloke 0:9d29b886d41b 348 sendUSSDCommand( &modem, USSD_COMMAND_BALANCE, true );
SomeRandomBloke 0:9d29b886d41b 349 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 350 timestampMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 351 #endif
SomeRandomBloke 0:9d29b886d41b 352 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 353 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 354 } else if ( strncmp( cmdBuf, "INFO", 4 ) == 0 ) {
SomeRandomBloke 1:243371cb92c8 355 sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 2:787cbb491f8f 356 addNewMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 357 #endif
SomeRandomBloke 0:9d29b886d41b 358 // } else if ( strncmp( cmdBuf, "DEMO", 4 ) == 0 ) {
SomeRandomBloke 1:243371cb92c8 359 // matrixDemo();
SomeRandomBloke 1:243371cb92c8 360 // sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 2:787cbb491f8f 361 // addNewMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 362 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 363 } else if ( strncmp( cmdBuf, "CLEAR", 5 ) == 0 ) {
SomeRandomBloke 2:787cbb491f8f 364 addNewMessage( " " );
SomeRandomBloke 0:9d29b886d41b 365 #endif
SomeRandomBloke 0:9d29b886d41b 366 }
SomeRandomBloke 0:9d29b886d41b 367 }
SomeRandomBloke 0:9d29b886d41b 368 }
SomeRandomBloke 0:9d29b886d41b 369 }
SomeRandomBloke 0:9d29b886d41b 370 Thread::wait(3000);
SomeRandomBloke 0:9d29b886d41b 371 led3 = 0;
SomeRandomBloke 0:9d29b886d41b 372 }
SomeRandomBloke 0:9d29b886d41b 373 }
SomeRandomBloke 0:9d29b886d41b 374
SomeRandomBloke 0:9d29b886d41b 375 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 376 void displayScrollingLine(void const*)
SomeRandomBloke 0:9d29b886d41b 377 {
SomeRandomBloke 0:9d29b886d41b 378 int y,xmax,ymax;
SomeRandomBloke 0:9d29b886d41b 379 led.getXYMax(&xmax,&ymax);
SomeRandomBloke 3:59038ad536ac 380 boolean msgFinished = false;
SomeRandomBloke 0:9d29b886d41b 381
SomeRandomBloke 0:9d29b886d41b 382 while(true) {
SomeRandomBloke 0:9d29b886d41b 383 // shift the whole screen 6 times, one column at a time; making 1 character
SomeRandomBloke 2:787cbb491f8f 384 if( strlen( &msgBuf[currentMsg][0] ) > 10 ) {
SomeRandomBloke 0:9d29b886d41b 385 for (int x=0; x < 6; x++) {
SomeRandomBloke 0:9d29b886d41b 386 led.scrollLeft(1, 1);
SomeRandomBloke 0:9d29b886d41b 387 msgx--;
SomeRandomBloke 3:59038ad536ac 388 if( msgx <= 0 ) {
SomeRandomBloke 3:59038ad536ac 389 msgFinished = true;
SomeRandomBloke 3:59038ad536ac 390 getNextMessage = true;
SomeRandomBloke 3:59038ad536ac 391 }
SomeRandomBloke 3:59038ad536ac 392
SomeRandomBloke 0:9d29b886d41b 393 // fit as much as we can on the available display space
SomeRandomBloke 0:9d29b886d41b 394
SomeRandomBloke 2:787cbb491f8f 395 while (!led.putChar(msgx,0,msgBuf[currentMsg][crtPos])) { // zero return if it all fitted
SomeRandomBloke 0:9d29b886d41b 396 led.getXY(&msgx,&y);
SomeRandomBloke 0:9d29b886d41b 397 crtPos++; // we got all of the character on!!
SomeRandomBloke 2:787cbb491f8f 398 if (crtPos >= strlen(&msgBuf[currentMsg][0])) {
SomeRandomBloke 0:9d29b886d41b 399 crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 400 }
SomeRandomBloke 0:9d29b886d41b 401 }
SomeRandomBloke 0:9d29b886d41b 402 led.putShadowRam();
SomeRandomBloke 0:9d29b886d41b 403 Thread::wait(DISPDELAY);
SomeRandomBloke 0:9d29b886d41b 404 }
SomeRandomBloke 0:9d29b886d41b 405 }
SomeRandomBloke 3:59038ad536ac 406 // Look for a new message being available and current message has been displayed
SomeRandomBloke 3:59038ad536ac 407
SomeRandomBloke 0:9d29b886d41b 408 // TODO add minimum interval between message changes, e.g. 60s
SomeRandomBloke 3:59038ad536ac 409 if( resetMessage && msgFinished ) {
SomeRandomBloke 0:9d29b886d41b 410 led.clear();
SomeRandomBloke 0:9d29b886d41b 411 crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 412 msgx = 1;
SomeRandomBloke 3:59038ad536ac 413 msgFinished = false;
SomeRandomBloke 2:787cbb491f8f 414
SomeRandomBloke 2:787cbb491f8f 415 strncpy( &msgBuf[currentMsg][0], newMsgBuf, MAX_MSG_LENGTH );
SomeRandomBloke 2:787cbb491f8f 416 if( strlen( &msgBuf[currentMsg][0] ) > 10 ) {
SomeRandomBloke 2:787cbb491f8f 417 strcat( &msgBuf[currentMsg][0], " ");
SomeRandomBloke 0:9d29b886d41b 418 } else {
SomeRandomBloke 2:787cbb491f8f 419 led.putString(0,0, &msgBuf[currentMsg][0]);
SomeRandomBloke 0:9d29b886d41b 420 }
SomeRandomBloke 0:9d29b886d41b 421 resetMessage = false;
SomeRandomBloke 0:9d29b886d41b 422 }
SomeRandomBloke 0:9d29b886d41b 423 }
SomeRandomBloke 0:9d29b886d41b 424 }
SomeRandomBloke 0:9d29b886d41b 425
SomeRandomBloke 0:9d29b886d41b 426
SomeRandomBloke 3:59038ad536ac 427 /*
SomeRandomBloke 0:9d29b886d41b 428 void matrixDemo( void )
SomeRandomBloke 0:9d29b886d41b 429 {
SomeRandomBloke 0:9d29b886d41b 430 int xmax,ymax;
SomeRandomBloke 0:9d29b886d41b 431 led.getXYMax(&xmax,&ymax);
SomeRandomBloke 0:9d29b886d41b 432 led.clear();
SomeRandomBloke 0:9d29b886d41b 433 for( int n=0; n<3; n++ ) {
SomeRandomBloke 0:9d29b886d41b 434 for( int i=0; i<8; i++ ) {
SomeRandomBloke 0:9d29b886d41b 435 led.drawRectangle(i,i,xmax-i,ymax-i, 1);
SomeRandomBloke 0:9d29b886d41b 436 wait(0.05);
SomeRandomBloke 0:9d29b886d41b 437 }
SomeRandomBloke 0:9d29b886d41b 438 for( int i=7; i>=0; i-- ) {
SomeRandomBloke 0:9d29b886d41b 439 led.drawRectangle(i,i,xmax-i,ymax-i, 0);
SomeRandomBloke 0:9d29b886d41b 440 wait(0.05);
SomeRandomBloke 0:9d29b886d41b 441 }
SomeRandomBloke 0:9d29b886d41b 442 }
SomeRandomBloke 0:9d29b886d41b 443 led.clear();
SomeRandomBloke 0:9d29b886d41b 444 for( int n=0; n<3; n++ ) {
SomeRandomBloke 0:9d29b886d41b 445 for(int i=0; i<(xmax/2); i++) {
SomeRandomBloke 0:9d29b886d41b 446 led.drawCircle((xmax/2), 7, i, 1 );
SomeRandomBloke 0:9d29b886d41b 447 wait( 0.05 );
SomeRandomBloke 0:9d29b886d41b 448 }
SomeRandomBloke 0:9d29b886d41b 449 for(int i=(xmax/2); i>=0; i--) {
SomeRandomBloke 0:9d29b886d41b 450 led.drawCircle((xmax/2), 7, i, 0 );
SomeRandomBloke 0:9d29b886d41b 451 wait( 0.05 );
SomeRandomBloke 0:9d29b886d41b 452 }
SomeRandomBloke 0:9d29b886d41b 453 }
SomeRandomBloke 0:9d29b886d41b 454 wait(2);
SomeRandomBloke 0:9d29b886d41b 455
SomeRandomBloke 0:9d29b886d41b 456 led.clear();
SomeRandomBloke 2:787cbb491f8f 457 led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y);
SomeRandomBloke 0:9d29b886d41b 458 }
SomeRandomBloke 3:59038ad536ac 459 */
SomeRandomBloke 0:9d29b886d41b 460
SomeRandomBloke 0:9d29b886d41b 461 void showTime()
SomeRandomBloke 0:9d29b886d41b 462 {
SomeRandomBloke 0:9d29b886d41b 463 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 464 char timebuf[20];
SomeRandomBloke 0:9d29b886d41b 465 strftime(timebuf, 20, "%X ", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 466 led.putString(12,8, timebuf );
SomeRandomBloke 0:9d29b886d41b 467 }
SomeRandomBloke 0:9d29b886d41b 468
SomeRandomBloke 0:9d29b886d41b 469 void showDate()
SomeRandomBloke 0:9d29b886d41b 470 {
SomeRandomBloke 0:9d29b886d41b 471 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 472 char timebuf[20];
SomeRandomBloke 0:9d29b886d41b 473 strftime(timebuf, 20, "%d/%m/%Y ", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 474 led.putString(4,8, timebuf );
SomeRandomBloke 0:9d29b886d41b 475 }
SomeRandomBloke 0:9d29b886d41b 476 #endif
SomeRandomBloke 0:9d29b886d41b 477
SomeRandomBloke 0:9d29b886d41b 478
SomeRandomBloke 0:9d29b886d41b 479 int main()
SomeRandomBloke 0:9d29b886d41b 480 {
SomeRandomBloke 0:9d29b886d41b 481 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 482 debug_pc.baud(57600);
SomeRandomBloke 0:9d29b886d41b 483 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 484 debug_pc.printf("SMS to LED Matrix display\n");
SomeRandomBloke 0:9d29b886d41b 485 #endif
SomeRandomBloke 0:9d29b886d41b 486 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 487 printer.begin();
SomeRandomBloke 0:9d29b886d41b 488 debug_pc.printf("SMS To Printer\n");
SomeRandomBloke 0:9d29b886d41b 489 timestampMessage("SMS To Printer Starting");
SomeRandomBloke 0:9d29b886d41b 490 #endif
SomeRandomBloke 0:9d29b886d41b 491 #endif
SomeRandomBloke 0:9d29b886d41b 492 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 493 int displayCount = 10;
SomeRandomBloke 0:9d29b886d41b 494 bool displayTime = true;
SomeRandomBloke 0:9d29b886d41b 495
SomeRandomBloke 2:787cbb491f8f 496 led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y); // Use all displays as 128x8 display
SomeRandomBloke 0:9d29b886d41b 497 led.clear();
SomeRandomBloke 0:9d29b886d41b 498 led.setBrightness(2);
SomeRandomBloke 3:59038ad536ac 499 //led.displayOff(); // Turn off display for now until receiver task has started
SomeRandomBloke 0:9d29b886d41b 500 bool twoLineDisplay = (LED_MAX_DISPLAY_X > 1);
SomeRandomBloke 0:9d29b886d41b 501 led.putString( 0, 0, "SMS to LED Display" );
SomeRandomBloke 0:9d29b886d41b 502
SomeRandomBloke 0:9d29b886d41b 503 Thread::wait(3000); // Wait for display to initialise after power up
SomeRandomBloke 0:9d29b886d41b 504 #endif
SomeRandomBloke 0:9d29b886d41b 505
SomeRandomBloke 0:9d29b886d41b 506 // Set initial blank message
SomeRandomBloke 2:787cbb491f8f 507 resetMessageBuffers();
SomeRandomBloke 0:9d29b886d41b 508
SomeRandomBloke 0:9d29b886d41b 509 Thread receiveTask(receiveSMS, NULL, osPriorityNormal, 1024 * 8); // try 6 next
SomeRandomBloke 0:9d29b886d41b 510 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 511 Thread displayTask(displayScrollingLine, NULL, osPriorityNormal, 1024 * 4);
SomeRandomBloke 0:9d29b886d41b 512 #endif
SomeRandomBloke 0:9d29b886d41b 513
SomeRandomBloke 0:9d29b886d41b 514 // Show we are still working by alternitively flashing LED1 adn LED2, once a second
SomeRandomBloke 0:9d29b886d41b 515 led1 = 0; // Working
SomeRandomBloke 0:9d29b886d41b 516 led2 = 1; // Alternate with led1
SomeRandomBloke 3:59038ad536ac 517 led3 = 0; // Received and processing SMS
SomeRandomBloke 0:9d29b886d41b 518 led4 = 0; // Processing USSD message queue
SomeRandomBloke 0:9d29b886d41b 519 while(1) {
SomeRandomBloke 0:9d29b886d41b 520 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 521 // Only display date/time is we have a 2 row display
SomeRandomBloke 0:9d29b886d41b 522 if( twoLineDisplay ) {
SomeRandomBloke 0:9d29b886d41b 523 if( --displayCount <= 0 ) {
SomeRandomBloke 0:9d29b886d41b 524 displayTime = !displayTime;
SomeRandomBloke 0:9d29b886d41b 525 displayCount = 10;
SomeRandomBloke 0:9d29b886d41b 526 led.putString(0,8, " " );
SomeRandomBloke 0:9d29b886d41b 527 }
SomeRandomBloke 0:9d29b886d41b 528 if( displayTime ) {
SomeRandomBloke 0:9d29b886d41b 529 showTime();
SomeRandomBloke 0:9d29b886d41b 530 } else {
SomeRandomBloke 0:9d29b886d41b 531 showDate();
SomeRandomBloke 0:9d29b886d41b 532 }
SomeRandomBloke 0:9d29b886d41b 533 }
SomeRandomBloke 0:9d29b886d41b 534 #endif
SomeRandomBloke 0:9d29b886d41b 535 led1=!led1;
SomeRandomBloke 0:9d29b886d41b 536 led2=!led2;
SomeRandomBloke 0:9d29b886d41b 537 Thread::wait(1000);
SomeRandomBloke 0:9d29b886d41b 538 }
SomeRandomBloke 0:9d29b886d41b 539 }