Gets messages form the pc and translates it to I2C and back.

Dependencies:   DevInterfaces I2Cinterfaces MCP4725 mbed

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?

UserRevisionLine numberNew contents of line
katrijnverhasselt 0:b40341017545 1 #include "Translator.h"
katrijnverhasselt 0:b40341017545 2
katrijnverhasselt 2:2330ad8b1baa 3 Translator::MessageInfo::MessageInfo() {
katrijnverhasselt 2:2330ad8b1baa 4 Reset();
katrijnverhasselt 2:2330ad8b1baa 5 }
katrijnverhasselt 0:b40341017545 6
katrijnverhasselt 0:b40341017545 7 bool Translator::MessageInfo::IsNone() {
katrijnverhasselt 0:b40341017545 8 return CommandType == NONE;
katrijnverhasselt 0:b40341017545 9 }
katrijnverhasselt 0:b40341017545 10
katrijnverhasselt 0:b40341017545 11 bool Translator::MessageInfo::IsRead() {
katrijnverhasselt 0:b40341017545 12 return CommandType == READ;
katrijnverhasselt 0:b40341017545 13 }
katrijnverhasselt 0:b40341017545 14
katrijnverhasselt 0:b40341017545 15 bool Translator::MessageInfo::IsWrite() {
katrijnverhasselt 0:b40341017545 16 return CommandType == WRITE;
katrijnverhasselt 0:b40341017545 17 }
katrijnverhasselt 0:b40341017545 18
katrijnverhasselt 2:2330ad8b1baa 19 bool Translator::MessageInfo::IsError() {
katrijnverhasselt 2:2330ad8b1baa 20 return CommandType = ERROR;
katrijnverhasselt 2:2330ad8b1baa 21 }
katrijnverhasselt 2:2330ad8b1baa 22
katrijnverhasselt 2:2330ad8b1baa 23 void Translator::MessageInfo::Reset() {
katrijnverhasselt 2:2330ad8b1baa 24 CommandType = NONE;
katrijnverhasselt 2:2330ad8b1baa 25 Address = 0;
katrijnverhasselt 2:2330ad8b1baa 26 for (int i = 0; i < Rules::MaxDataSize; i++)
katrijnverhasselt 2:2330ad8b1baa 27 Data[i] = 0;
katrijnverhasselt 0:b40341017545 28 }
katrijnverhasselt 0:b40341017545 29
katrijnverhasselt 2:2330ad8b1baa 30 Translator::Translator() {}
katrijnverhasselt 0:b40341017545 31
katrijnverhasselt 0:b40341017545 32
katrijnverhasselt 2:2330ad8b1baa 33 bool Translator::Translate(MessageInfo info, char cmd[Rules::MaxCmdSize], int* const cmdSize) {
katrijnverhasselt 2:2330ad8b1baa 34 if (MessageInfo::NONE)
katrijnverhasselt 2:2330ad8b1baa 35 return false;
katrijnverhasselt 2:2330ad8b1baa 36
katrijnverhasselt 2:2330ad8b1baa 37 cmd[Rules::StartCharIndex] = Rules::StartChar;
katrijnverhasselt 2:2330ad8b1baa 38 cmd[Rules::AddressIndex] = info.Address;
katrijnverhasselt 2:2330ad8b1baa 39 for (int i = 0; i < Rules::MaxDataSize; i++)
katrijnverhasselt 2:2330ad8b1baa 40 cmd[Rules::DataIndex+i] = info.Data[i];
katrijnverhasselt 2:2330ad8b1baa 41
katrijnverhasselt 0:b40341017545 42 switch (info.CommandType) {
katrijnverhasselt 0:b40341017545 43 case MessageInfo::NONE:
katrijnverhasselt 0:b40341017545 44 return false;
katrijnverhasselt 0:b40341017545 45 case MessageInfo::READ:
katrijnverhasselt 0:b40341017545 46 *cmdSize = Rules::ReadCmdSize;
katrijnverhasselt 2:2330ad8b1baa 47 cmd[Rules::CharsToReadIndex] = Rules::ReadCmdSize-Rules::PrefixSize;
katrijnverhasselt 2:2330ad8b1baa 48 cmd[Rules::CommandIndex] = Rules::ReadChar;
katrijnverhasselt 0:b40341017545 49 break;
katrijnverhasselt 0:b40341017545 50 case MessageInfo::WRITE:
katrijnverhasselt 0:b40341017545 51 *cmdSize = Rules::WriteCmdSize;
katrijnverhasselt 2:2330ad8b1baa 52 cmd[Rules::CharsToReadIndex] = Rules::WriteCmdSize - Rules::PrefixSize;
katrijnverhasselt 2:2330ad8b1baa 53 cmd[Rules::CommandIndex] = Rules::WriteChar;
katrijnverhasselt 0:b40341017545 54 break;
katrijnverhasselt 0:b40341017545 55 case MessageInfo::ERROR:
katrijnverhasselt 2:2330ad8b1baa 56 *cmdSize = Rules::MaxCmdSize;
katrijnverhasselt 2:2330ad8b1baa 57 cmd[Rules::CharsToReadIndex] = Rules::MaxCmdSize - Rules::PrefixSize;
katrijnverhasselt 2:2330ad8b1baa 58 cmd[Rules::CommandIndex] = Rules::ErrorChar;
katrijnverhasselt 0:b40341017545 59 break;
katrijnverhasselt 0:b40341017545 60 default:
katrijnverhasselt 0:b40341017545 61 return false;
katrijnverhasselt 0:b40341017545 62 }
katrijnverhasselt 0:b40341017545 63
katrijnverhasselt 0:b40341017545 64 return true;
katrijnverhasselt 0:b40341017545 65 }
katrijnverhasselt 0:b40341017545 66
katrijnverhasselt 0:b40341017545 67
katrijnverhasselt 2:2330ad8b1baa 68 Translator::EErrorCode Translator::Translate(const char* const cmd, int cmdSize, MessageInfo* const info) {
katrijnverhasselt 0:b40341017545 69 // Check starting character
katrijnverhasselt 0:b40341017545 70 if (cmd[Rules::StartCharIndex] != Rules::StartChar) {
katrijnverhasselt 2:2330ad8b1baa 71 return INVALID;
katrijnverhasselt 0:b40341017545 72 }
katrijnverhasselt 0:b40341017545 73 // Check length of command
katrijnverhasselt 2:2330ad8b1baa 74 if (cmdSize < Rules::PrefixSize || (int)(unsigned char)cmd[Rules::CharsToReadIndex] > cmdSize - Rules::PrefixSize)
katrijnverhasselt 2:2330ad8b1baa 75 return INCOMPLETE;
katrijnverhasselt 2:2330ad8b1baa 76 if ((int)(unsigned char)cmd[Rules::CharsToReadIndex] < cmdSize - Rules::PrefixSize)
katrijnverhasselt 2:2330ad8b1baa 77 return INVALID;
katrijnverhasselt 2:2330ad8b1baa 78
katrijnverhasselt 2:2330ad8b1baa 79 // If complete message was read, check if correct size with respect to default read/write length
katrijnverhasselt 2:2330ad8b1baa 80 if ((cmd[Rules::CommandIndex] == 'r' && cmdSize != Rules::ReadCmdSize) || (cmd[Rules::CommandIndex] == 'w' && cmdSize != Rules::WriteCmdSize))
katrijnverhasselt 2:2330ad8b1baa 81 return INVALID;
katrijnverhasselt 2:2330ad8b1baa 82
katrijnverhasselt 2:2330ad8b1baa 83 // Message should be correct
katrijnverhasselt 2:2330ad8b1baa 84 info->Reset();
katrijnverhasselt 0:b40341017545 85
katrijnverhasselt 0:b40341017545 86 // Populate info
katrijnverhasselt 2:2330ad8b1baa 87 info->Address = (int)(unsigned char)cmd[Rules::AddressIndex];
katrijnverhasselt 2:2330ad8b1baa 88 switch (cmd[Rules::CommandIndex]) {
katrijnverhasselt 0:b40341017545 89 case 'r':
katrijnverhasselt 0:b40341017545 90 info->CommandType = MessageInfo::READ;
katrijnverhasselt 2:2330ad8b1baa 91 for (int i = 0; i < Rules::ReadDataSize; i++)
katrijnverhasselt 2:2330ad8b1baa 92 info->Data[i] = cmd[Rules::DataIndex+i];
katrijnverhasselt 0:b40341017545 93 break;
katrijnverhasselt 0:b40341017545 94 case 'w':
katrijnverhasselt 0:b40341017545 95 info->CommandType = MessageInfo::WRITE;
katrijnverhasselt 2:2330ad8b1baa 96 for (int i = 0; i < Rules::WriteDataSize; i++)
katrijnverhasselt 2:2330ad8b1baa 97 info->Data[i] = cmd[Rules::DataIndex+i];
katrijnverhasselt 0:b40341017545 98 break;
katrijnverhasselt 2:2330ad8b1baa 99 case 'e':
katrijnverhasselt 2:2330ad8b1baa 100 info->CommandType = MessageInfo::ERROR;
katrijnverhasselt 0:b40341017545 101 break;
katrijnverhasselt 0:b40341017545 102 default:
katrijnverhasselt 2:2330ad8b1baa 103 return INVALID;
katrijnverhasselt 0:b40341017545 104 }
katrijnverhasselt 2:2330ad8b1baa 105 for (int i = 0; i < cmdSize-Rules::DataIndex; i++)
katrijnverhasselt 2:2330ad8b1baa 106 info->Data[i] = cmd[Rules::DataIndex+i];
katrijnverhasselt 2:2330ad8b1baa 107
katrijnverhasselt 2:2330ad8b1baa 108 return SUCCES;
katrijnverhasselt 0:b40341017545 109 }