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