{"id":1127,"date":"2017-07-09T01:06:03","date_gmt":"2017-07-09T00:06:03","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=1127"},"modified":"2017-07-09T01:07:38","modified_gmt":"2017-07-09T00:07:38","slug":"ev3-minifig-inventory","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2017\/ev3-minifig-inventory","title":{"rendered":"EV3 &#8211; minifig inventory"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 2 of 2 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/ev3-and-chromecast\" class=\"series-304\" title=\"EV3 and Chromecast\">EV3 and Chromecast<\/a><\/div><p>Now that communication with the Chromecast is working let&#8217;s make a small game with the few parts that I have in my &#8220;holiday bag&#8221;:<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"EV3 + Chromecast + Dimensions Toy Pad\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/Oe1gGvAR29g?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>I&#8217;m using the LEGO Dimensions Toy Pad to read the NFC tags. I already scanned the ID&#8217;s of a blue and an orange tag and saved two JPEG images of the Frenchman LEGO minifigure and also the Invisible Woman (sorry, only had one minifig with me these holydays) on the web server folder so each time one of these NFC tags is recognized it&#8217;s JPEG image is presented on the TV.<\/p>\n<p>The code is based on my tutorial for the <a href=\"http:\/\/www.ev3dev.org\/docs\/tutorials\/using-lego-dimensions-toy-pad\/\">using the LEGO Dimensions Toy Pad with ev3dev<\/a>, I just updated it to work with Python 3:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n#!\/usr\/bin\/python3\r\n\r\nimport usb.core\r\nimport usb.util\r\nfrom time import sleep\r\nimport pychromecast\r\n\r\nTOYPAD_INIT = &#x5B;0x55, 0x0f, 0xb0, 0x01, 0x28, 0x63, 0x29, 0x20, 0x4c, 0x45, 0x47, 0x4f, 0x20, 0x32, 0x30, 0x31, 0x34, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]\r\n\r\nOFF   = &#x5B;0,0,0]\r\nRED   = &#x5B;255,0,0]\r\nGREEN = &#x5B;0,255,0]\r\nBLUE  = &#x5B;0,0,255]\r\nORANGE= &#x5B;255,30,0]\r\n\r\nALL_PADS   = 0\r\nCENTER_PAD = 1\r\nLEFT_PAD   = 2\r\nRIGHT_PAD  = 3\r\n\r\n# Actions\r\nTAG_INSERTED = 0\r\nTAG_REMOVED  = 1\r\n\r\n# UIDs can be retrieved with Android App (most probably in hexadecimal)\r\nbluetag = (4, 77, 198, 218, 162, 64, 129)         # a Blue Tag from LEGO Dimensions\r\norangetag = (4, 91, 182, 122, 177, 73, 129)       # an Orange Tag from LEGO Dimensions\r\n\r\nDELAY_PLAY = 1.75     # 1.5 NOT OK\r\n\r\ndef init_usb():\r\n    global dev\r\n\r\n    dev = usb.core.find(idVendor=0x0e6f, idProduct=0x0241)\r\n\r\n    if dev is None:\r\n        print('Device not found')\r\n    else:\r\n        print('Device found:')\r\n        if dev.is_kernel_driver_active(0):\r\n            dev.detach_kernel_driver(0)\r\n\r\n        print(usb.util.get_string(dev, dev.iProduct))\r\n\r\n        dev.set_configuration()\r\n        dev.write(1,TOYPAD_INIT)\r\n\r\n    return dev\r\n\r\n\r\ndef send_command(dev,command):\r\n\r\n    # calculate checksum\r\n    checksum = 0\r\n    for word in command:\r\n        checksum = checksum + word\r\n        if checksum &gt;= 256:\r\n            checksum -= 256\r\n        message = command+&#x5B;checksum]\r\n\r\n    # pad message\r\n    while(len(message) &lt; 32):\r\n        message.append(0x00)\r\n\r\n    # send message\r\n    dev.write(1, message)\r\n\r\n    return\r\n\r\n\r\ndef switch_pad(pad, colour):\r\n    send_command(dev,&#x5B;0x55, 0x06, 0xc0, 0x02, pad, colour&#x5B;0], colour&#x5B;1], colour&#x5B;2],])\r\n    return\r\n\r\n\r\ndef uid_compare(uid1, uid2):\r\n    match = True\r\n    for i in range(0,7):\r\n        if (uid1&#x5B;i] != uid2&#x5B;i]) :\r\n            match = False\r\n    return match \r\n\r\n\r\ndef main():\r\n\r\n    # init chromecast\r\n    chromecasts = pychromecast.get_chromecasts()\r\n    cast = next(cc for cc in chromecasts if cc.device.friendly_name == &quot;NOMAD&quot;)\r\n    cast.wait()\r\n    mc = cast.media_controller\r\n    print(&quot;Blacking chromecast...&quot;)\r\n    mc.play_media('http:\/\/192.168.43.104:3000\/black.png', 'image\/png')\r\n\r\n    result=init_usb()\r\n    # print(result)\r\n    if dev != None :\r\n        print('Running...')\r\n        mc.stop()\r\n        mc.play_media('http:\/\/192.168.43.104:3000\/presentminifig.png', 'image\/png')\r\n        print('Present a minifg')\r\n\r\n        while True:\r\n            try:\r\n                in_packet = dev.read(0x81, 32, timeout = 10)\r\n                bytelist = list(in_packet)\r\n\r\n                if not bytelist:\r\n                    pass\r\n                elif bytelist&#x5B;0] != 0x56: # NFC packets start with 0x56\r\n                    pass\r\n                else:\r\n                    pad_num = bytelist&#x5B;2]\r\n                    uid_bytes = bytelist&#x5B;6:13]\r\n#                    print(uid_bytes)\r\n                    match_orange = uid_compare(uid_bytes, orangetag)\r\n                    match_blue = uid_compare(uid_bytes, bluetag)\r\n\r\n                    action = bytelist&#x5B;5]\r\n                    if action == TAG_INSERTED :\r\n                        if match_orange:\r\n                            switch_pad(pad_num, ORANGE)\r\n                            mc.play_media('http:\/\/192.168.43.104:3000\/french-man-2.png', 'image\/png')\r\n                        elif match_blue:\r\n                            mc.play_media('http:\/\/192.168.43.104:3000\/invisible-woman-2b.png', 'image\/png')\r\n                            switch_pad(pad_num, BLUE)\r\n                        else:\r\n                            # some other tag\r\n                            switch_pad(pad_num, GREEN)\r\n                    else:\r\n                        # some tag removed\r\n                        switch_pad(pad_num, OFF)\r\n                        mc.stop()\r\n                        # sleep(1)\r\n                        mc.play_media('http:\/\/192.168.43.104:3000\/presentminifig.png', 'image\/png')\r\n                        sleep(DELAY_PLAY)\r\n\r\n            except usb.USBError:\r\n                pass\r\n\r\n        switch_pad(ALL_PADS,OFF)\r\n    return\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n  \r\n\r\n<\/pre>\n<p>You probably need to install pyusb for python3. With Ubuntu I just needed<\/p>\n<pre>sudo apt install python3-usb<\/pre>\n<p>but it is not available for Debian Jessie so<\/p>\n<pre>sudo pip3 install pyusb --pre<\/pre>\n<p>(ev3dev is changing to Debian Stretch that also has python3-usb)<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 2  of 2 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/ev3-and-chromecast\" class=\"series-304\" title=\"EV3 and Chromecast\">EV3 and Chromecast<\/a><\/div><p>Now that communication with the Chromecast is working let&#8217;s make a small game with the few parts that I have in my &#8220;holiday bag&#8221;: I&#8217;m using the LEGO Dimensions Toy Pad to read the NFC tags. I already scanned the ID&#8217;s of a blue and an orange tag and saved two JPEG images of the &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2017\/ev3-minifig-inventory\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;EV3 &#8211; minifig inventory&#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":[13,157,19,158],"tags":[305,307,306],"series":[304],"class_list":["post-1127","post","type-post","status-publish","format-standard","hentry","category-ev3dev","category-ev3dev-en","category-lego-mindstorms","category-lego-mindstorms-en","tag-chromecast","tag-lego-dimensions","tag-nfc","series-ev3-and-chromecast"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-ib","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1127","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=1127"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1127\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1127"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}