{"id":1716,"date":"2021-10-08T18:39:01","date_gmt":"2021-10-08T16:39:01","guid":{"rendered":"http:\/\/f6kmf.fr\/?p=1716"},"modified":"2021-10-08T18:39:03","modified_gmt":"2021-10-08T16:39:03","slug":"arduino-codez-elegamment-soyez-feignants","status":"publish","type":"post","link":"https:\/\/f6kmf.fr\/index.php\/2021\/10\/08\/arduino-codez-elegamment-soyez-feignants\/","title":{"rendered":"Arduino : codez \u00e9l\u00e9gamment, soyez feignants !"},"content":{"rendered":"\n<p class=\"has-medium-font-size\">Marre des paquets de \u00a0\u00bb Serial.print() \u00a0\u00bb pour le debug, qui rendent le code illisible? Et si on pouvait squeezer tout \u00e7a sur une ligne ??<\/p>\n\n\n\n<p class=\"has-background has-drop-cap has-very-light-gray-background-color\">D\u00e9j\u00e0, que je vous dise, l&rsquo; id\u00e9e est bien trop brillante, elle n&rsquo; est pas de moi, je fais juste le perroquet, l&rsquo;id\u00e9e est de Ralph S. Bacon, et vous trouverez  la vid\u00e9o sur le sujet en anglais ici :<a href=\"https:\/\/youtu.be\/lhwk5vJ1iMA\">https:\/\/www.youtube.com\/watch?v=lhwk5vJ1iMA<\/a><\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">En fait, tout repose sur une librairie qui permet \u00e0 votre arduino de formater un flux de donn\u00e9es s\u00e9rie . J&rsquo;en vois qui on d\u00e9j\u00e0 les cheveux tout debout sur le cr\u00e2ne, calmez vous, c&rsquo; est tout simple :<\/p>\n\n\n\n<p class=\"has-background has-luminous-vivid-amber-background-color\">Consid\u00e9rons le code ci-dessous, tir\u00e9 de l&rsquo; exemple de Ralph : <a href=\"https:\/\/github.com\/RalphBacon\/227-printf-for-Arduino-and-ESP32\/blob\/main\/Sketch\/printf-For-Arduino\/printf-For-Arduino.ino\">https:\/\/github.com\/RalphBacon\/227-printf-for-Arduino-and-ESP32\/blob\/main\/Sketch\/printf-For-Arduino\/printf-For-Arduino.ino<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Arduino.h>\n#define BLINK_LED pinMode(13,OUTPUT);digitalWrite(13, millis() % 1000 > 800)\n\n\/\/ Include  the library from Embedded Artistry\n#include \"LibPrintf.h\"\n\n\/\/ Some values for us to play with\nint intValue = 42; \/\/ 42 is the meaning of Life, the Universe &amp; Everything (HgttG)\nlong longValue = 1000000L; \/\/ One million (long)\ndouble floatValue = 3.1415926; \/\/ Pi\nbool boolValue = true; \/\/ true, not false\n\n\/\/ Forward declaration: waits until key pressed for demo\nvoid waitForKeyPress();\n\nvoid setup()\n{\n    \/\/ Set the baud rate, just like usual\n    Serial.begin(115200);\n\n    \/\/ You can still use Serial.print statements if you really, really want\n    Serial.println(\"Setup completed.\");\n\n    \/\/ How would we normally output the following line:\n    \/\/ \"Iteration 42 has a value of 1000000 (derived from 3.1415926)\"    \n    Serial.print(\"Iteration \");\n    Serial.print(intValue);\n    Serial.print(\" has a value of \");\n    Serial.print(longValue);\n    Serial.print(\" (derived from \");\n    Serial.print(floatValue);\n    Serial.println(\")\");\n\n    waitForKeyPress();\n    printf(\"Iteration %i has a value of %li (derived from %f)\\n\", intValue, longValue, floatValue);\n\n    waitForKeyPress();\n    \/\/ Force preceeding blanks, leading zeroes and many (inaccurate) decimal places\n    printf(\"Iteration %10i has a value of %.10li (derived from %6.10f)\\n\", intValue, longValue, floatValue);\n\n    waitForKeyPress();\n    \/\/ You can make the width of any variable programmable\n    printf(\"Width of %*.i depends on the next parameter!\\n\", 8, intValue);\n\n    waitForKeyPress();\n    \/\/ Boolean values (really integers)\n    printf(\"Lies can never be %s\\n\", boolValue ? \"true\" : \"false\");\n\n    waitForKeyPress();\n    \/\/ Concatenate on two lines\n    printf(\"The meaning of life is \");\n\n    waitForKeyPress();\n    if (intValue == 42)\n    {\n        printf(\"%d\\n\", intValue);\n    }\n    else\n    {\n        printf(\"unknown!\\n\");\n    }<\/code><\/pre>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\">On voit d\u00e9j\u00e0 le #include \u00ab\u00a0libPrintf.h\u00a0\u00bb, il faudra bien sur charger et installer cette lib avant de faire quoi que ce soit. Ensuite, dans le setup(), on initialise la transmission, et on commence \u00e0 la mani\u00e8re ancienne, \u00e0 envoyer notre flux, bout par bout, ligne par ligne <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Serial.print(\"Iteration \");\n    Serial.print(intValue);\n    Serial.print(\" has a value of \");\n    Serial.print(longValue);\n    Serial.print(\" (derived from \");\n    Serial.print(floatValue);\n    Serial.println(\")\");<\/code><\/pre>\n\n\n\n<p style=\"background-color:#f8ca7a\" class=\"has-background\">7 lignes pour passer 3 valeurs, juste pour le debug, boff !<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">La ligne suivante :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><br>waitForKeyPress();       <br>                <br>    printf(\"Iteration %i has a value of %li (derived from %f)\\n\", intValue, longValue, floatValue);       <br>                 <\/pre>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">nous montre comment utiliser printf pour formater les donn\u00e9es \u00e0 passer :<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">On met le tout entre \u00a0\u00bb \u00ab\u00a0, comme un string, et on ins\u00e8re chaque variable dans le string, avec un \u00a0\u00bb %\u00a0\u00bb suivi du type de donn\u00e9e, et les variables juste apr\u00e8s le string, appel\u00e9es par leur nom : l&rsquo; iteration \u00a0\u00bb intValue est un entier (%i), qui a une valeur de \u00ab\u00a0longValue\u00a0\u00bb, un entier long,(%li) d\u00e9riv\u00e9 de \u00ab\u00a0floatValue\u00a0\u00bb, un nombre a virgule flottante (%f).<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">You get the idea, je vous fais pas la traduction de toute la video, faut bien que vous bossiez un peu aussi, et puis moi, l&rsquo; informatique, einh ?<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">Je vous invite \u00e0 poursuivre la vid\u00e9o et l&rsquo; \u00e9tude des lignes suivantes, et je vous recommande de lire le github de Ralph, et d&rsquo; \u00e9tudier la doc de la fonction sprintf sur cplusplus.com.<\/p>\n\n\n\n<p class=\"has-background has-luminous-vivid-amber-background-color\">Ah, ben oui, la doc c++, faut se l&rsquo; avaler, et rassurez vous, c&rsquo; est pas la derni\u00e8re, mais bon, vous savez, \u00eatre feignant, c&rsquo;est un m\u00e9tier ! C&rsquo;est comme tout, si on veut y arriver, il faut bosser !!<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">Plus s\u00e9rieusement (qui a dit \u00a0\u00bb poil aux dents\u00a0\u00bb ??? )<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">Le langage Arduino, c&rsquo;est tr\u00e8s simple (hum, hum !! ). C&rsquo;est fait pour!<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\">Mais des fois \u00e7a rend le truc un peu pataud, un peu lent, et on voudrait bien pour cette fois un truc un peu plus fin, plus&#8230;. C&rsquo; est l\u00e0 qu&rsquo; il faut se rappeler que le langage Arduino, c&rsquo;est du C\/C++ \u00ab\u00a0modifi\u00e9\u00a0\u00bb \u00e0 la sauce Arduino. Et que parfois, on aurait un avantage \u00e0 utiliser le C\/C++, voire m\u00eame l&rsquo; assembleur ( \u00ab\u00a0for the braves!\u00a0\u00bb, avec un clin d&rsquo;oeil \u00e0 Dave Jones, de EEVBlog ) pour gratter qui de la place, qui de la vitesse d&rsquo;ex\u00e9cution, m\u00eame si \u00e0 ce point l\u00e0, il faut se demander si on n&rsquo; aurait pas int\u00e9r\u00eat \u00e0 changer de microcontr\u00f4leur. La librairie pr\u00e9sent\u00e9e n&rsquo;est qu&rsquo; un exemple, est libre a vous d&rsquo;en trouver d&rsquo; autre, d&rsquo; en \u00e9crire et de les publier sur github .<\/p>\n\n\n\n<p style=\"background-color:#a4e48b\" class=\"has-background\">Un cours complet sur le C++ pour les braves : <a href=\"https:\/\/openclassrooms.com\/fr\/courses\/1894236-programmez-avec-le-langage-c\">https:\/\/openclassrooms.com\/fr\/courses\/1894236-programmez-avec-le-langage-c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Une librairie arduino pour utiliser les entr\u00e9es\/sorties standard du C++<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[27,23],"tags":[],"class_list":["post-1716","post","type-post","status-publish","format-standard","hentry","category-arduino","category-informatique-programmation"],"_links":{"self":[{"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/posts\/1716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/comments?post=1716"}],"version-history":[{"count":8,"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/posts\/1716\/revisions"}],"predecessor-version":[{"id":1724,"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/posts\/1716\/revisions\/1724"}],"wp:attachment":[{"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/media?parent=1716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/categories?post=1716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/f6kmf.fr\/index.php\/wp-json\/wp\/v2\/tags?post=1716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}