sqefqsdf
Dependencies: C12832 EthernetInterface LM75B mbed-rtos mbed
Fork of app-board-LM75B by
IOController.cpp@6:77a4c45f6416, 2017-03-23 (annotated)
- Committer:
- gimohd
- Date:
- Thu Mar 23 12:51:27 2017 +0000
- Revision:
- 6:77a4c45f6416
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gimohd | 6:77a4c45f6416 | 1 | #include "IOController.h" |
gimohd | 6:77a4c45f6416 | 2 | |
gimohd | 6:77a4c45f6416 | 3 | IOController::IOController() |
gimohd | 6:77a4c45f6416 | 4 | { |
gimohd | 6:77a4c45f6416 | 5 | |
gimohd | 6:77a4c45f6416 | 6 | printf("-------------STARTING PROGRAM------------\r\n"); |
gimohd | 6:77a4c45f6416 | 7 | printf("-----------------------------------------\r\n"); |
gimohd | 6:77a4c45f6416 | 8 | |
gimohd | 6:77a4c45f6416 | 9 | //INPUT |
gimohd | 6:77a4c45f6416 | 10 | temperatureSensor = new TemperatureSensor(p28, p27); |
gimohd | 6:77a4c45f6416 | 11 | potentiometer = new Potentiometer(p19); |
gimohd | 6:77a4c45f6416 | 12 | joystick = new Joystick(p15,p12,p13,p16,p14); |
gimohd | 6:77a4c45f6416 | 13 | |
gimohd | 6:77a4c45f6416 | 14 | //OUTPUT |
gimohd | 6:77a4c45f6416 | 15 | speaker = new Speaker(p26); |
gimohd | 6:77a4c45f6416 | 16 | rgbLed = new RGB(p23,p24,p25); |
gimohd | 6:77a4c45f6416 | 17 | lcd = new LCD(p5, p7, p6, p8, p11); |
gimohd | 6:77a4c45f6416 | 18 | destinationID = 114; |
gimohd | 6:77a4c45f6416 | 19 | |
gimohd | 6:77a4c45f6416 | 20 | //ETHERNET |
gimohd | 6:77a4c45f6416 | 21 | //communication = new Communication(); |
gimohd | 6:77a4c45f6416 | 22 | receivePacket = new Packet(); |
gimohd | 6:77a4c45f6416 | 23 | /* |
gimohd | 6:77a4c45f6416 | 24 | std::vector<uint8_t> IDN; |
gimohd | 6:77a4c45f6416 | 25 | IDN.push_back(110); |
gimohd | 6:77a4c45f6416 | 26 | IDN.push_back(113); |
gimohd | 6:77a4c45f6416 | 27 | std::vector<int16_t> TMP; |
gimohd | 6:77a4c45f6416 | 28 | TMP.push_back(temperatureSensor->temp_short()); |
gimohd | 6:77a4c45f6416 | 29 | TMP.push_back(temperatureSensor->temp_short()); |
gimohd | 6:77a4c45f6416 | 30 | receivePacket = new Packet(2, 111, 240, IDN , TMP); |
gimohd | 6:77a4c45f6416 | 31 | printf("Packet 1 :'%s'\r\n",receivePacket->toDataString().c_str()); |
gimohd | 6:77a4c45f6416 | 32 | receivePacket2 = new Packet(); |
gimohd | 6:77a4c45f6416 | 33 | receivePacket2->dataInput(receivePacket->toDataString()); |
gimohd | 6:77a4c45f6416 | 34 | printf("Packet 2 :'%s'\r\n",receivePacket2->toDataString().c_str()); |
gimohd | 6:77a4c45f6416 | 35 | std::string str = receivePacket->toDataString(); |
gimohd | 6:77a4c45f6416 | 36 | for (int i =0; i<str.size();i++){ |
gimohd | 6:77a4c45f6416 | 37 | printf("char i: %c\r\n",str[i]); |
gimohd | 6:77a4c45f6416 | 38 | } |
gimohd | 6:77a4c45f6416 | 39 | float s =temperatureSensor->average(TMP); |
gimohd | 6:77a4c45f6416 | 40 | printf("average: %f",s); |
gimohd | 6:77a4c45f6416 | 41 | */ |
gimohd | 6:77a4c45f6416 | 42 | startReceiving = false; |
gimohd | 6:77a4c45f6416 | 43 | rgbLed->off(); |
gimohd | 6:77a4c45f6416 | 44 | } |
gimohd | 6:77a4c45f6416 | 45 | |
gimohd | 6:77a4c45f6416 | 46 | IOController::~IOController() |
gimohd | 6:77a4c45f6416 | 47 | { |
gimohd | 6:77a4c45f6416 | 48 | delete temperatureSensor; |
gimohd | 6:77a4c45f6416 | 49 | delete potentiometer; |
gimohd | 6:77a4c45f6416 | 50 | delete joystick; |
gimohd | 6:77a4c45f6416 | 51 | delete speaker; |
gimohd | 6:77a4c45f6416 | 52 | delete rgbLed; |
gimohd | 6:77a4c45f6416 | 53 | delete lcd; |
gimohd | 6:77a4c45f6416 | 54 | delete receivePacket; |
gimohd | 6:77a4c45f6416 | 55 | delete communication; |
gimohd | 6:77a4c45f6416 | 56 | } |
gimohd | 6:77a4c45f6416 | 57 | |
gimohd | 6:77a4c45f6416 | 58 | int IOController::run() |
gimohd | 6:77a4c45f6416 | 59 | { |
gimohd | 6:77a4c45f6416 | 60 | Joystick::Direction direction = joystick-> getDirection();//get direction input |
gimohd | 6:77a4c45f6416 | 61 | switch(direction) { |
gimohd | 6:77a4c45f6416 | 62 | case Joystick::UP: { |
gimohd | 6:77a4c45f6416 | 63 | lcd->cls(); |
gimohd | 6:77a4c45f6416 | 64 | lcd->locate(0,3); |
gimohd | 6:77a4c45f6416 | 65 | lcd->printf("Receiving mode"); |
gimohd | 6:77a4c45f6416 | 66 | communication = new Communication(); |
gimohd | 6:77a4c45f6416 | 67 | communication->connect(); |
gimohd | 6:77a4c45f6416 | 68 | startReceiving = true; |
gimohd | 6:77a4c45f6416 | 69 | break; |
gimohd | 6:77a4c45f6416 | 70 | } |
gimohd | 6:77a4c45f6416 | 71 | case Joystick::DOWN: { |
gimohd | 6:77a4c45f6416 | 72 | |
gimohd | 6:77a4c45f6416 | 73 | /*float temperature = temperatureSensor->read(); |
gimohd | 6:77a4c45f6416 | 74 | lcd->cls(); |
gimohd | 6:77a4c45f6416 | 75 | lcd->locate(0,3); |
gimohd | 6:77a4c45f6416 | 76 | lcd->printf("Temp = %.3f\n", temperature); |
gimohd | 6:77a4c45f6416 | 77 | */ |
gimohd | 6:77a4c45f6416 | 78 | rgbLed->setRgbColor(potentiometer->readValue()); |
gimohd | 6:77a4c45f6416 | 79 | std::string test; |
gimohd | 6:77a4c45f6416 | 80 | //12, 101, 1, 8, 230, 1, 105 |
gimohd | 6:77a4c45f6416 | 81 | char ca[] = {0xAA,0x00,0xFF,0x55}; |
gimohd | 6:77a4c45f6416 | 82 | |
gimohd | 6:77a4c45f6416 | 83 | for (int i = 0; i<sizeof(ca);i++){ |
gimohd | 6:77a4c45f6416 | 84 | test.push_back(ca[i]); |
gimohd | 6:77a4c45f6416 | 85 | } |
gimohd | 6:77a4c45f6416 | 86 | char c = receivePacket->crc(test,0); |
gimohd | 6:77a4c45f6416 | 87 | lcd->cls(); |
gimohd | 6:77a4c45f6416 | 88 | lcd->locate(0,3); |
gimohd | 6:77a4c45f6416 | 89 | lcd->printf("Dest. ID= %d\n", c); |
gimohd | 6:77a4c45f6416 | 90 | break; |
gimohd | 6:77a4c45f6416 | 91 | } |
gimohd | 6:77a4c45f6416 | 92 | |
gimohd | 6:77a4c45f6416 | 93 | case Joystick::RIGHT: { |
gimohd | 6:77a4c45f6416 | 94 | destinationID = (destinationID < 117) ? destinationID+1 : destinationID; |
gimohd | 6:77a4c45f6416 | 95 | lcd->cls(); |
gimohd | 6:77a4c45f6416 | 96 | lcd->locate(0,3); |
gimohd | 6:77a4c45f6416 | 97 | lcd->printf("Dest. ID= %d\n", destinationID); |
gimohd | 6:77a4c45f6416 | 98 | wait_ms(300); |
gimohd | 6:77a4c45f6416 | 99 | break; |
gimohd | 6:77a4c45f6416 | 100 | } |
gimohd | 6:77a4c45f6416 | 101 | |
gimohd | 6:77a4c45f6416 | 102 | case Joystick::LEFT: { |
gimohd | 6:77a4c45f6416 | 103 | destinationID = (destinationID > 100) ? destinationID-1 : destinationID; |
gimohd | 6:77a4c45f6416 | 104 | lcd->cls(); |
gimohd | 6:77a4c45f6416 | 105 | lcd->locate(0,3); |
gimohd | 6:77a4c45f6416 | 106 | lcd->printf("Dest ID= %d\n", destinationID); |
gimohd | 6:77a4c45f6416 | 107 | wait_ms(300); |
gimohd | 6:77a4c45f6416 | 108 | break; |
gimohd | 6:77a4c45f6416 | 109 | } |
gimohd | 6:77a4c45f6416 | 110 | case Joystick::MIDDLE: { |
gimohd | 6:77a4c45f6416 | 111 | communication = new Communication(); |
gimohd | 6:77a4c45f6416 | 112 | communication->setOwnID(114); |
gimohd | 6:77a4c45f6416 | 113 | communication->connect(); |
gimohd | 6:77a4c45f6416 | 114 | destinationID = 113; |
gimohd | 6:77a4c45f6416 | 115 | receivePacket->addValues(receivePacket->getNUM(),destinationID,potentiometer->readValue(), communication->getOwnID() ,temperatureSensor->temp_short()); |
gimohd | 6:77a4c45f6416 | 116 | lcd->cls(); |
gimohd | 6:77a4c45f6416 | 117 | lcd->locate(0,3); |
gimohd | 6:77a4c45f6416 | 118 | lcd->printf("Sending to %d\n", destinationID); |
gimohd | 6:77a4c45f6416 | 119 | communication->sendDataPacket(receivePacket->toDataString()); |
gimohd | 6:77a4c45f6416 | 120 | startReceiving = true; |
gimohd | 6:77a4c45f6416 | 121 | wait_ms(500); |
gimohd | 6:77a4c45f6416 | 122 | break; |
gimohd | 6:77a4c45f6416 | 123 | } |
gimohd | 6:77a4c45f6416 | 124 | default: { |
gimohd | 6:77a4c45f6416 | 125 | if(startReceiving) { |
gimohd | 6:77a4c45f6416 | 126 | std::string buf; |
gimohd | 6:77a4c45f6416 | 127 | //get data from other mbed |
gimohd | 6:77a4c45f6416 | 128 | buf = communication->getData(); |
gimohd | 6:77a4c45f6416 | 129 | |
gimohd | 6:77a4c45f6416 | 130 | //info into Packet |
gimohd | 6:77a4c45f6416 | 131 | receivePacket->dataInput(buf); |
gimohd | 6:77a4c45f6416 | 132 | |
gimohd | 6:77a4c45f6416 | 133 | //Set color |
gimohd | 6:77a4c45f6416 | 134 | rgbLed->setRgbColor(receivePacket->getPWM()); |
gimohd | 6:77a4c45f6416 | 135 | |
gimohd | 6:77a4c45f6416 | 136 | //Set speaker |
gimohd | 6:77a4c45f6416 | 137 | speaker->play(receivePacket->getNUM()); |
gimohd | 6:77a4c45f6416 | 138 | |
gimohd | 6:77a4c45f6416 | 139 | //Average temperature on LCD |
gimohd | 6:77a4c45f6416 | 140 | lcd->cls(); |
gimohd | 6:77a4c45f6416 | 141 | lcd->locate(0,3); |
gimohd | 6:77a4c45f6416 | 142 | float s =temperatureSensor->average(receivePacket->getTMP()); |
gimohd | 6:77a4c45f6416 | 143 | lcd->printf("Average %f\n", s); |
gimohd | 6:77a4c45f6416 | 144 | |
gimohd | 6:77a4c45f6416 | 145 | |
gimohd | 6:77a4c45f6416 | 146 | |
gimohd | 6:77a4c45f6416 | 147 | if (!receivePacket->idCheck(communication->getOwnID()) || !receivePacket->crcCheck()){ |
gimohd | 6:77a4c45f6416 | 148 | lcd->printf("GAME OVER\n", s); |
gimohd | 6:77a4c45f6416 | 149 | return -1; |
gimohd | 6:77a4c45f6416 | 150 | } |
gimohd | 6:77a4c45f6416 | 151 | //change packet to data |
gimohd | 6:77a4c45f6416 | 152 | receivePacket->addValues(receivePacket->getNUM(),destinationID,potentiometer->readValue(), communication->getOwnID() ,temperatureSensor->temp_short()); |
gimohd | 6:77a4c45f6416 | 153 | |
gimohd | 6:77a4c45f6416 | 154 | //sending data to next |
gimohd | 6:77a4c45f6416 | 155 | communication->sendDataPacket(receivePacket->toDataString()); |
gimohd | 6:77a4c45f6416 | 156 | wait_ms(110); |
gimohd | 6:77a4c45f6416 | 157 | break; |
gimohd | 6:77a4c45f6416 | 158 | |
gimohd | 6:77a4c45f6416 | 159 | } |
gimohd | 6:77a4c45f6416 | 160 | } |
gimohd | 6:77a4c45f6416 | 161 | } |
gimohd | 6:77a4c45f6416 | 162 | return 1; |
gimohd | 6:77a4c45f6416 | 163 | } |