Matthew Mellor / Mbed 2 deprecated sendFootSensorsPacketsFinalUDP

Dependencies:   MCP32082Two mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mcp3208.h"
00003 
00004 //Packet Strategy One for UDP
00005 //Send out data at 1ms as normal
00006 //after specified numReads send out data with
00007 //terminated character
00008 //On UDP/ESP8266 side read until you see the termination character
00009 
00010 int readCounter = 1; 
00011 int numReads = 45; //This value is numDataSetsInPacket in matlab and arduino code
00012 int counterNum = 0;
00013 
00014 MCP3208 input1(dp2, dp1, dp6, dp9);
00015 
00016 Serial pc(dp16,dp15);
00017 
00018 Ticker datalog; //Create the timer object
00019 
00020 char datastr0[5];
00021 char datastr1[5];
00022 char datastr2[5];
00023 char datastr3[5];
00024 char datastr4[5];
00025 char datastr5[5];
00026 
00027 
00028 // Credit: Erik Olieman
00029 void intToString(char *buffer, int value)
00030 {
00031     int temp;
00032     temp = value / 1000;
00033     buffer[0] = temp + '0';
00034     value = value - temp * 1000;
00035 
00036     temp = value / 100;
00037     buffer[1] = temp + '0';
00038     value = value - temp * 100;
00039 
00040     temp = value / 10;
00041     buffer[2] = temp + '0';
00042     value = value - temp * 10;
00043 
00044     temp = value / 1;
00045     buffer[3] = temp + '0';
00046     value = value - temp * 1;
00047 
00048     buffer[4] = '\0';
00049 }
00050 
00051 bool tickerActivated = false;
00052 
00053 void log_data(){
00054     tickerActivated = true;
00055 }
00056 
00057 int main(){
00058     pc.baud(921600); //Set baud rate
00059     pc.printf("Working!!\n\r");
00060 
00061     datalog.attach_us(&log_data,1000); // 1000us = 1ms
00062 
00063     while(1) {
00064         intToString(datastr0,input1.binary(0));
00065         intToString(datastr1,input1.binary(1));
00066         intToString(datastr2,input1.binary(2));
00067         intToString(datastr3,input1.binary(3));
00068         intToString(datastr4,input1.binary(4));
00069         intToString(datastr5,input1.binary(5));
00070         
00071         if(tickerActivated == true) { //This is true every 1ms
00072             tickerActivated = false;
00073             if(readCounter == numReads){
00074                 readCounter = 1;
00075                 counterNum = counterNum + 1;
00076                 if(counterNum >= 9999) counterNum = 0;
00077                 pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5);
00078             }
00079             else{
00080                 readCounter++;
00081                 counterNum = counterNum + 1;
00082                 if(counterNum >= 9999) counterNum = 0;
00083                 
00084                 pc.printf("%s,%s,%s,%s,%s,%s,",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5);  
00085             }
00086         }
00087     }
00088 }