![](/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@1:8ba039abd9b8, 2016-05-18 (annotated)
- Committer:
- katrijnverhasselt
- Date:
- Wed May 18 11:35:45 2016 +0000
- Revision:
- 1:8ba039abd9b8
- Parent:
- 0:b40341017545
- Child:
- 2:2330ad8b1baa
Fixed it!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
katrijnverhasselt | 0:b40341017545 | 1 | #include "Reader.h" |
katrijnverhasselt | 0:b40341017545 | 2 | |
katrijnverhasselt | 0:b40341017545 | 3 | // Default constructor |
katrijnverhasselt | 0:b40341017545 | 4 | Reader::Reader() |
katrijnverhasselt | 0:b40341017545 | 5 | : pc(USBTX, USBRX), |
katrijnverhasselt | 0:b40341017545 | 6 | _isNewMessageReceived(false), |
katrijnverhasselt | 0:b40341017545 | 7 | _lastMessage(), |
katrijnverhasselt | 0:b40341017545 | 8 | _lastMessageSize(0), |
katrijnverhasselt | 0:b40341017545 | 9 | _buffer(), |
katrijnverhasselt | 0:b40341017545 | 10 | _bufferSize(0), |
katrijnverhasselt | 0:b40341017545 | 11 | _lastChar(0), |
katrijnverhasselt | 0:b40341017545 | 12 | _isMessageStarted(false), |
katrijnverhasselt | 0:b40341017545 | 13 | _charsToRead(-1), |
katrijnverhasselt | 0:b40341017545 | 14 | _index(0) |
katrijnverhasselt | 0:b40341017545 | 15 | { |
katrijnverhasselt | 0:b40341017545 | 16 | // Attach read function to pc |
katrijnverhasselt | 0:b40341017545 | 17 | pc.attach(this, &Reader::pcCallback); |
katrijnverhasselt | 0:b40341017545 | 18 | } |
katrijnverhasselt | 0:b40341017545 | 19 | |
katrijnverhasselt | 0:b40341017545 | 20 | |
katrijnverhasselt | 0:b40341017545 | 21 | bool Reader::IsNewMessageReceived() { |
katrijnverhasselt | 0:b40341017545 | 22 | // If no message received, return false |
katrijnverhasselt | 0:b40341017545 | 23 | if (!_isNewMessageReceived) |
katrijnverhasselt | 0:b40341017545 | 24 | return false; |
katrijnverhasselt | 0:b40341017545 | 25 | |
katrijnverhasselt | 0:b40341017545 | 26 | // If message received, set to false and return true |
katrijnverhasselt | 0:b40341017545 | 27 | _isNewMessageReceived = false; |
katrijnverhasselt | 0:b40341017545 | 28 | return true; |
katrijnverhasselt | 0:b40341017545 | 29 | } |
katrijnverhasselt | 0:b40341017545 | 30 | |
katrijnverhasselt | 0:b40341017545 | 31 | Translator::MessageInfo Reader::GetLastMessageInfo() const { |
katrijnverhasselt | 0:b40341017545 | 32 | return _lastMessageInfo; |
katrijnverhasselt | 0:b40341017545 | 33 | } |
katrijnverhasselt | 0:b40341017545 | 34 | |
katrijnverhasselt | 0:b40341017545 | 35 | const int8_t* Reader::GetLastMessage() const { |
katrijnverhasselt | 0:b40341017545 | 36 | return _lastMessage; |
katrijnverhasselt | 0:b40341017545 | 37 | } |
katrijnverhasselt | 0:b40341017545 | 38 | |
katrijnverhasselt | 0:b40341017545 | 39 | int Reader::GetLastMessageSize() const { |
katrijnverhasselt | 0:b40341017545 | 40 | return _lastMessageSize; |
katrijnverhasselt | 0:b40341017545 | 41 | } |
katrijnverhasselt | 0:b40341017545 | 42 | |
katrijnverhasselt | 0:b40341017545 | 43 | void Reader::pcCallback() { |
katrijnverhasselt | 0:b40341017545 | 44 | // Get the sent character |
katrijnverhasselt | 0:b40341017545 | 45 | _lastChar = pc.getc(); |
katrijnverhasselt | 0:b40341017545 | 46 | if (_lastChar == Translator::Rules::StartChar) { |
katrijnverhasselt | 0:b40341017545 | 47 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 48 | } |
katrijnverhasselt | 0:b40341017545 | 49 | // Add character to buffer |
katrijnverhasselt | 0:b40341017545 | 50 | _buffer[_index] = _lastChar; |
katrijnverhasselt | 0:b40341017545 | 51 | // Check if valid message by translating it |
katrijnverhasselt | 0:b40341017545 | 52 | Translator::MessageInfo info; |
katrijnverhasselt | 0:b40341017545 | 53 | if (!Translator::Translate(_buffer, _index + 1, &info)) { |
katrijnverhasselt | 0:b40341017545 | 54 | _index++; |
katrijnverhasselt | 0:b40341017545 | 55 | return; |
katrijnverhasselt | 0:b40341017545 | 56 | } |
katrijnverhasselt | 0:b40341017545 | 57 | |
katrijnverhasselt | 0:b40341017545 | 58 | // Translation succesful, copy to _info |
katrijnverhasselt | 0:b40341017545 | 59 | _lastMessageInfo = info; |
katrijnverhasselt | 0:b40341017545 | 60 | // Notify user of new message |
katrijnverhasselt | 0:b40341017545 | 61 | _isNewMessageReceived = true; |
katrijnverhasselt | 0:b40341017545 | 62 | // Reset counter |
katrijnverhasselt | 0:b40341017545 | 63 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 64 | return; |
katrijnverhasselt | 0:b40341017545 | 65 | |
katrijnverhasselt | 0:b40341017545 | 66 | |
katrijnverhasselt | 0:b40341017545 | 67 | |
katrijnverhasselt | 0:b40341017545 | 68 | |
katrijnverhasselt | 0:b40341017545 | 69 | //printf("'%c' (%i) ; ", _lastChar, (int)_lastChar); |
katrijnverhasselt | 0:b40341017545 | 70 | // If no ongoing message and starting character was received |
katrijnverhasselt | 0:b40341017545 | 71 | if (_lastChar == Translator::Rules::StartChar) { |
katrijnverhasselt | 0:b40341017545 | 72 | //printf("First char: %c", _lastChar); |
katrijnverhasselt | 0:b40341017545 | 73 | // Get ready to receive the message |
katrijnverhasselt | 0:b40341017545 | 74 | _isMessageStarted = true; |
katrijnverhasselt | 0:b40341017545 | 75 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 76 | _charsToRead = -1; // value < 0 indicates no length was read yet |
katrijnverhasselt | 0:b40341017545 | 77 | } // If message was started but no length was read yet |
katrijnverhasselt | 0:b40341017545 | 78 | else if (_charsToRead < 0) { |
katrijnverhasselt | 0:b40341017545 | 79 | // Get ready to receive the command |
katrijnverhasselt | 0:b40341017545 | 80 | _charsToRead = (int)_lastChar; |
katrijnverhasselt | 0:b40341017545 | 81 | //printf("CharsToRead: %i", _charsToRead); |
katrijnverhasselt | 0:b40341017545 | 82 | //_buffer = new int8_t[_charsToRead]; |
katrijnverhasselt | 0:b40341017545 | 83 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 84 | } // If message was started and size is known |
katrijnverhasselt | 0:b40341017545 | 85 | else if (_charsToRead > 0) { |
katrijnverhasselt | 0:b40341017545 | 86 | //printf("Storing '%c' (%i)", _lastChar, (int)_lastChar); |
katrijnverhasselt | 0:b40341017545 | 87 | // Read _charsToRead characters and store in _buffer |
katrijnverhasselt | 0:b40341017545 | 88 | if (_index < _charsToRead) { |
katrijnverhasselt | 0:b40341017545 | 89 | _buffer[_index] = _lastChar; |
katrijnverhasselt | 0:b40341017545 | 90 | _index++; |
katrijnverhasselt | 0:b40341017545 | 91 | |
katrijnverhasselt | 0:b40341017545 | 92 | // If message was fully received |
katrijnverhasselt | 0:b40341017545 | 93 | if (_index == _charsToRead) { |
katrijnverhasselt | 0:b40341017545 | 94 | // Copy _buffer to _lastMessage (see Utility.h) |
katrijnverhasselt | 0:b40341017545 | 95 | DeepCopy(_buffer, _charsToRead, _lastMessage); |
katrijnverhasselt | 0:b40341017545 | 96 | _lastMessageSize = _charsToRead; |
katrijnverhasselt | 0:b40341017545 | 97 | // Reset values |
katrijnverhasselt | 0:b40341017545 | 98 | _isMessageStarted = false; |
katrijnverhasselt | 0:b40341017545 | 99 | _charsToRead = -1; |
katrijnverhasselt | 0:b40341017545 | 100 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 101 | // Let user know that a new message was received |
katrijnverhasselt | 0:b40341017545 | 102 | _isNewMessageReceived = true; |
katrijnverhasselt | 0:b40341017545 | 103 | } |
katrijnverhasselt | 0:b40341017545 | 104 | } |
katrijnverhasselt | 0:b40341017545 | 105 | } |
katrijnverhasselt | 0:b40341017545 | 106 | //printf(" / "); |
katrijnverhasselt | 0:b40341017545 | 107 | } |