aboutsummaryrefslogtreecommitdiff
path: root/src/buffermanager.cpp
blob: 717cd25de8892365ad1abac707669a4e259e5dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
    Copyright (C) 2014 BARRATERO Laurent                                   

    AeroUp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by 
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Aeroup is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
*/

/*
 *       Filename:  buffermanager.cpp
 *
 *    Description:  take the parsed, tokenized lines and manage them.
 *                  send also some rules test
 *
 *        Version:  0.3.2
 *        Created:  30/12/2013 12:12:46
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  BARATTERO Laurent, laurentba<at>larueluberlu.net
 *   Organization:  La rue Luberlu
 */

#include "buffermanager.hpp"

using namespace std;


/*
 *       Class:  BufferManager
 *      Method:  BufferManager
 * Description:  constructor
 */

BufferManager::BufferManager (Buffer* theBuff, Rules* theRules):
                    buff(theBuff),
                    rules(theRules)
{
} 

/*
 *       Class:  BufferManager
 *      Method:  completesCreateCommand
 * Description:  Second pass to buff, Completes createCommand work
 */
  void
BufferManager::completesCreateCommand()
{
  verifyEndFileError();
  addDefsubToBuff();
}

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: verifyEndFileError
 * Description:  Verification which require that EOF is reached
 *               executed after the multiple call
 *               to the createCommand function by GloSeq instance.
 */
  void
BufferManager::verifyEndFileError()
{
  /* verify there is no DEFSUB without ENDSUB before EOF */
  rules->endTestDefsub();

}

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: addDefsubToBuff
 * Description:  adds the defsub adress to every sub of the buffer 
 */
  void
BufferManager::addDefsubToBuff()
{
  string sub_name = buff->addDefsubToBuff();
  /* If a sub is not defined, we throw an error */
  if(!sub_name.empty())
  {
    rules->noDefsubError(sub_name);
  }
}

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: createCommand
 * Description:  fills the buffer with a token line 
 */

  void
BufferManager::createCommand (vector <string> tokens, int cmd_type, int nb_line)
{

  switch (cmd_type) {

    case C_FCT:	
      rules->isStandardCmdPossible(nb_line, C_FCT);
      colorsFillBuff(tokens);
      break;

    case D_FCT:	
      rules->isStandardCmdPossible(nb_line, D_FCT);
      delayFillBuff(tokens);
      break;

    case RAMP_FCT:	
      rules->isStandardCmdPossible(nb_line, RAMP_FCT);
      rampFillBuff(tokens);
      break;

    case L_FCT:	
      rules->setloop(nb_line);
      loopFillBuff(tokens);
      break;

    case E_FCT:	
      rules->unsetloop(nb_line);
      oneByteFillBuff(E_CMD);
      break;

    case R_FCT:	
      rules->isStandardCmdPossible(nb_line, R_FCT);
      oneColorFillBuff(tokens, R_CMD);
      break;

    case G_FCT:	
      rules->isStandardCmdPossible(nb_line, G_FCT);
      oneColorFillBuff(tokens, G_CMD);
      break;

    case B_FCT:	
      rules->isStandardCmdPossible(nb_line, B_FCT);
      oneColorFillBuff(tokens, B_CMD);
      break;

    case SUB_FCT:	
      rules->isStandardCmdPossible(nb_line, SUB_FCT);
      subFillBuff(tokens);
      break;

    case DEFSUB_FCT:	
      rules->setDefsub(nb_line);
      defsubFillBuff(tokens, nb_line);
      break;

    case ENDSUB_FCT:	
      rules->unsetDefsub(nb_line);
      oneByteFillBuff(ENDSUB_CMD);
      break;

    case END_FCT:	
      rules->setEnd(nb_line);
      oneByteFillBuff(END_CMD);
      break;
  }				/* -----  end switch  ----- */
  return ;
}		/* -----  end of method BufferManager::createCommand  ----- */



/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: oneColorFillBuff
 * Description:  ( *FillBuff function ) generic method for R, G, B commands 
 */

  void
BufferManager::oneColorFillBuff(vector <string> tokens, int col)
{
  buff->addChr(col);
  buffAddOneColor(tokens[1]);
}


/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: ColorFillBuff
 * Description:  ( *FillBuff function ) Color C
 */
  void
BufferManager::colorsFillBuff (vector <string> tokens)
{
  buff->addChr(C_CMD);
  buffAddTriColors(tokens);
}


/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: delayFillBuff
 * Description:  ( *FillBuff function ) Delay D
 */

  void
BufferManager::delayFillBuff (vector <string> tokens)
{
  int tmp_byte;
  tmp_byte = stoi(tokens[1]);
  if (verifyByteOverflow(tmp_byte) != -1)
  {
    buff->addChr(D_LOW_CMD);
    buff->addChr((uint8_t)tmp_byte);
    return;
  }
  else if(verify2BytesOverflow(tmp_byte) != -1)
  {
    buff->addChr(D_HIGH_CMD);
    buff->addChr((uint8_t)(tmp_byte & 0xff));
    buff->addChr((uint8_t)(tmp_byte >> 8));
    return;
  }
  else
    exit(EXIT_FAILURE);
}		/* -----  end of method BufferManager::delayFillBuff  ----- */

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: loopFillBuff
 * Description:  ( *FillBuff function ) Loop L
 */
  void
BufferManager::loopFillBuff (vector <string> tokens)
{
  buff->addChr(L_CMD);
  int tmp_byte;
    tmp_byte = stoi(tokens[1]);
    if(verifyByteOverflow(tmp_byte)!= -1)
    {
      buff->addChr((uint8_t)tmp_byte);
    }
    else 
    {
      exit(EXIT_FAILURE);
    }
}		/* -----  end of method BufferManager::loopFillBuff  ----- */

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: oneByteFillBuff
 * Description:  ( *FillBuff function ) Generic for E, END, ENDSUB
 */
  void
BufferManager::oneByteFillBuff(int cmd)
{
  buff->addChr(cmd);
}		/* -----  end of method BufferManager::endLoopFillBuff  ----- */
  
/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: rampFillBuff
 * Description:  ( *FillBuff function ) RAMP
 */
  void 
BufferManager::rampFillBuff(std::vector<std::string> tokens)
{
  int tmp_byte;
  tmp_byte = stoi(tokens[4]);
  if (verifyByteOverflow(tmp_byte) != -1)
  {
    buff->addChr(RAMP_LOW_CMD);
    buffAddTriColors(tokens);
    buff->addChr(tmp_byte);
  }
  else if(verify2BytesOverflow(tmp_byte) != -1)
  {
    buff->addChr(RAMP_HIGH_CMD);
    buffAddTriColors(tokens);
    buff->addChr((uint8_t)(tmp_byte & 0xff));
    buff->addChr((uint8_t)(tmp_byte >> 8));
    return;
  }
  else
    exit(EXIT_FAILURE);
}		/* -----  end of method BufferManager::rampFillBuff  ----- */


/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: subFillBuff
 * Description:  ( *FillBuff function ) SUB
 *               - Add SUB_CMD to buff
 *               - Stores the buffer position of the SUB LSB value in a map
 *               - And add two tempory '\0' value to buff for LSB & MSB  
 */
  void
BufferManager::subFillBuff(std::vector<std::string> tokens)
{
  buff->addChr(SUB_CMD);
  buff->addSubPos(tokens[1]);
  buff->addChr(0);
  buff->addChr(0);
} /* -----  end of method BufferManager::subFillBuff ----- */

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: defsubFillBuff
 * Description:  ( *FillBuff function ) DEFSUB
 *               - Add the beginning position of the DEFSUB 
 *                  function in the buffer to a map.
 *               - throws an error if the same function is defined twice
 */
  void
BufferManager::defsubFillBuff(std::vector<std::string> tokens, int nb_line)
{
  if(buff->addDefsubPos(tokens[1]))
  {
    rules->twiceDefsubError(nb_line);
  }

} /* ----- end of method BufferManager::defsubFillBuff function ----- */



/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: verifyByteOverflow
 */
  int
BufferManager::verifyByteOverflow(int value)
{
  if (value < 256 && value > -1)
  {
    return value;
  }
  else
    return -1;
} /* ----- end of method BufferManager::verifyByteOverflow function ----- */

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: verify2BytesOverflow
 */
  int
BufferManager::verify2BytesOverflow(int value)
{
  if (value < 65536 && value > 255)
  {
    return value;
  }
  else
    return -1;
} /* ----- end of method BufferManager::verify2BytesOverflow function ----- */

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: buffAddTriColors
 */
  void
BufferManager::buffAddTriColors(vector<string>& tokens)
{
  for(int i = 1; i < 4; i++)
  {
    buffAddOneColor(tokens[i]);
  }
} /* ----- end of method BufferManager::buffAddTriColors function ----- */

/*
 *       Class:  BufferManager
 *      Method:  BufferManager :: buffAddOneColor
 */
  void
BufferManager::buffAddOneColor(string& tok)
{
    int tmp_byte;
    tmp_byte = stoi(tok);
    if(verifyByteOverflow(tmp_byte)!= -1)
    {
      buff->addChr((uint8_t)tmp_byte);
    }
    else {
      exit(EXIT_FAILURE);
    }
} /* ----- end of method BufferManager::buffAddOneColor function ----- */