forked from Integreight/1Sheeld-Arduino-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneSheeld.cpp
606 lines (558 loc) · 16.8 KB
/
OneSheeld.cpp
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
Project: 1Sheeld Library
File: OneSheeld.cpp
Version: 1.2
Compiler: Arduino avr-gcc 4.3.2
Author: Integreight
Date: 2014.5
*/
#define FROM_ONESHEELD_LIBRARY
#include "OneSheeld.h"
#include "HardwareSerial.h"
#include "stdarg.h"
bool OneSheeldClass::isInit=false;
byte OneSheeldClass::shieldsCounter=0;
unsigned long OneSheeldClass::lastTimeFrameSent=0;
bool OneSheeldClass::inACallback=false;
bool OneSheeldClass::callbacksInterrupts=false;
bool OneSheeldClass::isFirstFrame=false;
ShieldParent * OneSheeldClass::shieldsArray[]={0};
// #ifdef INTERNET_SHIELD
byte OneSheeldClass::requestsCounter=0;
HttpRequest ** OneSheeldClass::requestsArray=(HttpRequest**)malloc(sizeof(HttpRequest*)*MAX_NO_OF_REQUESTS);
// #endif
//Class Constructor
OneSheeldClass::OneSheeldClass(Stream &s) :OneSheeldSerial(s)
{
shield=0;
instance=0;
functions=0;
counter=0;
argumentcounter=0;
datalengthcounter=0;
argumentnumber=0;
endFrame=0;
numberOfDataMalloced=0;
isArgumentsNumberMalloced=false;
isArgumentLengthMalloced=false;
callbacksInterrupts=false;
framestart =false;
isOneSheeldConnected =false;
isAppConnectionCallBack = false;
isShieldFrameCallback = false;
isSerialDataCallback = false;
}
//Library Starter
void OneSheeldClass::begin(long baudRate)
{
#if defined(__AVR_ATmega32U4__) || defined(ARDUINO_LINUX)
Serial1.begin(baudRate);
#else
Serial.begin(baudRate);
#endif
}
//Blocking function
void OneSheeldClass::waitForAppConnection()
{
isOneSheeldConnected = false;
while(!isOneSheeldConnected)
{
OneSheeld.processInput();
}
}
//Library Starter
void OneSheeldClass::begin()
{
begin(115200);
sendShieldFrame(ONESHEELD_ID,0,CHECK_APP_CONNECTION,0);
isInit=true;
// #ifdef INTERNET_SHIELD
for(int i=0;i<requestsCounter;i++)
requestsArray[i]->sendInitFrame();
free(requestsArray);
// #endif
}
void OneSheeldClass::addToShieldsArray(ShieldParent * shield)
{
if(shieldsCounter==SHIELDS_NO) return;
shieldsArray[shieldsCounter++] = shield;
}
// #ifdef INTERNET_SHIELD
void OneSheeldClass::addToUnSentRequestsArray(HttpRequest * request)
{
if(requestsCounter==MAX_NO_OF_REQUESTS) return;
requestsArray[requestsCounter++] = request;
}
// #endif
bool OneSheeldClass::isInitialized()
{
return isInit;
}
void OneSheeldClass::setOnNewShieldFrame(void (*userFunction)(byte shieldID, byte instanceID, byte functionID, byte argNo,byte *argumentL,byte **arguments))
{
isShieldFrameCallback=true;
shieldFrameCallback=userFunction;
}
void OneSheeldClass::setOnNewSerialData(void (*userFunction)(byte))
{
isSerialDataCallback=true;
serialDataCallback=userFunction;
}
//Frame Sender for Output Shields
void OneSheeldClass::sendShieldFrame(byte shieldID, byte instanceID, byte functionID, byte argNo, ...)
{
unsigned long mill=millis()+1;
unsigned long localLastTimeFrameSent=lastTimeFrameSent;
if(shieldID!=ONESHEELD_ID&&isFirstFrame&&localLastTimeFrameSent&&(mill-localLastTimeFrameSent)<TIME_GAP){
if(inACallback){
OneSheeldClass TempOneSheeld(OneSheeldSerial);
ShieldParent::setOneSheeldInstance(TempOneSheeld);
while((millis()<(TIME_GAP+localLastTimeFrameSent))||TempOneSheeld.framestart)
{
if(TempOneSheeld.OneSheeldSerial.available())
TempOneSheeld.processInput(TempOneSheeld.OneSheeldSerial.read());
}
ShieldParent::unSetOneSheeldInstance();
}else
while((millis()<(TIME_GAP+localLastTimeFrameSent))||framestart)
{
if(OneSheeldSerial.available())
OneSheeld.processInput(OneSheeldSerial.read());
}
}
isFirstFrame=true;
va_list arguments ;
va_start (arguments,argNo);
OneSheeldSerial.write((byte)START_OF_FRAME);
OneSheeldSerial.write(LIBRARY_VERSION);
OneSheeldSerial.write(shieldID);
OneSheeldSerial.write(instanceID);
OneSheeldSerial.write(functionID);
OneSheeldSerial.write(argNo);
OneSheeldSerial.write(255-argNo);
for (int i=0 ; i<argNo ; i++)
{
FunctionArg * temp = va_arg(arguments, FunctionArg *);
OneSheeldSerial.write(temp->getLength());
OneSheeldSerial.write(255-(temp->getLength()));
for (int j=0 ; j<temp->getLength() ; j++)
{
byte* tempData=temp->getData();
OneSheeldSerial.write(tempData[j]);
}
delete(temp);
}
OneSheeldSerial.write((byte)END_OF_FRAME);
va_end(arguments);
if(shieldID!=ONESHEELD_ID)lastTimeFrameSent=millis()+1;
}
void OneSheeldClass::sendShieldFrame(byte shieldID, byte instanceID, byte functionID, byte argNo, FunctionArg ** arguments)
{
unsigned long mill=millis()+1;
unsigned long localLastTimeFrameSent=lastTimeFrameSent;
if(shieldID!=ONESHEELD_ID&&isFirstFrame&&localLastTimeFrameSent&&(mill-localLastTimeFrameSent)<TIME_GAP){
if(inACallback){
OneSheeldClass TempOneSheeld(OneSheeldSerial);
ShieldParent::setOneSheeldInstance(TempOneSheeld);
while((millis()<(TIME_GAP+localLastTimeFrameSent))||TempOneSheeld.framestart)
{
if(TempOneSheeld.OneSheeldSerial.available())
TempOneSheeld.processInput(TempOneSheeld.OneSheeldSerial.read());
}
ShieldParent::unSetOneSheeldInstance();
}else
while((millis()<(TIME_GAP+localLastTimeFrameSent))||framestart)
{
if(OneSheeldSerial.available())
OneSheeld.processInput(OneSheeldSerial.read());
}
}
isFirstFrame=true;
OneSheeldSerial.write((byte)START_OF_FRAME);
OneSheeldSerial.write(LIBRARY_VERSION);
OneSheeldSerial.write(shieldID);
OneSheeldSerial.write(instanceID);
OneSheeldSerial.write(functionID);
OneSheeldSerial.write(argNo);
OneSheeldSerial.write(255-argNo);
for (int i=0 ; i<argNo ; i++)
{
OneSheeldSerial.write(arguments[i]->getLength());
OneSheeldSerial.write(255-(arguments[i]->getLength()));
for (int j=0 ; j<arguments[i]->getLength() ; j++)
{
byte* tempData=arguments[i]->getData();
OneSheeldSerial.write(tempData[j]);
}
}
OneSheeldSerial.write((byte)END_OF_FRAME);
if(shieldID!=ONESHEELD_ID)lastTimeFrameSent=millis()+1;
}
bool OneSheeldClass::isAppConnected()
{
return isOneSheeldConnected;
}
void OneSheeldClass::setOnAppConnected(void (*userFunction)(bool isAppConnected))
{
isAppConnectedCallBack = userFunction;
isAppConnectionCallBack = true;
}
//Shield_ID Getter
byte OneSheeldClass::getShieldId()
{
return shield;
}
//Instance_ID Getter
byte OneSheeldClass::getInstanceId()
{
return instance;
}
//Funtcion_ID Getter
byte OneSheeldClass::getFunctionId()
{
return functions;
}
//ArgumentsNumber Getter
byte OneSheeldClass::getArgumentNo()
{
return argumentnumber;
}
//ArgumentLength Getter
byte OneSheeldClass::getArgumentLength(byte x)
{
return argumentL[x];
}
//Data Getter
byte * OneSheeldClass::getArgumentData(byte x)
{
if(argumentL[x]!=0)
return arguments[x];
else return NULL;
}
//Convert float to array of bytes
byte * OneSheeldClass::convertFloatToBytes(float data)
{
convertFloatUnion.number = data;
return convertFloatUnion.floatBytes;
}
//Convert array of bytes to float
float OneSheeldClass::convertBytesToFloat(byte * data)
{
convertFloatUnion.floatBytes[0] = data[0];
convertFloatUnion.floatBytes[1] = data[1];
convertFloatUnion.floatBytes[2] = data[2];
convertFloatUnion.floatBytes[3] = data[3];
return convertFloatUnion.number;
}
//Incomming Frames processing
void OneSheeldClass::processInput(int data)
{
if(data==-1)return;
if(!framestart&&data==START_OF_FRAME)
{
freeMemoryAllocated();
counter=0;
framestart=true;
arguments=0;
argumentL=0;
counter++;
#ifdef DEBUG
Serial.print("C1 ");
#endif
}
else if(counter==4&&framestart) //data is the no of arguments
{
#ifdef DEBUG
Serial.print("C4 ");
#endif
datalengthcounter=0;
argumentcounter=0;
argumentnumber=data;
counter++;
}
else if(counter==5&&framestart) //data is the no of arguments
{
#ifdef DEBUG
Serial.print("C5 ");
#endif
if((255-argumentnumber)==data&&argumentnumber==0){
counter=9;
return;
}
else if((255-argumentnumber)==data){
arguments=(byte**)malloc(sizeof(byte*)*argumentnumber);//new byte*[argumentnumber]; //assigning the first dimension of the pointer (allocating dynamically space for 2d array)
#ifdef DEBUG
Serial.print("M1 ");
#endif
isArgumentsNumberMalloced=true;
argumentL=(byte*)malloc(sizeof(byte)*argumentnumber);//new byte [argumentnumber];
#ifdef DEBUG
Serial.print("M2 ");
#endif
isArgumentLengthMalloced=true;
counter++;
}
else{
framestart=false;
freeMemoryAllocated();
return;
}
}
else if (counter==6&&framestart) // data is the first argument length
{
#ifdef DEBUG
Serial.print("C6 ");
#endif
argumentL[argumentcounter]=data;
counter++;
}
else if (counter==7&&framestart) // data is the first argument Data information
{
#ifdef DEBUG
Serial.print("C7 ");
#endif
if((255-argumentL[argumentcounter])==data){
if(argumentL[argumentcounter]!=0)
{
arguments[argumentcounter]=(byte*)malloc(sizeof(byte)*argumentL[argumentcounter]); // assigning the second dimensional of the pointer
#ifdef DEBUG
Serial.print("M3 ");
#endif
counter++;
}
else
{
arguments[argumentcounter]=NULL;
datalengthcounter=0;
argumentcounter++;
if(argumentcounter==argumentnumber)
counter=9;
else
counter=6;
}
numberOfDataMalloced++;
}
else{
framestart=false;
freeMemoryAllocated();
return;
}
}
else if (counter==8&&framestart)
{
#ifdef DEBUG
Serial.print("C8 ");
#endif
if(arguments[argumentcounter]!=NULL)arguments[argumentcounter][datalengthcounter++]=data;
if (datalengthcounter==argumentL[argumentcounter])
{
datalengthcounter=0;
argumentcounter++;
if(argumentcounter==argumentnumber)
{
counter++; //increment the counter to take the last byte which is the end of the frame
}
else
{
counter=6;
}
}
}
else if(counter==9&&framestart)
{
#ifdef DEBUG
Serial.print("C9 ");
#endif
endFrame=data;
if(endFrame==END_OF_FRAME) //if the endframe is equal to zero send to shields and free memory
{
sendToShields();
if(isShieldFrameCallback)
{
enteringACallback();
shieldFrameCallback(shield,instance,functions,argumentnumber,argumentL,arguments);
exitingACallback();
}
freeMemoryAllocated();
}
else //if endframe wasn't equal to zero make sure that the memory is free anyway
{
freeMemoryAllocated();
}
}
else if(framestart){
if(counter==1){
shield=data;
bool found = false;
if(shield == ONESHEELD_ID || isShieldFrameCallback) found = true;
else
for (int i=0;i<shieldsCounter;i++) {
if (shield == shieldsArray[i]->getShieldId()){
found = true;
}
}
if (!found) {
framestart=false;
freeMemoryAllocated();
return;
}
}
else if(counter==2){
instance=data;
#ifdef DEBUG
Serial.print("C2 ");
#endif
}
else if(counter==3){
functions=data;
#ifdef DEBUG
Serial.print("C3 ");
#endif
}
counter++;
}
}
void OneSheeldClass::processInput()
{
while(OneSheeldSerial.available())
{
byte data=OneSheeldSerial.read();
processInput(data);
if(isSerialDataCallback)
{
enteringACallback();
serialDataCallback(data);
exitingACallback();
}
}
}
void OneSheeldClass::freeMemoryAllocated(){
framestart=false;
if(isArgumentsNumberMalloced){
for(int i=0;i<numberOfDataMalloced;i++)
{
if(arguments[i]!=NULL)free(arguments[i]);
#ifdef DEBUG
Serial.print("F3 ");
#endif
}
numberOfDataMalloced=0;
free(arguments);
#ifdef DEBUG
Serial.print("F1 ");
#endif
isArgumentsNumberMalloced=false;
}
if(isArgumentLengthMalloced){
free(argumentL);
#ifdef DEBUG
Serial.println("F2 ");
#endif
isArgumentLengthMalloced=false;
}
}
//Data Sender to Input Shields
void OneSheeldClass::sendToShields()
{
//Checking the Shield-ID
byte number_Of_Shield= OneSheeld.getShieldId();
switch (number_Of_Shield)
{
case ONESHEELD_ID :processFrame();break;
default:
for(int i=0 ;i<shieldsCounter;i++)
{
shieldsArray[i]->processFrame();
}
}
}
void OneSheeldClass::processFrame(){
byte functionId = getFunctionId();
//Check the function ID
if(functionId == DISCONNECTION_CHECK_FUNCTION)
{
isOneSheeldConnected=false;
if(isAppConnectionCallBack)(*isAppConnectedCallBack)(isOneSheeldConnected);
}
else if(functionId == CONNECTION_CHECK_FUNCTION)
{
isOneSheeldConnected=true;
if(isAppConnectionCallBack)(*isAppConnectedCallBack)(isOneSheeldConnected);
}
else if(functionId == LIBRARY_VERSION_REQUEST)
{
sendShieldFrame(ONESHEELD_ID,0,SEND_LIBRARY_VERSION,0);
}
}
//PulseWidthModulation Getter
unsigned char OneSheeldClass::analogRead(int pin)
{
double period=(double)pulseIn(pin,HIGH)+(double)pulseIn(pin,LOW);;
double duty=(double)pulseIn(pin,HIGH);
double fraction=duty/period;
unsigned char pwm_out=(unsigned char)(ceil)(fraction*255);
return pwm_out;
}
void OneSheeldClass::enteringACallback()
{
if(!isInACallback())
{
inACallback=true;
sendShieldFrame(ONESHEELD_ID,0,CALLBACK_ENTERED,0);
}
}
void OneSheeldClass::exitingACallback()
{
if(isInACallback())
{
inACallback=false;
sendShieldFrame(ONESHEELD_ID,0,CALLBACK_EXITED,0);
}
}
bool OneSheeldClass::isInACallback()
{
return inACallback && !callbacksInterrupts;
}
bool OneSheeldClass::isCallbacksInterruptsSet()
{
return callbacksInterrupts;
}
void OneSheeldClass::disableCallbacksInterrupts()
{
callbacksInterrupts=false;
}
void OneSheeldClass::enableCallbacksInterrupts()
{
callbacksInterrupts=true;
}
void OneSheeldClass::delay(unsigned long time)
{
unsigned long now=millis();
if(inACallback)
{
OneSheeldClass TempOneSheeld(OneSheeldSerial);
ShieldParent::setOneSheeldInstance(TempOneSheeld);
while((millis()<(now+time))||TempOneSheeld.framestart)
{
if(TempOneSheeld.OneSheeldSerial.available())
TempOneSheeld.processInput(TempOneSheeld.OneSheeldSerial.read());
}
ShieldParent::unSetOneSheeldInstance();
}else
while((millis()<(now+time))||framestart)
{
if(OneSheeldSerial.available())
OneSheeld.processInput(OneSheeldSerial.read());
}
}
//Instantiating Object
#if defined(__AVR_ATmega32U4__) || defined(ARDUINO_LINUX)
OneSheeldClass OneSheeld(Serial1);
void serialEvent1()
#else
OneSheeldClass OneSheeld(Serial);
void serialEvent()
#endif
{
OneSheeld.processInput();
}