AceRoutine  1.0
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
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_ROUTINE_STREAM_LINE_READER_H
26 #define ACE_ROUTINE_STREAM_LINE_READER_H
27 
28 #include <Arduino.h> // Stream
29 #include <AceRoutine.h>
30 #include "InputLine.h"
31 
32 namespace ace_routine {
33 namespace cli {
34 
41  public:
53  char* buffer, int bufferSize):
54  mChannel(channel),
55  mStream(stream),
56  mBuf(buffer),
57  mBufSize(bufferSize)
58  {}
59 
60  int runCoroutine() override {
61  InputLine input;
62  char c;
63  COROUTINE_LOOP() {
64  COROUTINE_AWAIT(mStream.available() > 0);
65  while (mStream.available() > 0) {
66  c = mStream.read();
67  mBuf[mIndex] = c;
68  mIndex++;
69  if (mIndex >= mBufSize - 1) {
70  input.status = InputLine::kStatusOverflow;
71  input.line = mBuf;
72  mFlushLine = true;
73  resetBuffer();
74  COROUTINE_CHANNEL_WRITE(mChannel, input);
75  } else if (c == '\n' || c == '\r') {
76  input.status =
77  mFlushLine ? InputLine::kStatusOverflow : InputLine::kStatusOk;
78  input.line = mBuf;
79  mFlushLine = false;
80  resetBuffer();
81  COROUTINE_CHANNEL_WRITE(mChannel, input);
82  }
83  }
84  }
85  }
86 
87  private:
88  // Disable copy-constructor and assignment operator
89  StreamLineReader(const StreamLineReader&) = delete;
90  StreamLineReader& operator=(const StreamLineReader&) = delete;
91 
97  void resetBuffer() {
98  mBuf[mIndex] = '\0';
99  mIndex = 0;
100  }
101 
103  Stream& mStream;
104  char* const mBuf;
105  int const mBufSize;
106 
107  int mIndex = 0;
108  bool mFlushLine = false;
109 };
110 
111 }
112 }
113 
114 #endif
#define COROUTINE_AWAIT(condition)
Yield until condition is true, then execution continues.
Definition: Coroutine.h:159
Message sent from StreamLineReader to CommandDispatcher coroutines through Channel<InputLine> channel...
Definition: InputLine.h:36
int runCoroutine() override
The body of the coroutine.
#define COROUTINE_LOOP()
Mark the beginning of a coroutine loop.
Definition: Coroutine.h:129
An AceRoutine coroutine that reads lines (terminated by &#39;\n&#39; or &#39;\r&#39; from the Stream device...
An unbuffered synchronized channel.
Definition: Channel.h:43
StreamLineReader(ace_routine::Channel< InputLine > &channel, Stream &stream, char *buffer, int bufferSize)
Constructor.
Base class of all coroutines.
Definition: Coroutine.h:255