Gets messages form the pc and translates it to I2C and back.
Dependencies: DevInterfaces I2Cinterfaces MCP4725 mbed
main.cpp@2:2330ad8b1baa, 2016-06-15 (annotated)
- Committer:
- katrijnverhasselt
- Date:
- Wed Jun 15 10:53:32 2016 +0000
- Revision:
- 2:2330ad8b1baa
- Parent:
- 1:8ba039abd9b8
Finished programming. This program now simply extract only the info it needs and sends all of it immediately through its I2C port.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
katrijnverhasselt | 0:b40341017545 | 1 | #define MCP4728EXAMPLEVER "1.10 " |
katrijnverhasselt | 0:b40341017545 | 2 | |
katrijnverhasselt | 0:b40341017545 | 3 | #include <string> |
katrijnverhasselt | 0:b40341017545 | 4 | #include "mbed.h" |
katrijnverhasselt | 0:b40341017545 | 5 | |
katrijnverhasselt | 0:b40341017545 | 6 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
katrijnverhasselt | 0:b40341017545 | 7 | PinName const SDA = PTE25; |
katrijnverhasselt | 0:b40341017545 | 8 | PinName const SCL = PTE24; |
katrijnverhasselt | 0:b40341017545 | 9 | #elif defined (TARGET_KL05Z) |
katrijnverhasselt | 0:b40341017545 | 10 | PinName const SDA = PTB4; |
katrijnverhasselt | 0:b40341017545 | 11 | PinName const SCL = PTB3; |
katrijnverhasselt | 0:b40341017545 | 12 | #elif defined (TARGET_K20D50M) |
katrijnverhasselt | 0:b40341017545 | 13 | PinName const SDA = PTB1; |
katrijnverhasselt | 0:b40341017545 | 14 | PinName const SCL = PTB0; |
katrijnverhasselt | 0:b40341017545 | 15 | #else |
katrijnverhasselt | 0:b40341017545 | 16 | #error TARGET NOT DEFINED |
katrijnverhasselt | 0:b40341017545 | 17 | #endif |
katrijnverhasselt | 0:b40341017545 | 18 | |
katrijnverhasselt | 0:b40341017545 | 19 | |
katrijnverhasselt | 0:b40341017545 | 20 | #include "I2C.h" |
katrijnverhasselt | 0:b40341017545 | 21 | #include "I2CInterface.h" |
katrijnverhasselt | 0:b40341017545 | 22 | #include "MBEDI2CInterface.h" |
katrijnverhasselt | 0:b40341017545 | 23 | #include "DACInterface.h" |
katrijnverhasselt | 0:b40341017545 | 24 | #include "dev_interface_def.h" |
katrijnverhasselt | 0:b40341017545 | 25 | #include "mcp4725.h" |
katrijnverhasselt | 0:b40341017545 | 26 | #include "Translator.h" |
katrijnverhasselt | 0:b40341017545 | 27 | #include "Reader.h" |
katrijnverhasselt | 0:b40341017545 | 28 | |
katrijnverhasselt | 0:b40341017545 | 29 | // Initialize LEDs |
katrijnverhasselt | 0:b40341017545 | 30 | PwmOut rled(LED1); |
katrijnverhasselt | 0:b40341017545 | 31 | PwmOut gled(LED2); |
katrijnverhasselt | 0:b40341017545 | 32 | PwmOut bled(LED3); |
katrijnverhasselt | 0:b40341017545 | 33 | |
katrijnverhasselt | 2:2330ad8b1baa | 34 | //Serial pc(USBTX, USBRX); |
katrijnverhasselt | 2:2330ad8b1baa | 35 | |
katrijnverhasselt | 2:2330ad8b1baa | 36 | // Sends the message to the pc |
katrijnverhasselt | 2:2330ad8b1baa | 37 | bool Send(Translator::MessageInfo info); |
katrijnverhasselt | 2:2330ad8b1baa | 38 | void SendError(); |
katrijnverhasselt | 2:2330ad8b1baa | 39 | void SendError(int address); |
katrijnverhasselt | 2:2330ad8b1baa | 40 | void SendError(char* data, int dataSize); |
katrijnverhasselt | 2:2330ad8b1baa | 41 | void SendError(int address, char* data, int dataSize); |
katrijnverhasselt | 2:2330ad8b1baa | 42 | void SendError(Translator::MessageInfo info); |
katrijnverhasselt | 2:2330ad8b1baa | 43 | |
katrijnverhasselt | 2:2330ad8b1baa | 44 | // Turns LED blue |
katrijnverhasselt | 2:2330ad8b1baa | 45 | void SignalWaiting(); |
katrijnverhasselt | 2:2330ad8b1baa | 46 | // Turns LED green |
katrijnverhasselt | 2:2330ad8b1baa | 47 | void SignalReceived(); |
katrijnverhasselt | 2:2330ad8b1baa | 48 | // Turns LED red |
katrijnverhasselt | 2:2330ad8b1baa | 49 | void SignalError(); |
katrijnverhasselt | 0:b40341017545 | 50 | |
katrijnverhasselt | 0:b40341017545 | 51 | int main(void) { |
katrijnverhasselt | 0:b40341017545 | 52 | // <SDA en SCL zijn de pins hierboven gedefinieerd, wss hardware eigenschappen dat je mag negeren> |
katrijnverhasselt | 0:b40341017545 | 53 | // Create MbedI2C interface |
katrijnverhasselt | 0:b40341017545 | 54 | MBEDI2CInterface mbedi2c(SDA, SCL); |
katrijnverhasselt | 0:b40341017545 | 55 | // <volgende lijn is voor indien de Mbed veranderd, enkel bovenstaande lijn geupdate moet worden> |
katrijnverhasselt | 0:b40341017545 | 56 | // Create pointer to MBEDI2CInterface's parent |
katrijnverhasselt | 0:b40341017545 | 57 | I2CInterface* i2cdev= &mbedi2c; |
katrijnverhasselt | 0:b40341017545 | 58 | // Set maximum voltage of I2C board |
katrijnverhasselt | 0:b40341017545 | 59 | float Vdd = 4.7; |
katrijnverhasselt | 0:b40341017545 | 60 | // Create communication object using the MbedI2C interface |
katrijnverhasselt | 0:b40341017545 | 61 | MCP4725 dac(i2cdev ,0, Vdd); |
katrijnverhasselt | 0:b40341017545 | 62 | |
katrijnverhasselt | 0:b40341017545 | 63 | // Create input buffer reader |
katrijnverhasselt | 0:b40341017545 | 64 | Reader reader; |
katrijnverhasselt | 0:b40341017545 | 65 | |
katrijnverhasselt | 0:b40341017545 | 66 | // Start infinite loop |
katrijnverhasselt | 0:b40341017545 | 67 | while (true) { |
katrijnverhasselt | 2:2330ad8b1baa | 68 | SignalWaiting(); |
katrijnverhasselt | 0:b40341017545 | 69 | if (!reader.IsNewMessageReceived()) |
katrijnverhasselt | 0:b40341017545 | 70 | continue; // Do nothing as long as no new complete message was received |
katrijnverhasselt | 2:2330ad8b1baa | 71 | SignalReceived(); |
katrijnverhasselt | 2:2330ad8b1baa | 72 | |
katrijnverhasselt | 0:b40341017545 | 73 | |
katrijnverhasselt | 2:2330ad8b1baa | 74 | |
katrijnverhasselt | 2:2330ad8b1baa | 75 | // Get the formatted message |
katrijnverhasselt | 1:8ba039abd9b8 | 76 | Translator::MessageInfo info = reader.GetLastMessageInfo(); |
katrijnverhasselt | 0:b40341017545 | 77 | |
katrijnverhasselt | 2:2330ad8b1baa | 78 | // If write was specified |
katrijnverhasselt | 2:2330ad8b1baa | 79 | if (info.IsWrite()) { |
katrijnverhasselt | 2:2330ad8b1baa | 80 | // Issue write |
katrijnverhasselt | 2:2330ad8b1baa | 81 | if (i2cdev->write(info.Address, info.Data, Translator::Rules::WriteDataSize, false) != 0) { |
katrijnverhasselt | 2:2330ad8b1baa | 82 | //if (dac.setDACvalue((int)(unsigned char)info.Data[1]<<4+(int)(unsigned char)info.Data[2]>>4, 0) != 0) { |
katrijnverhasselt | 2:2330ad8b1baa | 83 | SendError(info); |
katrijnverhasselt | 2:2330ad8b1baa | 84 | continue; |
katrijnverhasselt | 2:2330ad8b1baa | 85 | } |
katrijnverhasselt | 2:2330ad8b1baa | 86 | // Issue also a read |
katrijnverhasselt | 2:2330ad8b1baa | 87 | //if (i2cdev->read(info.Address, info.Data, Translator::Rules::ReadDataSize, false) != 0) { |
katrijnverhasselt | 2:2330ad8b1baa | 88 | // //SendError(info); |
katrijnverhasselt | 2:2330ad8b1baa | 89 | // continue; |
katrijnverhasselt | 2:2330ad8b1baa | 90 | //} |
katrijnverhasselt | 2:2330ad8b1baa | 91 | if (!Send(info)) { |
katrijnverhasselt | 2:2330ad8b1baa | 92 | SendError(info); |
katrijnverhasselt | 0:b40341017545 | 93 | } |
katrijnverhasselt | 2:2330ad8b1baa | 94 | } |
katrijnverhasselt | 2:2330ad8b1baa | 95 | // If read was specified |
katrijnverhasselt | 2:2330ad8b1baa | 96 | else if (info.IsRead()) { |
katrijnverhasselt | 2:2330ad8b1baa | 97 | // read from DAC |
katrijnverhasselt | 2:2330ad8b1baa | 98 | if (i2cdev->read(info.Address, info.Data, sizeof(info.Data)/sizeof(info.Data[0]), false) != 0) { |
katrijnverhasselt | 2:2330ad8b1baa | 99 | SendError(info); |
katrijnverhasselt | 2:2330ad8b1baa | 100 | continue; |
katrijnverhasselt | 0:b40341017545 | 101 | } |
katrijnverhasselt | 2:2330ad8b1baa | 102 | // send read values to pc |
katrijnverhasselt | 2:2330ad8b1baa | 103 | if (!Send(info)) |
katrijnverhasselt | 2:2330ad8b1baa | 104 | SendError(); |
katrijnverhasselt | 0:b40341017545 | 105 | } |
katrijnverhasselt | 2:2330ad8b1baa | 106 | // If error was specified |
katrijnverhasselt | 2:2330ad8b1baa | 107 | else if (info.IsError()) { |
katrijnverhasselt | 2:2330ad8b1baa | 108 | // Send the error back |
katrijnverhasselt | 2:2330ad8b1baa | 109 | if (!Send(info)) |
katrijnverhasselt | 2:2330ad8b1baa | 110 | SendError(); |
katrijnverhasselt | 2:2330ad8b1baa | 111 | } |
katrijnverhasselt | 2:2330ad8b1baa | 112 | |
katrijnverhasselt | 2:2330ad8b1baa | 113 | // Don't overdo it |
katrijnverhasselt | 2:2330ad8b1baa | 114 | wait_ms(500); |
katrijnverhasselt | 0:b40341017545 | 115 | } |
katrijnverhasselt | 0:b40341017545 | 116 | } |
katrijnverhasselt | 0:b40341017545 | 117 | |
katrijnverhasselt | 2:2330ad8b1baa | 118 | |
katrijnverhasselt | 2:2330ad8b1baa | 119 | bool Send(Translator::MessageInfo info) { |
katrijnverhasselt | 2:2330ad8b1baa | 120 | // Create buffer |
katrijnverhasselt | 2:2330ad8b1baa | 121 | int bufferSize; |
katrijnverhasselt | 2:2330ad8b1baa | 122 | char buffer[Translator::Rules::MaxCmdSize]; |
katrijnverhasselt | 2:2330ad8b1baa | 123 | // Translate to raw message |
katrijnverhasselt | 2:2330ad8b1baa | 124 | if (!Translator::Translate(info, buffer, &bufferSize)) |
katrijnverhasselt | 2:2330ad8b1baa | 125 | return false; |
katrijnverhasselt | 2:2330ad8b1baa | 126 | // Send every character |
katrijnverhasselt | 2:2330ad8b1baa | 127 | for (int i = 0; i < bufferSize; i++) |
katrijnverhasselt | 2:2330ad8b1baa | 128 | printf("%c", buffer[i]); |
katrijnverhasselt | 2:2330ad8b1baa | 129 | return true; |
katrijnverhasselt | 2:2330ad8b1baa | 130 | } |
katrijnverhasselt | 2:2330ad8b1baa | 131 | |
katrijnverhasselt | 2:2330ad8b1baa | 132 | void SendError() { |
katrijnverhasselt | 2:2330ad8b1baa | 133 | SendError(0,0,0); |
katrijnverhasselt | 2:2330ad8b1baa | 134 | } |
katrijnverhasselt | 2:2330ad8b1baa | 135 | |
katrijnverhasselt | 2:2330ad8b1baa | 136 | void SendError(int address) { |
katrijnverhasselt | 2:2330ad8b1baa | 137 | SendError(address, 0, 0); |
katrijnverhasselt | 2:2330ad8b1baa | 138 | } |
katrijnverhasselt | 2:2330ad8b1baa | 139 | |
katrijnverhasselt | 2:2330ad8b1baa | 140 | void SendError(char* data, int dataSize) { |
katrijnverhasselt | 2:2330ad8b1baa | 141 | SendError(0, data, dataSize); |
katrijnverhasselt | 2:2330ad8b1baa | 142 | } |
katrijnverhasselt | 2:2330ad8b1baa | 143 | |
katrijnverhasselt | 2:2330ad8b1baa | 144 | void SendError(int address, char* data , int dataSize) { |
katrijnverhasselt | 2:2330ad8b1baa | 145 | // Create MessageInfo with error |
katrijnverhasselt | 2:2330ad8b1baa | 146 | Translator::MessageInfo info; |
katrijnverhasselt | 2:2330ad8b1baa | 147 | info.Address = address; |
katrijnverhasselt | 2:2330ad8b1baa | 148 | for (int i = 0; i < dataSize; i++) |
katrijnverhasselt | 2:2330ad8b1baa | 149 | info.Data[i] = data[i]; |
katrijnverhasselt | 2:2330ad8b1baa | 150 | SendError(info); |
katrijnverhasselt | 2:2330ad8b1baa | 151 | } |
katrijnverhasselt | 2:2330ad8b1baa | 152 | |
katrijnverhasselt | 2:2330ad8b1baa | 153 | void SendError(Translator::MessageInfo info) { |
katrijnverhasselt | 2:2330ad8b1baa | 154 | SignalError(); |
katrijnverhasselt | 2:2330ad8b1baa | 155 | info.CommandType = Translator::MessageInfo::ERROR; |
katrijnverhasselt | 2:2330ad8b1baa | 156 | Send(info); |
katrijnverhasselt | 2:2330ad8b1baa | 157 | } |
katrijnverhasselt | 2:2330ad8b1baa | 158 | |
katrijnverhasselt | 2:2330ad8b1baa | 159 | void SignalWaiting() { |
katrijnverhasselt | 2:2330ad8b1baa | 160 | // Blue light |
katrijnverhasselt | 2:2330ad8b1baa | 161 | rled = 1.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 162 | gled = 1.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 163 | bled = 0.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 164 | } |
katrijnverhasselt | 2:2330ad8b1baa | 165 | |
katrijnverhasselt | 2:2330ad8b1baa | 166 | void SignalReceived() { |
katrijnverhasselt | 2:2330ad8b1baa | 167 | // Green light |
katrijnverhasselt | 2:2330ad8b1baa | 168 | rled = 1.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 169 | gled = 0.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 170 | bled = 1.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 171 | } |
katrijnverhasselt | 2:2330ad8b1baa | 172 | |
katrijnverhasselt | 2:2330ad8b1baa | 173 | void SignalError() { |
katrijnverhasselt | 2:2330ad8b1baa | 174 | // Red light |
katrijnverhasselt | 2:2330ad8b1baa | 175 | rled = 0.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 176 | gled = 1.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 177 | bled = 1.0f; |
katrijnverhasselt | 2:2330ad8b1baa | 178 | } |
katrijnverhasselt | 2:2330ad8b1baa | 179 | |
katrijnverhasselt | 2:2330ad8b1baa | 180 | |
katrijnverhasselt | 2:2330ad8b1baa | 181 | |
katrijnverhasselt | 2:2330ad8b1baa | 182 | |
katrijnverhasselt | 2:2330ad8b1baa | 183 | |
katrijnverhasselt | 2:2330ad8b1baa | 184 | |
katrijnverhasselt | 2:2330ad8b1baa | 185 | |
katrijnverhasselt | 2:2330ad8b1baa | 186 | |
katrijnverhasselt | 2:2330ad8b1baa | 187 | |
katrijnverhasselt | 2:2330ad8b1baa | 188 |