SMS message display on LED Matrix board with printer option

Dependencies:   AdafruitThermalPrinter HT1632_LedMatrix VodafoneUSBModem mbed-rtos mbed

Committer:
SomeRandomBloke
Date:
Mon Apr 08 20:55:20 2013 +0000
Revision:
6:323d7422ca8b
Parent:
5:a2c0e95f9a0b
Child:
7:b46d5d98434a
Increased brightness

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 4:a364da55b42e 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 4:a364da55b42e 99 static char msgBuf[MAX_MSG_LENGTH];
SomeRandomBloke 4:a364da55b42e 100 //static int currentMsg = 0;
SomeRandomBloke 4:a364da55b42e 101 //static int numberOfMsgs = 0;
SomeRandomBloke 4:a364da55b42e 102 static bool 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 4:a364da55b42e 147
SomeRandomBloke 4:a364da55b42e 148 void setNewMessage( 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 4:a364da55b42e 157 strcpy( msgBuf, " " );
SomeRandomBloke 4:a364da55b42e 158 newMsgBuf[0] = '\0';
SomeRandomBloke 2:787cbb491f8f 159 }
SomeRandomBloke 0:9d29b886d41b 160 #endif
SomeRandomBloke 0:9d29b886d41b 161
SomeRandomBloke 0:9d29b886d41b 162 void sendUSSDCommand( VodafoneUSBModem *_modem, char *ussdCommand, bool setScrolling )
SomeRandomBloke 0:9d29b886d41b 163 {
SomeRandomBloke 0:9d29b886d41b 164 led4 = 1;
SomeRandomBloke 0:9d29b886d41b 165 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 166 debug_pc.printf("Sending %s on USSD channel\n", ussdCommand);
SomeRandomBloke 0:9d29b886d41b 167 #endif
SomeRandomBloke 0:9d29b886d41b 168 int ret = _modem->sendUSSD(ussdCommand, newMsgBuf, MAX_MSG_LENGTH);
SomeRandomBloke 0:9d29b886d41b 169 // Check for correct response
SomeRandomBloke 0:9d29b886d41b 170 if(ret) {
SomeRandomBloke 0:9d29b886d41b 171 // Non 0 value indicates an error??
SomeRandomBloke 0:9d29b886d41b 172 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 173 debug_pc.printf("Send USSD command returned %d\n", ret);
SomeRandomBloke 0:9d29b886d41b 174 #endif
SomeRandomBloke 0:9d29b886d41b 175 led4 = 0;
SomeRandomBloke 0:9d29b886d41b 176 return;
SomeRandomBloke 0:9d29b886d41b 177 }
SomeRandomBloke 0:9d29b886d41b 178
SomeRandomBloke 0:9d29b886d41b 179 // Should only display message if one has been received.
SomeRandomBloke 0:9d29b886d41b 180 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 181 debug_pc.printf("Result of command: %s\n", newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 182 #endif
SomeRandomBloke 0:9d29b886d41b 183
SomeRandomBloke 0:9d29b886d41b 184 led4 = 0;
SomeRandomBloke 0:9d29b886d41b 185
SomeRandomBloke 0:9d29b886d41b 186 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 187 resetMessage = setScrolling;
SomeRandomBloke 0:9d29b886d41b 188 #endif
SomeRandomBloke 0:9d29b886d41b 189 }
SomeRandomBloke 0:9d29b886d41b 190
SomeRandomBloke 0:9d29b886d41b 191 void setTime(VodafoneUSBModem *_modem )
SomeRandomBloke 0:9d29b886d41b 192 {
SomeRandomBloke 0:9d29b886d41b 193 char numBuf[10];
SomeRandomBloke 0:9d29b886d41b 194 // Set RTC using received message, format is DD-MMM-YYYY HH:MM
SomeRandomBloke 0:9d29b886d41b 195 // Month is in text name, e.g. NOV, time is using 24 hour clock.
SomeRandomBloke 0:9d29b886d41b 196 struct tm t;
SomeRandomBloke 0:9d29b886d41b 197 int retryCount = 3;
SomeRandomBloke 0:9d29b886d41b 198
SomeRandomBloke 0:9d29b886d41b 199 while( retryCount ) {
SomeRandomBloke 0:9d29b886d41b 200 sendUSSDCommand( _modem, USSD_COMMAND_TIME, false );
SomeRandomBloke 0:9d29b886d41b 201
SomeRandomBloke 0:9d29b886d41b 202 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 203 debug_pc.printf("Time received is %s\n", newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 204 #endif
SomeRandomBloke 0:9d29b886d41b 205
SomeRandomBloke 0:9d29b886d41b 206 t.tm_sec = 0; // 0-59
SomeRandomBloke 0:9d29b886d41b 207 strncpy(numBuf, &newMsgBuf[15], 2 );
SomeRandomBloke 0:9d29b886d41b 208 t.tm_min = atoi(numBuf); // 0-59
SomeRandomBloke 0:9d29b886d41b 209 strncpy(numBuf, &newMsgBuf[12], 2 );
SomeRandomBloke 0:9d29b886d41b 210 t.tm_hour = atoi(numBuf); // 0-23
SomeRandomBloke 0:9d29b886d41b 211 strncpy(numBuf, &newMsgBuf[0], 2 );
SomeRandomBloke 0:9d29b886d41b 212 t.tm_mday = atoi(numBuf); // 1-31
SomeRandomBloke 0:9d29b886d41b 213 strncpy(numBuf, &newMsgBuf[3], 3 );
SomeRandomBloke 0:9d29b886d41b 214 t.tm_mon = 0; // 0-11
SomeRandomBloke 0:9d29b886d41b 215 for( int i=0; i<12; i++ ) {
SomeRandomBloke 0:9d29b886d41b 216 if( strncmp( months[i], numBuf, 3 ) == 0 ) {
SomeRandomBloke 0:9d29b886d41b 217 t.tm_mon = i; // 0-11
SomeRandomBloke 0:9d29b886d41b 218 break;
SomeRandomBloke 0:9d29b886d41b 219 }
SomeRandomBloke 0:9d29b886d41b 220 }
SomeRandomBloke 0:9d29b886d41b 221 strncpy(numBuf, &newMsgBuf[9], 2 );
SomeRandomBloke 0:9d29b886d41b 222 t.tm_year = 100 + atoi( numBuf ); // year since 1900
SomeRandomBloke 0:9d29b886d41b 223
SomeRandomBloke 0:9d29b886d41b 224 if( t.tm_year >110 ) {
SomeRandomBloke 0:9d29b886d41b 225 // convert to timestamp and display
SomeRandomBloke 0:9d29b886d41b 226 time_t seconds = mktime(&t);
SomeRandomBloke 0:9d29b886d41b 227 set_time( seconds );
SomeRandomBloke 0:9d29b886d41b 228 retryCount = 0; // No more retries, terminate while
SomeRandomBloke 0:9d29b886d41b 229 } else {
SomeRandomBloke 0:9d29b886d41b 230 // Failed to set time, decrement tries
SomeRandomBloke 0:9d29b886d41b 231 retryCount--;
SomeRandomBloke 0:9d29b886d41b 232 }
SomeRandomBloke 0:9d29b886d41b 233 }
SomeRandomBloke 0:9d29b886d41b 234 }
SomeRandomBloke 0:9d29b886d41b 235
SomeRandomBloke 1:243371cb92c8 236 void getOwnNumber(VodafoneUSBModem *_modem, char *numPtr )
SomeRandomBloke 1:243371cb92c8 237 {
SomeRandomBloke 1:243371cb92c8 238 int retryCount = 3;
SomeRandomBloke 1:243371cb92c8 239
SomeRandomBloke 1:243371cb92c8 240 while( retryCount ) {
SomeRandomBloke 1:243371cb92c8 241 sendUSSDCommand( _modem, USSD_COMMAND_OWN_NUMBER, false );
SomeRandomBloke 1:243371cb92c8 242
SomeRandomBloke 1:243371cb92c8 243 #ifdef DEBUG
SomeRandomBloke 1:243371cb92c8 244 debug_pc.printf("Own Number received %s\n", newMsgBuf);
SomeRandomBloke 1:243371cb92c8 245 #endif
SomeRandomBloke 2:787cbb491f8f 246 if( strlen(newMsgBuf) > 0 ) {
SomeRandomBloke 2:787cbb491f8f 247 // Save number in array pointed to be numPtr
SomeRandomBloke 2:787cbb491f8f 248 strncpy(ownNumber, newMsgBuf, strlen(newMsgBuf) );
SomeRandomBloke 2:787cbb491f8f 249 retryCount = 0;
SomeRandomBloke 2:787cbb491f8f 250 } else
SomeRandomBloke 1:243371cb92c8 251 retryCount--;
SomeRandomBloke 1:243371cb92c8 252 }
SomeRandomBloke 1:243371cb92c8 253 }
SomeRandomBloke 3:59038ad536ac 254
SomeRandomBloke 1:243371cb92c8 255
SomeRandomBloke 2:787cbb491f8f 256 char linkStateStr[6][13] = { "Unknown ", "Registering ", "Denied ", "No Signal ", "Home Network", "Roaming " };
SomeRandomBloke 2:787cbb491f8f 257 char bearerStr[6][5] = {"Unkn", "GSM ", "EDGE", "3G ", "HSPA", "LTE " };
SomeRandomBloke 0:9d29b886d41b 258
SomeRandomBloke 0:9d29b886d41b 259 void receiveSMS(void const*)
SomeRandomBloke 0:9d29b886d41b 260 {
SomeRandomBloke 0:9d29b886d41b 261 VodafoneUSBModem modem;
SomeRandomBloke 0:9d29b886d41b 262 time_t seconds = time(NULL);
SomeRandomBloke 3:59038ad536ac 263 int pRssi = 0;
SomeRandomBloke 3:59038ad536ac 264 LinkMonitor::REGISTRATION_STATE pRegistrationState;
SomeRandomBloke 3:59038ad536ac 265 LinkMonitor::BEARER pBearer;
SomeRandomBloke 0:9d29b886d41b 266
SomeRandomBloke 0:9d29b886d41b 267 threadRestartCount++;
SomeRandomBloke 0:9d29b886d41b 268
SomeRandomBloke 3:59038ad536ac 269 // Getting the link state seems to speed up startup of the library
SomeRandomBloke 2:787cbb491f8f 270 modem.getLinkState( &pRssi,&pRegistrationState, &pBearer);
SomeRandomBloke 2:787cbb491f8f 271 #ifdef DEBUG
SomeRandomBloke 2:787cbb491f8f 272 debug_pc.printf("Link state Rssi: %d, Registration state %x Bearer %x\n",pRssi,pRegistrationState, pBearer);
SomeRandomBloke 2:787cbb491f8f 273 #endif
SomeRandomBloke 2:787cbb491f8f 274 sprintf(newMsgBuf, "Link State: %s on %s ", linkStateStr[pRegistrationState], bearerStr[pBearer] );
SomeRandomBloke 3:59038ad536ac 275
SomeRandomBloke 3:59038ad536ac 276 #ifdef USE_LED
SomeRandomBloke 4:a364da55b42e 277 setNewMessage( newMsgBuf );
SomeRandomBloke 3:59038ad536ac 278 #endif
SomeRandomBloke 3:59038ad536ac 279 #ifdef USE_PRINTER
SomeRandomBloke 3:59038ad536ac 280 timestampMessage( newMsgBuf );
SomeRandomBloke 3:59038ad536ac 281 #endif
SomeRandomBloke 0:9d29b886d41b 282
SomeRandomBloke 2:787cbb491f8f 283 Thread::wait(3000);
SomeRandomBloke 0:9d29b886d41b 284
SomeRandomBloke 3:59038ad536ac 285 // Get own number from the network
SomeRandomBloke 3:59038ad536ac 286 getOwnNumber( &modem, ownNumber );
SomeRandomBloke 1:243371cb92c8 287 #ifdef USE_LED
SomeRandomBloke 3:59038ad536ac 288 sprintf(newMsgBuf, "Own number %s", ownNumber );
SomeRandomBloke 4:a364da55b42e 289 setNewMessage( newMsgBuf );
SomeRandomBloke 1:243371cb92c8 290 #endif
SomeRandomBloke 1:243371cb92c8 291
SomeRandomBloke 0:9d29b886d41b 292 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 293 // Check if time already set, if not then get it. Use year = 0 as test
SomeRandomBloke 0:9d29b886d41b 294 struct tm *t = localtime(&seconds);
SomeRandomBloke 3:59038ad536ac 295 if( t->tm_year == 0 )
SomeRandomBloke 0:9d29b886d41b 296 setTime( &modem );
SomeRandomBloke 0:9d29b886d41b 297
SomeRandomBloke 0:9d29b886d41b 298 sprintf(newMsgBuf, "Thread Start %d\r\n", threadRestartCount );
SomeRandomBloke 0:9d29b886d41b 299 timestampMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 300 #endif
SomeRandomBloke 0:9d29b886d41b 301 #ifdef USE_LED
SomeRandomBloke 2:787cbb491f8f 302 //strcpy( &msgBuf[currentMsg][0], INFO_MSG );
SomeRandomBloke 1:243371cb92c8 303 sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 4:a364da55b42e 304 setNewMessage( newMsgBuf );
SomeRandomBloke 2:787cbb491f8f 305
SomeRandomBloke 0:9d29b886d41b 306 led.displayOn();
SomeRandomBloke 0:9d29b886d41b 307 #endif
SomeRandomBloke 0:9d29b886d41b 308 char num[17];
SomeRandomBloke 3:59038ad536ac 309 size_t smsCount;
SomeRandomBloke 3:59038ad536ac 310 getNextMessage = true;
SomeRandomBloke 0:9d29b886d41b 311
SomeRandomBloke 0:9d29b886d41b 312 while(true) {
SomeRandomBloke 3:59038ad536ac 313 if( modem.getSMCount(&smsCount) == OK ) {
SomeRandomBloke 3:59038ad536ac 314 if( smsCount > 0 && getNextMessage ) {
SomeRandomBloke 0:9d29b886d41b 315 led3 = 1;
SomeRandomBloke 3:59038ad536ac 316 getNextMessage = false;
SomeRandomBloke 0:9d29b886d41b 317 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 318 debug_pc.printf("%d SMS to read\n", smsCount);
SomeRandomBloke 0:9d29b886d41b 319 #endif
SomeRandomBloke 0:9d29b886d41b 320 if( modem.getSM(num, newMsgBuf, MAX_MSG_LENGTH) == OK ) {
SomeRandomBloke 0:9d29b886d41b 321 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 322 debug_pc.printf("%s : %s\n", num, newMsgBuf);
SomeRandomBloke 0:9d29b886d41b 323 #endif
SomeRandomBloke 0:9d29b886d41b 324 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 325 // Print date/time, then message
SomeRandomBloke 0:9d29b886d41b 326 timestampMessage( newMsgBuf );
SomeRandomBloke 3:59038ad536ac 327 #ifndef USE_LED
SomeRandomBloke 3:59038ad536ac 328 // Force processing of next messages if only printer being used
SomeRandomBloke 3:59038ad536ac 329 // If LED display then update when displayed once
SomeRandomBloke 3:59038ad536ac 330 getNextMessage = true;
SomeRandomBloke 3:59038ad536ac 331 #endif
SomeRandomBloke 0:9d29b886d41b 332 #endif
SomeRandomBloke 0:9d29b886d41b 333 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 334 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 335 #endif
SomeRandomBloke 0:9d29b886d41b 336 strncpy( cmdBuf, newMsgBuf, 10 );
SomeRandomBloke 0:9d29b886d41b 337 stoupper( cmdBuf ); // This is a destructive function, original characters are uppercased
SomeRandomBloke 0:9d29b886d41b 338 if( strncmp( cmdBuf, "BALANCE", 7 ) == 0 ) {
SomeRandomBloke 0:9d29b886d41b 339 sendUSSDCommand( &modem, USSD_COMMAND_BALANCE, true );
SomeRandomBloke 0:9d29b886d41b 340 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 341 timestampMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 342 #endif
SomeRandomBloke 0:9d29b886d41b 343 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 344 resetMessage = true;
SomeRandomBloke 0:9d29b886d41b 345 } else if ( strncmp( cmdBuf, "INFO", 4 ) == 0 ) {
SomeRandomBloke 1:243371cb92c8 346 sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 4:a364da55b42e 347 setNewMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 348 #endif
SomeRandomBloke 0:9d29b886d41b 349 // } else if ( strncmp( cmdBuf, "DEMO", 4 ) == 0 ) {
SomeRandomBloke 1:243371cb92c8 350 // matrixDemo();
SomeRandomBloke 1:243371cb92c8 351 // sprintf(newMsgBuf, "%s %s ", INFO_MSG, ownNumber );
SomeRandomBloke 4:a364da55b42e 352 // setNewMessage( newMsgBuf );
SomeRandomBloke 0:9d29b886d41b 353 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 354 } else if ( strncmp( cmdBuf, "CLEAR", 5 ) == 0 ) {
SomeRandomBloke 4:a364da55b42e 355 setNewMessage( " " );
SomeRandomBloke 0:9d29b886d41b 356 #endif
SomeRandomBloke 0:9d29b886d41b 357 }
SomeRandomBloke 0:9d29b886d41b 358 }
SomeRandomBloke 0:9d29b886d41b 359 }
SomeRandomBloke 0:9d29b886d41b 360 }
SomeRandomBloke 0:9d29b886d41b 361 Thread::wait(3000);
SomeRandomBloke 0:9d29b886d41b 362 led3 = 0;
SomeRandomBloke 0:9d29b886d41b 363 }
SomeRandomBloke 0:9d29b886d41b 364 }
SomeRandomBloke 0:9d29b886d41b 365
SomeRandomBloke 0:9d29b886d41b 366 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 367 void displayScrollingLine(void const*)
SomeRandomBloke 0:9d29b886d41b 368 {
SomeRandomBloke 0:9d29b886d41b 369 int y,xmax,ymax;
SomeRandomBloke 0:9d29b886d41b 370 led.getXYMax(&xmax,&ymax);
SomeRandomBloke 4:a364da55b42e 371 bool msgFinished = true;
SomeRandomBloke 0:9d29b886d41b 372
SomeRandomBloke 0:9d29b886d41b 373 while(true) {
SomeRandomBloke 0:9d29b886d41b 374 // shift the whole screen 6 times, one column at a time; making 1 character
SomeRandomBloke 4:a364da55b42e 375 if( strlen( &msgBuf[0] ) > 10 ) {
SomeRandomBloke 0:9d29b886d41b 376 for (int x=0; x < 6; x++) {
SomeRandomBloke 0:9d29b886d41b 377 led.scrollLeft(1, 1);
SomeRandomBloke 0:9d29b886d41b 378 msgx--;
SomeRandomBloke 3:59038ad536ac 379 if( msgx <= 0 ) {
SomeRandomBloke 3:59038ad536ac 380 msgFinished = true;
SomeRandomBloke 3:59038ad536ac 381 getNextMessage = true;
SomeRandomBloke 3:59038ad536ac 382 }
SomeRandomBloke 3:59038ad536ac 383
SomeRandomBloke 0:9d29b886d41b 384 // fit as much as we can on the available display space
SomeRandomBloke 0:9d29b886d41b 385
SomeRandomBloke 4:a364da55b42e 386 while (!led.putChar(msgx,0,msgBuf[crtPos])) { // zero return if it all fitted
SomeRandomBloke 0:9d29b886d41b 387 led.getXY(&msgx,&y);
SomeRandomBloke 0:9d29b886d41b 388 crtPos++; // we got all of the character on!!
SomeRandomBloke 4:a364da55b42e 389 if (crtPos >= strlen(msgBuf)) {
SomeRandomBloke 0:9d29b886d41b 390 crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 391 }
SomeRandomBloke 0:9d29b886d41b 392 }
SomeRandomBloke 0:9d29b886d41b 393 led.putShadowRam();
SomeRandomBloke 0:9d29b886d41b 394 Thread::wait(DISPDELAY);
SomeRandomBloke 0:9d29b886d41b 395 }
SomeRandomBloke 5:a2c0e95f9a0b 396 } else {
SomeRandomBloke 5:a2c0e95f9a0b 397 getNextMessage = true;
SomeRandomBloke 5:a2c0e95f9a0b 398 msgFinished = true;
SomeRandomBloke 0:9d29b886d41b 399 }
SomeRandomBloke 3:59038ad536ac 400 // Look for a new message being available and current message has been displayed
SomeRandomBloke 3:59038ad536ac 401
SomeRandomBloke 0:9d29b886d41b 402 // TODO add minimum interval between message changes, e.g. 60s
SomeRandomBloke 3:59038ad536ac 403 if( resetMessage && msgFinished ) {
SomeRandomBloke 0:9d29b886d41b 404 led.clear();
SomeRandomBloke 0:9d29b886d41b 405 crtPos = 0;
SomeRandomBloke 0:9d29b886d41b 406 msgx = 1;
SomeRandomBloke 3:59038ad536ac 407 msgFinished = false;
SomeRandomBloke 2:787cbb491f8f 408
SomeRandomBloke 4:a364da55b42e 409 strncpy( msgBuf, newMsgBuf, MAX_MSG_LENGTH );
SomeRandomBloke 4:a364da55b42e 410 if( strlen( msgBuf ) > 10 ) {
SomeRandomBloke 4:a364da55b42e 411 strcat( msgBuf, " ");
SomeRandomBloke 0:9d29b886d41b 412 } else {
SomeRandomBloke 4:a364da55b42e 413 led.putString(0,0, msgBuf);
SomeRandomBloke 0:9d29b886d41b 414 }
SomeRandomBloke 0:9d29b886d41b 415 resetMessage = false;
SomeRandomBloke 0:9d29b886d41b 416 }
SomeRandomBloke 0:9d29b886d41b 417 }
SomeRandomBloke 0:9d29b886d41b 418 }
SomeRandomBloke 0:9d29b886d41b 419
SomeRandomBloke 0:9d29b886d41b 420
SomeRandomBloke 3:59038ad536ac 421 /*
SomeRandomBloke 0:9d29b886d41b 422 void matrixDemo( void )
SomeRandomBloke 0:9d29b886d41b 423 {
SomeRandomBloke 0:9d29b886d41b 424 int xmax,ymax;
SomeRandomBloke 0:9d29b886d41b 425 led.getXYMax(&xmax,&ymax);
SomeRandomBloke 0:9d29b886d41b 426 led.clear();
SomeRandomBloke 0:9d29b886d41b 427 for( int n=0; n<3; n++ ) {
SomeRandomBloke 0:9d29b886d41b 428 for( int i=0; i<8; i++ ) {
SomeRandomBloke 0:9d29b886d41b 429 led.drawRectangle(i,i,xmax-i,ymax-i, 1);
SomeRandomBloke 0:9d29b886d41b 430 wait(0.05);
SomeRandomBloke 0:9d29b886d41b 431 }
SomeRandomBloke 0:9d29b886d41b 432 for( int i=7; i>=0; i-- ) {
SomeRandomBloke 0:9d29b886d41b 433 led.drawRectangle(i,i,xmax-i,ymax-i, 0);
SomeRandomBloke 0:9d29b886d41b 434 wait(0.05);
SomeRandomBloke 0:9d29b886d41b 435 }
SomeRandomBloke 0:9d29b886d41b 436 }
SomeRandomBloke 0:9d29b886d41b 437 led.clear();
SomeRandomBloke 0:9d29b886d41b 438 for( int n=0; n<3; n++ ) {
SomeRandomBloke 0:9d29b886d41b 439 for(int i=0; i<(xmax/2); i++) {
SomeRandomBloke 0:9d29b886d41b 440 led.drawCircle((xmax/2), 7, i, 1 );
SomeRandomBloke 0:9d29b886d41b 441 wait( 0.05 );
SomeRandomBloke 0:9d29b886d41b 442 }
SomeRandomBloke 0:9d29b886d41b 443 for(int i=(xmax/2); i>=0; i--) {
SomeRandomBloke 0:9d29b886d41b 444 led.drawCircle((xmax/2), 7, i, 0 );
SomeRandomBloke 0:9d29b886d41b 445 wait( 0.05 );
SomeRandomBloke 0:9d29b886d41b 446 }
SomeRandomBloke 0:9d29b886d41b 447 }
SomeRandomBloke 0:9d29b886d41b 448 wait(2);
SomeRandomBloke 0:9d29b886d41b 449
SomeRandomBloke 0:9d29b886d41b 450 led.clear();
SomeRandomBloke 2:787cbb491f8f 451 led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y);
SomeRandomBloke 0:9d29b886d41b 452 }
SomeRandomBloke 3:59038ad536ac 453 */
SomeRandomBloke 0:9d29b886d41b 454
SomeRandomBloke 0:9d29b886d41b 455 void showTime()
SomeRandomBloke 0:9d29b886d41b 456 {
SomeRandomBloke 0:9d29b886d41b 457 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 458 char timebuf[20];
SomeRandomBloke 0:9d29b886d41b 459 strftime(timebuf, 20, "%X ", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 460 led.putString(12,8, timebuf );
SomeRandomBloke 0:9d29b886d41b 461 }
SomeRandomBloke 0:9d29b886d41b 462
SomeRandomBloke 0:9d29b886d41b 463 void showDate()
SomeRandomBloke 0:9d29b886d41b 464 {
SomeRandomBloke 0:9d29b886d41b 465 time_t seconds = time(NULL);
SomeRandomBloke 0:9d29b886d41b 466 char timebuf[20];
SomeRandomBloke 0:9d29b886d41b 467 strftime(timebuf, 20, "%d/%m/%Y ", localtime(&seconds));
SomeRandomBloke 0:9d29b886d41b 468 led.putString(4,8, timebuf );
SomeRandomBloke 0:9d29b886d41b 469 }
SomeRandomBloke 0:9d29b886d41b 470 #endif
SomeRandomBloke 0:9d29b886d41b 471
SomeRandomBloke 0:9d29b886d41b 472
SomeRandomBloke 0:9d29b886d41b 473 int main()
SomeRandomBloke 0:9d29b886d41b 474 {
SomeRandomBloke 0:9d29b886d41b 475 #ifdef DEBUG
SomeRandomBloke 0:9d29b886d41b 476 debug_pc.baud(57600);
SomeRandomBloke 0:9d29b886d41b 477 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 478 debug_pc.printf("SMS to LED Matrix display\n");
SomeRandomBloke 0:9d29b886d41b 479 #endif
SomeRandomBloke 0:9d29b886d41b 480 #ifdef USE_PRINTER
SomeRandomBloke 0:9d29b886d41b 481 printer.begin();
SomeRandomBloke 0:9d29b886d41b 482 debug_pc.printf("SMS To Printer\n");
SomeRandomBloke 0:9d29b886d41b 483 timestampMessage("SMS To Printer Starting");
SomeRandomBloke 0:9d29b886d41b 484 #endif
SomeRandomBloke 0:9d29b886d41b 485 #endif
SomeRandomBloke 0:9d29b886d41b 486 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 487 int displayCount = 10;
SomeRandomBloke 0:9d29b886d41b 488 bool displayTime = true;
SomeRandomBloke 0:9d29b886d41b 489
SomeRandomBloke 2:787cbb491f8f 490 led.init(LED_MAX_DISPLAY_X,LED_MAX_DISPLAY_Y); // Use all displays as 128x8 display
SomeRandomBloke 0:9d29b886d41b 491 led.clear();
SomeRandomBloke 6:323d7422ca8b 492 led.setBrightness(10);
SomeRandomBloke 3:59038ad536ac 493 //led.displayOff(); // Turn off display for now until receiver task has started
SomeRandomBloke 0:9d29b886d41b 494 bool twoLineDisplay = (LED_MAX_DISPLAY_X > 1);
SomeRandomBloke 0:9d29b886d41b 495 led.putString( 0, 0, "SMS to LED Display" );
SomeRandomBloke 0:9d29b886d41b 496
SomeRandomBloke 0:9d29b886d41b 497 Thread::wait(3000); // Wait for display to initialise after power up
SomeRandomBloke 0:9d29b886d41b 498 #endif
SomeRandomBloke 0:9d29b886d41b 499
SomeRandomBloke 0:9d29b886d41b 500 // Set initial blank message
SomeRandomBloke 2:787cbb491f8f 501 resetMessageBuffers();
SomeRandomBloke 0:9d29b886d41b 502
SomeRandomBloke 0:9d29b886d41b 503 Thread receiveTask(receiveSMS, NULL, osPriorityNormal, 1024 * 8); // try 6 next
SomeRandomBloke 0:9d29b886d41b 504 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 505 Thread displayTask(displayScrollingLine, NULL, osPriorityNormal, 1024 * 4);
SomeRandomBloke 0:9d29b886d41b 506 #endif
SomeRandomBloke 0:9d29b886d41b 507
SomeRandomBloke 0:9d29b886d41b 508 // Show we are still working by alternitively flashing LED1 adn LED2, once a second
SomeRandomBloke 0:9d29b886d41b 509 led1 = 0; // Working
SomeRandomBloke 0:9d29b886d41b 510 led2 = 1; // Alternate with led1
SomeRandomBloke 3:59038ad536ac 511 led3 = 0; // Received and processing SMS
SomeRandomBloke 0:9d29b886d41b 512 led4 = 0; // Processing USSD message queue
SomeRandomBloke 0:9d29b886d41b 513 while(1) {
SomeRandomBloke 0:9d29b886d41b 514 #ifdef USE_LED
SomeRandomBloke 0:9d29b886d41b 515 // Only display date/time is we have a 2 row display
SomeRandomBloke 0:9d29b886d41b 516 if( twoLineDisplay ) {
SomeRandomBloke 0:9d29b886d41b 517 if( --displayCount <= 0 ) {
SomeRandomBloke 0:9d29b886d41b 518 displayTime = !displayTime;
SomeRandomBloke 0:9d29b886d41b 519 displayCount = 10;
SomeRandomBloke 0:9d29b886d41b 520 led.putString(0,8, " " );
SomeRandomBloke 0:9d29b886d41b 521 }
SomeRandomBloke 0:9d29b886d41b 522 if( displayTime ) {
SomeRandomBloke 0:9d29b886d41b 523 showTime();
SomeRandomBloke 0:9d29b886d41b 524 } else {
SomeRandomBloke 0:9d29b886d41b 525 showDate();
SomeRandomBloke 0:9d29b886d41b 526 }
SomeRandomBloke 0:9d29b886d41b 527 }
SomeRandomBloke 0:9d29b886d41b 528 #endif
SomeRandomBloke 0:9d29b886d41b 529 led1=!led1;
SomeRandomBloke 0:9d29b886d41b 530 led2=!led2;
SomeRandomBloke 0:9d29b886d41b 531 Thread::wait(1000);
SomeRandomBloke 0:9d29b886d41b 532 }
SomeRandomBloke 0:9d29b886d41b 533 }