myTetris is a giant (~6ft tall) physical version of Tetris using a 10x20 grid of RGB LEDs and a myRIO. The game is played using any modern web browser with WebSockets support - this includes mobile and tablet devices. Not only is the myRIO running the tetris game engine and controlling the LED array, it also serves a web-page (via LabVIEW Web Services) using the built-in Wifi of the myRIO. You can tap the buttons on the web-page or use the arrow keys on your keyboard to move the tetrominoes. The web page will also update with your current score. When the game ends, it shows you a game over screen that highlights some statistics about your game performance as well as your final score. The communication between the browser and the myRIO is done using WebSockets, a modern web protocol that allows the transfer of data similar to a TCP/IP connection with a browser using JavaScript. This allows you to play on any WebSockets compatible browser with no extra installation or software required.
(The video has music - you have been warned!)
I'm a full-time LabVIEW Software Engineer (and Certified LabVIEW Developer!) working for Intelligent Energy Ltd. Previously I worked for a defence company as a control engineer for a couple of years and, before that, I studied Mechatronics at the University of Sheffield. Intelligent Energy Ltd. makes hydrogen fuel cells and fuel cell systems (such as the Upp mobile phone charger) and our small LabVIEW team writes software for testing the fuel cells, fuel cell systems and fuel cell system components. While we mostly develop LabVIEW PC Software, we use CompactRIOs and Single Board RIOs in a lot of our test rigs - so I'm pretty familiar with programming embedded systems using LabVIEW FPGA & Real-Time. I've also done (and still occasionally do) Web Development - writing database driven websites using PHP/MySQL (and HTML/CSS/JavaScript). This means I like looking for opportunities to combine web technologies with LabVIEW to leverage the best of each. MediaMongrels (in the video/pictures) is just the 'brand' I do all of my Web Development and cool/funky personal technology projects under - feel free to get in touch via the NI community/forums, email or my website.
Giant Tetris is a personal project that I've been working on for over a year. It started with buying a small strand of the RGB LEDs from Sparkfun (or their UK distributor - proto-pic.co.uk). I was so impressed with the brightness and colours I knew I had to do something with them. Tetris sprung to mind as the basis of a cool LED project - as someone that enjoys videogames a lot, I thought it would be the perfect 'retro gaming' folly! Every month or so I'd make a little bit of progress towards it - buying the 200 LEDs one month, getting someone to design the mechanical body & frame in other months, ordering or assembling some bits another month etc. until it was all finished!
As for the brains of the Giant Tetris, I was able to use an Arduino at first to drive the LEDs to test that they worked correctly. I then moved onto using an FTDI USB to Serial converter in bit-bang mode to output SPI data so I could drive the LEDs directly from the PC with LabVIEW (using the LabVIEW driver that FTDI provide). This allowed me to very easily get a demo of the Tetris running on my PC by adapting a version of Tetris I found on the NI Forums to output the game board to the LED panel. This was great - but I really wanted to have it run as a standalone device without requiring a PC, which lead me back to the Arduino... until NI Week 2013 came around and NI announced the myRIO - an educational version of the CompactRIO, which I'm familiar with using at work. I knew I had to get my hands on one and use it for the Giant Tetris!
Arduino's are great low-cost devices - but, I'm a LabVIEW Developer (so I enjoy graphical coding) and I wanted to be able to do a lot more with my LED panel than would be possible with an Arduino (improved gameplay speed, Wifi/WebSockets and the potential for using the myRIO's built-in audio I/O).
The Giant Tetris doesn't have any moving parts (except the electrons ), so the only mechanical considerations were for the panel of the giant LED wall. Tetris required 200 LED compartments (which was done by slotting pieces of aluminium together, along with frosted plastic on the front to diffuse the light). The back panel is just an aluminium sheet with 12mm holes drilled in it for inserting the LEDs. I also had the stand designed from standard aluminium profile, which I was able to assemble really quickly - it's also possible for me to rearrange the stand, to make the LED wall stand horizontally, just in case I get grand ideas of becoming a DJ and using it for trippy visualisations!
The electronics for this project are fantastically simple!
The RGB LEDs have 4 connections: 5V Power, Ground, Clock and Data. Mine arrived already assembled in strands of 50 LEDs and you can connect multiple strands together
You should wire power to each individual strand because they draw quite a lot of current - 5A to have all 200 LEDs on at full white brightness.
The LEDs are driven by a WS2801 chip using SPI which clocks in 24-bits on the data line (8 for each of red, green & blue) and then passes any subsequent bits to the next WS2801 chip in the strand. The first chip receives 200x24 bits and the last chip receives just 24 bits. The only other electronics I used is a logic level converter which is required if you're using a microcontroller/device that has 3.3V logic, such as the myRIO, to scale it up to 5V for the LEDs. You can pick these up super cheap and they're very easy to insert onto breadboard or solder to the proto-board that ships free with the myRIO.
The Clock and Data wires are wired to the SPI.CLK and SPI.MOSI pins respectively and the Ground is connected to a DGND pin of the myRIO.
For myTetris, I housed the myRIO in a plastic box with an enclosed power supply that provides enough 5V current to power the strands of LEDs and a 12V supply for the myRIO.
I also made a simple wired controller using 4 push-buttons soldered onto some breadboard, connected to some digital inputs on the myRIO. This allowed me to test and develop the software quickly until I had the web-browser control implemented.
The software for the Giant Tetris is designed as a start-up application (.rtexe) for the myRIO so that it starts running as soon as the myRIO has booted-up!! A LabVIEW Web Service (these have been so vastly improved in LabVIEW 2013...being able to serve static web pages was so easy!) serves a HTML page at (http://tetrisrio/tetris - the hostname of the myRIO and the name of the Web Service) and when connected, the HTML page establishes a connection to the myRIO from the browser using WebSockets and can then send/receive commands.
The top level VI has four main loops...
To improve the performance of the display update loop, I ended up copying the code out of the myRIO SPI Express VI to do the configuration of the SPI once at the beginning instead of every time the loop ran.
The actual 'game' of Tetris is a single VI in the main state machine that receives button commands, from either the browser or a wired controller, and has a timer for the 'falling' of the pieces. It's implemented using the queued message handler design pattern, to ensure that the system does not 'miss' any incoming commands from the user! There's lots of information on the web about how Tetris works (including a very long 'Tetris Guidelines' document) and lots of help for programming it. I ended up picking and choosing the features I wanted in my implementation. The most complicated part is to do with performing array manipulation to check for collisions and check for completed rows etc.
One of the really cool things about the myRIO is that its default FPGA 'personality' (FPGA bitfile) and the associated express VIs make it incredibly easy to interface with the inputs/outputs of the myRIO. I was amazed that I was able to output SPI data to light up the LED panel without having to write any LabVIEW FPGA code at all! Coming from doing a lot of work with cRIOs and FPGA Chassis (e.g. NI 9148) I was expecting to have to write my own SPI protocol using LabVIEW FPGA, but I was able to do everything with the myRIO API and the default personality that NI provide.
Kudos to the NI myRIO team for that!
The other seriously cool thing with the myRIO is how easy it is to set up - sometimes finding a cRIO/sbRIO in MAX once you've switched it on can be tricky (usually because of IP address settings) but, with the myRIO, I just had to plug it in via USB and it showed up straight away in MAX. I was then able to very quickly configure it to connect wirelessly using my WiFi router. I hope this is a sign of things to come!
WebSockets is a web technology that allows a browser and a server to communicate in a way similar to TCP/IP. The browser sends a WebSocket upgrade request (very similar to a HTTP request), the server sends a HTTP response with specific headers and then you can send/receive data through the open connection. To implement this in LabVIEW I used the TCP/IP VIs and some additional VIs for performing the HTTP handshaking and framing the websockets data. In my case, the browser is communicating directly with the myRIO (or it could be other RT targets or PCs) with no intermediate server.
I've been waiting for an excuse use WebSockets as a tech demo for my work - we have used LabVIEW Remote Front Panels in the past for communicating with LabVIEW (a cRIO). Although it was easy to setup, it requires the LabVIEW run-time engine to be installed and only works with certain browsers (Internet Explorer). Having done quite a lot of web development, I know that there is an absolutely vast pool of resources for using a web browser for displaying data and doing asynchronous communications using JavaScript and I'd like to leverage that to create great looking user interfaces (using HTML and CSS) that anyone can view/use with any web browser - including mobile devices (and especially tablets - iPad etc.)! WebSockets fulfils the requirement of having data update in real-time and allow the user to press buttons & enter values.
Not only could I use WebSockets as a controller, but I could also have the myRIO send data back to the player (as a secondary display) such as the next piece and the players score. When the game ends, you get a popup that shows you how long you played for, how many rows you cleared etc.
The web browser side of it does require a little knowledge of HTML/Javascript but if you have that then it's pretty simple:
Sample JavaScript for WebSockets (opening connection, receiving data and sending button presses):
function connect(){
var socket;
var host = "ws://tetrisrio:8080";
var socket = new WebSocket(host);
socket.onopen = function(){
// code when the connection is established
}
socket.onmessage = function(msg){
// code when a message is received
var jsonData = JSON.parse(msg.data) // data is sent from LabVIEW as json encoded clusters as a command/data pair - but it could just be strings
switch(jsonData.command){
case 'score':
// update the score
$('#score').html(jsonData.data);
break;
// etc...
}
}
$('.tetris_button').click(function(event){
var text = event.target.id;
socket.send(text);
});
socket.onclose = function(){
// code when the connection is closed
}
}
After implementing the WebSockets for Giant Tetris - I was amazed at how low the latency was (the time between pressing the button and the screen updating) - it's unnoticeable! This is coming from someone who considers themself to a be a 'serious gamer' where network 'lag' can be really frustrating and it was something I was really worried about with using a web browser to play over a network but it hasn't been an issue at all.
The coolest thing about WebSockets/Wifi/myRIO/Giant Tetris - I can face the LED panel towards the window, then play the game using my phone... from the other side of the road!
I did also consider wired controllers - I was hoping to take advantage of the fact that the myRIO uses the Linux RT Operating System and connect a wired USB Xbox 360 controller to the myRIO as another way of playing but I ran into some issues trying to get it to work (so if anyone can offer any assistance, please post!). I could, of course, always use something like a SNES controller if I decide I need to use something more 'physical'... and more simple than the modern game control pads!
I have attached a .zip file that contains a LabVIEW 2013 myRIO project for the Giant Tetris. It includes everything except for my WebSockets implementation and the connection handler but you'd be able to play with a wired controller using some pushbuttons and four digital inputs on the myRIO. To run/re-create this project you would need the 200 RGB LEDs so I'm not sure how likely that is to happen but I hope it might serve as some inspiration/sample code. With a little modification, you may be able to 'simulate' the game without any hardware.
The code is provided as-is for anyone to use/modify/play around with but please credit me if you plan on re-distributing it etc.
I'd also welcome any feedback on the code - all part of the never ending journey of becoming a better LabVIEW developer!
I do have some further plans of some other cool things I can do with a web-connected 6ft LED panel...
Of course...if anyone has any other great ideas - I'd love to hear them!
Thanks to NI for providing me with a myRIO for this project - I really think the myRIO is a great device for teaching LabVIEW (particularly RT, FPGA and communicating between them), electronics, mechatronics, control and I wish they had been around back when I was a student!
I also wish to thank the various companies and people that helped me with the design and supplying of parts - It's amazing when companies will be so helpful towards someone working on some wacky personal project with low order quantities!
My current high score, as per the screenshot above, is 78 rows cleared with a score of 23220 - if ever you see the Giant Tetris at a UK NI Event in the future I encourage you to have a go and see if you can beat it!
Feel free to send me screenshots of your own scores if you do play and I'll see about posting them here!
Great!
Any chance you will share the websocket code with us?
I'll try to make the WebSockets code available soon - when I get chance I want to put a proper demo together of some of the things you can do with it so when I've got that sorted I'll try and put another post up with the details.
very easy but interesting!!!
Very impressive. Does your Websockets implementation support streaming data? I second the vote for providing the Websockets code. I am hoping the code can be used for streaming data from devices with Websockets capability via Wifi to Labview.
Just seen this project on the demo floor at the NIWeek exhibition in Austin, Texas! Superb!
I have finally been able to tidy up the WebSockets code and have made the code available for it here: LabVIEW WebSockets Library.