![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Gets messages form the pc and translates it to I2C and back.
Dependencies: DevInterfaces I2Cinterfaces MCP4725 mbed
Diff: Translator.cpp
- Revision:
- 2:2330ad8b1baa
- Parent:
- 1:8ba039abd9b8
--- a/Translator.cpp Wed May 18 11:35:45 2016 +0000 +++ b/Translator.cpp Wed Jun 15 10:53:32 2016 +0000 @@ -1,10 +1,8 @@ #include "Translator.h" -Translator::MessageInfo::MessageInfo() - : CommandType(NONE), - Channel(0), - DacValue(0) -{} +Translator::MessageInfo::MessageInfo() { + Reset(); +} bool Translator::MessageInfo::IsNone() { return CommandType == NONE; @@ -18,71 +16,46 @@ return CommandType == WRITE; } -bool Translator::MessageInfo::IsOption() { - return CommandType = OPTION; +bool Translator::MessageInfo::IsError() { + return CommandType = ERROR; +} + +void Translator::MessageInfo::Reset() { + CommandType = NONE; + Address = 0; + for (int i = 0; i < Rules::MaxDataSize; i++) + Data[i] = 0; } -int Translator::Rules::StartCharIndex = 0; -int8_t Translator::Rules::StartChar = '!'; -int Translator::Rules::CharsToReadIndex = 1; -int Translator::Rules::CharsToReadOffset = CharsToReadIndex+1; -int Translator::Rules::rwoIndex = 2; -int8_t Translator::Rules::ReadChar = 'r'; -int8_t Translator::Rules::WriteChar = 'w'; -int8_t Translator::Rules::OptionChar = 'o'; -int8_t Translator::Rules::ErrorChar = 'e'; -//int Translator::Rules::ChannelIndex = 3; -int Translator::Rules::DataIndex = 3; -int Translator::Rules::DataLength = 2; - -int Translator::Rules::ReadCmdSize = rwoIndex + 1; -int Translator::Rules::WriteCmdSize = DataIndex + DataLength; -int Translator::Rules::ErrorCmdSize = rwoIndex + 1; +Translator::Translator() {} -Translator::Translator() - : _rawMessage(0), - _size(0) -{} - - -bool Translator::Translate(MessageInfo info, int8_t** cmd, int* const cmdSize) { +bool Translator::Translate(MessageInfo info, char cmd[Rules::MaxCmdSize], int* const cmdSize) { + if (MessageInfo::NONE) + return false; + + cmd[Rules::StartCharIndex] = Rules::StartChar; + cmd[Rules::AddressIndex] = info.Address; + for (int i = 0; i < Rules::MaxDataSize; i++) + cmd[Rules::DataIndex+i] = info.Data[i]; + switch (info.CommandType) { case MessageInfo::NONE: return false; case MessageInfo::READ: *cmdSize = Rules::ReadCmdSize; - // Add extra character for '\0' - *cmd = new int8_t[Rules::ReadCmdSize+1]; - (*cmd)[Rules::StartCharIndex] = Rules::StartChar; - (*cmd)[Rules::CharsToReadIndex] = Rules::ReadCmdSize-Rules::CharsToReadOffset; - (*cmd)[Rules::rwoIndex] = Rules::ReadChar; - //(*cmd)[Rules::ChannelIndex] = info.Channel; - (*cmd)[Rules::ReadCmdSize] = '\0'; + cmd[Rules::CharsToReadIndex] = Rules::ReadCmdSize-Rules::PrefixSize; + cmd[Rules::CommandIndex] = Rules::ReadChar; break; case MessageInfo::WRITE: *cmdSize = Rules::WriteCmdSize; - // Add extra character for '\0' - *cmd = new int8_t[Rules::WriteCmdSize+1]; - (*cmd)[Rules::StartCharIndex] = Rules::StartChar; - (*cmd)[Rules::CharsToReadIndex] = Rules::WriteCmdSize - Rules::CharsToReadOffset; - (*cmd)[Rules::rwoIndex] = Rules::WriteChar; - //(*cmd)[Rules::ChannelIndex] = info.Channel; - for (int i = 0; i < Rules::DataLength; i++) - (*cmd)[Rules::DataIndex + i] = (info.DacValue >> (Rules::DataLength - i - 1) * 8) & 0xFF; - (*cmd)[Rules::WriteCmdSize] = '\0'; + cmd[Rules::CharsToReadIndex] = Rules::WriteCmdSize - Rules::PrefixSize; + cmd[Rules::CommandIndex] = Rules::WriteChar; break; - case MessageInfo::OPTION: - // Not implemented yet - return false; case MessageInfo::ERROR: - *cmdSize = Rules::ErrorCmdSize; - // Add extra character for '\0' - *cmd = new int8_t[Rules::ErrorCmdSize+1]; - (*cmd)[Rules::StartCharIndex] = Rules::StartChar; - (*cmd)[Rules::CharsToReadIndex] = Rules::ErrorCmdSize - Rules::CharsToReadOffset; - (*cmd)[Rules::rwoIndex] = Rules::ErrorChar; - (*cmd)[Rules::ErrorCmdSize] = '\0'; + *cmdSize = Rules::MaxCmdSize; + cmd[Rules::CharsToReadIndex] = Rules::MaxCmdSize - Rules::PrefixSize; + cmd[Rules::CommandIndex] = Rules::ErrorChar; break; default: return false; @@ -92,43 +65,45 @@ } -bool Translator::Translate(const int8_t* const cmd, int cmdSize, MessageInfo* const info) { +Translator::EErrorCode Translator::Translate(const char* const cmd, int cmdSize, MessageInfo* const info) { // Check starting character if (cmd[Rules::StartCharIndex] != Rules::StartChar) { - return false; + return INVALID; } // Check length of command - if (cmd[Rules::CharsToReadIndex] + Rules::CharsToReadOffset != cmdSize) { - return false; - } + if (cmdSize < Rules::PrefixSize || (int)(unsigned char)cmd[Rules::CharsToReadIndex] > cmdSize - Rules::PrefixSize) + return INCOMPLETE; + if ((int)(unsigned char)cmd[Rules::CharsToReadIndex] < cmdSize - Rules::PrefixSize) + return INVALID; + + // If complete message was read, check if correct size with respect to default read/write length + if ((cmd[Rules::CommandIndex] == 'r' && cmdSize != Rules::ReadCmdSize) || (cmd[Rules::CommandIndex] == 'w' && cmdSize != Rules::WriteCmdSize)) + return INVALID; + + // Message should be correct + info->Reset(); // Populate info - switch (cmd[Rules::rwoIndex]) { + info->Address = (int)(unsigned char)cmd[Rules::AddressIndex]; + switch (cmd[Rules::CommandIndex]) { case 'r': info->CommandType = MessageInfo::READ; + for (int i = 0; i < Rules::ReadDataSize; i++) + info->Data[i] = cmd[Rules::DataIndex+i]; break; case 'w': info->CommandType = MessageInfo::WRITE; + for (int i = 0; i < Rules::WriteDataSize; i++) + info->Data[i] = cmd[Rules::DataIndex+i]; break; - case 'o': - info->CommandType = MessageInfo::OPTION; + case 'e': + info->CommandType = MessageInfo::ERROR; break; default: - return false; + return INVALID; } - //info->Channel = cmd[Rules::ChannelIndex]; - info->DacValue = ByteUnshift(cmd+Rules::DataIndex, Rules::DataLength); - return true; -} - - -void Translator::ResetMessageInfo() { - _message.CommandType = MessageInfo::NONE; - _message.Channel = 0; - _message.DacValue = 0; -} - -void Translator::InvalidateMessage() { - _rawMessage = 0; - _size = 0; + for (int i = 0; i < cmdSize-Rules::DataIndex; i++) + info->Data[i] = cmd[Rules::DataIndex+i]; + + return SUCCES; } \ No newline at end of file