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

Dependencies:   DevInterfaces I2Cinterfaces MCP4725 mbed

Committer:
katrijnverhasselt
Date:
Wed May 18 11:22:41 2016 +0000
Revision:
0:b40341017545
Child:
1:8ba039abd9b8
Full program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
katrijnverhasselt 0:b40341017545 1 #include "Translator.h"
katrijnverhasselt 0:b40341017545 2
katrijnverhasselt 0:b40341017545 3 Translator::MessageInfo::MessageInfo()
katrijnverhasselt 0:b40341017545 4 : CommandType(NONE),
katrijnverhasselt 0:b40341017545 5 Channel(0),
katrijnverhasselt 0:b40341017545 6 DacValue(0)
katrijnverhasselt 0:b40341017545 7 {}
katrijnverhasselt 0:b40341017545 8
katrijnverhasselt 0:b40341017545 9 bool Translator::MessageInfo::IsNone() {
katrijnverhasselt 0:b40341017545 10 return CommandType == NONE;
katrijnverhasselt 0:b40341017545 11 }
katrijnverhasselt 0:b40341017545 12
katrijnverhasselt 0:b40341017545 13 bool Translator::MessageInfo::IsRead() {
katrijnverhasselt 0:b40341017545 14 return CommandType == READ;
katrijnverhasselt 0:b40341017545 15 }
katrijnverhasselt 0:b40341017545 16
katrijnverhasselt 0:b40341017545 17 bool Translator::MessageInfo::IsWrite() {
katrijnverhasselt 0:b40341017545 18 return CommandType == WRITE;
katrijnverhasselt 0:b40341017545 19 }
katrijnverhasselt 0:b40341017545 20
katrijnverhasselt 0:b40341017545 21 bool Translator::MessageInfo::IsOption() {
katrijnverhasselt 0:b40341017545 22 return CommandType = OPTION;
katrijnverhasselt 0:b40341017545 23 }
katrijnverhasselt 0:b40341017545 24
katrijnverhasselt 0:b40341017545 25 int Translator::Rules::StartCharIndex = 0;
katrijnverhasselt 0:b40341017545 26 int8_t Translator::Rules::StartChar = '!';
katrijnverhasselt 0:b40341017545 27 int Translator::Rules::CharsToReadIndex = 1;
katrijnverhasselt 0:b40341017545 28 int Translator::Rules::CharsToReadOffset = CharsToReadIndex+1;
katrijnverhasselt 0:b40341017545 29 int Translator::Rules::rwoIndex = 2;
katrijnverhasselt 0:b40341017545 30 int8_t Translator::Rules::ReadChar = 'r';
katrijnverhasselt 0:b40341017545 31 int8_t Translator::Rules::WriteChar = 'w';
katrijnverhasselt 0:b40341017545 32 int8_t Translator::Rules::OptionChar = 'o';
katrijnverhasselt 0:b40341017545 33 int8_t Translator::Rules::ErrorChar = 'e';
katrijnverhasselt 0:b40341017545 34 //int Translator::Rules::ChannelIndex = 3;
katrijnverhasselt 0:b40341017545 35 int Translator::Rules::DataIndex = 3;
katrijnverhasselt 0:b40341017545 36 int Translator::Rules::DataLength = 2;
katrijnverhasselt 0:b40341017545 37
katrijnverhasselt 0:b40341017545 38 int Translator::Rules::ReadCmdSize = rwoIndex + 1;
katrijnverhasselt 0:b40341017545 39 int Translator::Rules::WriteCmdSize = DataIndex + DataLength;
katrijnverhasselt 0:b40341017545 40 int Translator::Rules::ErrorCmdSize = rwoIndex + 1;
katrijnverhasselt 0:b40341017545 41
katrijnverhasselt 0:b40341017545 42
katrijnverhasselt 0:b40341017545 43 Translator::Translator()
katrijnverhasselt 0:b40341017545 44 : _rawMessage(0),
katrijnverhasselt 0:b40341017545 45 _size(0)
katrijnverhasselt 0:b40341017545 46 {}
katrijnverhasselt 0:b40341017545 47
katrijnverhasselt 0:b40341017545 48
katrijnverhasselt 0:b40341017545 49 bool Translator::Translate(MessageInfo info, int8_t** cmd, int* const cmdSize) {
katrijnverhasselt 0:b40341017545 50 switch (info.CommandType) {
katrijnverhasselt 0:b40341017545 51 case MessageInfo::NONE:
katrijnverhasselt 0:b40341017545 52 return false;
katrijnverhasselt 0:b40341017545 53 case MessageInfo::READ:
katrijnverhasselt 0:b40341017545 54 *cmdSize = Rules::ReadCmdSize;
katrijnverhasselt 0:b40341017545 55 // Add extra character for '\0'
katrijnverhasselt 0:b40341017545 56 *cmd = new int8_t[Rules::ReadCmdSize+1];
katrijnverhasselt 0:b40341017545 57 (*cmd)[Rules::StartCharIndex] = Rules::StartChar;
katrijnverhasselt 0:b40341017545 58 (*cmd)[Rules::CharsToReadIndex] = Rules::ReadCmdSize-Rules::CharsToReadOffset;
katrijnverhasselt 0:b40341017545 59 (*cmd)[Rules::rwoIndex] = Rules::ReadChar;
katrijnverhasselt 0:b40341017545 60 //(*cmd)[Rules::ChannelIndex] = info.Channel;
katrijnverhasselt 0:b40341017545 61 (*cmd)[Rules::ReadCmdSize] = '\0';
katrijnverhasselt 0:b40341017545 62 break;
katrijnverhasselt 0:b40341017545 63 case MessageInfo::WRITE:
katrijnverhasselt 0:b40341017545 64 *cmdSize = Rules::WriteCmdSize;
katrijnverhasselt 0:b40341017545 65 // Add extra character for '\0'
katrijnverhasselt 0:b40341017545 66 *cmd = new int8_t[Rules::WriteCmdSize+1];
katrijnverhasselt 0:b40341017545 67 (*cmd)[Rules::StartCharIndex] = Rules::StartChar;
katrijnverhasselt 0:b40341017545 68 (*cmd)[Rules::CharsToReadIndex] = Rules::WriteCmdSize - Rules::CharsToReadOffset;
katrijnverhasselt 0:b40341017545 69 (*cmd)[Rules::rwoIndex] = Rules::WriteChar;
katrijnverhasselt 0:b40341017545 70 //(*cmd)[Rules::ChannelIndex] = info.Channel;
katrijnverhasselt 0:b40341017545 71 for (int i = 0; i < Rules::DataLength; i++)
katrijnverhasselt 0:b40341017545 72 (*cmd)[Rules::DataIndex + i] = (info.DacValue >> (Rules::DataLength - i - 1) * 8) & 0xFF;
katrijnverhasselt 0:b40341017545 73 (*cmd)[Rules::WriteCmdSize] = '\0';
katrijnverhasselt 0:b40341017545 74 break;
katrijnverhasselt 0:b40341017545 75 case MessageInfo::OPTION:
katrijnverhasselt 0:b40341017545 76 // Not implemented yet
katrijnverhasselt 0:b40341017545 77 return false;
katrijnverhasselt 0:b40341017545 78 case MessageInfo::ERROR:
katrijnverhasselt 0:b40341017545 79 *cmdSize = Rules::ErrorCmdSize;
katrijnverhasselt 0:b40341017545 80 // Add extra character for '\0'
katrijnverhasselt 0:b40341017545 81 *cmd = new int8_t[Rules::ErrorCmdSize+1];
katrijnverhasselt 0:b40341017545 82 (*cmd)[Rules::StartCharIndex] = Rules::StartChar;
katrijnverhasselt 0:b40341017545 83 (*cmd)[Rules::CharsToReadIndex] = Rules::ErrorCmdSize - Rules::CharsToReadOffset;
katrijnverhasselt 0:b40341017545 84 (*cmd)[Rules::rwoIndex] = Rules::ErrorChar;
katrijnverhasselt 0:b40341017545 85 (*cmd)[Rules::ErrorCmdSize] = '\0';
katrijnverhasselt 0:b40341017545 86 break;
katrijnverhasselt 0:b40341017545 87 default:
katrijnverhasselt 0:b40341017545 88 return false;
katrijnverhasselt 0:b40341017545 89 }
katrijnverhasselt 0:b40341017545 90
katrijnverhasselt 0:b40341017545 91 return true;
katrijnverhasselt 0:b40341017545 92 }
katrijnverhasselt 0:b40341017545 93
katrijnverhasselt 0:b40341017545 94
katrijnverhasselt 0:b40341017545 95 bool Translator::Translate(const int8_t* const cmd, int cmdSize, MessageInfo* const info) {
katrijnverhasselt 0:b40341017545 96 // Check starting character
katrijnverhasselt 0:b40341017545 97 if (cmd[Rules::StartCharIndex] != Rules::StartChar) {
katrijnverhasselt 0:b40341017545 98 printf("No start char");
katrijnverhasselt 0:b40341017545 99 return false;
katrijnverhasselt 0:b40341017545 100 }
katrijnverhasselt 0:b40341017545 101 // Check length of command
katrijnverhasselt 0:b40341017545 102 if (cmd[Rules::CharsToReadIndex] + Rules::CharsToReadOffset != cmdSize) {
katrijnverhasselt 0:b40341017545 103 printf("Incorrect size");
katrijnverhasselt 0:b40341017545 104 return false;
katrijnverhasselt 0:b40341017545 105 }
katrijnverhasselt 0:b40341017545 106
katrijnverhasselt 0:b40341017545 107 // Populate info
katrijnverhasselt 0:b40341017545 108 switch (cmd[Rules::rwoIndex]) {
katrijnverhasselt 0:b40341017545 109 case 'r':
katrijnverhasselt 0:b40341017545 110 info->CommandType = MessageInfo::READ;
katrijnverhasselt 0:b40341017545 111 break;
katrijnverhasselt 0:b40341017545 112 case 'w':
katrijnverhasselt 0:b40341017545 113 info->CommandType = MessageInfo::WRITE;
katrijnverhasselt 0:b40341017545 114 break;
katrijnverhasselt 0:b40341017545 115 case 'o':
katrijnverhasselt 0:b40341017545 116 info->CommandType = MessageInfo::OPTION;
katrijnverhasselt 0:b40341017545 117 break;
katrijnverhasselt 0:b40341017545 118 default:
katrijnverhasselt 0:b40341017545 119 return false;
katrijnverhasselt 0:b40341017545 120 }
katrijnverhasselt 0:b40341017545 121 //info->Channel = cmd[Rules::ChannelIndex];
katrijnverhasselt 0:b40341017545 122 info->DacValue = ByteUnshift(cmd+Rules::DataIndex, Rules::DataLength);
katrijnverhasselt 0:b40341017545 123 return true;
katrijnverhasselt 0:b40341017545 124 }
katrijnverhasselt 0:b40341017545 125
katrijnverhasselt 0:b40341017545 126
katrijnverhasselt 0:b40341017545 127 void Translator::ResetMessageInfo() {
katrijnverhasselt 0:b40341017545 128 _message.CommandType = MessageInfo::NONE;
katrijnverhasselt 0:b40341017545 129 _message.Channel = 0;
katrijnverhasselt 0:b40341017545 130 _message.DacValue = 0;
katrijnverhasselt 0:b40341017545 131 }
katrijnverhasselt 0:b40341017545 132
katrijnverhasselt 0:b40341017545 133 void Translator::InvalidateMessage() {
katrijnverhasselt 0:b40341017545 134 _rawMessage = 0;
katrijnverhasselt 0:b40341017545 135 _size = 0;
katrijnverhasselt 0:b40341017545 136 }