{"id":1722,"date":"2021-02-16T08:31:08","date_gmt":"2021-02-16T08:31:08","guid":{"rendered":"https:\/\/ofalcao.pt\/blog\/?p=1722"},"modified":"2021-02-18T07:48:04","modified_gmt":"2021-02-18T07:48:04","slug":"lego-and-dmx","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2021\/lego-and-dmx","title":{"rendered":"LEGO and DMX"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 1 of 6 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/lego-and-dmx\" class=\"series-363\" title=\"LEGO and DMX\">LEGO and DMX<\/a><\/div>\n<p>Playing with MIDI, reading a lot, browsing through musical equipments&#8230; and <a href=\"http:\/\/www.erwinrol.com\/page\/articles\/dmx512\/\" data-type=\"URL\" data-id=\"http:\/\/www.erwinrol.com\/page\/articles\/dmx512\/\">DMX<\/a> cames out.<\/p>\n\n\n\n<p>DMX is like a custom <a href=\"https:\/\/en.wikipedia.org\/wiki\/RS-485\">RS-485<\/a> network. A master device sends data at 250 kHz to slave devices, each of them listening to a different &#8220;channel&#8221; or address. Typical DMX devices (&#8220;fixtures&#8221;) are stage lights but you can find fog machines, laser projectors, bubble machines, relay boards, analog interfaces&#8230;<\/p>\n\n\n\n<p>On a studio scenario you can even join MIDI with DMX so you can create a performance combining music and lights or other kind of effects.<\/p>\n\n\n\n<p>Generating a 250 kHz stream of data isn&#8217;t difficult, there are FTDI devices that can do that. I could use the same kind of setup I use to <a href=\"https:\/\/ofalcao.pt\/blog\/series\/using-a-ftdi-adapter-as-an-ir\" data-type=\"URL\" data-id=\"https:\/\/ofalcao.pt\/blog\/series\/using-a-ftdi-adapter-as-an-ir\">generate LEGO Power Functions IR signals<\/a> to generate DMX signals.<\/p>\n\n\n\n<p>In fact, people are already doing it. There are a few tutorials on how to create a DMX controller for less than $10 but I prefer <a href=\"https:\/\/www.instructables.com\/Pro-USB-DMX-Controller-and-terminator-For-QLC\/\" data-type=\"URL\" data-id=\"https:\/\/www.instructables.com\/Pro-USB-DMX-Controller-and-terminator-For-QLC\/\">this one<\/a> (it costed me a bit more than 10\u20ac but it was really straightforward, just a FTDI485 cable and a XLR 3-pin female connector.<\/p>\n\n\n\n<p>It can be used with <a href=\"https:\/\/www.qlcplus.org\/\" data-type=\"URL\" data-id=\"https:\/\/www.qlcplus.org\/\">QLC+<\/a> (an open source DMX controller software) but that&#8217;s overkill for a MINDSTORMS EV3 so after a short search I found  a python library to use it directly (<a href=\"https:\/\/pypi.org\/project\/dmx485\/\" data-type=\"URL\" data-id=\"https:\/\/pypi.org\/project\/dmx485\/\">dmx485<\/a>).<\/p>\n\n\n\n<p>So you connect your DYI DMX adapter to your EV3:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1218334.312176] usb 1-1.2: New USB device found, idVendor=0403, idProduct=6001<br>[1218334.312248] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3<br>[1218334.312287] usb 1-1.2: Product: USB-RS485 Cable<br>[1218334.312322] usb 1-1.2: Manufacturer: FTDI<br>[1218334.312355] usb 1-1.2: SerialNumber: FT4NMHF6<br>[1218334.800717] usbcore: registered new interface driver usbserial<br>[1218334.936770] usbcore: registered new interface driver ftdi_sio<br>[1218334.937337] usbserial: USB Serial support registered for FTDI USB Serial Device<br>[1218334.938673] ftdi_sio 1-1.2:1.0: FTDI USB Serial Device converter detected<br>[1218334.939512] usb 1-1.2: Detected FT232RL<br>[1218334.956748] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0<\/pre>\n\n\n\n<p>Here it is assigned  to  &#8216;\/dev\/ttyUSB0&#8217;<\/p>\n\n\n\n<p>Now we just need to install &#8216;dmx485&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip3 install dmx485<\/pre>\n\n\n\n<p> (if you have a fresh &#8216;ev3dev&#8217; installation you probably need to install &#8216;pip3&#8217; first) <\/p>\n\n\n\n<p>For testing, the script on &#8216;dmx485&#8217; homepage is enough but I adapted it for my first python DMX script to control my cheap DMX RGB PAR spotlight (after reading the manual to find how it works &#8211; it uses 4 DMX channels for Master, Red, Green and Blue values)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!\/usr\/bin\/env python3\nimport time\nimport dmx\nimport random\n\nrandom.seed()\n\nCMD = [0]*512\nCMD[3] = 255  # Master Dimmer\nCMD[4] = 0    # Red Dimmer\nCMD[5] = 0    # Green Dimmer\nCMD[6] = 0    # Blue Dimmer\n\nBLANK = [0]*512\n\nDELAY = 0.2\nsender = dmx.DMX_Serial('\/dev\/ttyUSB0')\nsender.start()\n\n# WHITE\nCMD[4] = 255\nCMD[5] = 255\nCMD[6] = 255\nsender.set_data(bytes(CMD))\ntime.sleep(2)\nsender.set_data(bytes(BLANK))\n\n# RED dimmer\nCMD[4] = 0\nCMD[5] = 0\nCMD[6] = 0\nfor i in range(0, 256, 8):\n    CMD[4] = i\n    sender.set_data(bytes(CMD))\n    time.sleep(DELAY)\nsender.set_data(bytes(BLANK))\n\n# GREEN dimmer\nCMD[4] = 0\nfor i in range(0, 256, 8):\n    CMD[5] = i\n    sender.set_data(bytes(CMD))\n    time.sleep(DELAY)\nsender.set_data(bytes(BLANK))\n\n# BLUE dimmer\nCMD[5] = 0\nfor i in range(0, 256, 8):\n    CMD[6] = i\n    sender.set_data(bytes(CMD))\n    time.sleep(DELAY)\nsender.set_data(bytes(BLANK))\n\n# MASTER dimmer\nCMD[4] = 255\nCMD[5] = 255\nCMD[6] = 255\nfor i in range(0, 256, 8):\n    CMD[3] = i\n    sender.set_data(bytes(CMD))\n    time.sleep(DELAY)\nsender.set_data(bytes(BLANK))\n\n# RGB random\nCMD[3] = 255\nCMD[4] = 0\nCMD[5] = 0\nCMD[6] = 0\nfor step in range(50):\n    CMD[4] = random.randint(0,255)\n    CMD[5] = random.randint(0,255)\n    CMD[6] = random.randint(0,255)\n    print(CMD[4], CMD[5], CMD[6])\n    sender.set_data(bytes(CMD))\n    time.sleep(DELAY)\nsender.set_data(bytes(BLANK))<\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"LEGO MINDSTORMS DMX Controller\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/ni7dvF5yOek?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>\n<\/div><\/figure>\n\n\n\n<p>It was amazingly easy.<\/p>\n\n\n\n<p>Now the tough part is using this same USB DMX adapter to listen to DMX signals and make my own LEGO MINDSTORMS DMX fixture. I am not keen to continuously polling the bus so I probably have to spend a few more euros to buy a smarter USB DMX adapter that offloads the reception task.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 1  of 6 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/lego-and-dmx\" class=\"series-363\" title=\"LEGO and DMX\">LEGO and DMX<\/a><\/div><p>Playing with MIDI, reading a lot, browsing through musical equipments&#8230; and DMX cames out. DMX is like a custom RS-485 network. A master device sends data at 250 kHz to slave devices, each of them listening to a different &#8220;channel&#8221; or address. Typical DMX devices (&#8220;fixtures&#8221;) are stage lights but you can find fog machines, &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2021\/lego-and-dmx\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;LEGO and DMX&#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":[1],"tags":[],"series":[363],"class_list":["post-1722","post","type-post","status-publish","format-standard","hentry","category-sem-categoria","series-lego-and-dmx"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-rM","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1722","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=1722"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1722\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1722"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}