RSeries astromech firmware
JawaCommander.h
Go to the documentation of this file.
1 #ifndef JawaCommander_h
2 #define JawaCommander_h
3 
4 #include "JawaEvent.h"
5 
7 {
8 public:
10 
15  bool parseCommand(const char* inputStr)
16  {
17  char ch, cmd;
18  int addr = 0;
19  const char* str = inputStr;
20  if ((ch = *str++) == '\0')
21  return true;
22  if (isdigit(ch))
23  {
24  do
25  {
26  addr = addr * 10 + (ch - '0');
27  ch = *str++;
28  } while (isdigit(ch));
29  }
30  switch (cmd = ch)
31  {
32  case 'A':
33  case 'D':
34  JawaEvent::process(addr, cmd);
35  return true;
36  case 'B': case 'C':
37  case 'E': case 'I':
38  case 'N': case 'O':
39  case 'R': case 'S':
40  case 'T': case 'W':
41  {
42  int arg = 0;
43  ch = *str++;
44  if (isdigit(ch))
45  {
46  do
47  {
48  arg = arg * 10 + (ch - '0');
49  ch = *str++;
50  } while (isdigit(ch));
51  }
52  else
53  {
54  /* Invalid */
55  return false;
56  }
57  JawaEvent::process(addr, cmd, arg);
58  return true;
59  }
60  case 'L':
61  /* Loadable font not supported */
62  return false;
63  case 'M':
64  {
65  char *bm = fMessageBuffer;
66  char *bme = fMessageBuffer + fBufferSize - 1;
67  *bm = '\0';
68  while ((ch = *str++) != '\0' && bm < bme)
69  {
70  *bm++ = ch;
71  *bm = '\0';
72  }
74  return true;
75  }
76  case 'P':
77  {
78  int arg = 0;
79  ch = *str++;
80  if (isdigit(ch))
81  arg = ch - '0';
82  else if (ch >= 'A' && ch <= 'Z')
83  arg = 10 + ch - 'A';
84  else
85  /* Invalid */
86  return false;
87  ch = *str++;
88  if (!isdigit(ch))
89  /* Invalid */
90  return false;
91  int value = 0;
92  do
93  {
94  value = value * 10 + (ch - '0');
95  ch = *str++;
96  } while (isdigit(ch));
97  JawaEvent::process(addr, cmd, arg, value);
98  return true;
99  }
100  case 'V':
101  {
102  char fname[13];
103  char *fn = fname;
104  char *fne = fname + sizeof(fname) - 1;
105  if ((ch = *str) == '0')
106  {
107  JawaEvent::process(addr, cmd);
108  return true;
109  }
110  *fn = '\0';
111  while ((ch = *str++) != '\0' && fn < fne)
112  {
113  *fn++ = ch;
114  *fn = '\0';
115  }
117  return true;
118  }
119  case 'X':
120  // TODO
121  /* Exmine new channel values not supported */
122  return false;
123  case 'Z':
124  JawaEvent::process(addr, cmd);
125  return true;
126  case ':':
127  /* Ignore comments */
128  return false;
129  default:
130  /* Invalid */
131  return false;
132  }
133  return false;
134  }
135 
139  void process(const char ch)
140  {
141  if (fSkipEOL)
142  {
143  if (ch != '\n' && ch != '\r')
144  return;
145  fSkipEOL = false;
146  fNewLine = true;
147  }
148  if (fNewLine && isspace(ch))
149  return;
150  fNewLine = false;
151  if (ch == '\n' || ch == '\r')
152  {
153  if (fPtr != fBuffer)
154  {
156  }
157  fPtr = fBuffer;
158  fNewLine = true;
159  }
160  else if (fPtr < fBuffer + fBufferSize - 1)
161  {
162  *fPtr++ = ch;
163  *fPtr = '\0';
164  }
165  else
166  {
168  fPtr = fBuffer;
169  fSkipEOL = true;
170  }
171  }
172 
174  {
175  return *base();
176  }
177 
181  void process(const char* msg)
182  {
183  for (char ch; (ch = *msg) != '\0'; msg++)
184  process(ch);
185  process('\r');
186  }
187 
191  inline char* getBuffer()
192  {
193  return fBuffer;
194  }
195 
199  inline size_t getBufferSize()
200  {
201  return fBufferSize;
202  }
203 
207  inline size_t getMessageSize()
208  {
209  return fMsgSize;
210  }
211 
212 protected:
214  {
215  static JawaCommanderBase* sBase;
216  return &sBase;
217  }
218 
219  bool fNewLine = true;
220  bool fSkipEOL = false;
221  uint8_t fBufferSize;
222  uint8_t fMsgSize;
223  char* fBuffer;
225  char* fPtr;
226 };
227 
235 template<uint8_t BUFFER_SIZE=20, uint8_t MSG_SIZE=20>
237 {
238 public:
240  {
241  fPtr = fBuffer = fBufferStorage;
242  fMessageBuffer = fMessageBufferStorage;
243  fBufferSize = BUFFER_SIZE;
244  fMsgSize = MSG_SIZE;
245  *base() = this;
246  }
247 
248 private:
249  char fBufferStorage[BUFFER_SIZE];
250  char fMessageBufferStorage[MSG_SIZE];
251 };
252 
253 #endif
JawaCommanderBase::getBuffer
char * getBuffer()
Return a direct pointer to the parsers input buffer.
Definition: JawaCommander.h:191
JawaCommanderBase::fMessageBuffer
char * fMessageBuffer
Definition: JawaCommander.h:224
JawaCommanderBase::getMessageSize
size_t getMessageSize()
Return the maximum size of the input buffer.
Definition: JawaCommander.h:207
JawaCommanderBase::base
static JawaCommanderBase ** base()
Definition: JawaCommander.h:213
JawaCommanderBase::fMsgSize
uint8_t fMsgSize
Definition: JawaCommander.h:222
JawaCommanderBase::parseCommand
bool parseCommand(const char *inputStr)
Parse the specified command string.
Definition: JawaCommander.h:15
JawaCommanderBase::get
static JawaCommanderBase * get()
Definition: JawaCommander.h:173
JawaCommanderBase::process
void process(const char *msg)
Append a string of character to the parsers input buffer.
Definition: JawaCommander.h:181
JawaCommander
JAWA Lite command parser.
Definition: JawaCommander.h:236
JawaCommanderBase::fBuffer
char * fBuffer
Definition: JawaCommander.h:223
JawaCommanderBase
Definition: JawaCommander.h:6
JawaEvent::process
static void process(int addr, char cmd, int arg=0, int value=0)
Definition: JawaEvent.h:48
JawaCommanderBase::fBufferSize
uint8_t fBufferSize
Definition: JawaCommander.h:221
JawaCommanderBase::JawaCommanderBase
JawaCommanderBase()
Definition: JawaCommander.h:9
JawaEvent.h
JawaCommanderBase::fNewLine
bool fNewLine
Definition: JawaCommander.h:219
JawaCommanderBase::getBufferSize
size_t getBufferSize()
Return the maximum size of the input buffer.
Definition: JawaCommander.h:199
JawaCommanderBase::fPtr
char * fPtr
Definition: JawaCommander.h:225
JawaCommander::JawaCommander
JawaCommander()
Definition: JawaCommander.h:239
JawaCommanderBase::process
void process(const char ch)
Append a single character to the parsers input buffer.
Definition: JawaCommander.h:139
JawaCommanderBase::fSkipEOL
bool fSkipEOL
Definition: JawaCommander.h:220