AceUtils  0.5.0
Useful Arduino utilties which are too small as separate libraries, but complex enough to be shared among multiple projects, and often 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 ACE_UTILS_CLI_STREAM_LINE_READER_H
26 #define ACE_UTILS_CLI_STREAM_LINE_READER_H
27 
28 #include <Arduino.h> // Stream
29 #include <AceRoutine.h>
30 #include "InputLine.h"
31 
32 namespace ace_utils {
33 namespace cli {
34 
40 class StreamLineReader: public ace_routine::Coroutine {
41  public:
52  StreamLineReader(ace_routine::Channel<InputLine>& channel, Stream& stream,
53  char* buffer, int bufferSize):
54  mChannel(channel),
55  mStream(stream),
56  mBuf(buffer),
57  mBufSize(bufferSize)
58  {}
59 
64  int runCoroutine() override {
65  InputLine input;
66  char c;
67  COROUTINE_LOOP() {
68  COROUTINE_AWAIT(mStream.available() > 0);
69  while (mStream.available() > 0) {
70  c = mStream.read();
71  mBuf[mIndex] = c;
72  mIndex++;
73  if (mIndex >= mBufSize - 1) {
74  input.status = InputLine::kStatusOverflow;
75  input.line = mBuf;
76  mFlushLine = true;
77  resetBuffer();
78  COROUTINE_CHANNEL_WRITE(mChannel, input);
79  } else if (c == '\n' || c == '\r') {
80  input.status =
81  mFlushLine ? InputLine::kStatusOverflow : InputLine::kStatusOk;
82  input.line = mBuf;
83  mFlushLine = false;
84  resetBuffer();
85  COROUTINE_CHANNEL_WRITE(mChannel, input);
86  }
87  }
88  }
89  }
90 
91  private:
92  // Disable copy-constructor and assignment operator
93  StreamLineReader(const StreamLineReader&) = delete;
94  StreamLineReader& operator=(const StreamLineReader&) = delete;
95 
101  void resetBuffer() {
102  mBuf[mIndex] = '\0';
103  mIndex = 0;
104  }
105 
106  ace_routine::Channel<InputLine>& mChannel;
107  Stream& mStream;
108  char* const mBuf;
109  int const mBufSize;
110 
111  int mIndex = 0;
112  bool mFlushLine = false;
113 };
114 
115 } // cli
116 } // ace_utils
117 
118 #endif
ace_utils::cli::InputLine
Message sent from StreamLineReader to CommandDispatcher coroutines through Channel<InputLine> channel...
Definition: InputLine.h:36
ace_utils::cli::StreamLineReader::StreamLineReader
StreamLineReader(ace_routine::Channel< InputLine > &channel, Stream &stream, char *buffer, int bufferSize)
Constructor.
Definition: StreamLineReader.h:52
ace_utils::cli::StreamLineReader
An AceRoutine coroutine that reads lines (terminated by '\n' or '\r' from the Stream device,...
Definition: StreamLineReader.h:40
ace_utils::cli::StreamLineReader::runCoroutine
int runCoroutine() override
Main body of the coroutine that reads a character from the input stream and writes it into the output...
Definition: StreamLineReader.h:64