Commanders
Arduino buttons/bus library
TextInterpreter.cpp
1 #include "TextInterpreter.hpp"
2 
3 #ifndef NO_SERIALCOMMANDER
5 {
6  this->Init();
7 }
8 
10 {
11  this->id = 0;
12  this->id2 = 255;
13  this->lastEventType = COMMANDERS_EVENT_NONE;
14  this->data = 0;
15  this->neg_sign = false;
16  this->step = TEXTINTERPRETER_STEP_ID;
17 }
18 
19 unsigned long TextInterpreter::SendChar(char inCharacter)
20 {
21  if (inCharacter == 'n')
22  {
23  unsigned long foundID = UNDEFINED_ID;
24 
25  /* end of the command by a 'n' : data OK */
26  if (this->id != UNDEFINED_ID && this->id != 0)
27  {
28  {
29  if (this->lastEventType == COMMANDERS_EVENT_NONE)
30  this->lastEventType = COMMANDERS_EVENT_MOVEPOSITIONID;
31 
32 #ifdef COMMANDERS_DEBUG_MODE
33  if (this->lastEventType != COMMANDERS_EVENT_TOGGLE && this->lastEventType != COMMANDERS_EVENT_MOVEPOSITIONID)
34  {
35  Serial.print(F("data = "));
36  if (this->neg_sign)
37  Serial.print(F("-"));
38  Serial.println(this->data);
39  }
40 #endif
41 
42  if (this->neg_sign == true)
43  this->data = -this->data;
44  }
45 
46 #ifndef NO_DCCCOMMANDER
47  if (this->id2 != 255)
48  foundID = DCCINT(this->id, this->id2);
49  else
50 #endif
51  foundID = this->id;
52  Commanders::RaiseEvent(foundID, this->lastEventType, this->data);
53  Commanders::SetLastEventType(this->lastEventType);
54  Commanders::SetLastEventData(this->data);
55  }
56 
57  this->Init();
58  return foundID;
59  }
60 
61  if (inCharacter == (char)-1)
62  return UNDEFINED_ID; /* loop if empty buffer */
63 
64  if (inCharacter == ':')
65  {
66  if (this->step == TEXTINTERPRETER_STEP_ID)
67  {
68  this->step = TEXTINTERPRETER_STEP_ID2;
69  return UNDEFINED_ID;
70  }
71  }
72 
73  if (inCharacter == ' ' || inCharacter == ',' || inCharacter == ';' || inCharacter == '/')
74  {
75 #ifdef COMMANDERS_DEBUG_VERBOSE_MODE
76  Serial.println(F("separator"));
77 #endif
78  switch (step)
79  {
80  case TEXTINTERPRETER_STEP_ID:
81 #ifdef COMMANDERS_DEBUG_MODE
82  Serial.print(F("id = "));
83  Serial.println(this->id);
84 #endif
85  this->step = TEXTINTERPRETER_STEP_TYPE;
86  break;
87  case TEXTINTERPRETER_STEP_ID2:
88 #ifdef COMMANDERS_DEBUG_MODE
89  Serial.print(F("id = DCCINT("));
90  Serial.print(this->id);
91  Serial.print(F(","));
92  Serial.print(this->id2);
93  Serial.print(F(")"));
94 #endif
95  this->step = TEXTINTERPRETER_STEP_TYPE;
96  break;
97  case TEXTINTERPRETER_STEP_TYPE:
98 #ifdef COMMANDERS_DEBUG_MODE
99  Serial.print(F("event = "));
100  Serial.println(this->lastEventType);
101 #endif
102  this->step = TEXTINTERPRETER_STEP_DATA;
103  this->data = 0;
104  break;
105  case TEXTINTERPRETER_STEP_DATA:
106 #ifdef COMMANDERS_DEBUG_MODE
107  Serial.print(F("data = "));
108  if (this->neg_sign)
109  Serial.print(F("-"));
110  Serial.println(this->data);
111 #endif
112  this->step = TEXTINTERPRETER_STEP_END;
113  break;
114  default:
115  break;
116  }
117  return UNDEFINED_ID;
118  }
119 
120  if (step == TEXTINTERPRETER_STEP_END) /* end of useful input ! */
121  return UNDEFINED_ID;
122 
123 #ifdef COMMANDERS_DEBUG_VERBOSE_MODE
124  Serial.print(F("read "));
125  Serial.println(inCharacter, DEC);
126 #endif
127 
128  switch (this->step)
129  {
130  case TEXTINTERPRETER_STEP_ID:
131  if (inCharacter >= 48 && inCharacter <= 57)
132  {
133  this->id *= 10; /* *10 => shift to left */
134  this->id = ((inCharacter - 48) + this->id); /* 48 because of ASCII value (1 => 49 in ASCII) */
135  this->data = 0;
136  }
137  break;
138 
139  case TEXTINTERPRETER_STEP_ID2:
140  if (inCharacter >= 48 && inCharacter <= 57)
141  {
142  if (this->id2 == 255)
143  this->id2 = 0;
144  else
145  this->id2 *= 10; /* *10 => shift to left */
146  this->id2 = ((inCharacter - 48) + this->id2); /* 48 because of ASCII value (1 => 49 in ASCII) */
147  }
148  break;
149 
150  case TEXTINTERPRETER_STEP_TYPE:
151  if (this->lastEventType == COMMANDERS_EVENT_NONE)
152  {
153  if (inCharacter == 't' || inCharacter == 'T') this->lastEventType = COMMANDERS_EVENT_TOGGLE;
154  if (inCharacter == 'm' || inCharacter == 'M') this->lastEventType = COMMANDERS_EVENT_MOVE;
155  if (inCharacter == 'p' || inCharacter == 'P') this->lastEventType = COMMANDERS_EVENT_MOVEPOSITION;
156  if (inCharacter == 'd' || inCharacter == 'D') this->lastEventType = COMMANDERS_EVENT_MOVEPOSITIONID;
157  if (inCharacter == 'i' || inCharacter == 'I') this->lastEventType = COMMANDERS_EVENT_MOVEPOSITIONINDEX;
158  if (inCharacter == 'c' || inCharacter == 'C') this->lastEventType = COMMANDERS_EVENT_CONFIG;
159  }
160  break;
161 
162  case TEXTINTERPRETER_STEP_DATA:
163  if (inCharacter == '-')
164  this->neg_sign = true;
165  if (inCharacter >= 48 && inCharacter <= 57)
166  {
167  this->data *= 10; /* *10 => shift to left */
168  this->data = ((inCharacter - 48) + this->data); /* 48 because of ASCII value (1 => 49 in ASCII) */
169  }
170 
171  break;
172  default:
173  break;
174  }
175  return UNDEFINED_ID;
176 }
177 
178 unsigned long TextInterpreter::SendString(char *inpString)
179 {
180  char *p = inpString;
181 
182  while (*p != 0)
183  this->SendChar(*(p++));
184 
185  return this->SendChar('n'); // End of command
186 }
187 #endif
unsigned long SendString(char *inpString)
static void SetLastEventData(int inData)
Definition: Commanders.hpp:93
static void SetLastEventType(COMMANDERS_EVENT_TYPE inEvent)
Definition: Commanders.hpp:89
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27
#define UNDEFINED_ID
Definition: Events.h:38
unsigned long SendChar(char inCharacter)