![](/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
Reader.cpp
- Committer:
- katrijnverhasselt
- Date:
- 2016-05-18
- Revision:
- 1:8ba039abd9b8
- Parent:
- 0:b40341017545
- Child:
- 2:2330ad8b1baa
File content as of revision 1:8ba039abd9b8:
#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)) { _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(" / "); }