Projectlab Elektronica-ICT KULeuven

Dependencies:   EthernetInterface TMP102 TextLCD mbed-rtos mbed

werking.pdf

Files at this revision

API Documentation at this revision

Comitter:
seppeduwe
Date:
Sun Mar 16 15:08:10 2014 +0000
Parent:
0:ae3af7d18c4a
Child:
2:1243006bb879
Commit message:
Main goed

Changed in this revision

Display.cpp Show annotated file Show diff for this revision Revisions of this file
Display.h Show annotated file Show diff for this revision Revisions of this file
Frame.cpp Show annotated file Show diff for this revision Revisions of this file
Frame.h Show annotated file Show diff for this revision Revisions of this file
Potentiometer.cpp Show annotated file Show diff for this revision Revisions of this file
Potentiometer.h Show annotated file Show diff for this revision Revisions of this file
Server.cpp Show annotated file Show diff for this revision Revisions of this file
Server.h Show annotated file Show diff for this revision Revisions of this file
TCPServer.cpp Show diff for this revision Revisions of this file
TCPServer.h Show diff for this revision Revisions of this file
TMP102.lib Show annotated file Show diff for this revision Revisions of this file
Temperature.cpp Show annotated file Show diff for this revision Revisions of this file
Temperature.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
--- a/Display.cpp	Fri Mar 14 19:41:14 2014 +0000
+++ b/Display.cpp	Sun Mar 16 15:08:10 2014 +0000
@@ -3,17 +3,20 @@
 
 Display::Display()
 {
-    //setup the LCD
     lcd = new TextLCD(p14, p16, p17, p18, p19, p20); // rs, e, d4-d7
-    //print the initial text on it
-    printf("LCD is up\n");
 }
 
 //set the text on the display
 void Display::setText(char* text)
 {
+    lcd->cls();
     lcd->locate(0,0);
-    //lcd->clr();
-    printf("Text %s\n\r", text);
     lcd->printf(text);
+}
+//set the advanced text on the display
+void Display::setText(char* text, int value)
+{
+    lcd->cls();
+    lcd->locate(0,0);
+    lcd->printf(text,value);
 }
\ No newline at end of file
--- a/Display.h	Fri Mar 14 19:41:14 2014 +0000
+++ b/Display.h	Sun Mar 16 15:08:10 2014 +0000
@@ -11,6 +11,7 @@
 public:
     Display();
     void setText(char* text);
+    void setText(char* text, int value);
 };
 
 #endif
\ No newline at end of file
--- a/Frame.cpp	Fri Mar 14 19:41:14 2014 +0000
+++ b/Frame.cpp	Sun Mar 16 15:08:10 2014 +0000
@@ -11,61 +11,68 @@
 //ID0–IDn(n x 8bit): lijst van reeds bezochte mbeds
 //CRC (16bit): CRC16 checksum(x16+ x15+ x2+ 1) checksumop “LEN IDD TMP PWM ID0…IDn”
 //EOF (16bit): end of frame (0xCC 0xDD)
-
-//char buf[]={0xAA,0xBB,0x0F,0x0B,0x0F,0xA0,0x02,0x05,0x08,0x09,0x0A,0xF2,0xE4,0xCC,0xDD}; //BB 0E A0 0F 32 02 05 08 09 F2 E4 CCDD";
-//            SOF  SOF  LEN  IDD  TMP  TMP  PWM  TUN  ID0  ID1  ID2  CRC CRC   EOF  EOF
-
-void decode(char* frame)
+Frame::Frame()
 {
-    int length;
-    int IdDestinationMbed;
-    int temperature;
-    int pWMDutycycle;
-    int tune;
-    char* IDs;
-    char* CRC;
-    int numberOfFrames;
+    IdDestinationMbed=0;
+    temperature=0;
+    pWMDutycycle=0;
+    tune=0;
+    lengthIDs=0;
+    IDs= new char[0];
+}
+Frame::Frame(    int IdDestinationMbed,
+                 int temperature,
+                 int pWMDutycycle,
+                 int tune,
+                 int lengthIDs,
+                 char* IDs)
+{
+    this->IdDestinationMbed=IdDestinationMbed;
+    this->temperature=temperature;
+    this->pWMDutycycle=pWMDutycycle;
+    this->tune=tune;
+    this->lengthIDs=lengthIDs;
+    this->IDs=IDs;
+}
 
-    length = (int)frame[2];
-    numberOfFrames = length- 12;
-    IDs = new char[numberOfFrames];
+int Frame::Decode(char* frame)
+{
+    int length = (int)frame[2];
+    lengthIDs = length- 12;
+    IDs = new char[lengthIDs];
 
     if ( frame[0]==0xAA && frame[1]==0xBB && frame[length-2]==0xCC && frame[length-1]==0xDD) {
 
         IdDestinationMbed = (int) frame[3];
-        temperature = (int) ((frame[4]<<8) | frame[5]);
+        //temperature = (int) (0.026862*((frame[4]<<8) | frame[5])-25)  ;
+        temperature = (int) ((frame[4]<<8) | frame[5])  ;
         pWMDutycycle = (int)frame[6];
         tune = (int) frame [7];
+        crc= (int) ((frame[length-4]<<8) | frame[length-3]);
+        for (int i=0; i<lengthIDs; i++) {
+            IDs[i]=frame[i+8];
+        }
 
-        for (int i=0; i<numberOfFrames; i++) {
-            IDs[i]=frame[i+numberOfFrames];
-        }
         printf("Decode length: %d IdDestinationMbed: %d temperature: %d \n\r",length,IdDestinationMbed,temperature);
         printf("pWMDutycycle: %d tune: %d \n\r",pWMDutycycle,tune);
-
+        for(int i=0; i<lengthIDs; i++) {
+            printf("IDs %d \n\r",IDs[i]);
+        }
     }
+    return CheckCRC(frame);
 }
 
-int encode(
-    int IdDestinationMbed,
-    int temperature,
-    int pWMDutycycle,
-    int tune,
-    int lengthIDs,
-    char* IDs,
-    char* encode
-)
+int Frame::Encode(char* encode)
 {
+    memset(encode, 0, 255);
     int length = 12+lengthIDs;
-    //encode = new char[length];
 
     encode[0]=0xAA;
     encode[1]=0xBB;
-
     encode[2]= (char) length;
     encode[3]= (char) IdDestinationMbed;
-    encode[4]= (char) (temperature>>8);
-    encode[5]= (char) temperature;
+    encode[4]= (char) (((int)(37.227*temperature+930.68))>>8);
+    encode[5]= (int) (37.227*temperature+930.68);
     encode[6]= (char) pWMDutycycle;
     encode[7]= (char) tune;
 
@@ -73,12 +80,6 @@
         encode[8+i]=IDs[i];
     }
 
-    //copy over static fields
-    /* for (int i=2; i<7; i++) {
-         CRC[i-2]= encode[i];
-     }*/
-
-
     // Calculate CRC
     int CRCint = MakeCRC(encode[2],encode[3],encode[4],encode[5],encode[6],IDs,lengthIDs);
 
@@ -90,7 +91,7 @@
     return length;
 }
 
-int MakeCRC(
+int Frame::MakeCRC(
     char LEN, //lenght of frame
     char IDD, //Destination ID
     char TMP0,//first tmp char
@@ -114,4 +115,83 @@
         crcData[i+5]= data[i];
     }
     return calculate_crc16(crcData, lenghtData+5);
+}
+
+void Frame::testEncode(char* frame)
+{
+    printf("testEncode\n\r");
+    char controle[]= {0xAA,0xBB,0x0F,0x0B,0x0F,0xA0,0x02,0x05,0x08,0x09,0x0A,0x4B,0x7C,0xCC,0xDD};
+    //                 SOF  SOF  LEN  IDD  TMP  TMP  PWM  TUN  ID0  ID1  ID2  CRC CRC  EOF  EOF
+    for (int i=0; i<15; i++) {
+        // loop all chars and check if there right
+        if(controle[i]!=frame[i]) {
+            printf("ERROR in frame %d : %X != %X\n\r",i,(unsigned char) controle[i],(unsigned char)frame[i] );
+        }
+    }
+    printf("End testEncode\n\r");
+}
+int Frame::CheckCRC(char* frame)
+{
+    if(crc == MakeCRC((char)(12+lengthIDs),(char)IdDestinationMbed,(char)(temperature>>8),(char)temperature,(char)pWMDutycycle,(char* )IDs,lengthIDs))
+        return 1;
+    else return 0;
+}
+void Frame::AddID(int id)
+{
+    lengthIDs++;
+    char* IDs2 = new char[lengthIDs];
+    for(int i=0; i<lengthIDs; i++) {
+        IDs2[i] = IDs[i];
+    }
+    IDs2[lengthIDs-1]=id;
+
+    char* tempIDs=IDs;
+    IDs=IDs2;
+    delete tempIDs;
+}
+
+int Frame::CheckMyID(int id)
+{
+    for(int i=0; i<lengthIDs; i++) {
+        if(id == IDs[i])
+            return 1;
+    }
+    return 0;
+}
+
+void Frame::setIdDestinationMbed(int IdDestinationMbed)
+{
+    this->IdDestinationMbed=IdDestinationMbed;
+}
+int Frame::getIdDestinationMbed(void)
+{
+    return IdDestinationMbed;
+}
+void Frame::setTemperature(int temperature)
+{
+    this->temperature=temperature;
+}
+int Frame::getTemperature(void)
+{
+    return temperature;
+}
+void Frame::setPWMDutycycle(int pWMDutycycle)
+{
+    this->pWMDutycycle=pWMDutycycle;
+}
+void Frame::setTune(int tune)
+{
+    this->tune=tune;
+}
+int Frame::getTune(void)
+{
+   return tune;
+}
+void Frame::setLengthIDs(int lengthIDs)
+{
+    this->lengthIDs=lengthIDs;
+}
+void Frame::setIDs(char* IDs)
+{
+    this->IDs=IDs;
 }
\ No newline at end of file
--- a/Frame.h	Fri Mar 14 19:41:14 2014 +0000
+++ b/Frame.h	Sun Mar 16 15:08:10 2014 +0000
@@ -9,17 +9,32 @@
     int tune;
     int lengthIDs;
     char* IDs;
-    char* encode;
+    int crc;
 public:
     Frame(int IdDestinationMbed,
         int temperature,
         int pWMDutycycle,
         int tune,
         int lengthIDs,
-        char* IDs,
-        char* encode);
-    int encode(char* encode);
-    int decode(char* frame);
+        char* IDs);
+    Frame();
+    int Encode(char* frame);
+    int Decode(char* frame);
     int MakeCRC(char LEN, char IDD, char TMP0, char TMP1, char PWM, char* data, int lenghtData);
+    void testEncode(char* encode);
+    int CheckCRC(char* frame);
+    int CheckMyID(int id);
+    void AddID(int id);
+    //int Length(char* frame);
+    void setIdDestinationMbed(int IdDestinationMbed);
+    int getIdDestinationMbed(void);
+    void setTemperature(int temperature);
+    int getTemperature(void);
+    void setPWMDutycycle(int pWMDutycycle);
+    void setTune(int tune);
+    int getTune(void);
+    void setLengthIDs(int lengthIDs);
+    void setIDs(char* IDs);
+    
 };
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Potentiometer.cpp	Sun Mar 16 15:08:10 2014 +0000
@@ -0,0 +1,13 @@
+#include "mbed.h"
+#include "Potentiometer.h"
+
+Potentiometer::Potentiometer()
+{
+    potentiometer = new AnalogIn(p15);
+}
+
+int Potentiometer::getPotentiometer()
+{
+    int returnvalue = (int) (potentiometer->read()*100);
+    return returnvalue;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Potentiometer.h	Sun Mar 16 15:08:10 2014 +0000
@@ -0,0 +1,12 @@
+#ifndef Potentiometer_H
+#define Potentiometer_H
+
+class  Potentiometer
+{
+private:
+    AnalogIn* potentiometer;
+public:
+    Potentiometer();
+    int getPotentiometer();
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Server.cpp	Sun Mar 16 15:08:10 2014 +0000
@@ -0,0 +1,78 @@
+#include "mbed.h"
+#include "Server.h"
+#include "EthernetInterface.h"
+
+Server::Server(char* sourceAddress)
+{
+    eth = new EthernetInterface();
+    eth->init(sourceAddress,"255.255.255.0","");
+    eth->connect(500);
+
+    server = new TCPSocketServer();
+    
+    server->bind(4000);
+    server->listen();
+    
+    socket = new TCPSocketConnection();
+    socket->set_blocking(false, 10);
+    server->set_blocking(false, 10);
+    
+
+}
+void Server::printStatus()
+{
+    printf("IP Address is %s\n\r", eth->getIPAddress());
+}
+int Server::connect(char* destinationAddr)
+{
+    socket->set_blocking(false, 10);
+    if(socket->connect(destinationAddr, 4000) < 0) {
+        printf("Unable to connect to (%s) on port (%d)\n\r", destinationAddr, 4000);
+        return 0;
+    }
+    return 1;
+}
+void Server::send(char* data)
+{
+    socket->send_all(data, 256);
+    printf("Send data %s\n\r",data);
+    socket->close();
+}
+int Server::read(char* buffer)
+{
+    resetSocket();
+    server->accept(*socket);
+
+    int connectionRead = 0;
+    if(socket->is_connected()) {
+
+        printf("connection incoming from: %s \n\r",socket->get_address());
+        while(true) {
+            int n = socket->receive(buffer, 256);
+            if (n <= 0) break;
+        }
+        printf("-------------\n\r");
+        printf("\n reciving:  %s\n\r",buffer);
+        printf("\n\r");
+        connectionRead = 1;
+    }
+    socket->close();
+    return connectionRead;
+}
+
+int Server::close()
+{
+    socket->close();
+    return 1;
+}
+Server::~Server()
+{
+    eth->disconnect();
+}
+void Server::resetSocket()
+{
+    socket->close();
+    delete socket;
+    socket = new TCPSocketConnection();
+    socket->set_blocking(false, 10);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Server.h	Sun Mar 16 15:08:10 2014 +0000
@@ -0,0 +1,21 @@
+#include "EthernetInterface.h"
+#ifndef Server_H
+#define Server_H
+
+class  Server {
+    private:
+        EthernetInterface* eth;
+        TCPSocketConnection* socket;
+        TCPSocketServer* server;
+    public:
+        Server(char* sourceAddress);
+        void send(char* data);
+        int read(char* buffer);
+        void printStatus(void);
+        int connect(char* destinationAddress);
+        int close(void);
+        void resetSocket(void);
+        ~Server(); 
+};
+
+#endif
\ No newline at end of file
--- a/TCPServer.cpp	Fri Mar 14 19:41:14 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,78 +0,0 @@
-#include "mbed.h"
-#include "TCPServer.h"
-#include "EthernetInterface.h"
-
-TCPServer::TCPServer(char* sourceAddress)
-{
-    eth = new EthernetInterface();
-    eth->init(sourceAddress,"255.255.255.0","");
-    eth->connect();
-
-    server = new TCPSocketServer();
-    
-    server->bind(4000);
-    server->listen();
-    
-    socket = new TCPSocketConnection();
-    socket->set_blocking(false, 10);
-    server->set_blocking(false, 10);
-    
-
-}
-void TCPServer::printStatus()
-{
-    printf("IP Address is %s\n\r", eth->getIPAddress());
-}
-int TCPServer::connect(char* destinationAddr)
-{
-    socket->set_blocking(false, 10);
-    if(socket->connect(destinationAddr, 4000) < 0) {
-        printf("Unable to connect to (%s) on port (%d)\n\r", destinationAddr, 4000);
-        return 0;
-    }
-    return 1;
-}
-void TCPServer::send(char* data)
-{
-    socket->send_all(data, 256);
-    printf("Send data %s\n\r",data);
-    socket->close();
-}
-int TCPServer::read(char* buffer)
-{
-    resetSocket();
-    server->accept(*socket);
-
-    int connectionRead = 0;
-    if(socket->is_connected()) {
-
-        printf("connection incoming from: %s \n\r",socket->get_address());
-        while(true) {
-            int n = socket->receive(buffer, 256);
-            if (n <= 0) break;
-        }
-        printf("-------------\n\r");
-        printf("\n reciving:  %s\n\r",buffer);
-        printf("\n\r");
-        connectionRead = 1;
-    }
-    socket->close();
-    return connectionRead;
-}
-
-int TCPServer::close()
-{
-    socket->close();
-    return 1;
-}
-TCPServer::~TCPServer()
-{
-    eth->disconnect();
-}
-void TCPServer::resetSocket()
-{
-    socket->close();
-    delete socket;
-    socket = new TCPSocketConnection();
-    socket->set_blocking(false, 10);
-}
\ No newline at end of file
--- a/TCPServer.h	Fri Mar 14 19:41:14 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-#include "EthernetInterface.h"
-#ifndef TCPServer_H
-#define TCPServer_H
-
-class  TCPServer {
-    private:
-        EthernetInterface* eth;
-        TCPSocketConnection* socket;
-        TCPSocketServer* server;
-    public:
-        TCPServer(char* sourceAddress);
-        void send(char* data);
-        int read(char* buffer);
-        void printStatus(void);
-        int connect(char* destinationAddress);
-        int close(void);
-        void resetSocket(void);
-        ~TCPServer(); 
-};
-
-#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102.lib	Sun Mar 16 15:08:10 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/chris/code/TMP102/#7585766ad401
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Temperature.cpp	Sun Mar 16 15:08:10 2014 +0000
@@ -0,0 +1,13 @@
+#include "mbed.h"
+#include "Temperature.h"
+#include "TMP102.h"
+Temperature::Temperature()
+{
+    temperature= new TMP102(p28, p27, 0x90); //A0 pin is connected to ground
+}
+
+//set the text on the display
+double Temperature::getTemperature()
+{
+    return (double) temperature->read();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Temperature.h	Sun Mar 16 15:08:10 2014 +0000
@@ -0,0 +1,12 @@
+#ifndef Temperature_H
+#define Temperature_H
+#include "TMP102.h"
+class  Temperature
+{
+private:
+    TMP102* temperature;
+public:
+    Temperature();
+    double getTemperature();
+};
+#endif
\ No newline at end of file
--- a/main.cpp	Fri Mar 14 19:41:14 2014 +0000
+++ b/main.cpp	Sun Mar 16 15:08:10 2014 +0000
@@ -1,71 +1,90 @@
 #include "mbed.h"
-#include "TCPServer.h"
-//#include "TCPFrame.h"
-#define MYMBED   07
+#include "Server.h"
 #include "Frame.h"
 #include "Tune.h"
 #include "Display.h"
+#include "Temperature.h"
+#include "Potentiometer.h"
 
-DigitalOut connectedLed(LED1); //connected
-DigitalOut RecieveLed(LED2); //recieve
-DigitalOut SendLed(LED3); //send
-InterruptIn button(p8);
-DigitalOut myled(LED1);
-bool SendISR;
-
-void flip()
+InterruptIn buttonSend(p8);
+bool Send;
+void SendISR()
 {
-    SendISR=1;
+    Send=1;
 }
 
+InterruptIn buttonDestinationIpUp(p7);
+int IpDestination=10;
+bool BoolIpDestination;
+char destIp[14];
+void DestinationIpUp()
+{
+    BoolIpDestination=1;
+}
+
+//PWM-signaal gegenereerd op basis van de potentiometerwaarde
+PwmOut PWMout(p22);
 int main()
 {
-    button.rise(&flip);  // attach the address of the flip function to the rising edge
-
-    //pb.mode(PullUp);
-    char testIDs[]= {0x08,0x09,0x0A};
-    char buf[256];
-    encodeTest(11,4000,2,5,3,testIDs,buf);
-
-    TCPServer server("192.168.0.107");
-
-    Display* display=new Display();
-    display->setText("dfnklsdnfsdnf\ntttt");
+    buttonSend.rise(&SendISR);  // attach the address of the flip function to the rising edge
+    buttonDestinationIpUp.rise(&DestinationIpUp);  // attach the address of the flip function to the rising edge
 
-    display->setText("Seppd");
-    /* while(1) {
-         if(!pb) {
-             myled = 1;
-             server.connect("192.168.0.102");
-             server.send(buf);
-         }
-         else{
-             wait(0.5);
-             myled = 1;
-             wait(0.5);
-             myled = 0;
-             }
-         //char buf1[]= {0xAA,0xBB,0x0F,0x0B,0x0F,0xA0,0x02,0x05,0x08,0x09,0x0A,0x4B,0x7C,0xCC,0xDD};
+    char buf[256];
+    Frame* testFrame = new Frame();
+    Server server("192.168.0.107");
+    server.printStatus();
+    Display* display=new Display();
+    display->setText("Seppe's MBED");
+    Tune* tune = new Tune();
+    Temperature temp;
+    Potentiometer* potentiometer = new Potentiometer();
 
-         //char buf1[256];
-     }*/
     while(1) {
         if(server.read(buf)) {
-            server.connect("192.168.0.102");
-            server.send(buf);
+            if(!testFrame->Decode(buf))
+                printf("Frame CRC Error \n\r");
+            if(testFrame->getIdDestinationMbed()==7) {
+                printf("Voor mij\n\r");
+                PWMout=potentiometer->getPotentiometer()/100;
+                tune->play(testFrame->getTune());
+                display->setText("Temp: %d",testFrame->getTemperature());
+                testFrame->setIdDestinationMbed(IpDestination);
+                testFrame->setTemperature(temp.getTemperature());
+                testFrame->setPWMDutycycle(potentiometer->getPotentiometer());
+                testFrame->setTune(testFrame->getTune()+1);
+                testFrame->AddID(7);
+                testFrame->Encode(buf);
+            }
+            if(!testFrame->CheckMyID(7)) {
+                server.connect(destIp);
+                server.send(buf);
+            }
         }
+        if(BoolIpDestination) {
+            if(IpDestination!=15)
+                IpDestination++;
+            else
+                IpDestination=1;
+            sprintf(destIp,"192.168.0.1%02i",IpDestination);
+            display->setText("192.168.0.1%02i",IpDestination);
+            BoolIpDestination=0;
+            wait(0.5);
+        }
+        if(Send) {
+            testFrame->setIdDestinationMbed(IpDestination);
+            testFrame->setTemperature(temp.getTemperature());
+            testFrame->setPWMDutycycle(potentiometer->getPotentiometer());
+            testFrame->setTune(1);
+            testFrame->AddID(7);
+            //char testIDs[]= {0x08,0x09,0x0A};
+            //testFrame->setLengthIDs(3);
+            //testFrame->setIDs(testIDs);
+            testFrame->Encode(buf);
 
-        if(SendISR) {
-            server.connect("192.168.0.102");
+            server.connect(destIp);
             server.send(buf);
-            SendISR=0;
+            Send=0;
+            wait(0.5);
         }
     }
-    //TCPFrame frame(buf1);
-    //frame.decode();
-    //frame.print();
-
-
-
-
 }