{"id":929,"date":"2016-07-04T17:33:41","date_gmt":"2016-07-04T16:33:41","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=929"},"modified":"2016-07-04T17:33:41","modified_gmt":"2016-07-04T16:33:41","slug":"wedo-2-0-colors-with-python-again","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2016\/wedo-2-0-colors-with-python-again","title":{"rendered":"WeDo 2.0 colors with python (again)"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 5 of 6 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/wedo-2-0-reverse-engineering\" class=\"series-269\" title=\"WeDo 2.0 - reverse engineering\">WeDo 2.0 - reverse engineering<\/a><\/div><p>After some head aches with the WeDo 2.0 SDK I found out that the WeDo 2.0 Hub has 2 modes for the RGB LED device:<\/p>\n<ul>\n<li>Indexed<\/li>\n<li>Absolute<\/li>\n<\/ul>\n<p>&#8220;Indexed&#8221; is the one I used before &#8211; only 10 colors are available. This the mode used in the WeDo 2.0 App, and when in this mode the command used to write to the RGB LED accepts only one byte as argument, which works as an &#8220;index&#8221; to 10 predefined colors. Why only 10 if the same byte can address up to 255 colors? Internal memory limitations?<\/p>\n<p>The same command accepts\u00a0 also 3 one-byte arguments (Red, Green and Blue) but only when the RGB LED mode is &#8220;Absolute&#8221;. This is clear in the SDK&#8230; what is not so clear is how to change from default (power on) Indexed mode to Absolute?<\/p>\n<p>After reading many Java files I found out how: we use the &#8220;Input Command&#8221; characteristic (handle 0x3a) and send it this command:<\/p>\n<pre>0102061701010000000201<\/pre>\n<p>I&#8217;ll explain the format of the &#8220;Input Command&#8221; in another post, but for now this is the meaning:<\/p>\n<ul>\n<li>first two bytes (0102) is the header used to set definitions<\/li>\n<li>the third is the port of the device (06 is the RGB LED)<\/li>\n<li>the fourth is the type of the device (17h a RGB LED)<\/li>\n<li>the fifth is the mode (01 is Absolute, 00 is Indexed)<\/li>\n<li>the sixth to nineth bytes is the delta for notifications to be noticed (so 01 00 00 00 = 1d is the minimum)<\/li>\n<li>the tenth byte is the unit format to be used (02 is &#8220;SI&#8221;, internation standard)<\/li>\n<li>the eleventh and last byte is to disable or enable notifications (01)<\/li>\n<\/ul>\n<p>So we can now use any of the 16777216 color tones available:<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"LEGO WeDO 2.0 RGB LED in Absolute mode\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/8nVVZ7jtcyw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<p>The pyhton script used for the video above (the video just shows a small part)<\/p>\n<pre>#!\/usr\/bin\/python\r\n# run with sudo\r\n\r\nfrom gattlib import GATTRequester\r\nfrom time import sleep\r\n\r\nBTdevice = \"hci0\"\u00a0\u00a0 \u00a0# BlueTooth 4.0 BLE capable device\r\nWeDo2HubAddress\u00a0 = \"A0:E6:F8:1E:58:57\"\r\nInputCommand_hnd = 0x3a\r\nOutputCommand_hnd\u00a0 = 0x3d\r\nRGBAbsoluteMode_cmd = str(bytearray([01,02,06,17,01,01,00,00,00,02,01]))\r\nRGBAbsoluteOutput_cmd = str(bytearray([06,04,03]))\r\n\r\nDELAY\u00a0\u00a0\u00a0\u00a0\u00a0 = 0.3\r\n\r\nreq = GATTRequester(WeDo2HubAddress,True,BTdevice)\r\nsleep(DELAY)\r\n\r\n# configure RBG LED to Absolute Mode\r\nreq.write_by_handle(InputCommand_hnd,RGBAbsoluteMode_cmd)\r\n\r\n# loop all colors\r\nwhile True:\r\n\u00a0 for blue in range (0,256,16):\r\n\u00a0\u00a0\u00a0 for green in range (0,256,16):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for red in range (0,256,16):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 req.write_by_handle(OutputCommand_hnd, RGBAbsoluteOutput_cmd+chr(red)+chr(green)+chr(blue))<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 5  of 6 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/wedo-2-0-reverse-engineering\" class=\"series-269\" title=\"WeDo 2.0 - reverse engineering\">WeDo 2.0 - reverse engineering<\/a><\/div><p>After some head aches with the WeDo 2.0 SDK I found out that the WeDo 2.0 Hub has 2 modes for the RGB LED device: Indexed Absolute &#8220;Indexed&#8221; is the one I used before &#8211; only 10 colors are available. This the mode used in the WeDo 2.0 App, and when in this mode the &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2016\/wedo-2-0-colors-with-python-again\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;WeDo 2.0 colors with python (again)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[38,20,98,271],"tags":[],"series":[269],"class_list":["post-929","post","type-post","status-publish","format-standard","hentry","category-ble","category-linux","category-python","category-wedo-2-0","series-wedo-2-0-reverse-engineering"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-eZ","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=929"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/929\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=929"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}