asdf

Dependencies:   SDFileSystem mbed

Fork of All_Combined_Real by JanPaul Bergeson

Files at this revision

API Documentation at this revision

Comitter:
colingm
Date:
Wed Mar 23 14:32:41 2016 +0000
Parent:
4:70411c8dadcc
Commit message:
asdf

Changed in this revision

XBeeLib.cpp Show annotated file Show diff for this revision Revisions of this file
XBeeLib.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
xbee.cpp Show annotated file Show diff for this revision Revisions of this file
xbee.h Show annotated file Show diff for this revision Revisions of this file
diff -r 70411c8dadcc -r 6532c5a5f576 XBeeLib.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XBeeLib.cpp	Wed Mar 23 14:32:41 2016 +0000
@@ -0,0 +1,69 @@
+#include "XBeeLib.h"
+#include "xbee.h"
+
+#define UINT64(msb,lsb)     (uint64_t)(((uint64_t)(msb) << 32) | (lsb))
+
+#define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
+#define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x41030E8A)
+#define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
+
+#define XBEE_BAUD_RATE 115200
+
+#define MAX_BYTES 256
+#define MAX_SIZE 512
+
+Serial *log_serial;
+XBee xbee;
+Serial xbeeSerial(D1, D0); 
+
+int frameID = 0;
+
+void send_data_to_remote_node(char *data, int frame, int framePart, int length)
+{
+    unsigned char outFrame[MAX_SIZE];
+//    log_serial->printf("Old Length: %04x\r\n", (uint16_t)(length + 11));
+    int frameLen = xbee.Send((unsigned char*)data, length, outFrame, REMOTE_NODE_ADDR64);
+    
+    log_serial->printf("Message prepared to send\r\n");
+    
+    for (int i = 0; i < frameLen; i++) {
+//        log_serial->printf("%02x ", outFrame[i]);
+        xbeeSerial.putc(outFrame[i]);
+    }
+//    log_serial->printf("\r\n");
+    log_serial->printf("Message finished sending\r\n");
+}
+
+void XBeeInit(Serial *pcLogger)
+{
+    log_serial = pcLogger;
+    xbeeSerial.baud(XBEE_BAUD_RATE);
+}
+
+void XBeeSend(const char *message, int length)
+{
+    int framePart = 0;
+    for (int i = 0; i < length; i += MAX_BYTES) {
+        log_serial->printf("partition at %d\r\n", i);
+        int arraySize = length - i;
+        if (arraySize > MAX_BYTES + 1)
+            arraySize = MAX_BYTES + 1;
+            
+        char partition[arraySize];
+        int j;
+        for (j = i; ((j - i) < MAX_BYTES) && (j < length); j++) {
+            partition[j-i] = message[j];
+        }
+        
+        send_data_to_remote_node(partition, frameID, framePart, j-i);
+        
+        wait_ms(25);
+    }
+    
+    frameID++;
+}
+
+void XBeeDestroy()
+{
+    
+}
\ No newline at end of file
diff -r 70411c8dadcc -r 6532c5a5f576 XBeeLib.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XBeeLib.h	Wed Mar 23 14:32:41 2016 +0000
@@ -0,0 +1,12 @@
+#ifndef XBEE_LIB_H
+#define XBEE_LIB_H
+
+#include "mbed.h"
+
+void XBeeInit(Serial *pcLogger);
+
+void XBeeSend(const char *message, int length);
+
+void XBeeDestroy();
+
+#endif
\ No newline at end of file
diff -r 70411c8dadcc -r 6532c5a5f576 main.cpp
--- a/main.cpp	Tue Mar 22 17:08:13 2016 +0000
+++ b/main.cpp	Wed Mar 23 14:32:41 2016 +0000
@@ -3,6 +3,7 @@
 #include "SDFileSystem.h"
 #include "SPI.h"
 #include "temp_sensor.h"
+#include "XBeeLib.h"
 
 #define NUM_PIXELS 2048
 #define BUF_SIZE 1024
@@ -47,7 +48,7 @@
 
 void read_pixels()
 {
-    int data[NUM_PIXELS];
+    char data[NUM_PIXELS];
     for (int i = 0; i < NUM_PIXELS; i++) {
 
         while (!pixel_rdy);
@@ -67,6 +68,8 @@
     pc.printf("\r\n");
     fprintf(fpData, "\r\n");
     fclose(fpData);
+    
+    XBeeSend((const char *)data, 2048);
 }
 
 void gps_read() {
@@ -105,6 +108,7 @@
     duino.baud(9600);
     pc.printf("Initializing ...\r\n");
 
+    XBeeInit(&pc);
     fifo_cs = 1;
     trigger = 0;
     wait(0.5f);
@@ -116,7 +120,7 @@
 
         led = !led;
 
-        gps_read();
+//        gps_read();
         
         check_temp();
         // Display result
diff -r 70411c8dadcc -r 6532c5a5f576 xbee.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.cpp	Wed Mar 23 14:32:41 2016 +0000
@@ -0,0 +1,113 @@
+#include <string.h>
+#include "xbee.h"
+
+
+int XBee::Receive(unsigned char *inBuff, int len, unsigned char *outBuff){
+    int unescapeLen = 0;
+    unsigned char checksum = 0;
+    unsigned char LSB = 0;
+
+    if (inBuff[0] != 0x7E)
+        return 0;
+
+    if (len < 10)
+        return 0;
+
+    unescapeLen = unescape(inBuff, len, outBuff);
+
+    // Check we have at least the amount of bytes indicated by LSB
+    LSB = outBuff[2]; 
+    if (LSB > (unescapeLen - 4))
+        return 0;
+
+    // Calculate our checksum
+    // (char will overflow, no need to AND for lower bytes)
+    for (int i=3; i<LSB+4; i++){
+        checksum += outBuff[i];
+    }
+
+    if (checksum != 0xFF)
+        return 0;
+
+    return LSB+4;
+}
+
+
+int XBee::Send(unsigned char *msg, int len, unsigned char *outBuff, const uint64_t addr){
+    unsigned char buf[len + 15];
+    int escapedLen = 0;
+    unsigned char checksum = 0;
+
+    buf[0] = 0x7E;
+    buf[1] = (len + 11) >> 8;
+    buf[2] = len + 11;
+    buf[3] = 0x00;  // transmit request
+    buf[4] = 0x00;  // Frame ID
+    buf[5] = addr >> 56;
+    buf[6] = addr >> 48;
+    buf[7] = addr >> 40;
+    buf[8] = addr >> 32;
+    buf[9] = addr >> 24;
+    buf[10] = addr >> 16;
+    buf[11] = addr >> 8;
+    buf[12] = addr;
+    buf[13] = 0x01;  // Disable acknowledge
+    memcpy(&buf[14], msg, len);
+
+    for (int i=3;i<len+14;i++){
+        checksum += buf[i];
+    }
+
+    buf[len+14] = 0xFF - checksum;
+    escapedLen = escape(buf, len+15, outBuff);
+
+    return escapedLen;
+}
+
+int XBee::escape(unsigned char *input, int inLen, unsigned char *output){
+    int pos = 1;
+
+    output[0] = input[0];
+    for (int i=1; i<inLen; i++){
+        switch(input[i]){
+            case 0x7D:
+            case 0x7E:
+            case 0x11:
+            case 0x13:
+                output[pos++] = 0x7D;
+                output[pos++] = input[i] ^ 0x20;
+                break;
+            default:
+                output[pos++] = input[i];
+                break;
+      }
+   }
+
+   return pos;
+}
+
+int XBee::unescape(unsigned char *input, int inLen, unsigned char *output){
+    int pos = 1;
+    bool skip = false;
+    unsigned char curr = 0;
+
+    output[0] = input[0];
+    for (int i=1; i<inLen; i++) {
+        if (skip){
+            skip = false;
+            continue;
+        }
+
+        if (input[i] == 0x7D){
+            curr = input[i+1] ^ 0x20;
+            skip = true;
+        }else{
+            curr = input[i];
+        }
+
+        output[pos] = curr;
+        pos++;
+    }
+
+    return pos;
+}
\ No newline at end of file
diff -r 70411c8dadcc -r 6532c5a5f576 xbee.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.h	Wed Mar 23 14:32:41 2016 +0000
@@ -0,0 +1,21 @@
+#ifndef XBEE_H
+#define XBEE_H
+
+#include "mbed.h"
+
+#define MAX_MESSAGE 256
+
+class XBee {
+    public:
+        XBee(){;}
+        ~XBee(){;}
+        
+        enum receive_state { INIT, LENGTH, TYPE, ADDR_LONG, ADDR_SHORT, OPTIONS, MESSAGE, CHECKSUM };
+        
+        int Receive(unsigned char *inbuf, int len, unsigned char *outbuf);
+        int Send(unsigned char *buf, int len, unsigned char *outbuf, uint64_t addr);
+    private:
+        int escape(unsigned char *input, int inlen, unsigned char *output);
+        int unescape(unsigned char *input, int inlen, unsigned char *output);
+};
+#endif
\ No newline at end of file