LM005A Lora Module test

Dependents:   LoRaTest OnenetTest

Revision:
0:5b5a1b5f1ae4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM005A/LM005A.cpp	Thu Apr 19 13:15:25 2018 +0000
@@ -0,0 +1,237 @@
+/* LM005A Example
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LM005A.h"
+#include <cstring>
+
+#define   LM005A_DEFAULT_BAUD_RATE   115200
+
+LM005A::LM005A(PinName tx, PinName rx, bool debug)
+    : _serial(tx, rx, LM005A_DEFAULT_BAUD_RATE), 
+      _parser(&_serial),
+      _fail(false)
+{
+    _serial.set_baud( LM005A_DEFAULT_BAUD_RATE );
+    _parser.debug_on(debug);
+    _parser.set_delimiter("\r\n");
+     _parser.oob("+RXDONE:", callback(this, &LM005A::_packet_handler));
+   //  _parser.oob("+JOIN:", callback(this, &LM005A::_tx_handler));
+   
+}
+
+const char * LM005A::get_firmware_version()
+{   
+        if (!(_parser.send("AT+VERSION?")
+        && _parser.recv("+VERSION:%s\n",buffer)
+        && _parser.recv("OK"))) {
+        return NULL;
+    }
+   return buffer;
+    
+}
+bool LM005A::init(void)
+{
+    if (!_parser.recv("Bootup Completed!!!"))
+    return false;
+    return true;
+      }
+bool LM005A::reset(void)
+{
+        if (!(_parser.send("AT+RESET")
+        && _parser.recv("+RESET")
+        && _parser.recv("OK")
+        &&_parser.recv("Bootup Completed!!!"))) {
+        return false;
+    }  
+  return true;
+}
+ 
+    int LM005A::set_mode(int mode)
+    {
+          int res;
+    if (!(_parser.send("AT+MODE=%d",mode)
+        && _parser.recv("+MODE:%d", &res)
+        && _parser.recv("OK"))) {
+        return -1;
+    }
+
+    return res;
+        }
+int LM005A::set_class(int class_mode)
+{
+      int res;
+    if (!(_parser.send("AT+CLASS=%d",class_mode)
+        && _parser.recv("+CLASS:%d", &res)
+        && _parser.recv("OK"))) {
+        return -1;
+    }
+
+    return res;
+    }
+int LM005A::set_band(int gap,int band)
+{
+   int g,f;
+    if (!(_parser.send("AT+FIXEDBAND=%d,%d",gap,band)
+        && _parser.recv("+FIXEDBAND:%d,%d\n",&g ,&f)
+        && _parser.recv("OK"))) {
+        return 0;
+    }
+
+    return f;  }
+int  LM005A::set_power(int power)
+ {  
+    int res;
+    if (!(_parser.send("AT+POWER=%d",power)
+        && _parser.recv("+POWER:%d\n", &res)
+        && _parser.recv("OK"))) {
+        return 0;
+    }
+
+    return res;
+ }
+
+bool LM005A::join()
+{
+ 
+    if (!(_parser.send("AT+JOIN")
+        && _parser.recv("OK"))) {
+        return false;
+    }
+    return true;
+}
+
+ const char *LM005A::getEUI(int type)
+ {
+     if (type==0)
+     {  if (!(_parser.send("AT+DEVEUI?")
+        && _parser.recv("+DEVEUI%s\n", buffer)
+        && _parser.recv("OK"))) {
+        return NULL;
+         }
+     }
+     else
+     {
+         if (!(_parser.send("AT+APPEUI?")
+        && _parser.recv("+APPEUI%s\n", buffer)
+        && _parser.recv("OK"))) {
+        return NULL;
+         }
+          }
+ return buffer;
+}
+const char *LM005A::getDeviceAddress(void)
+{
+      
+    
+    if (!(_parser.send("AT+DEVADDR?")
+        && _parser.recv("+DEVADDR:%s\n", buffer)
+        && _parser.recv("OK"))) {
+        return NULL;
+    }
+
+    return buffer;
+} 
+ 
+unsigned char hex_char(char c)
+{
+    if ('0' <= c && c <= '9') return (unsigned char)(c - '0');
+    if ('A' <= c && c <= 'F') return (unsigned char)(c - 'A' + 10);
+    if ('a' <= c && c <= 'f') return (unsigned char)(c - 'a' + 10);
+  //  printf("Error Val=%d\n",c);
+    return 0xFF;
+}
+bool LM005A::send(int port,int ack, const void *data, uint32_t amount)
+{
+  
+    if (!(_parser.send("AT+MSG=%d,%d,%d,%.*s",port,ack,amount,amount,data)
+        && _parser.recv("+MSG:TXDONE")
+        && _parser.recv("+MSG:RXDONE")
+        && _parser.recv("OK")))
+             return false;   
+       return  true;
+}
+bool LM005A::sendhex(int port,int ack, const void *data, uint32_t amount)
+{
+  
+    if (!(_parser.send("AT+MSGHEX=%d,%d,%d,%.*s",port,ack,amount,amount,data)
+        && _parser.recv("+MSGHEX:TXDONE")
+        && _parser.recv("OK")))
+             return false;   
+       return  true;
+}
+
+void LM005A::_packet_handler()
+{  uint32_t amount;
+char rBuf[128];
+int i,p;
+char val;
+  if (!_parser.recv("%d,",&amount)) {
+        return;
+    }
+    printf("amount=%d\n",amount);
+    rCounter=rCounter+amount;
+    _parser.read(rBuf,amount*3-1);
+    p=0;
+    for (i=0;i<amount;i++)
+      { 
+     val=hex_char( rBuf[p++]);
+     val=(val<<4)+hex_char( rBuf[p++]);
+     p++;
+      rBuffer[rIndex++]=val;
+      }
+     rBuffer[rIndex++]=0x00;
+  //   printf("Rec=%0.*s",amount,rBuffer); 
+   
+ }
+ 
+
+int32_t LM005A::recv(int id, void *data, uint32_t amount)
+{ 
+  rIndex=0;
+rCounter=0;
+
+    while (true) {      
+         if (rCounter>0) 
+         {   
+             memcpy(data,rBuffer,rCounter);
+            break;
+         
+         }
+        if (!_parser.process_oob()) {
+            return -1;
+        }
+    }
+     return rCounter;
+}
+
+
+void LM005A::setTimeout(uint32_t timeout_ms)
+{
+    _parser.set_timeout(timeout_ms);
+}
+
+
+
+void LM005A::attach(Callback<void()> func)
+{
+    _serial.sigio(func);
+}
+
+
+
+
+
+