Create this program
Dependencies: mbed HCSR04 HMC6352 PID TextLCD
Diff: communication.h
- Revision:
- 2:a8bbf677e774
- Child:
- 3:901d18b901b4
diff -r d0cada9a5381 -r a8bbf677e774 communication.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/communication.h Thu Aug 06 00:19:29 2015 +0000 @@ -0,0 +1,94 @@ +/** + * @file : communication.h (1.0) + * @brief : manage communication + * @author : Shinnosuke KOIKE + * @date : 2015/08/06 + */ + +#ifndef COMMUNICATION_H +#define COMMUNICATION_H + +#include "mbed.h" + +class CommuMaster { +public: + CommuMaster(PinName sda, PinName scl); + void receiveData(int correctFrequency, char addr, char data[], char length); + +private: + I2C* i2cmaster; +}; + +class CommuSlave { +public: + CommuSlave(PinName sda, PinName scl); + void sendData(int correctFrequency, char addr, char data[], char length); + +private: + I2CSlave* i2cslave; +}; + +// initialize +CommuMaster::CommuMaster(PinName sda, PinName scl) { + i2cmaster = new I2C(sda, scl); +} + +// receive some data +void CommuMaster::receiveData(int correctFrequency, char addr, char data[], char length) { + i2cmaster->frequency(correctFrequency); + i2cmaster->read(addr | 1, data, length, false); +} + +// initialize +CommuSlave::CommuSlave(PinName sda, PinName scl) { + i2cslave = new I2CSlave(sda, scl); +} + +// send some data +void CommuSlave::sendData(int correctFrequency, char addr, char data[], char length) { + i2cslave->frequency(correctFrequency); + i2cslave->address(addr); + i2cslave->write(data, length); +} + +#endif + +/** + * example program(master) + +#include "mbed.h" + +#define DATA_NUM 8 + +int main(void) { + CommuMaster master(D5, D6); + char addr = 0xA0; + int freq = 2000; + char data[DATA_NUM]; + char length = 8; + + while (1) { + master.receiveData(freq, addr, data, length); + for (int i = 0; i < DATA_NUM; i++) { + pc.printf("%d ", data[i]); + } + pc.putchar('\n'); + } +} + + * example program(slave) + +#include "mbed.h" + +int main(void) { + CommuSlave slave(D5, D6); + char addr = 0xA0; + int freq = 2000; + char data[8] = {1, 1, 0, 0, 0, 0, 0, 1}; + char length; + + while (1) { + slave.sendData(freq, addr, data, length); + } +} +*/