jkjkjkjk

Dependencies:   mbed-rtos mbed

Fork of all_combined_week6 by green rosh

Revision:
3:9b597ed04ef4
Parent:
0:cbe0ea884289
Child:
4:79b97f9cab00
--- a/slave.cpp	Fri Jul 11 05:14:12 2014 +0000
+++ b/slave.cpp	Fri Jul 11 08:54:12 2014 +0000
@@ -1,98 +1,89 @@
 #include "slave.h"
-SensorData Sensor;
+#include "HK.h"
+ 
+extern SensorData Sensor;
 I2CSlave slave(p28,p27);                       //configuring pins p27, p28 as I2Cslave
-Serial green(USBTX, USBRX);
-
+Serial screen (USBTX,USBRX);
 void write_to_master(char send)                           //function to write data to master
         {
            int acknowledge; 
+           int loopvariable4=1;
+           while(loopvariable4)
+           {
            acknowledge = slave.write(send);    //sending the byte to master
            if(acknowledge==1)
             {
-              green.printf(" acknowledge %d sent %x \n",acknowledge,send);
+              screen.printf(" acknowledge %d sent %x \n",acknowledge,send);
+              loopvariable4=0;
             }
+           } 
         }
         
- void split(float data)                      //function to split data into 4 individual bytes and send to master
-        {
-            union convert
-              {              
-                   char byte[4];             //array containing individual bytes
-                   float number;             //floating point number to be split into four bytes 
-              }v;
-              v.number=data; 
-              write_to_master(v.byte[0]);
-              write_to_master(v.byte[1]);
-              write_to_master(v.byte[2]);
-              write_to_master(v.byte[3]);
-              green.printf("sent %f\n",v.number);
-        }
-
-
-
-void FUNC_I2C_SLAVE_MAIN()
+ 
+ 
+ 
+void FUNC_I2C_SLAVE_MAIN(int iterations)
 {
     wait(0.5);
-    slave.address(0x20);                           //assigning slave address
-    int Switch_Variable;
-    int ReadAddressed=1;
-    int WriteGeneral=3;
+    screen.printf("\nSlave entered\n");
+    slave.address(slave_address);                           //assigning slave address
+    char Switch_Variable;
+    
+    
     int loopvariable1=1;
     int loopvariable2=0;
+    int loopvariable3=1;
 //initialising dummy sensor data                         
-//    SensorData Sensor;
-    Sensor.voltage[0]=0.1;Sensor.current[0]=0.0;Sensor.temp[0]=1.0;
-    Sensor.voltage[1]=0.1;Sensor.current[1]=1.1;Sensor.temp[1]=2.0;
-    Sensor.voltage[2]=0.2;Sensor.current[2]=2.2;Sensor.temp[2]=2.0;
-    Sensor.voltage[3]=0.3;Sensor.current[3]=3.3;Sensor.temp[3]=3.0;
-    Sensor.voltage[4]=0.4;Sensor.current[4]=4.4;Sensor.temp[4]=4.0;
-    Sensor.voltage[5]=0.5;Sensor.current[5]=5.5;Sensor.temp[5]=5.0;
-    Sensor.voltage[6]=0.6;Sensor.current[6]=6.6;Sensor.temp[6]=6.0;
-    Sensor.voltage[7]=0.7;Sensor.current[7]=7.7;Sensor.temp[7]=7.0;
-    Sensor.voltage[8]=0.8;Sensor.current[8]=8.8;Sensor.temp[8]=8.0;
-    Sensor.voltage[9]=0.9;Sensor.current[9]=9.9;Sensor.temp[9]=20.5;
+    
     while(loopvariable1)
     { 
 //to read data from master       
     if(slave.receive()==WriteGeneral)                   //checking if slave is addressed to write
     {
       Switch_Variable=slave.read();                   //receiving data
-      green.printf("switch variable=%d\n",Switch_Variable);
+      screen.printf("switch variable=%d\n",Switch_Variable);
       slave.stop();                          //reset slave to default receiving state
       loopvariable1=0;
 //to interpret and write data to master       
       switch(Switch_Variable)
       {
       
-       case 1: while(loopvariable2<30)
-                {
+       case '1':    while(loopvariable3)
+                  {
                      if(slave.receive()==ReadAddressed)             //check if slave is addressed to read 
                    {
+                      while(loopvariable2<iterations)
+                     {
                       if(loopvariable2%3==0)
                           {
-                          green.printf("\nvoltage%d\n",loopvariable2/3);
-                          split(Sensor.voltage[loopvariable2/3]);
+                          screen.printf("\nvoltage%d\n",loopvariable2/3);
+                          write_to_master(Sensor.Voltage[loopvariable2/3]);
                           }
                       else if(loopvariable2%3==1)
                           {   
-                          green.printf("\ncurrent%d\n",loopvariable2/3);
-                          split(Sensor.current[loopvariable2/3]);
+                          screen.printf("\ncurrent%d\n",loopvariable2/3);
+                          write_to_master(Sensor.Current[loopvariable2/3]);
                           }
                       else if(loopvariable2%3==2)
                           {   
-                          green.printf("\ntemp%d\n",loopvariable2/3);
-                          split(Sensor.temp[loopvariable2/3]);
+                          screen.printf("\ntemp%d\n",loopvariable2/3);
+                          write_to_master(Sensor.Temperature[loopvariable2/3]);
                           }         
                       loopvariable2++;
-                      slave.stop();      
-                    }
-                  }
+                       }//while(loopvariable2<30)
+                      slave.stop();    
+                      loopvariable3=0;  
+                    }//if(read addressed)
+                    
+                    }//while(loopvariable3)
+                  
                   break;
-         case 2 : green.printf(" telecommand 2\n");
+         case '2' : screen.printf(" telecommand 2\n");
                   break;         
                  
         }//switch case ends
    }   
 }
-   green.printf("done");
+   screen.printf("done");
 }
+ 
\ No newline at end of file