AceRoutine  0.2
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  virtual int runCoroutine() override {
61  InputLine input;
62  COROUTINE_LOOP() {
63  COROUTINE_AWAIT(mStream.available() > 0);
64  while (mStream.available() > 0) {
65  char 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 
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 
113 #endif
virtual int runCoroutine() override
The body of the coroutine.
#define COROUTINE_AWAIT(condition)
Yield until condition is true, then execution continues.
Definition: Coroutine.h:162
Message sent from StreamLineReader to CommandDispatcher coroutines through Channel<InputLine> channel...
Definition: InputLine.h:36
#define COROUTINE_LOOP()
Mark the beginning of a coroutine loop.
Definition: Coroutine.h:132
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:271