Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Committer:
KoiShin_Sakana
Date:
Sat Aug 08 12:39:18 2015 +0000
Revision:
5:e07e380ddb93
Parent:
3:901d18b901b4
update kicker.h; add comment; update dribbler.h(mikan)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KoiShin_Sakana 2:a8bbf677e774 1 /**
KoiShin_Sakana 2:a8bbf677e774 2 * @file : communication.h (1.0)
KoiShin_Sakana 2:a8bbf677e774 3 * @brief : manage communication
KoiShin_Sakana 2:a8bbf677e774 4 * @author : Shinnosuke KOIKE
KoiShin_Sakana 2:a8bbf677e774 5 * @date : 2015/08/06
KoiShin_Sakana 2:a8bbf677e774 6 */
KoiShin_Sakana 2:a8bbf677e774 7
KoiShin_Sakana 2:a8bbf677e774 8 #ifndef COMMUNICATION_H
KoiShin_Sakana 2:a8bbf677e774 9 #define COMMUNICATION_H
KoiShin_Sakana 2:a8bbf677e774 10
KoiShin_Sakana 2:a8bbf677e774 11 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 12
KoiShin_Sakana 2:a8bbf677e774 13 class CommuMaster {
KoiShin_Sakana 2:a8bbf677e774 14 public:
KoiShin_Sakana 2:a8bbf677e774 15 CommuMaster(PinName sda, PinName scl);
KoiShin_Sakana 3:901d18b901b4 16 ~CommuMaster();
KoiShin_Sakana 3:901d18b901b4 17 void init(int correctFlequency, char addr);
KoiShin_Sakana 3:901d18b901b4 18 void receiveData(char data[]);
KoiShin_Sakana 2:a8bbf677e774 19
KoiShin_Sakana 2:a8bbf677e774 20 private:
KoiShin_Sakana 2:a8bbf677e774 21 I2C* i2cmaster;
KoiShin_Sakana 3:901d18b901b4 22 char addr;
KoiShin_Sakana 2:a8bbf677e774 23 };
KoiShin_Sakana 2:a8bbf677e774 24
KoiShin_Sakana 2:a8bbf677e774 25 class CommuSlave {
KoiShin_Sakana 2:a8bbf677e774 26 public:
KoiShin_Sakana 2:a8bbf677e774 27 CommuSlave(PinName sda, PinName scl);
KoiShin_Sakana 3:901d18b901b4 28 ~CommuSlave();
KoiShin_Sakana 3:901d18b901b4 29 void init(int correctFlequency, char addr);
KoiShin_Sakana 3:901d18b901b4 30 void sendData(char data[]);
KoiShin_Sakana 2:a8bbf677e774 31
KoiShin_Sakana 2:a8bbf677e774 32 private:
KoiShin_Sakana 2:a8bbf677e774 33 I2CSlave* i2cslave;
KoiShin_Sakana 2:a8bbf677e774 34 };
KoiShin_Sakana 2:a8bbf677e774 35
KoiShin_Sakana 2:a8bbf677e774 36 // initialize
KoiShin_Sakana 2:a8bbf677e774 37 CommuMaster::CommuMaster(PinName sda, PinName scl) {
KoiShin_Sakana 2:a8bbf677e774 38 i2cmaster = new I2C(sda, scl);
KoiShin_Sakana 2:a8bbf677e774 39 }
KoiShin_Sakana 2:a8bbf677e774 40
KoiShin_Sakana 3:901d18b901b4 41 CommuMaster::~CommuMaster() {
KoiShin_Sakana 3:901d18b901b4 42 delete i2cmaster;
KoiShin_Sakana 3:901d18b901b4 43 }
KoiShin_Sakana 3:901d18b901b4 44
KoiShin_Sakana 5:e07e380ddb93 45 // set frequency and address
KoiShin_Sakana 3:901d18b901b4 46 void CommuMaster::init(int correctFrequency, char addr) {
KoiShin_Sakana 3:901d18b901b4 47 i2cmaster->frequency(correctFrequency);
KoiShin_Sakana 3:901d18b901b4 48 this->addr = addr;
KoiShin_Sakana 3:901d18b901b4 49 }
KoiShin_Sakana 3:901d18b901b4 50
KoiShin_Sakana 2:a8bbf677e774 51 // receive some data
KoiShin_Sakana 3:901d18b901b4 52 void CommuMaster::receiveData(char data[]) {
KoiShin_Sakana 3:901d18b901b4 53 i2cmaster->read(this->addr | 1, data, 8, false);
KoiShin_Sakana 2:a8bbf677e774 54 }
KoiShin_Sakana 2:a8bbf677e774 55
KoiShin_Sakana 2:a8bbf677e774 56 // initialize
KoiShin_Sakana 2:a8bbf677e774 57 CommuSlave::CommuSlave(PinName sda, PinName scl) {
KoiShin_Sakana 2:a8bbf677e774 58 i2cslave = new I2CSlave(sda, scl);
KoiShin_Sakana 2:a8bbf677e774 59 }
KoiShin_Sakana 2:a8bbf677e774 60
KoiShin_Sakana 3:901d18b901b4 61 CommuSlave::~CommuSlave() {
KoiShin_Sakana 3:901d18b901b4 62 delete i2cslave;
KoiShin_Sakana 3:901d18b901b4 63 }
KoiShin_Sakana 3:901d18b901b4 64
KoiShin_Sakana 5:e07e380ddb93 65 // set frequency and address
KoiShin_Sakana 3:901d18b901b4 66 void CommuSlave::init(int correctFrequency, char addr) {
KoiShin_Sakana 2:a8bbf677e774 67 i2cslave->frequency(correctFrequency);
KoiShin_Sakana 2:a8bbf677e774 68 i2cslave->address(addr);
KoiShin_Sakana 3:901d18b901b4 69 }
KoiShin_Sakana 3:901d18b901b4 70
KoiShin_Sakana 3:901d18b901b4 71 // send some data
KoiShin_Sakana 3:901d18b901b4 72 void CommuSlave::sendData(char data[]) {
KoiShin_Sakana 3:901d18b901b4 73 i2cslave->write(data, 8);
KoiShin_Sakana 2:a8bbf677e774 74 }
KoiShin_Sakana 2:a8bbf677e774 75
KoiShin_Sakana 2:a8bbf677e774 76 #endif
KoiShin_Sakana 2:a8bbf677e774 77
KoiShin_Sakana 2:a8bbf677e774 78 /**
KoiShin_Sakana 2:a8bbf677e774 79 * example program(master)
KoiShin_Sakana 2:a8bbf677e774 80
KoiShin_Sakana 2:a8bbf677e774 81 #include "mbed.h"
KoiShin_Sakana 5:e07e380ddb93 82 #include "communication.h"
KoiShin_Sakana 2:a8bbf677e774 83
KoiShin_Sakana 2:a8bbf677e774 84 #define DATA_NUM 8
KoiShin_Sakana 2:a8bbf677e774 85
KoiShin_Sakana 2:a8bbf677e774 86 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 87 CommuMaster master(D5, D6);
KoiShin_Sakana 3:901d18b901b4 88 int freq = 2000;
KoiShin_Sakana 2:a8bbf677e774 89 char addr = 0xA0;
KoiShin_Sakana 2:a8bbf677e774 90 char data[DATA_NUM];
KoiShin_Sakana 3:901d18b901b4 91 master.init(freq, addr);
KoiShin_Sakana 2:a8bbf677e774 92
KoiShin_Sakana 2:a8bbf677e774 93 while (1) {
KoiShin_Sakana 3:901d18b901b4 94 master.receiveData(data);
KoiShin_Sakana 2:a8bbf677e774 95 for (int i = 0; i < DATA_NUM; i++) {
KoiShin_Sakana 5:e07e380ddb93 96 pc.printf("%d ", data[i]); // display "1 1 0 0 0 0 0 1 "
KoiShin_Sakana 2:a8bbf677e774 97 }
KoiShin_Sakana 2:a8bbf677e774 98 pc.putchar('\n');
KoiShin_Sakana 2:a8bbf677e774 99 }
KoiShin_Sakana 2:a8bbf677e774 100 }
KoiShin_Sakana 2:a8bbf677e774 101
KoiShin_Sakana 2:a8bbf677e774 102 * example program(slave)
KoiShin_Sakana 2:a8bbf677e774 103
KoiShin_Sakana 2:a8bbf677e774 104 #include "mbed.h"
KoiShin_Sakana 5:e07e380ddb93 105 #include "communication.h"
KoiShin_Sakana 2:a8bbf677e774 106
KoiShin_Sakana 2:a8bbf677e774 107 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 108 CommuSlave slave(D5, D6);
KoiShin_Sakana 3:901d18b901b4 109 int freq = 2000;
KoiShin_Sakana 2:a8bbf677e774 110 char addr = 0xA0;
KoiShin_Sakana 2:a8bbf677e774 111 char data[8] = {1, 1, 0, 0, 0, 0, 0, 1};
KoiShin_Sakana 3:901d18b901b4 112 slave.init(freq, addr);
KoiShin_Sakana 2:a8bbf677e774 113
KoiShin_Sakana 2:a8bbf677e774 114 while (1) {
KoiShin_Sakana 3:901d18b901b4 115 slave.sendData(data);
KoiShin_Sakana 2:a8bbf677e774 116 }
KoiShin_Sakana 2:a8bbf677e774 117 }
KoiShin_Sakana 2:a8bbf677e774 118 */