thinger-io / Arduino-Library

IOTMP Arduino Library for connecting devices to thinger.io #IoT
https://thinger.io
MIT License
111 stars 66 forks source link

API is complicated #1

Closed q2dg closed 8 years ago

q2dg commented 8 years ago

<< , & , [ ] ...strange stuff.

thinger-io commented 8 years ago

what is complicated ? an overloaded operator << to represet input, & for data reference, and [] for defining lambdas. Not sure your knowledge about C++, but this is no strange!

q2dg commented 8 years ago

That's the fact...average Arduino users doesn't know C++ (stdin??, memory reference??, lambdas??). If they are not your target, no problem, but if it is, I think code looks scary for them. But maybe I'm wrong...it was just a suggestion

thinger-io commented 8 years ago

Yes, you are right that at a first look it may look complicated, but it is in fact much more easier and cleaner than doing it in the c plain old way (creating a void function and referencing it, passing a pointer, or calling a function for setting the callback). Arduino is evolving and it is supporting some C++11 features, so i think it is worth that the people can start working with them, as the technology evolves. Why stick to the old features if we have new tools? When I started writing the libraries I were one of the first using lambdas. Now I can see them also in the ESP8266 Arduino libraries... We just need learn something more to work better...

Anyway, there is not too much to understand to the average Arduino user, just to know that there are three different ways for defining accessible resources from the Internet in their devices. They need to fill the function block.

Input Using the operator << pointing to the resource name represents input

thing["led"] << [](pson& in){ 
     digitalWrite(BUILTIN_LED, in ? HIGH : LOW); 
};

Output Using the operator >> pointing out of the resource name means output

thing["millis"] >> [](pson& out){ 
      out = millis(); 
};

Input/Output Here it is used the operator = for defining a function that have both input and output

thing["in_out"] = [](pson& in, pson& out){
      out["sum"] = (long)in["value1"] + (long)in["value2"];
      out["mult"] = (long)in["value1"] * (long)in["value2"];
};

If we do that with old c, we can expect something like the following code:

Defining three different functions:

void led(pson* in){
    digitalWrite(BUILTIN_LED, *in ? HIGH : LOW); 
}
void millis(pson* out){
    *out = millis(); 
}
void in_out(pson* in, pson* out){
    out["sum"] = (long)*in["value1"] + (long)*in["value2"];
    out["mult"] = (long)*in["value1"] * (long)*in["value2"];
}

And assigning that to the resources:

thing["led"].set_input_function(led);
thing["millis"].set_output_function(millis);
thing["in_out"].set_intput_output_function(in_out);

It is not bad at all, but it looks uglier to me. Anyway both alternatives can be working together easily... Do you think it is worth allow the Arduino users using also the second alternative?

q2dg commented 8 years ago

Well, I understand your reasoning, ...so if it is more work for you allowing the "classical" alternative, I wouldn't do it. However, maybe this (so clear) explanation you have written to me could be published as a paragraph in README.md...it would clarify this aspect to many people that still live in the "classical" background. Thanks.

If you don't mind, I'll close the issue

thinger-io commented 8 years ago

Thanks, I will update the README explaining how the resources works. We are starting... so all feedback is so appreciated. I'll look to the second alternative, as it is straightforward to develop and it is true that some old school guys can be more confortable with them.

Thanks again!