Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Committer:
KoiShin_Sakana
Date:
Fri Aug 07 06:34:36 2015 +0000
Revision:
3:901d18b901b4
Parent:
2:a8bbf677e774
Child:
5:e07e380ddb93
update communication.h and infrared.h; create dribbler.h, kicker.h and line.h

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 3:901d18b901b4 45 void CommuMaster::init(int correctFrequency, char addr) {
KoiShin_Sakana 3:901d18b901b4 46 i2cmaster->frequency(correctFrequency);
KoiShin_Sakana 3:901d18b901b4 47 this->addr = addr;
KoiShin_Sakana 3:901d18b901b4 48 }
KoiShin_Sakana 3:901d18b901b4 49
KoiShin_Sakana 2:a8bbf677e774 50 // receive some data
KoiShin_Sakana 3:901d18b901b4 51 void CommuMaster::receiveData(char data[]) {
KoiShin_Sakana 3:901d18b901b4 52 i2cmaster->read(this->addr | 1, data, 8, false);
KoiShin_Sakana 2:a8bbf677e774 53 }
KoiShin_Sakana 2:a8bbf677e774 54
KoiShin_Sakana 2:a8bbf677e774 55 // initialize
KoiShin_Sakana 2:a8bbf677e774 56 CommuSlave::CommuSlave(PinName sda, PinName scl) {
KoiShin_Sakana 2:a8bbf677e774 57 i2cslave = new I2CSlave(sda, scl);
KoiShin_Sakana 2:a8bbf677e774 58 }
KoiShin_Sakana 2:a8bbf677e774 59
KoiShin_Sakana 3:901d18b901b4 60 CommuSlave::~CommuSlave() {
KoiShin_Sakana 3:901d18b901b4 61 delete i2cslave;
KoiShin_Sakana 3:901d18b901b4 62 }
KoiShin_Sakana 3:901d18b901b4 63
KoiShin_Sakana 3:901d18b901b4 64 void CommuSlave::init(int correctFrequency, char addr) {
KoiShin_Sakana 2:a8bbf677e774 65 i2cslave->frequency(correctFrequency);
KoiShin_Sakana 2:a8bbf677e774 66 i2cslave->address(addr);
KoiShin_Sakana 3:901d18b901b4 67 }
KoiShin_Sakana 3:901d18b901b4 68
KoiShin_Sakana 3:901d18b901b4 69 // send some data
KoiShin_Sakana 3:901d18b901b4 70 void CommuSlave::sendData(char data[]) {
KoiShin_Sakana 3:901d18b901b4 71 i2cslave->write(data, 8);
KoiShin_Sakana 2:a8bbf677e774 72 }
KoiShin_Sakana 2:a8bbf677e774 73
KoiShin_Sakana 2:a8bbf677e774 74 #endif
KoiShin_Sakana 2:a8bbf677e774 75
KoiShin_Sakana 2:a8bbf677e774 76 /**
KoiShin_Sakana 2:a8bbf677e774 77 * example program(master)
KoiShin_Sakana 2:a8bbf677e774 78
KoiShin_Sakana 2:a8bbf677e774 79 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 80
KoiShin_Sakana 2:a8bbf677e774 81 #define DATA_NUM 8
KoiShin_Sakana 2:a8bbf677e774 82
KoiShin_Sakana 2:a8bbf677e774 83 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 84 CommuMaster master(D5, D6);
KoiShin_Sakana 3:901d18b901b4 85 int freq = 2000;
KoiShin_Sakana 2:a8bbf677e774 86 char addr = 0xA0;
KoiShin_Sakana 2:a8bbf677e774 87 char data[DATA_NUM];
KoiShin_Sakana 3:901d18b901b4 88 master.init(freq, addr);
KoiShin_Sakana 2:a8bbf677e774 89
KoiShin_Sakana 2:a8bbf677e774 90 while (1) {
KoiShin_Sakana 3:901d18b901b4 91 master.receiveData(data);
KoiShin_Sakana 2:a8bbf677e774 92 for (int i = 0; i < DATA_NUM; i++) {
KoiShin_Sakana 2:a8bbf677e774 93 pc.printf("%d ", data[i]);
KoiShin_Sakana 2:a8bbf677e774 94 }
KoiShin_Sakana 2:a8bbf677e774 95 pc.putchar('\n');
KoiShin_Sakana 2:a8bbf677e774 96 }
KoiShin_Sakana 2:a8bbf677e774 97 }
KoiShin_Sakana 2:a8bbf677e774 98
KoiShin_Sakana 2:a8bbf677e774 99 * example program(slave)
KoiShin_Sakana 2:a8bbf677e774 100
KoiShin_Sakana 2:a8bbf677e774 101 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 102
KoiShin_Sakana 2:a8bbf677e774 103 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 104 CommuSlave slave(D5, D6);
KoiShin_Sakana 3:901d18b901b4 105 int freq = 2000;
KoiShin_Sakana 2:a8bbf677e774 106 char addr = 0xA0;
KoiShin_Sakana 2:a8bbf677e774 107 char data[8] = {1, 1, 0, 0, 0, 0, 0, 1};
KoiShin_Sakana 3:901d18b901b4 108 slave.init(freq, addr);
KoiShin_Sakana 2:a8bbf677e774 109
KoiShin_Sakana 2:a8bbf677e774 110 while (1) {
KoiShin_Sakana 3:901d18b901b4 111 slave.sendData(data);
KoiShin_Sakana 2:a8bbf677e774 112 }
KoiShin_Sakana 2:a8bbf677e774 113 }
KoiShin_Sakana 2:a8bbf677e774 114 */