Vodafone K3770 dongle enabled receipt printer.

Dependencies:   AdafruitThermalPrinter VodafoneK3770LibArchive mbed

Fork of 3GPrinter by Ashley Mills

Revision:
1:59abeafd95d2
Parent:
0:76691d4f3960
--- a/main.cpp	Thu May 31 16:17:49 2012 +0000
+++ b/main.cpp	Fri Jun 29 08:47:09 2012 +0000
@@ -5,124 +5,74 @@
 
 #include "core/fwk.h"
 #include "mbed.h"
-
 #include "rtos.h"
-
 #include "if/VodafoneK3770.h"
-#include "HTTPClient.h"
 #include "AdafruitThermal.h"
 
 DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-void setLeds(bool a, bool b, bool c, bool d) {
-  led1 = a;
-  led2 = b;
-  led3 = c;
-  led4 = d;
-}
 
 extern "C" void HardFault_Handler() {
-  error("Hard Fault!\n");
+    error("Hard Fault!\n");
 }
 
-void test(void const*) {
-  VodafoneK3770 modem;
-  HTTPClient http;
-  AdafruitThermal printer(p13,p14);
-  printer.begin();
+// this is the main thread which checks for messages and sends them to the printer
+void printerLoop(void const*) {
+    VodafoneK3770 modem;                // setup modem
+    AdafruitThermal printer(p13,p14);   // setup printer
+    printer.begin();                    // and init it
 
-  char str[512];
-  char numBuffer[20];
-  char msgBuffer[256];
+    // declare space for phone number and message storage
+    char numBuffer[20], msgBuffer[256];
+    size_t numSMS; // variable to track number of received messages
+
+    //modem.sendSM("07825608771","this is a test");
+
+    // loop forever
+    while(1) {
+        DBG("Checking SM count");
 
-  //int ret = modem.connect("pp.vodafone.co.uk", "web", "web");
-  //int ret = modem.connect("internet", "web", "web");
-  int ret = OK;
-  if(ret != OK)
-    return;
-  
-  size_t numSMS;
-  
-  //modem.sendSM("07825608771","this is a test");
-  
-  while(true) {
-    DBG("Checking SM count");
-    
-    if(modem.getSMCount(&numSMS)==OK) {
-       DBG("getSMCount success");
-       DBG("numSMS: %d",numSMS);
-       if(numSMS>0) {
-         DBG("SM count > 0");
-         if(modem.getSM(numBuffer,msgBuffer,256)==OK) {
-           DBG("message received");
-           DBG("num: %s",numBuffer);
-           DBG("msg: %s",msgBuffer);
-           
-           printer.print(msgBuffer);
-           printer.feed(1);           
-         }
-       }
-       
+        // retrieve the short message count into numSMS
+        if(modem.getSMCount(&numSMS)==OK) {
+            DBG("getSMCount success");
+            DBG("numSMS: %d",numSMS);
+            // check if any short messages have been received
+            if(numSMS>0) {
+                DBG("SM count > 0");
+                // get the oldest short message in the queue
+                if(modem.getSM(numBuffer,msgBuffer,256)==OK) {
+                    DBG("message received");
+                    DBG("num: %s",numBuffer);
+                    DBG("msg: %s",msgBuffer);
+                    // send it to the printer
+                    printer.print(msgBuffer);
+                    printer.print("\r\n");
+                    // linefeed a couple of times
+                    printer.feed(2);
+                }
+            }
+        }
+        // wait 500ms
+        Thread::wait(500);
     }
-    Thread::wait(5000);
-    
-  }
-  
-  //
-    
-  DBG("Trying to fetch page...");
-  ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
-  if (ret == OK) {
-    DBG("Page fetched successfully - read %d characters", strlen(str));
-    DBG("Result: %s", str);
-  } else {
-    WARN("Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
-  }
-
-  HTTPMap map;
-  HTTPText text(str, 512);
-  map.put("Hello", "World");
-  map.put("test", "1234");
-  DBG("Trying to post data...");
-  ret = http.post("http://httpbin.org/post", map, &text);
-  if (ret == OK) {
-    DBG("Executed POST successfully - read %d characters", strlen(str));
-    DBG("Result: %s", str);
-  } else {
-    WARN("Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
-  }
-  
-  
-  modem.disconnect();
-  DBG("Disconnected");
-
-  while (1) {
-    Thread::wait(100);
-  }
 }
 
-void keepAlive(void const*) {
-  while (1) {
-    led1 = !led1;
-    Thread::wait(500);
-  }
-}
-
+// this is the main thread
 int main() {
+    // init the debug macros
+    DBG_INIT();
 
-  DBG_INIT();
-  /*
-  VodafoneK3770 threeg;
-  int ret = threeg.connect("pp.vodafone.co.uk", "web", "web");
-  if(ret==OK) {
-    DBG("connection OK");
-  }
-  */
+    // create and launch printer thread
+    Thread testTask(
+        printerLoop, // function name
+        NULL, osPriorityNormal, 1024 * 4);
+    
+    // loop forever, relinquish thread every 500ms
+    while(1) {
+        // toggle led1
+        led1 = !led1;
+        // wait 500ms
+        Thread::wait(500);
+    }
 
-  Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
-  keepAlive(NULL);
-
-  return 0;
+    return 0;
 }