Gets messages form the pc and translates it to I2C and back.
Dependencies: DevInterfaces I2Cinterfaces MCP4725 mbed
Reader.cpp@0:b40341017545, 2016-05-18 (annotated)
- 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?
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 | printf("Translate failed on "); |
katrijnverhasselt | 0:b40341017545 | 55 | for (int i = 0; i <= _index; i++) |
katrijnverhasselt | 0:b40341017545 | 56 | printf("%c", _buffer[i]); |
katrijnverhasselt | 0:b40341017545 | 57 | _index++; |
katrijnverhasselt | 0:b40341017545 | 58 | return; |
katrijnverhasselt | 0:b40341017545 | 59 | } |
katrijnverhasselt | 0:b40341017545 | 60 | |
katrijnverhasselt | 0:b40341017545 | 61 | // Translation succesful, copy to _info |
katrijnverhasselt | 0:b40341017545 | 62 | _lastMessageInfo = info; |
katrijnverhasselt | 0:b40341017545 | 63 | // Notify user of new message |
katrijnverhasselt | 0:b40341017545 | 64 | _isNewMessageReceived = true; |
katrijnverhasselt | 0:b40341017545 | 65 | // Reset counter |
katrijnverhasselt | 0:b40341017545 | 66 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 67 | return; |
katrijnverhasselt | 0:b40341017545 | 68 | |
katrijnverhasselt | 0:b40341017545 | 69 | |
katrijnverhasselt | 0:b40341017545 | 70 | |
katrijnverhasselt | 0:b40341017545 | 71 | |
katrijnverhasselt | 0:b40341017545 | 72 | //printf("'%c' (%i) ; ", _lastChar, (int)_lastChar); |
katrijnverhasselt | 0:b40341017545 | 73 | // If no ongoing message and starting character was received |
katrijnverhasselt | 0:b40341017545 | 74 | if (_lastChar == Translator::Rules::StartChar) { |
katrijnverhasselt | 0:b40341017545 | 75 | //printf("First char: %c", _lastChar); |
katrijnverhasselt | 0:b40341017545 | 76 | // Get ready to receive the message |
katrijnverhasselt | 0:b40341017545 | 77 | _isMessageStarted = true; |
katrijnverhasselt | 0:b40341017545 | 78 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 79 | _charsToRead = -1; // value < 0 indicates no length was read yet |
katrijnverhasselt | 0:b40341017545 | 80 | } // If message was started but no length was read yet |
katrijnverhasselt | 0:b40341017545 | 81 | else if (_charsToRead < 0) { |
katrijnverhasselt | 0:b40341017545 | 82 | // Get ready to receive the command |
katrijnverhasselt | 0:b40341017545 | 83 | _charsToRead = (int)_lastChar; |
katrijnverhasselt | 0:b40341017545 | 84 | //printf("CharsToRead: %i", _charsToRead); |
katrijnverhasselt | 0:b40341017545 | 85 | //_buffer = new int8_t[_charsToRead]; |
katrijnverhasselt | 0:b40341017545 | 86 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 87 | } // If message was started and size is known |
katrijnverhasselt | 0:b40341017545 | 88 | else if (_charsToRead > 0) { |
katrijnverhasselt | 0:b40341017545 | 89 | //printf("Storing '%c' (%i)", _lastChar, (int)_lastChar); |
katrijnverhasselt | 0:b40341017545 | 90 | // Read _charsToRead characters and store in _buffer |
katrijnverhasselt | 0:b40341017545 | 91 | if (_index < _charsToRead) { |
katrijnverhasselt | 0:b40341017545 | 92 | _buffer[_index] = _lastChar; |
katrijnverhasselt | 0:b40341017545 | 93 | _index++; |
katrijnverhasselt | 0:b40341017545 | 94 | |
katrijnverhasselt | 0:b40341017545 | 95 | // If message was fully received |
katrijnverhasselt | 0:b40341017545 | 96 | if (_index == _charsToRead) { |
katrijnverhasselt | 0:b40341017545 | 97 | // Copy _buffer to _lastMessage (see Utility.h) |
katrijnverhasselt | 0:b40341017545 | 98 | DeepCopy(_buffer, _charsToRead, _lastMessage); |
katrijnverhasselt | 0:b40341017545 | 99 | _lastMessageSize = _charsToRead; |
katrijnverhasselt | 0:b40341017545 | 100 | // Reset values |
katrijnverhasselt | 0:b40341017545 | 101 | _isMessageStarted = false; |
katrijnverhasselt | 0:b40341017545 | 102 | _charsToRead = -1; |
katrijnverhasselt | 0:b40341017545 | 103 | _index = 0; |
katrijnverhasselt | 0:b40341017545 | 104 | // Let user know that a new message was received |
katrijnverhasselt | 0:b40341017545 | 105 | _isNewMessageReceived = true; |
katrijnverhasselt | 0:b40341017545 | 106 | } |
katrijnverhasselt | 0:b40341017545 | 107 | } |
katrijnverhasselt | 0:b40341017545 | 108 | } |
katrijnverhasselt | 0:b40341017545 | 109 | //printf(" / "); |
katrijnverhasselt | 0:b40341017545 | 110 | } |