AceUtils  0.3
Useful Arduino utilties which are too small as separate libraries, but complex enough to have external dependencies to other libraries.
CommandManager.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_COMMAND_MANAGER_H
26 #define CLI_COMMAND_MANAGER_H
27 
28 #include <AceRoutine.h> // Coroutine, Channel
29 #include "CommandDispatcher.h"
30 #include "StreamLineReader.h"
31 
32 namespace cli {
33 
80 template<uint8_t BUF_SIZE, uint8_t ARGV_SIZE>
81 class CommandManager: public ace_routine::Coroutine {
82  public:
83 
95  CommandManager(const CommandHandler* const* commands, uint8_t numCommands,
96  Stream& serial, const char* prompt = nullptr):
97  mCommands(commands),
98  mNumCommands(numCommands),
99  mSerial(serial),
100  mPrompt(prompt),
101  mStreamLineReader(mChannel, mSerial, mLineBuffer, BUF_SIZE),
102  mDispatcher(mChannel, mSerial, mCommands, mNumCommands,
103  mArgv, ARGV_SIZE, mPrompt) {}
104 
105  int runCoroutine() override {
106  mStreamLineReader.runCoroutine();
107  mDispatcher.runCoroutine();
108  return 0;
109  }
110 
112  const CommandDispatcher* getDispatcher() const { return &mDispatcher; }
113 
114  private:
115  const CommandHandler* const* const mCommands;
116  uint8_t const mNumCommands;
117  Stream& mSerial;
118  const char* const mPrompt;
119  ace_routine::Channel<InputLine> mChannel;
120  StreamLineReader mStreamLineReader;
121  CommandDispatcher mDispatcher;
122  char mLineBuffer[BUF_SIZE];
123  const char* mArgv[ARGV_SIZE];
124 };
125 
126 }
127 
128 #endif
cli::CommandDispatcher
A coroutine that reads lines from the Serial port, tokenizes the line on whitespace boundaries,...
Definition: CommandDispatcher.h:51
cli::CommandManager
A convenience wrapper around a CommandDispatcher that hides complexity of creating,...
Definition: CommandManager.h:81
cli::CommandManager::CommandManager
CommandManager(const CommandHandler *const *commands, uint8_t numCommands, Stream &serial, const char *prompt=nullptr)
Constructor.
Definition: CommandManager.h:95
cli::CommandManager::getDispatcher
const CommandDispatcher * getDispatcher() const
Return the CommandDispatcher.
Definition: CommandManager.h:112
cli::CommandHandler
Signature for a command handler.
Definition: CommandHandler.h:46
cli::StreamLineReader
An AceRoutine coroutine that reads lines (terminated by '\n' or '\r' from the Stream device,...
Definition: StreamLineReader.h:39