![](/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: Reader.cpp
- Revision:
- 0:b40341017545
- Child:
- 1:8ba039abd9b8
diff -r 000000000000 -r b40341017545 Reader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Reader.cpp Wed May 18 11:22:41 2016 +0000 @@ -0,0 +1,110 @@ +#include "Reader.h" + +// Default constructor +Reader::Reader() + : pc(USBTX, USBRX), + _isNewMessageReceived(false), + _lastMessage(), + _lastMessageSize(0), + _buffer(), + _bufferSize(0), + _lastChar(0), + _isMessageStarted(false), + _charsToRead(-1), + _index(0) +{ + // Attach read function to pc + pc.attach(this, &Reader::pcCallback); +} + + +bool Reader::IsNewMessageReceived() { + // If no message received, return false + if (!_isNewMessageReceived) + return false; + + // If message received, set to false and return true + _isNewMessageReceived = false; + return true; +} + +Translator::MessageInfo Reader::GetLastMessageInfo() const { + return _lastMessageInfo; +} + +const int8_t* Reader::GetLastMessage() const { + return _lastMessage; +} + +int Reader::GetLastMessageSize() const { + return _lastMessageSize; +} + +void Reader::pcCallback() { + // Get the sent character + _lastChar = pc.getc(); + if (_lastChar == Translator::Rules::StartChar) { + _index = 0; + } + // Add character to buffer + _buffer[_index] = _lastChar; + // Check if valid message by translating it + Translator::MessageInfo info; + if (!Translator::Translate(_buffer, _index + 1, &info)) { + printf("Translate failed on "); + for (int i = 0; i <= _index; i++) + printf("%c", _buffer[i]); + _index++; + return; + } + + // Translation succesful, copy to _info + _lastMessageInfo = info; + // Notify user of new message + _isNewMessageReceived = true; + // Reset counter + _index = 0; + return; + + + + + //printf("'%c' (%i) ; ", _lastChar, (int)_lastChar); + // If no ongoing message and starting character was received + if (_lastChar == Translator::Rules::StartChar) { + //printf("First char: %c", _lastChar); + // Get ready to receive the message + _isMessageStarted = true; + _index = 0; + _charsToRead = -1; // value < 0 indicates no length was read yet + } // If message was started but no length was read yet + else if (_charsToRead < 0) { + // Get ready to receive the command + _charsToRead = (int)_lastChar; + //printf("CharsToRead: %i", _charsToRead); + //_buffer = new int8_t[_charsToRead]; + _index = 0; + } // If message was started and size is known + else if (_charsToRead > 0) { + //printf("Storing '%c' (%i)", _lastChar, (int)_lastChar); + // Read _charsToRead characters and store in _buffer + if (_index < _charsToRead) { + _buffer[_index] = _lastChar; + _index++; + + // If message was fully received + if (_index == _charsToRead) { + // Copy _buffer to _lastMessage (see Utility.h) + DeepCopy(_buffer, _charsToRead, _lastMessage); + _lastMessageSize = _charsToRead; + // Reset values + _isMessageStarted = false; + _charsToRead = -1; + _index = 0; + // Let user know that a new message was received + _isNewMessageReceived = true; + } + } + } + //printf(" / "); +} \ No newline at end of file