SMS message display on LED Matrix board with printer option

Dependencies:   AdafruitThermalPrinter HT1632_LedMatrix VodafoneUSBModem mbed-rtos mbed

Committer:
SomeRandomBloke
Date:
Mon Feb 25 22:22:20 2013 +0000
Revision:
2:787cbb491f8f
Parent:
1:243371cb92c8
Child:
3:59038ad536ac
Latest updates with link status message

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 0:9d29b886d41b 63 // Cound try to get own number usine USSD request
SomeRandomBloke 1:243371cb92c8 64 #define INFO_MSG " Send a message to "
SomeRandomBloke 1:243371cb92c8 65 //#define INFO_MSG " Send a message to 07765946942 "
SomeRandomBloke 0:9d29b886d41b 66 #endif
SomeRandomBloke 0:9d29b886d41b 67
SomeRandomBloke 0:9d29b886d41b 68 // Vodafone USSD commands
SomeRandomBloke 0:9d29b886d41b 69 #define USSD_COMMAND_OWN_NUMBER "*#100#"
SomeRandomBloke 0:9d29b886d41b 70 #define USSD_COMMAND_BALANCE "*#134#"
SomeRandomBloke 0:9d29b886d41b 71 #define USSD_COMMAND_TIME "*#103#"
SomeRandomBloke 0:9d29b886d41b 72
SomeRandomBloke 0:9d29b886d41b 73
SomeRandomBloke 0:9d29b886d41b 74 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 75 Serial debug_pc(USBTX, USBRX); // tx, rx
SomeRandomBloke 0:9d29b886d41b 76 #endif
SomeRandomBloke 0:9d29b886d41b 77
SomeRandomBloke 0:9d29b886d41b 78 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 79 AdafruitThermal printer(p28,p27); // setup printer
SomeRandomBloke 0:9d29b886d41b 80 #endif
SomeRandomBloke 0:9d29b886d41b 81 // Define a maximum size for storage arrays, is 160 (SMS size) + a bit more.
SomeRandomBloke 0:9d29b886d41b 82 #define MAX_MSG_LENGTH 192
SomeRandomBloke 0:9d29b886d41b 83
SomeRandomBloke 0:9d29b886d41b 84 // Month list used in converting to integer for set_time
SomeRandomBloke 0:9d29b886d41b 85 char months[12][4] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
SomeRandomBloke 0:9d29b886d41b 86
SomeRandomBloke 0:9d29b886d41b 87 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 88
SomeRandomBloke 0:9d29b886d41b 89 #define LED_MAX_DISPLAY_X 4
SomeRandomBloke 0:9d29b886d41b 90 #define LED_MAX_DISPLAY_Y 1
SomeRandomBloke 0:9d29b886d41b 91
SomeRandomBloke 0:9d29b886d41b 92 // create object to control the LED Matrix
SomeRandomBloke 0:9d29b886d41b 93 HT1632_LedMatrix led = HT1632_LedMatrix();
SomeRandomBloke 0:9d29b886d41b 94 #define DISPDELAY 90
SomeRandomBloke 0:9d29b886d41b 95 #endif
SomeRandomBloke 0:9d29b886d41b 96
SomeRandomBloke 0:9d29b886d41b 97 // Message buffers. New message waiting to be displayed and current message being displayed
SomeRandomBloke 2:787cbb491f8f 98 #define MAX_NUM_MSGS 10
SomeRandomBloke 0:9d29b886d41b 99 static char cmdBuf[12];
SomeRandomBloke 0:9d29b886d41b 100 static char newMsgBuf[MAX_MSG_LENGTH];
SomeRandomBloke 2:787cbb491f8f 101 static char msgBuf[MAX_NUM_MSGS][MAX_MSG_LENGTH];
SomeRandomBloke 2:787cbb491f8f 102 static int currentMsg = 0;
SomeRandomBloke 2:787cbb491f8f 103 static int numberOfMsgs = 0;
SomeRandomBloke 0:9d29b886d41b 104
SomeRandomBloke 0:9d29b886d41b 105 #ifdef USE_LED
SomeRandomBloke 1:243371cb92c8 106 static char ownNumber[20];
SomeRandomBloke 1:243371cb92c8 107
SomeRandomBloke 0:9d29b886d41b 108 int crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 109 int msgx = 1; // position on message screen of current character, set to 1 to allow for first scroll
SomeRandomBloke 0:9d29b886d41b 110 bool resetMessage = false;
SomeRandomBloke 0:9d29b886d41b 111
SomeRandomBloke 0:9d29b886d41b 112 void matrixDemo( void );
SomeRandomBloke 0:9d29b886d41b 113 #endif
SomeRandomBloke 0:9d29b886d41b 114
SomeRandomBloke 0:9d29b886d41b 115 // Define the onboard LEDs to use as status indicators
SomeRandomBloke 0:9d29b886d41b 116 DigitalOut led1(LED1); // Activity
SomeRandomBloke 0:9d29b886d41b 117 DigitalOut led2(LED2); // Activity, alternates with led2
SomeRandomBloke 0:9d29b886d41b 118 DigitalOut led3(LED3); // SMS received, turns off after processed
SomeRandomBloke 2:787cbb491f8f 119 DigitalOut led4(LED4); // USSD request in progress
SomeRandomBloke 0:9d29b886d41b 120
SomeRandomBloke 0:9d29b886d41b 121 int threadRestartCount = 0;
SomeRandomBloke 0:9d29b886d41b 122
SomeRandomBloke 0:9d29b886d41b 123 // Convert string buffer to upper case, is destructive. Buffer will be changed.
SomeRandomBloke 0:9d29b886d41b 124 char* stoupper( char* s )
SomeRandomBloke 0:9d29b886d41b 125 {
SomeRandomBloke 0:9d29b886d41b 126 char* p = s;
SomeRandomBloke 0:9d29b886d41b 127 while (*p = toupper( *p )) p++;
SomeRandomBloke 0:9d29b886d41b 128 return s;
SomeRandomBloke 0:9d29b886d41b 129 }
SomeRandomBloke 0:9d29b886d41b 130
SomeRandomBloke 0:9d29b886d41b 131
SomeRandomBloke 0:9d29b886d41b 132 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 133 void timestampMessage( char *msg )
SomeRandomBloke 0:9d29b886d41b 134 {
SomeRandomBloke 0:9d29b886d41b 135 char timebuf[50];
SomeRandomBloke 0:9d29b886d41b 136 // Print date/time, then message
SomeRandomBloke 0:9d29b886d41b 137 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 138 strftime(timebuf, 50, "%d/%m/%Y %X\n", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 139 printer.print(timebuf);
SomeRandomBloke 0:9d29b886d41b 140 printer.print(msg);
SomeRandomBloke 0:9d29b886d41b 141 // linefeed a couple of times
SomeRandomBloke 0:9d29b886d41b 142 printer.feed(3);
SomeRandomBloke 0:9d29b886d41b 143 }
SomeRandomBloke 0:9d29b886d41b 144 #endif
SomeRandomBloke 0:9d29b886d41b 145
SomeRandomBloke 0:9d29b886d41b 146 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 147 // Load a new message
SomeRandomBloke 2:787cbb491f8f 148 void addNewMessage( char *newMsgStart )
SomeRandomBloke 0:9d29b886d41b 149 {
SomeRandomBloke 0:9d29b886d41b 150 strncpy( newMsgBuf, newMsgStart, MAX_MSG_LENGTH );
SomeRandomBloke 0:9d29b886d41b 151 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 152 }
SomeRandomBloke 2:787cbb491f8f 153
SomeRandomBloke 2:787cbb491f8f 154
SomeRandomBloke 2:787cbb491f8f 155 void resetMessageBuffers( )
SomeRandomBloke 2:787cbb491f8f 156 {
SomeRandomBloke 2:787cbb491f8f 157 for( int i=0; i<MAX_NUM_MSGS; i++ ) {
SomeRandomBloke 2:787cbb491f8f 158 strcpy( &msgBuf[i][0], " " );
SomeRandomBloke 2:787cbb491f8f 159 }
SomeRandomBloke 2:787cbb491f8f 160 currentMsg = 0;
SomeRandomBloke 2:787cbb491f8f 161 numberOfMsgs = 0;
SomeRandomBloke 2:787cbb491f8f 162 }
SomeRandomBloke 2:787cbb491f8f 163
SomeRandomBloke 2:787cbb491f8f 164 void checkAndSetNextMessage()
SomeRandomBloke 2:787cbb491f8f 165 {
SomeRandomBloke 2:787cbb491f8f 166 numberOfMsgs++;
SomeRandomBloke 2:787cbb491f8f 167
SomeRandomBloke 2:787cbb491f8f 168 }
SomeRandomBloke 2:787cbb491f8f 169
SomeRandomBloke 0:9d29b886d41b 170 #endif
SomeRandomBloke 0:9d29b886d41b 171
SomeRandomBloke 0:9d29b886d41b 172 void sendUSSDCommand( VodafoneUSBModem *_modem, char *ussdCommand, bool setScrolling )
SomeRandomBloke 0:9d29b886d41b 173 {
SomeRandomBloke 0:9d29b886d41b 174 led4 = 1;
SomeRandomBloke 0:9d29b886d41b 175 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 176 debug_pc.printf("Sending %s on USSD channel\n", ussdCommand);
SomeRandomBloke 0:9d29b886d41b 177 #endif
SomeRandomBloke 0:9d29b886d41b 178 int ret = _modem->sendUSSD(ussdCommand, newMsgBuf, MAX_MSG_LENGTH);
SomeRandomBloke 0:9d29b886d41b 179 // Check for correct response
SomeRandomBloke 0:9d29b886d41b 180 if(ret) {
SomeRandomBloke 0:9d29b886d41b 181 // Non 0 value indicates an error??
SomeRandomBloke 0:9d29b886d41b 182 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 183 debug_pc.printf("Send USSD command returned %d\n", ret);
SomeRandomBloke 0:9d29b886d41b 184 #endif
SomeRandomBloke 0:9d29b886d41b 185 led4 = 0;
SomeRandomBloke 0:9d29b886d41b 186 return;
SomeRandomBloke 0:9d29b886d41b 187 }
SomeRandomBloke 0:9d29b886d41b 188
SomeRandomBloke 0:9d29b886d41b 189 // Should only display message if one has been received.
SomeRandomBloke 0:9d29b886d41b 190 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 191 debug_pc.printf("Result of command: %s\n", newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 192 #endif
SomeRandomBloke 0:9d29b886d41b 193
SomeRandomBloke 0:9d29b886d41b 194 led4 = 0;
SomeRandomBloke 0:9d29b886d41b 195
SomeRandomBloke 0:9d29b886d41b 196 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 197 resetMessage = setScrolling;
SomeRandomBloke 0:9d29b886d41b 198 #endif
SomeRandomBloke 0:9d29b886d41b 199 }
SomeRandomBloke 0:9d29b886d41b 200
SomeRandomBloke 0:9d29b886d41b 201 void setTime(VodafoneUSBModem *_modem )
SomeRandomBloke 0:9d29b886d41b 202 {
SomeRandomBloke 0:9d29b886d41b 203 char numBuf[10];
SomeRandomBloke 0:9d29b886d41b 204 // Set RTC using received message, format is DD-MMM-YYYY HH:MM
SomeRandomBloke 0:9d29b886d41b 205 // Month is in text name, e.g. NOV, time is using 24 hour clock.
SomeRandomBloke 0:9d29b886d41b 206 struct tm t;
SomeRandomBloke 0:9d29b886d41b 207 int retryCount = 3;
SomeRandomBloke 0:9d29b886d41b 208
SomeRandomBloke 0:9d29b886d41b 209 while( retryCount ) {
SomeRandomBloke 0:9d29b886d41b 210 sendUSSDCommand( _modem, USSD_COMMAND_TIME, false );
SomeRandomBloke 0:9d29b886d41b 211
SomeRandomBloke 0:9d29b886d41b 212 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 213 debug_pc.printf("Time received is %s\n", newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 214 #endif
SomeRandomBloke 0:9d29b886d41b 215
SomeRandomBloke 0:9d29b886d41b 216 t.tm_sec = 0; // 0-59
SomeRandomBloke 0:9d29b886d41b 217 strncpy(numBuf, &newMsgBuf[15], 2 );
SomeRandomBloke 0:9d29b886d41b 218 t.tm_min = atoi(numBuf); // 0-59
SomeRandomBloke 0:9d29b886d41b 219 strncpy(numBuf, &newMsgBuf[12], 2 );
SomeRandomBloke 0:9d29b886d41b 220 t.tm_hour = atoi(numBuf); // 0-23
SomeRandomBloke 0:9d29b886d41b 221 strncpy(numBuf, &newMsgBuf[0], 2 );
SomeRandomBloke 0:9d29b886d41b 222 t.tm_mday = atoi(numBuf); // 1-31
SomeRandomBloke 0:9d29b886d41b 223 strncpy(numBuf, &newMsgBuf[3], 3 );
SomeRandomBloke 0:9d29b886d41b 224 t.tm_mon = 0; // 0-11
SomeRandomBloke 0:9d29b886d41b 225 for( int i=0; i<12; i++ ) {
SomeRandomBloke 0:9d29b886d41b 226 if( strncmp( months[i], numBuf, 3 ) == 0 ) {
SomeRandomBloke 0:9d29b886d41b 227 t.tm_mon = i; // 0-11
SomeRandomBloke 0:9d29b886d41b 228 break;
SomeRandomBloke 0:9d29b886d41b 229 }
SomeRandomBloke 0:9d29b886d41b 230 }
SomeRandomBloke 0:9d29b886d41b 231 strncpy(numBuf, &newMsgBuf[9], 2 );
SomeRandomBloke 0:9d29b886d41b 232 t.tm_year = 100 + atoi( numBuf ); // year since 1900
SomeRandomBloke 0:9d29b886d41b 233
SomeRandomBloke 0:9d29b886d41b 234 if( t.tm_year >110 ) {
SomeRandomBloke 0:9d29b886d41b 235 // convert to timestamp and display
SomeRandomBloke 0:9d29b886d41b 236 time_t seconds = mktime(&t);
SomeRandomBloke 0:9d29b886d41b 237 set_time( seconds );
SomeRandomBloke 0:9d29b886d41b 238 retryCount = 0; // No more retries, terminate while
SomeRandomBloke 0:9d29b886d41b 239 } else {
SomeRandomBloke 0:9d29b886d41b 240 // Failed to set time, decrement tries
SomeRandomBloke 0:9d29b886d41b 241 retryCount--;
SomeRandomBloke 0:9d29b886d41b 242 }
SomeRandomBloke 0:9d29b886d41b 243 }
SomeRandomBloke 0:9d29b886d41b 244 }
SomeRandomBloke 0:9d29b886d41b 245
SomeRandomBloke 1:243371cb92c8 246 #ifdef USE_LED
SomeRandomBloke 1:243371cb92c8 247 void getOwnNumber(VodafoneUSBModem *_modem, char *numPtr )
SomeRandomBloke 1:243371cb92c8 248 {
SomeRandomBloke 1:243371cb92c8 249 int retryCount = 3;
SomeRandomBloke 1:243371cb92c8 250
SomeRandomBloke 1:243371cb92c8 251 while( retryCount ) {
SomeRandomBloke 1:243371cb92c8 252 sendUSSDCommand( _modem, USSD_COMMAND_OWN_NUMBER, false );
SomeRandomBloke 1:243371cb92c8 253
SomeRandomBloke 1:243371cb92c8 254 #ifdef DEBUG
SomeRandomBloke 1:243371cb92c8 255 debug_pc.printf("Own Number received %s\n", newMsgBuf);
SomeRandomBloke 1:243371cb92c8 256 #endif
SomeRandomBloke 2:787cbb491f8f 257 if( strlen(newMsgBuf) > 0 ) {
SomeRandomBloke 2:787cbb491f8f 258 // Save number in array pointed to be numPtr
SomeRandomBloke 2:787cbb491f8f 259 strncpy(ownNumber, newMsgBuf, strlen(newMsgBuf) );
SomeRandomBloke 2:787cbb491f8f 260 retryCount = 0;
SomeRandomBloke 2:787cbb491f8f 261 } else
SomeRandomBloke 1:243371cb92c8 262 retryCount--;
SomeRandomBloke 1:243371cb92c8 263 }
SomeRandomBloke 1:243371cb92c8 264 }
SomeRandomBloke 1:243371cb92c8 265 #endif
SomeRandomBloke 1:243371cb92c8 266
SomeRandomBloke 2:787cbb491f8f 267 char linkStateStr[6][13] = { "Unknown ", "Registering ", "Denied ", "No Signal ", "Home Network", "Roaming " };
SomeRandomBloke 2:787cbb491f8f 268 char bearerStr[6][5] = {"Unkn", "GSM ", "EDGE", "3G ", "HSPA", "LTE " };
SomeRandomBloke 0:9d29b886d41b 269
SomeRandomBloke 0:9d29b886d41b 270 void receiveSMS(void const*)
SomeRandomBloke 0:9d29b886d41b 271 {
SomeRandomBloke 0:9d29b886d41b 272 VodafoneUSBModem modem;
SomeRandomBloke 0:9d29b886d41b 273 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 274
SomeRandomBloke 0:9d29b886d41b 275 threadRestartCount++;
SomeRandomBloke 0:9d29b886d41b 276
SomeRandomBloke 2:787cbb491f8f 277 int pRssi = 0;
SomeRandomBloke 2:787cbb491f8f 278 // addNewMessage( "Starting" );
SomeRandomBloke 0:9d29b886d41b 279
SomeRandomBloke 2:787cbb491f8f 280 LinkMonitor::REGISTRATION_STATE pRegistrationState;
SomeRandomBloke 2:787cbb491f8f 281 LinkMonitor::BEARER pBearer;
SomeRandomBloke 0:9d29b886d41b 282
SomeRandomBloke 2:787cbb491f8f 283 modem.getLinkState( &pRssi,&pRegistrationState, &pBearer);
SomeRandomBloke 2:787cbb491f8f 284 #ifdef DEBUG
SomeRandomBloke 2:787cbb491f8f 285 debug_pc.printf("Link state Rssi: %d, Registration state %x Bearer %x\n",pRssi,pRegistrationState, pBearer);
SomeRandomBloke 2:787cbb491f8f 286 #endif
SomeRandomBloke 2:787cbb491f8f 287 sprintf(newMsgBuf, "Link State: %s on %s ", linkStateStr[pRegistrationState], bearerStr[pBearer] );
SomeRandomBloke 2:787cbb491f8f 288 addNewMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 289
SomeRandomBloke 2:787cbb491f8f 290 Thread::wait(3000);
SomeRandomBloke 0:9d29b886d41b 291
SomeRandomBloke 1:243371cb92c8 292 #ifdef USE_LED
SomeRandomBloke 1:243371cb92c8 293 // Get own number fromt he network
SomeRandomBloke 1:243371cb92c8 294 getOwnNumber( &modem, ownNumber );
SomeRandomBloke 1:243371cb92c8 295 #endif
SomeRandomBloke 1:243371cb92c8 296
SomeRandomBloke 2:787cbb491f8f 297 // sprintf(msgBuf[&msgBuf[currentMsg][0], "Link state Rssi: %d, Registration state %x Bearer %x ",pRssi,pRegistrationState, pBearer);
SomeRandomBloke 0:9d29b886d41b 298 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 299 // Check if time already set, if not then get it. Use year = 0 as test
SomeRandomBloke 0:9d29b886d41b 300 struct tm *t = localtime(&seconds);
SomeRandomBloke 0:9d29b886d41b 301 if( t->tm_year == 0 ) {
SomeRandomBloke 0:9d29b886d41b 302 setTime( &modem );
SomeRandomBloke 0:9d29b886d41b 303 }
SomeRandomBloke 0:9d29b886d41b 304
SomeRandomBloke 0:9d29b886d41b 305 sprintf(newMsgBuf, "Thread Start %d\r\n", threadRestartCount );
SomeRandomBloke 0:9d29b886d41b 306 timestampMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 307 #endif
SomeRandomBloke 0:9d29b886d41b 308 #ifdef USE_LED
SomeRandomBloke 2:787cbb491f8f 309 //strcpy( &msgBuf[currentMsg][0], INFO_MSG );
SomeRandomBloke 1:243371cb92c8 310 sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 2:787cbb491f8f 311 addNewMessage( newMsgBuf );
SomeRandomBloke 2:787cbb491f8f 312
SomeRandomBloke 0:9d29b886d41b 313 led.displayOn();
SomeRandomBloke 0:9d29b886d41b 314 #endif
SomeRandomBloke 0:9d29b886d41b 315 char num[17];
SomeRandomBloke 0:9d29b886d41b 316 size_t smsCount;
SomeRandomBloke 0:9d29b886d41b 317
SomeRandomBloke 0:9d29b886d41b 318 while(true) {
SomeRandomBloke 0:9d29b886d41b 319 if( modem.getSMCount(&smsCount) ==OK ) {
SomeRandomBloke 0:9d29b886d41b 320 if( smsCount > 0) {
SomeRandomBloke 0:9d29b886d41b 321 led3 = 1;
SomeRandomBloke 0:9d29b886d41b 322 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 323 debug_pc.printf("%d SMS to read\n", smsCount);
SomeRandomBloke 0:9d29b886d41b 324 #endif
SomeRandomBloke 0:9d29b886d41b 325 if( modem.getSM(num, newMsgBuf, MAX_MSG_LENGTH) == OK ) {
SomeRandomBloke 0:9d29b886d41b 326
SomeRandomBloke 0:9d29b886d41b 327 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 328 debug_pc.printf("%s : %s\n", num, newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 329 #endif
SomeRandomBloke 0:9d29b886d41b 330 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 331 // Print date/time, then message
SomeRandomBloke 0:9d29b886d41b 332 timestampMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 333 #endif
SomeRandomBloke 0:9d29b886d41b 334 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 335 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 336 #endif
SomeRandomBloke 0:9d29b886d41b 337 strncpy( cmdBuf, newMsgBuf, 10 );
SomeRandomBloke 0:9d29b886d41b 338 stoupper( cmdBuf ); // This is a destructive function, original characters are uppercased
SomeRandomBloke 0:9d29b886d41b 339 if( strncmp( cmdBuf, "BALANCE", 7 ) == 0 ) {
SomeRandomBloke 0:9d29b886d41b 340 sendUSSDCommand( &modem, USSD_COMMAND_BALANCE, true );
SomeRandomBloke 0:9d29b886d41b 341 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 342 timestampMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 343 #endif
SomeRandomBloke 0:9d29b886d41b 344 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 345 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 346 } else if ( strncmp( cmdBuf, "INFO", 4 ) == 0 ) {
SomeRandomBloke 2:787cbb491f8f 347 // addNewMessage( INFO_MSG );
SomeRandomBloke 1:243371cb92c8 348 sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 2:787cbb491f8f 349 addNewMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 350 #endif
SomeRandomBloke 0:9d29b886d41b 351 // } else if ( strncmp( cmdBuf, "DEMO", 4 ) == 0 ) {
SomeRandomBloke 1:243371cb92c8 352 // matrixDemo();
SomeRandomBloke 1:243371cb92c8 353 // sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 2:787cbb491f8f 354 // addNewMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 355 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 356 } else if ( strncmp( cmdBuf, "CLEAR", 5 ) == 0 ) {
SomeRandomBloke 2:787cbb491f8f 357 addNewMessage( " " );
SomeRandomBloke 0:9d29b886d41b 358 #endif
SomeRandomBloke 0:9d29b886d41b 359 }
SomeRandomBloke 0:9d29b886d41b 360 }
SomeRandomBloke 0:9d29b886d41b 361 }
SomeRandomBloke 0:9d29b886d41b 362 }
SomeRandomBloke 0:9d29b886d41b 363 Thread::wait(3000);
SomeRandomBloke 0:9d29b886d41b 364 led3 = 0;
SomeRandomBloke 0:9d29b886d41b 365 }
SomeRandomBloke 0:9d29b886d41b 366 }
SomeRandomBloke 0:9d29b886d41b 367
SomeRandomBloke 0:9d29b886d41b 368 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 369 void displayScrollingLine(void const*)
SomeRandomBloke 0:9d29b886d41b 370 {
SomeRandomBloke 0:9d29b886d41b 371 int y,xmax,ymax;
SomeRandomBloke 0:9d29b886d41b 372 led.getXYMax(&xmax,&ymax);
SomeRandomBloke 0:9d29b886d41b 373
SomeRandomBloke 0:9d29b886d41b 374 while(true) {
SomeRandomBloke 0:9d29b886d41b 375 // shift the whole screen 6 times, one column at a time; making 1 character
SomeRandomBloke 2:787cbb491f8f 376 if( strlen( &msgBuf[currentMsg][0] ) > 10 ) {
SomeRandomBloke 0:9d29b886d41b 377 for (int x=0; x < 6; x++) {
SomeRandomBloke 0:9d29b886d41b 378 led.scrollLeft(1, 1);
SomeRandomBloke 0:9d29b886d41b 379 msgx--;
SomeRandomBloke 0:9d29b886d41b 380 // fit as much as we can on the available display space
SomeRandomBloke 0:9d29b886d41b 381
SomeRandomBloke 2:787cbb491f8f 382 while (!led.putChar(msgx,0,msgBuf[currentMsg][crtPos])) { // zero return if it all fitted
SomeRandomBloke 0:9d29b886d41b 383 led.getXY(&msgx,&y);
SomeRandomBloke 0:9d29b886d41b 384 crtPos++; // we got all of the character on!!
SomeRandomBloke 2:787cbb491f8f 385 if (crtPos >= strlen(&msgBuf[currentMsg][0])) {
SomeRandomBloke 0:9d29b886d41b 386 crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 387 }
SomeRandomBloke 0:9d29b886d41b 388 }
SomeRandomBloke 0:9d29b886d41b 389 led.putShadowRam();
SomeRandomBloke 0:9d29b886d41b 390 Thread::wait(DISPDELAY);
SomeRandomBloke 0:9d29b886d41b 391 }
SomeRandomBloke 0:9d29b886d41b 392 }
SomeRandomBloke 0:9d29b886d41b 393 // Look for a new message being available
SomeRandomBloke 0:9d29b886d41b 394 // TODO add minimum interval between message changes, e.g. 60s
SomeRandomBloke 0:9d29b886d41b 395 if( resetMessage ) {
SomeRandomBloke 0:9d29b886d41b 396 led.clear();
SomeRandomBloke 0:9d29b886d41b 397 crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 398 msgx = 1;
SomeRandomBloke 2:787cbb491f8f 399
SomeRandomBloke 2:787cbb491f8f 400 strncpy( &msgBuf[currentMsg][0], newMsgBuf, MAX_MSG_LENGTH );
SomeRandomBloke 2:787cbb491f8f 401 if( strlen( &msgBuf[currentMsg][0] ) > 10 ) {
SomeRandomBloke 2:787cbb491f8f 402 strcat( &msgBuf[currentMsg][0], " ");
SomeRandomBloke 0:9d29b886d41b 403 } else {
SomeRandomBloke 2:787cbb491f8f 404 led.putString(0,0, &msgBuf[currentMsg][0]);
SomeRandomBloke 0:9d29b886d41b 405 }
SomeRandomBloke 0:9d29b886d41b 406 resetMessage = false;
SomeRandomBloke 0:9d29b886d41b 407 }
SomeRandomBloke 0:9d29b886d41b 408 }
SomeRandomBloke 0:9d29b886d41b 409 }
SomeRandomBloke 0:9d29b886d41b 410
SomeRandomBloke 0:9d29b886d41b 411
SomeRandomBloke 0:9d29b886d41b 412 void matrixDemo( void )
SomeRandomBloke 0:9d29b886d41b 413 {
SomeRandomBloke 0:9d29b886d41b 414 int xmax,ymax;
SomeRandomBloke 0:9d29b886d41b 415 led.getXYMax(&xmax,&ymax);
SomeRandomBloke 0:9d29b886d41b 416 led.clear();
SomeRandomBloke 0:9d29b886d41b 417 for( int n=0; n<3; n++ ) {
SomeRandomBloke 0:9d29b886d41b 418 for( int i=0; i<8; i++ ) {
SomeRandomBloke 0:9d29b886d41b 419 led.drawRectangle(i,i,xmax-i,ymax-i, 1);
SomeRandomBloke 0:9d29b886d41b 420 wait(0.05);
SomeRandomBloke 0:9d29b886d41b 421 }
SomeRandomBloke 0:9d29b886d41b 422 for( int i=7; i>=0; i-- ) {
SomeRandomBloke 0:9d29b886d41b 423 led.drawRectangle(i,i,xmax-i,ymax-i, 0);
SomeRandomBloke 0:9d29b886d41b 424 wait(0.05);
SomeRandomBloke 0:9d29b886d41b 425 }
SomeRandomBloke 0:9d29b886d41b 426 }
SomeRandomBloke 0:9d29b886d41b 427 led.clear();
SomeRandomBloke 0:9d29b886d41b 428 for( int n=0; n<3; n++ ) {
SomeRandomBloke 0:9d29b886d41b 429 for(int i=0; i<(xmax/2); i++) {
SomeRandomBloke 0:9d29b886d41b 430 led.drawCircle((xmax/2), 7, i, 1 );
SomeRandomBloke 0:9d29b886d41b 431 wait( 0.05 );
SomeRandomBloke 0:9d29b886d41b 432 }
SomeRandomBloke 0:9d29b886d41b 433 for(int i=(xmax/2); i>=0; i--) {
SomeRandomBloke 0:9d29b886d41b 434 led.drawCircle((xmax/2), 7, i, 0 );
SomeRandomBloke 0:9d29b886d41b 435 wait( 0.05 );
SomeRandomBloke 0:9d29b886d41b 436 }
SomeRandomBloke 0:9d29b886d41b 437 }
SomeRandomBloke 0:9d29b886d41b 438 wait(2);
SomeRandomBloke 0:9d29b886d41b 439
SomeRandomBloke 0:9d29b886d41b 440 led.clear();
SomeRandomBloke 2:787cbb491f8f 441 led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y);
SomeRandomBloke 0:9d29b886d41b 442 }
SomeRandomBloke 0:9d29b886d41b 443
SomeRandomBloke 0:9d29b886d41b 444
SomeRandomBloke 0:9d29b886d41b 445 void showTime()
SomeRandomBloke 0:9d29b886d41b 446 {
SomeRandomBloke 0:9d29b886d41b 447 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 448 char timebuf[20];
SomeRandomBloke 0:9d29b886d41b 449 strftime(timebuf, 20, "%X ", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 450 led.putString(12,8, timebuf );
SomeRandomBloke 0:9d29b886d41b 451 }
SomeRandomBloke 0:9d29b886d41b 452
SomeRandomBloke 0:9d29b886d41b 453 void showDate()
SomeRandomBloke 0:9d29b886d41b 454 {
SomeRandomBloke 0:9d29b886d41b 455 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 456 char timebuf[20];
SomeRandomBloke 0:9d29b886d41b 457 strftime(timebuf, 20, "%d/%m/%Y ", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 458 led.putString(4,8, timebuf );
SomeRandomBloke 0:9d29b886d41b 459 }
SomeRandomBloke 0:9d29b886d41b 460 #endif
SomeRandomBloke 0:9d29b886d41b 461
SomeRandomBloke 0:9d29b886d41b 462
SomeRandomBloke 0:9d29b886d41b 463 int main()
SomeRandomBloke 0:9d29b886d41b 464 {
SomeRandomBloke 0:9d29b886d41b 465 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 466 debug_pc.baud(57600);
SomeRandomBloke 0:9d29b886d41b 467 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 468 debug_pc.printf("SMS to LED Matrix display\n");
SomeRandomBloke 0:9d29b886d41b 469 #endif
SomeRandomBloke 0:9d29b886d41b 470 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 471 printer.begin();
SomeRandomBloke 0:9d29b886d41b 472 debug_pc.printf("SMS To Printer\n");
SomeRandomBloke 0:9d29b886d41b 473 timestampMessage("SMS To Printer Starting");
SomeRandomBloke 0:9d29b886d41b 474 #endif
SomeRandomBloke 0:9d29b886d41b 475 #endif
SomeRandomBloke 0:9d29b886d41b 476 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 477 int displayCount = 10;
SomeRandomBloke 0:9d29b886d41b 478 bool displayTime = true;
SomeRandomBloke 0:9d29b886d41b 479
SomeRandomBloke 2:787cbb491f8f 480 led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y); // Use all displays as 128x8 display
SomeRandomBloke 0:9d29b886d41b 481 led.clear();
SomeRandomBloke 0:9d29b886d41b 482 led.setBrightness(2);
SomeRandomBloke 0:9d29b886d41b 483 //led.displayOff(); // Turn off display for now until receiver tast has started
SomeRandomBloke 0:9d29b886d41b 484 bool twoLineDisplay = (LED_MAX_DISPLAY_X > 1);
SomeRandomBloke 0:9d29b886d41b 485 led.putString( 0, 0, "SMS to LED Display" );
SomeRandomBloke 0:9d29b886d41b 486
SomeRandomBloke 0:9d29b886d41b 487 Thread::wait(3000); // Wait for display to initialise after power up
SomeRandomBloke 0:9d29b886d41b 488 #endif
SomeRandomBloke 0:9d29b886d41b 489
SomeRandomBloke 0:9d29b886d41b 490 // Set initial blank message
SomeRandomBloke 2:787cbb491f8f 491 resetMessageBuffers();
SomeRandomBloke 0:9d29b886d41b 492
SomeRandomBloke 0:9d29b886d41b 493 Thread receiveTask(receiveSMS, NULL, osPriorityNormal, 1024 * 8); // try 6 next
SomeRandomBloke 0:9d29b886d41b 494 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 495 Thread displayTask(displayScrollingLine, NULL, osPriorityNormal, 1024 * 4);
SomeRandomBloke 0:9d29b886d41b 496 #endif
SomeRandomBloke 0:9d29b886d41b 497
SomeRandomBloke 0:9d29b886d41b 498 // Show we are still working by alternitively flashing LED1 adn LED2, once a second
SomeRandomBloke 0:9d29b886d41b 499 led1 = 0; // Working
SomeRandomBloke 0:9d29b886d41b 500 led2 = 1; // Alternate with led1
SomeRandomBloke 0:9d29b886d41b 501 led3 = 0; // Received and processign SMS
SomeRandomBloke 0:9d29b886d41b 502 led4 = 0; // Processing USSD message queue
SomeRandomBloke 0:9d29b886d41b 503 while(1) {
SomeRandomBloke 0:9d29b886d41b 504 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 505 // Only display date/time is we have a 2 row display
SomeRandomBloke 0:9d29b886d41b 506 if( twoLineDisplay ) {
SomeRandomBloke 0:9d29b886d41b 507 if( --displayCount <= 0 ) {
SomeRandomBloke 0:9d29b886d41b 508 displayTime = !displayTime;
SomeRandomBloke 0:9d29b886d41b 509 displayCount = 10;
SomeRandomBloke 0:9d29b886d41b 510 led.putString(0,8, " " );
SomeRandomBloke 0:9d29b886d41b 511 }
SomeRandomBloke 0:9d29b886d41b 512 if( displayTime ) {
SomeRandomBloke 0:9d29b886d41b 513 showTime();
SomeRandomBloke 0:9d29b886d41b 514 } else {
SomeRandomBloke 0:9d29b886d41b 515 showDate();
SomeRandomBloke 0:9d29b886d41b 516 }
SomeRandomBloke 0:9d29b886d41b 517 }
SomeRandomBloke 0:9d29b886d41b 518 #endif
SomeRandomBloke 0:9d29b886d41b 519 led1=!led1;
SomeRandomBloke 0:9d29b886d41b 520 led2=!led2;
SomeRandomBloke 0:9d29b886d41b 521 Thread::wait(1000);
SomeRandomBloke 0:9d29b886d41b 522 }
SomeRandomBloke 0:9d29b886d41b 523
SomeRandomBloke 2:787cbb491f8f 524 //return 0;
SomeRandomBloke 0:9d29b886d41b 525 }