|
| 1 | +// |
| 2 | +// Nexus_Shell.h |
| 3 | +// |
| 4 | + |
| 5 | +namespace Nexus { |
| 6 | + |
| 7 | + typedef Task *(*CommandThunk)(Task *parent); |
| 8 | + |
| 9 | + struct Command { |
| 10 | + |
| 11 | + //Command(const char * name, CommandThunk command) : name(name), command(command) { } |
| 12 | + |
| 13 | + const char *name; |
| 14 | + CommandThunk command; |
| 15 | + |
| 16 | + }; |
| 17 | + |
| 18 | + class Buffer { |
| 19 | + |
| 20 | + public: |
| 21 | + |
| 22 | + Buffer() : _index(0) { } |
| 23 | + |
| 24 | + void push(char c) |
| 25 | + { |
| 26 | + _buffer[_index++] = c; |
| 27 | + } |
| 28 | + |
| 29 | + const char *value() |
| 30 | + { |
| 31 | + _buffer[_index] = '\0'; |
| 32 | + |
| 33 | + return _buffer; |
| 34 | + } |
| 35 | + |
| 36 | + void reset() |
| 37 | + { |
| 38 | + _index = 0; |
| 39 | + } |
| 40 | + |
| 41 | + private: |
| 42 | + |
| 43 | + char _buffer[30 + 1]; |
| 44 | + uint8_t _index; |
| 45 | + |
| 46 | + }; |
| 47 | + |
| 48 | + class Shell : public Task { |
| 49 | + |
| 50 | + public: |
| 51 | + |
| 52 | + Shell(const Command *commands) : Task(&TaskHelper<Shell>::run, F("Shell")), |
| 53 | + _commands(commands) |
| 54 | + { } |
| 55 | + |
| 56 | + const Command *getCommands() { return _commands; } |
| 57 | + |
| 58 | + void run(const Message& message) |
| 59 | + { |
| 60 | + KeyEvent keyEvent; Task *task; |
| 61 | + |
| 62 | + task_enter; |
| 63 | + |
| 64 | + getStream().print(F("] ")); |
| 65 | + |
| 66 | + for (;;) |
| 67 | + { |
| 68 | + task_wait(); |
| 69 | + |
| 70 | + if (keyEvent = message.get<KeyEvent>()) |
| 71 | + { |
| 72 | + switch (keyEvent.key) |
| 73 | + { |
| 74 | + break; case KeyEvent::KeyEnter: |
| 75 | + { |
| 76 | + getStream().print(F("\n")); |
| 77 | + |
| 78 | + task = executeCommand(_buffer.value()); |
| 79 | + |
| 80 | + getStream().print(F("] ")); |
| 81 | + } |
| 82 | + break; default: |
| 83 | + { |
| 84 | + insertCharacter(keyEvent.key); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + task_exit; |
| 91 | + } |
| 92 | + |
| 93 | + void insertCharacter(char c) |
| 94 | + { |
| 95 | + _buffer.push(c); |
| 96 | + |
| 97 | + getStream().print(c); |
| 98 | + } |
| 99 | + |
| 100 | + CommandThunk findCommand(const char *name) |
| 101 | + { |
| 102 | + for (const Command *command = _commands; pgm_ptr<void *>(command->name) != NULL; ++command) |
| 103 | + { |
| 104 | + //const char *commandName = (const char *)pgm_read_word(&command->name); |
| 105 | + const char *commandName = pgm_ptr<const char *>(command->name); |
| 106 | + const symbol commandName2 = pgm_ptr<const __FlashStringHelper *>(command->name); |
| 107 | + const CommandThunk thunk = (CommandThunk)pgm_read_word(&command->command); |
| 108 | + |
| 109 | + if (commandName2 == name) return thunk; |
| 110 | + //if (strcmp_P(name, commandName) == 0) return thunk; |
| 111 | + //if (strcmp_P(name, command->name) == 0) return command->command; |
| 112 | + //if (command->name == name) return command->command; |
| 113 | + } |
| 114 | + |
| 115 | + return NULL; |
| 116 | + } |
| 117 | + |
| 118 | + Task *executeCommand(const char *name) |
| 119 | + { |
| 120 | + Task *task; |
| 121 | + |
| 122 | + if (CommandThunk thunk = findCommand(name)) |
| 123 | + { |
| 124 | + task = thunk(this); |
| 125 | + } |
| 126 | + else getStream().println(F("Command not found")); |
| 127 | + |
| 128 | + _buffer.reset(); |
| 129 | + |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + |
| 133 | + private: |
| 134 | + |
| 135 | + const Command* _commands; |
| 136 | + Buffer _buffer; |
| 137 | + |
| 138 | + }; |
| 139 | + |
| 140 | +} |
0 commit comments