![](/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
Translator.cpp
- Committer:
- katrijnverhasselt
- Date:
- 2016-05-18
- Revision:
- 0:b40341017545
- Child:
- 1:8ba039abd9b8
File content as of revision 0:b40341017545:
#include "Translator.h" Translator::MessageInfo::MessageInfo() : CommandType(NONE), Channel(0), DacValue(0) {} bool Translator::MessageInfo::IsNone() { return CommandType == NONE; } bool Translator::MessageInfo::IsRead() { return CommandType == READ; } bool Translator::MessageInfo::IsWrite() { return CommandType == WRITE; } bool Translator::MessageInfo::IsOption() { return CommandType = OPTION; } 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() : _rawMessage(0), _size(0) {} bool Translator::Translate(MessageInfo info, int8_t** cmd, int* const cmdSize) { 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'; 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'; 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'; break; default: return false; } return true; } bool Translator::Translate(const int8_t* const cmd, int cmdSize, MessageInfo* const info) { // Check starting character if (cmd[Rules::StartCharIndex] != Rules::StartChar) { printf("No start char"); return false; } // Check length of command if (cmd[Rules::CharsToReadIndex] + Rules::CharsToReadOffset != cmdSize) { printf("Incorrect size"); return false; } // Populate info switch (cmd[Rules::rwoIndex]) { case 'r': info->CommandType = MessageInfo::READ; break; case 'w': info->CommandType = MessageInfo::WRITE; break; case 'o': info->CommandType = MessageInfo::OPTION; break; default: return false; } //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; }