Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Revision:
3:901d18b901b4
Parent:
2:a8bbf677e774
Child:
5:e07e380ddb93
--- a/communication.h	Thu Aug 06 00:19:29 2015 +0000
+++ b/communication.h	Fri Aug 07 06:34:36 2015 +0000
@@ -13,16 +13,21 @@
 class CommuMaster {
 public:
     CommuMaster(PinName sda, PinName scl);
-    void receiveData(int correctFrequency, char addr, char data[], char length);
+    ~CommuMaster();
+    void init(int correctFlequency, char addr);
+    void receiveData(char data[]);
 
 private:
     I2C* i2cmaster;
+    char addr;
 };
 
 class CommuSlave {
 public:
     CommuSlave(PinName sda, PinName scl);
-    void sendData(int correctFrequency, char addr, char data[], char length);
+    ~CommuSlave();
+    void init(int correctFlequency, char addr);
+    void sendData(char data[]);
 
 private:
     I2CSlave* i2cslave;
@@ -33,10 +38,18 @@
     i2cmaster = new I2C(sda, scl);
 }
 
+CommuMaster::~CommuMaster() {
+    delete i2cmaster;
+}
+
+void CommuMaster::init(int correctFrequency, char addr) {
+    i2cmaster->frequency(correctFrequency);
+    this->addr = addr;
+}
+
 // receive some data
-void CommuMaster::receiveData(int correctFrequency, char addr, char data[], char length) {
-    i2cmaster->frequency(correctFrequency);
-    i2cmaster->read(addr | 1, data, length, false);
+void CommuMaster::receiveData(char data[]) {
+    i2cmaster->read(this->addr | 1, data, 8, false);
 }
 
 // initialize
@@ -44,11 +57,18 @@
     i2cslave = new I2CSlave(sda, scl);
 }
 
-// send some data
-void CommuSlave::sendData(int correctFrequency, char addr, char data[], char length) {
+CommuSlave::~CommuSlave() {
+    delete i2cslave;
+}
+
+void CommuSlave::init(int correctFrequency, char addr) {
     i2cslave->frequency(correctFrequency);
     i2cslave->address(addr);
-    i2cslave->write(data, length);
+}
+
+// send some data
+void CommuSlave::sendData(char data[]) {
+    i2cslave->write(data, 8);
 }
 
 #endif
@@ -62,13 +82,13 @@
 
 int main(void) {
     CommuMaster master(D5, D6);
+    int  freq = 2000;
     char addr = 0xA0;
-    int  freq = 2000;
     char data[DATA_NUM];
-    char length = 8;
+    master.init(freq, addr);
 
     while (1) {
-        master.receiveData(freq, addr, data, length);
+        master.receiveData(data);
         for (int i = 0; i < DATA_NUM; i++) {
             pc.printf("%d ", data[i]);
         }
@@ -82,13 +102,13 @@
 
 int main(void) {
     CommuSlave slave(D5, D6);
+    int  freq = 2000;
     char addr = 0xA0;
-    int  freq = 2000;
     char data[8] = {1, 1, 0, 0, 0, 0, 0, 1};
-    char length;
+    slave.init(freq, addr);
 
     while (1) {
-        slave.sendData(freq, addr, data, length);
+        slave.sendData(data);
     }
 }
 */