Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c6b176e

Browse files
aentingermattiabertorello
authored andcommittedFeb 5, 2019
Rearranging function ordering for better readability
1 parent 5533454 commit c6b176e

File tree

1 file changed

+104
-90
lines changed

1 file changed

+104
-90
lines changed
 

‎ArduinoCloudThing.cpp

Lines changed: 104 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@
99
#include <math.h>
1010

1111
/******************************************************************************
12-
* PRIVATE FREE FUNCTIONS
12+
* PRIVATE FREE FUNCTION PROTOTYPES
1313
******************************************************************************/
1414

15-
/* Source Idea from https://tools.ietf.org/html/rfc7049 : Page: 50 */
16-
double convertCborHalfFloatToDouble(uint16_t const half_val) {
17-
int exp = (half_val >> 10) & 0x1f;
18-
int mant = half_val & 0x3ff;
19-
double val;
20-
if (exp == 0) val = ldexp(mant, -24);
21-
else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
22-
else val = mant == 0 ? INFINITY : NAN;
23-
return half_val & 0x8000 ? -val : val;
24-
}
15+
double convertCborHalfFloatToDouble(uint16_t const half_val);
16+
void extractProperty (ArduinoCloudProperty<int> * int_property, CborValue * cbor_value);
17+
void extractProperty (ArduinoCloudProperty<float> * float_property, CborValue * cbor_value);
18+
void extractProperty (ArduinoCloudProperty<bool> * bool_property, CborValue * cbor_value);
19+
void extractProperty (ArduinoCloudProperty<String> * string_property, CborValue * cbor_value);
20+
21+
/******************************************************************************
22+
* DEBUG FUNCTIONS
23+
******************************************************************************/
2524

2625
#if defined(DEBUG_MEMORY) && defined(ARDUINO_ARCH_SAMD)
2726
extern "C" char *sbrk(int i);
@@ -46,83 +45,6 @@ static void utox8(uint32_t val, char* s) {
4645
#define Serial DebugSerial
4746
#endif
4847

49-
void extractProperty(ArduinoCloudProperty<int> * int_property, CborValue * cbor_value)
50-
{
51-
if (cbor_value->type == CborIntegerType) {
52-
int val = 0;
53-
cbor_value_get_int(cbor_value, &val);
54-
if(int_property->isWriteableByCloud()) {
55-
int_property->writeByCloud(val);
56-
}
57-
} else if (cbor_value->type == CborDoubleType) {
58-
double val = 0.0;
59-
cbor_value_get_double(cbor_value, &val);
60-
if(int_property->isWriteableByCloud()) {
61-
int_property->writeByCloud(static_cast<int>(val));
62-
}
63-
} else if (cbor_value->type == CborFloatType) {
64-
float val = 0.0f;
65-
cbor_value_get_float(cbor_value, &val);
66-
if(int_property->isWriteableByCloud()) {
67-
int_property->writeByCloud(static_cast<int>(val));
68-
}
69-
} else if (cbor_value->type == CborHalfFloatType) {
70-
uint16_t val = 0;
71-
cbor_value_get_half_float(cbor_value, &val);
72-
if(int_property->isWriteableByCloud()) {
73-
int_property->writeByCloud(static_cast<int>(convertCborHalfFloatToDouble(val)));
74-
}
75-
}
76-
}
77-
78-
void extractProperty(ArduinoCloudProperty<float> * float_property, CborValue * cbor_value) {
79-
if (cbor_value->type == CborDoubleType) {
80-
double val = 0.0;
81-
cbor_value_get_double(cbor_value, &val);
82-
if(float_property->isWriteableByCloud()) {
83-
float_property->writeByCloud(static_cast<float>(val));
84-
}
85-
} else if (cbor_value->type == CborIntegerType) {
86-
int val = 0;
87-
cbor_value_get_int(cbor_value, &val);
88-
if(float_property->isWriteableByCloud()) {
89-
float_property->writeByCloud(static_cast<float>(val));
90-
}
91-
} else if (cbor_value->type == CborFloatType) {
92-
float val = 0.0f;
93-
cbor_value_get_float(cbor_value, &val);
94-
if(float_property->isWriteableByCloud()) {
95-
float_property->writeByCloud(val);
96-
}
97-
} else if (cbor_value->type == CborHalfFloatType) {
98-
uint16_t val = 0;
99-
cbor_value_get_half_float(cbor_value, &val);
100-
if(float_property->isWriteableByCloud()) {
101-
float_property->writeByCloud(static_cast<float>(convertCborHalfFloatToDouble(val)));
102-
}
103-
}
104-
}
105-
106-
void extractProperty(ArduinoCloudProperty<bool> * bool_property, CborValue * cbor_value) {
107-
bool val = false;
108-
if(cbor_value_get_boolean(cbor_value, &val) == CborNoError) {
109-
if(bool_property->isWriteableByCloud()) {
110-
bool_property->writeByCloud(val);
111-
}
112-
}
113-
}
114-
115-
void extractProperty(ArduinoCloudProperty<String> * string_property, CborValue * cbor_value) {
116-
char * val = 0;
117-
size_t val_size = 0;
118-
if(cbor_value_dup_text_string(cbor_value, &val, &val_size, cbor_value) == CborNoError) {
119-
if(string_property->isWriteableByCloud()) {
120-
string_property->writeByCloud(static_cast<char *>(val));
121-
}
122-
free(val);
123-
}
124-
}
125-
12648
/******************************************************************************
12749
* CTOR/DTOR
12850
******************************************************************************/
@@ -148,8 +70,8 @@ ArduinoCloudThing::ArduinoCloudThing(CloudProtocol const cloud_protocol)
14870
******************************************************************************/
14971

15072
void ArduinoCloudThing::begin() {
151-
_status = ON;
152-
addPropertyReal(_status, "status", Permission::Read);
73+
_status = ON;
74+
addPropertyReal(_status, "status", Permission::Read);
15375
}
15476

15577

@@ -493,3 +415,95 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue *
493415

494416
return next_state;
495417
}
418+
419+
/******************************************************************************
420+
* PRIVATE FREE FUNCTIONS
421+
******************************************************************************/
422+
423+
/* Source Idea from https://tools.ietf.org/html/rfc7049 : Page: 50 */
424+
double convertCborHalfFloatToDouble(uint16_t const half_val) {
425+
int exp = (half_val >> 10) & 0x1f;
426+
int mant = half_val & 0x3ff;
427+
double val;
428+
if (exp == 0) val = ldexp(mant, -24);
429+
else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
430+
else val = mant == 0 ? INFINITY : NAN;
431+
return half_val & 0x8000 ? -val : val;
432+
}
433+
434+
void extractProperty(ArduinoCloudProperty<int> * int_property, CborValue * cbor_value) {
435+
if (cbor_value->type == CborIntegerType) {
436+
int val = 0;
437+
cbor_value_get_int(cbor_value, &val);
438+
if(int_property->isWriteableByCloud()) {
439+
int_property->writeByCloud(val);
440+
}
441+
} else if (cbor_value->type == CborDoubleType) {
442+
double val = 0.0;
443+
cbor_value_get_double(cbor_value, &val);
444+
if(int_property->isWriteableByCloud()) {
445+
int_property->writeByCloud(static_cast<int>(val));
446+
}
447+
} else if (cbor_value->type == CborFloatType) {
448+
float val = 0.0f;
449+
cbor_value_get_float(cbor_value, &val);
450+
if(int_property->isWriteableByCloud()) {
451+
int_property->writeByCloud(static_cast<int>(val));
452+
}
453+
} else if (cbor_value->type == CborHalfFloatType) {
454+
uint16_t val = 0;
455+
cbor_value_get_half_float(cbor_value, &val);
456+
if(int_property->isWriteableByCloud()) {
457+
int_property->writeByCloud(static_cast<int>(convertCborHalfFloatToDouble(val)));
458+
}
459+
}
460+
}
461+
462+
void extractProperty(ArduinoCloudProperty<float> * float_property, CborValue * cbor_value) {
463+
if (cbor_value->type == CborDoubleType) {
464+
double val = 0.0;
465+
cbor_value_get_double(cbor_value, &val);
466+
if(float_property->isWriteableByCloud()) {
467+
float_property->writeByCloud(static_cast<float>(val));
468+
}
469+
} else if (cbor_value->type == CborIntegerType) {
470+
int val = 0;
471+
cbor_value_get_int(cbor_value, &val);
472+
if(float_property->isWriteableByCloud()) {
473+
float_property->writeByCloud(static_cast<float>(val));
474+
}
475+
} else if (cbor_value->type == CborFloatType) {
476+
float val = 0.0f;
477+
cbor_value_get_float(cbor_value, &val);
478+
if(float_property->isWriteableByCloud()) {
479+
float_property->writeByCloud(val);
480+
}
481+
} else if (cbor_value->type == CborHalfFloatType) {
482+
uint16_t val = 0;
483+
cbor_value_get_half_float(cbor_value, &val);
484+
if(float_property->isWriteableByCloud()) {
485+
float_property->writeByCloud(static_cast<float>(convertCborHalfFloatToDouble(val)));
486+
}
487+
}
488+
}
489+
490+
void extractProperty(ArduinoCloudProperty<bool> * bool_property, CborValue * cbor_value) {
491+
bool val = false;
492+
if(cbor_value_get_boolean(cbor_value, &val) == CborNoError) {
493+
if(bool_property->isWriteableByCloud()) {
494+
bool_property->writeByCloud(val);
495+
}
496+
}
497+
}
498+
499+
void extractProperty(ArduinoCloudProperty<String> * string_property, CborValue * cbor_value) {
500+
char * val = 0;
501+
size_t val_size = 0;
502+
if(cbor_value_dup_text_string(cbor_value, &val, &val_size, cbor_value) == CborNoError) {
503+
if(string_property->isWriteableByCloud()) {
504+
string_property->writeByCloud(static_cast<char *>(val));
505+
}
506+
free(val);
507+
}
508+
}
509+

0 commit comments

Comments
 (0)
Please sign in to comment.