AceUtils  0.3
Useful Arduino utilties which are too small as separate libraries, but complex enough to have external dependencies to other libraries.
StreamLineReader.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef CLI_STREAM_LINE_READER_H
26 #define CLI_STREAM_LINE_READER_H
27 
28 #include <Arduino.h> // Stream
29 #include <AceRoutine.h>
30 #include "InputLine.h"
31 
32 namespace cli {
33 
39 class StreamLineReader: public ace_routine::Coroutine {
40  public:
51  StreamLineReader(ace_routine::Channel<InputLine>& channel, Stream& stream,
52  char* buffer, int bufferSize):
53  mChannel(channel),
54  mStream(stream),
55  mBuf(buffer),
56  mBufSize(bufferSize)
57  {}
58 
59  int runCoroutine() override {
60  InputLine input;
61  char c;
62  COROUTINE_LOOP() {
63  COROUTINE_AWAIT(mStream.available() > 0);
64  while (mStream.available() > 0) {
65  c = mStream.read();
66  mBuf[mIndex] = c;
67  mIndex++;
68  if (mIndex >= mBufSize - 1) {
69  input.status = InputLine::kStatusOverflow;
70  input.line = mBuf;
71  mFlushLine = true;
72  resetBuffer();
73  COROUTINE_CHANNEL_WRITE(mChannel, input);
74  } else if (c == '\n' || c == '\r') {
75  input.status =
76  mFlushLine ? InputLine::kStatusOverflow : InputLine::kStatusOk;
77  input.line = mBuf;
78  mFlushLine = false;
79  resetBuffer();
80  COROUTINE_CHANNEL_WRITE(mChannel, input);
81  }
82  }
83  }
84  }
85 
86  private:
87  // Disable copy-constructor and assignment operator
88  StreamLineReader(const StreamLineReader&) = delete;
89  StreamLineReader& operator=(const StreamLineReader&) = delete;
90 
96  void resetBuffer() {
97  mBuf[mIndex] = '\0';
98  mIndex = 0;
99  }
100 
101  ace_routine::Channel<InputLine>& mChannel;
102  Stream& mStream;
103  char* const mBuf;
104  int const mBufSize;
105 
106  int mIndex = 0;
107  bool mFlushLine = false;
108 };
109 
110 }
111 
112 #endif
cli::InputLine
Message sent from StreamLineReader to CommandDispatcher coroutines through Channel<InputLine> channel...
Definition: InputLine.h:35
cli::StreamLineReader
An AceRoutine coroutine that reads lines (terminated by '\n' or '\r' from the Stream device,...
Definition: StreamLineReader.h:39
cli::StreamLineReader::StreamLineReader
StreamLineReader(ace_routine::Channel< InputLine > &channel, Stream &stream, char *buffer, int bufferSize)
Constructor.
Definition: StreamLineReader.h:51