Category Archives: Tech Tips

PaperCart – Make an Atari 2600 that plays QR codes

What is it about the Atari 2600?

It was the summer of 1983 at Jeff Mccall’s slumber party when I saw my first game console.

Crowded around the small TV we gawked at the thing – an Atari VCS.

The seven of us took turnst. Passing the joystick around like a sacred relic we navigated Pitfall Harry over hazardous lakes, crocodiles and scorpions.

One by one the other kids fell asleep.  Having no need of such mortal frivolity, I played Pitfall all night!

I fainted in the street the next day due to sleep deprivation.  Worth it.

The challenge

It’s kind of mind-blowing that games that originally sold for over $30 ($70+ in 2018 money) can now be completely stored in a QR code on a small piece of paper.

As a poignant visual metaphor for showing my kids how much technology has changed, I decided to create a Raspberry Pi based Atari that accepts “paper carts” of actual Atari 2600 games.

The requirements for my “PaperCart” Atari VCS:

  • Must use the real QR code format, no cheating by tweaking the format into something a standard QR reader couldn’t read
  • 100% of the game data must actually be read from the QR code, no game roms can be stored in the console, no cheating by just doing a look-up or something
  • Runs on a Raspberry Pi + Picamera with all open source software (well, except the game roms…)
  • Can convert rom files to .html QR codes on the command line, sort if need this or we’ll have nothing to print and read later
  • Easy enough to use that a kid can insert and remove the “paper carts” and the games will start and stop like you would expect a console to do
  • Standard HDMI out for the video and audio, USB controller to play

All about QR codes

The QR in QR Code stands for Quick Response.  It’s a kind of 2d barcode that was invented by a Japanese company named Denso Wave in 1994.  They put it into the public domain right from the get go, so it’s used a lot of places in a lot of ways.

QR codes have a secret power – they use something called Reed-Solomon error correction.  It has the amazing ability to fill in missing parts using extra parity data.  More parity data, more missing data can be reconstructed.  Not certain parts, ANY OF THE PARTS.  I know, right?

Reed-Solomon is also used in CDs, DVDS and Blu-ray, that’s why a scratched disc can still work.

Remember those .par files on Usenet you’d use when you were downloading a bunch of stuff in chunks?  Yep, parchives were based on Reed Solomon.

I hid a fun Atari fact in this code.

I’ve encoded some text in the above QR code with error correction set to Level H (High), which means up to 30% can be missing and you can STILL read it!

Go ahead, block some of it with your fingers, put your phone in camera mode and point it at the QR code above.  Does it work?  That’s the Reed-Solomon stuff kicking in.

QR codes automatically jump to larger sizes to encode more data – from version 1 to version 40.

Can you find your way out of this maze?  Does your brain hurt yet? Hope no one took that seriously and actually tried.

Above is a version 40, the most dense version.  My iPhone is able to read this one right off the screen too.  If you have problems, you can try zooming into the page a bit maybe.

This is the first 2900 characters of Alice In Wonderland.  We can store a max of 2,953 full bytes.  A byte is 8 “yes or no” bits.  With that, you can store a number between 0 and 255. 

Because text doesn’t need a whole byte, there are smarter ways to store it which would allow us to pack in much more than we did here –  but let’s ignore that as we’re only interested in binary data.

If I show the QR code too clearly, I might be enabling rom piracy and get in trouble. Weird, right?

This game (Stampede) has 2,048 bytes (2K) of rom data so it easily fits inside a single QR code.

Other Activision classics like Fishing Derby and Freeway are also 2K games but Pitfall! is 4K game.  Using gzip compression saves us nearly 20% but it’s still a bit too big to fit in a single QR code. To work around this I’ve added a “Side B” to the other side of the Pitfall! card.  Cart. Whatever it is.

My paper cart format stores some metadata so the reader can know how many QR codes are needed for the complete game, as well as if the data is for the same game or not by storing a rom hash in each piece.

Emulating a 2600 on a Raspberry Pi 3

I started with latest Retro Pi image and put that on a micro SD card.  RetroPi has an Atari 2600 emulator out of the box that can be directly run from the command line like this:

/opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ atari2600 <rom file data>

So now I just needed to write some software that will monitor what the Pi camera sees, read QR codes, notice when no QR code is present or it has changed and start/stop the emulator as appropriate.

Writing the software – PaperCart

Naturally I chose Proton SDK as the base as it handles most of what I need already.  For the QR reading, I use zbar, and for the webcam reading I use OpenCV and optionally raspicam instead. (no need for OpenCV on the Raspberry Pi linux build)  I put it on github here.

The PaperCart binary can also be used from the command line to create QR codes from rom files. (It uses QR-Code-generator)

RTPaperCart.exe -convert myrom.a26

or on the raspberry:

RTPaperCart -convert myrom.a26

It will generate myrom_1_of_1.html or if multiple QR codes are needed, a myrom_1_of_2.html and myrom_2_of_2.html and so on.  I opened in the web browser, cut and pasted them into photoshop, scaled them down (disable antialiasing!) to the correct size and printed them.

A quick note about zbar and decoding binary data in a QR code

If you want binary data to look exactly as it went in (and who wouldn’t?!), you need to do a little processing on it after zbar returns it with iconv.  Here is that magical function for any future googlers:

string FixQRBinaryDataEncoding(string input)
{
	iconv_t cd = iconv_open("ISO-8859-1", "UTF-8");
	if (cd == (iconv_t)-1)
	{
		LogMsg("iconv_open failed!");
		return "";
	}
	int buffSize = (int)input.length() * 2;

	string output;
	char *pOutputBuf = new char[buffSize]; //plenty of space
	size_t outbytes = buffSize;
	size_t inbytes = input.length();

	char *pOutPtr = pOutputBuf;
	char *pSrcPtr = &input.at(0);

	do {
		if (iconv(cd, RT_ICONV_CAST &pSrcPtr, &inbytes, &pOutPtr, &outbytes) == (size_t)-1)
		{
			LogMsg("iconv failed!");
			SAFE_DELETE_ARRAY(pOutputBuf);
			return "";
		}
	} while (inbytes > 0 && outbytes > 0);
	
	iconv_close(cd);
	int finalOutputByteSize = (int)buffSize-(int)outbytes;
	string temp;
	temp.resize(finalOutputByteSize);
	memcpy((void*)temp.c_str(), pOutputBuf, finalOutputByteSize);
	SAFE_DELETE_ARRAY(pOutputBuf);

	return temp;
}

Want to make your own?

It’s pretty straight forward if you’re comfortable with linux and Raspberry Pi stuff. Here are instructions to set it up and download/compile the necessary software.

(If you really wanted, it’s also possible to do this on Windows, more help on setting up Proton on Windows here, you’d also need to OpenCV libs and Visual Studio in  that case)

  • Write Retropie image to a microSD card, put it in your pi
  • Make sure the Atari 2600 emulator works in Retropie, setup a gamepad or something
  • Enable SSH logins so you can ssh in, I guess you don’t have to, but it makes cutting and pasting instructions below a lot easier

Open a shell to the Raspberry Pi (as user “pi” probably) and install RTPaperCart and the stuff it needs starting with:

sudo apt-get install libiconv-hook-dev libzbar-dev

Now we’ll install and compile raspicam, a lib to control the camera with.

Note: It acts a little weird, possibly because it’s using outdated MMAL stuff? In any case, it works “enough” but some fancier modes like binning didn’t seem to do anything.

cd ~
git clone https://github.com/cedricve/raspicam
cd raspicam;mkdir build;cd build
cmake ..
sudo make install
sudo ldconfig

Before we can build RTPaperCart, we’ll need Proton SDK:

cd ~
git clone https://github.com/SethRobinson/proton.git

Build Proton’s RTPack tool:

cd ~/proton/RTPack/linux
sh linux_compile.sh

Download and build RTPaperCart:

cd ~/proton
git clone https://github.com/SethRobinson/RTPaperCart.git
cd ~/proton/RTPaperCart/linux
sh linux_compile.sh

Build the media for it.  It converts the images to .rttex format.

.rttex is a Proton wrapper for many kinds of images.

cd ~/proton/RTPaperCart/media
sh update_media.sh

Now you’re ready to run the software (note: pkill Emulation Station first if that’s running):

cd ~/proton/RTPaperCart/bin
sh ./run_app.sh

You might see errors if your camera isn’t available. To enable your camera, plug in a USB one or install a Picamera and use “sudo raspi-config” to enable it under “Interfacing options“. (don’t forget to reboot)

If things work, you’ll see what your camera is seeing on your screen and if a QR code is read, the screen should go blank as it shells to run the atari emulator.

You can point your camera at a QR code on the screen and it will probably work, or go the extra mile and print paper versions because they are fun.  You don’t have to laminate them like I did, but that does help them feel more sturdy.

You might need to twist your camera lens to adjust the focus. 

I setup mine to automatically run when the Pi boots (and not Emulation Station) so it works very much like a console.

Running RTPaperCart /? will give a list of optional command line options like this:

-w <capture width>
-h <capture height>
-fps <capture fps>
-backgroundfps <background capture fps
-convert <filename to be converted to a QR code. rtpack and html will be created>

3D Printing the stand

I sort of imagined designing a stylish 2600 themed case with a slot for the paper cart and fully enclosing the Pi, but that would take skill and also require some kind of light inside so the QR could be read. 

So instead I did the minimum – a thing to hold the PI, camera, and easel where you insert the QR code paper.

I used Fusion 360 and designed the stand parametrically so you can fiddle with values to change sizes pretty easily.  The modules are designed to snap together, no screws needed.

You can download the Fusion 360 project here, the download button allows you to choose additional formats too. 

You need to kind of use common sense and print with supports where it looks like you need them.

Conclusion

So that’s great, but you’d like to store Grand Theft Auto 5 as QR codes because they are so convenient?

Let’s see, 70 gigabytes.  No problem. To convert that you’ll just need about 25 million QR codes.  You might want to order some extra ink now.

At one code per paper, the stack would reach a mile and a half into the sky.

If you got this far, you must also be a connoisseur of gaming history and old hardware.  Check these out too then:

They Create Worlds (Podcast on gaming history, no fluff)
Matt Chat (Interviews and info about old games in visual form)
The Retro Hour (Podcast with retro gaming interviews and news)
Atari 5200 Multi-ROM Cartridge Using Raspberry Pi  (Cool, something like this might make it possible to mod a real 2600 to read “paper cartridges”.  Small world, Dr. Scott M Baker wrote BBS stuff too, including Land Of Devastation as well as Door Driver, a utility that allowed a dumb kid like me to write BBS games)

How to setup quadview for competitive gaming

Why would you want a quadview setup?

I (badly) play PUBG with my family in a room where we can’t really sit close together and this always happens:

“Sniper in that building.  Upper left window”

“Which building?  Which window?!”

“Look at my screen…  here!”

Turning around to crane your neck at someone else’s computer will get you killed – quadview to the rescue!  A quadview setup puts a live feed of everyone’s screen next to your normal screen.

In the very unlikely chance you’re in a situation where this could be useful, here’s how to do it.

Hardware I use

An HDMI Quad Multi-viewer (like this one $99) – this has four HDMI inputs (to plug in each person’s view) and one HDMI output.  This is the thing that can actually take the four views and smoosh it into one HDMI signal.

Assuming that each team member is sitting far enough away from each other to need their own quad view, you also need to split this HDMI out into up to four outputs.

An HDMI 1in 4 out splitter (like this one – $18)

Up to four extra monitors to show the quad view – if you’re like me, you have random old monitors (or Contique) laying around that can display HDMI signals.

Getting the inputs – Use OBS, NOT screen mirroring!

So now you need a signal from each person’s computer to plug into the switcher.  I don’t think it’s possible to buy a video card that doesn’t have an extra HDMI out on it these days, but having that is step one.

Run an HDMI cable from the switcher input to a free HDMI output port on your video card.  Open Windows’ Display Settings and verify it’s showing up as the second display.

You may be tempted to choose “Mirror display 1 on display 2” – this will only give you headaches later!  It will reduce the refresh rate and resolution of your main monitor to match this second one; you probably don’t want this.  It’s also kind of weird and buggy in my experience.

Note: I play 100 hz refresh at 3440X1440 – the method we use below will automatically letterbox the screen to fit the 1920X1080 output for quadview nicely.

The best way is to “extend desktop” to the second monitor.  We’ll also need OBS Studio. It’s a great free open source app used primarily for streaming but works great for this.

Configuring OBS Studio

After installing OBS, the setup is pretty simple.

Add a “Game capture” to the sources property.

Now, this is key, right click your scene’s name under “Scenes” and choose “Fullscreen Projector” and set it to your second display.  If you start a game, you should see its output being “broadcast” via the HDMI going to the quad-view box’s input.

If you want to be cool, add name overlays in OBS.  That’s it, you’re ready to roll.

Some tips

  • It takes practice to use,  easy to forget it’s there.  If a squadmate is in a fire fight a quick look can give you important information on her situation before choosing to bust in crazy-like or stealthily approach from a specific direction.
  • If one of your local squad dies and you’re playing with a remote player, the dead person can switch to the remote teammate’s view so everybody can watch what he’s doing as well.
  • Turn “equipment hud” and “weapon hud” options to ON –  if the display is big enough it’s possible to see the players backpack, armor, and gun situation.
  • If you’re REALLY serious you can buy a more expensive box that puts the four inputs together into 4K output –  don’t know about you, but I sure don’t have extra 4k monitors sitting around though.

Got a Vive Pro – initial thoughts

Is it worth the money? How different is it?

So I broke down and got a Vive Pro despite its exorbitant price tag. Is it worth it?  Well… probably not, unless you’ve got money to burn.

I hate the idea of playing something like Skyrim VR with the old Vive when I know I could be seeing something prettier if I had better hardware.  It’s like that feeling of sadness I had playing Quest For Glory 2 before I had a sound card; I knew I was missing out on some of the experience.

It’s got some nifty tech inside that may be useful later though – dual cameras for AR stuff and Hololens-like collider detection as well as eventual 10×10 meter room support when the new base stations are released. (a 10×10 VR play space in Japan? let me pause to laugh uncontrollably followed by a single tear down the cheek)

In a year they’ll probably have one neat package with all the new stuff, so better for most to wait for that.

I can’t quite put my finger on it, but I felt like the optics were slightly blurrier on the peripheral areas as compared to the original.  Might be my imagination or something specific to my eyes, dunno.  I took some pics through the lenses with both devices with this setup to compare:

 

It’s all quite scientific, let me assure you. Yeah.

I couldn’t really notice a different in the edge lens distortion from the pics.  Here’s a comparison of the square from the middle, you can see there really is less screen-door effect now though.

Pics are from Skyrim VR

My NVidia 1080ti seems to run content at the same FPS as the old Vive, so so no real downside to the switch I guess.  It seems about equally as comfortable as the original Vive, that is to say, extremely uncomfortable.

4/24 2018 Update: HTC has announced an “aimed at the enterprise” $1399 Vive Pro full kit that includes the new 2.0 base stations and controllers, which in theory will offer better tracking and huge spaces.  A word of warning – unless they just started shipping with a new cable, the Vive Pro cable is the same length as the Vive, meaning larger spaces wouldn’t do you much good until the wireless addon is released later this year. (?)

How to get your Unity LLAPI/WebSocket WebGL app to run under https with AutoSSL & stunnel

<continuing my “blog about whatever random issue I last dealt with in the hopes that some poor soul with the same issue will google it one day” series>

The problem

So you made your new Unity webGL game using the LLAPI and it works fine from a http:// address.  But when you try with https, even with a valid https cert being installed, you get this error:

“Uncaught SecurityError: Failed to construct ‘WebSocket’: An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.”

This is your browser saying “Look, the website is https, but don’t let that fool you; it’s using a normal old web socket to send data under the hood which isn’t encrypted, so don’t trust this thing with your credit card numbers”.

Unity (at the time of this writing) has no internal support for what we really need to be using:  a Secure Web Socket.  So where http has https, ws has wss.  So how do we connect securely if our unity-based server binary can’t serve wss directly?

A little background info about CPanel & AutoSSL

Note: I’m using CentOS 7 on a dedicated server with WHM/CPanel

Setting up your website for proper SSL so it can have that wonderful green padlock used to be a painful and sometimes expensive ordeal.

But no longer!  Enter the magic of CPanel’s AutoSSL.  (I think it’s using Let’s Encrypt under the hood as a plugin?)  Behind the scenes, it will handle domain validation and setup everything for you.  While it does need to renew your cert every three months, it’s free and automatic.  Add four new domains?  They will all get valid certs within a day or so, it’s great.

We can use this same cert to make your websockets secure as long as they are hosted at the same domain.

Setting up stunnel

This is an open source utility that is likely already included on your linux server box, if it isn’t, go install it with yum or something.

It allows you to convert any socket into a secure socket.  For example, if you have a telnet port at 1000, you could setup stunnel to listen at 1001 securely and relay all information back to 1000.

The telnet connection has no idea what’s happening and sees no difference, but as long as the outside user can only access 1001, plain text information isn’t sent along the wire and one or both sides can be sure of the identity of who’s connecting.

Depending on the stunnel settings, it might be setup like https where the client doesn’t have to have any certain keys (what we want here), or it could be like a ssh where the client DOES need a whitelisted key.

A way to test a SSL port is to use OpenSSL from the command line on the host server via ssh.  For example (keep in mind 443 is the standard https port your website is probably using):

<at ssh prompt> openssl s_client -connect localhost:443

<info snipped>
subject=/OU=Domain Control Validated/OU=PositiveSSL/CN=host.toolfish.com
issuer=/C=US/ST=TX/L=Houston/O=cPanel, Inc./CN=cPanel, Inc. Certification Authority
---
No client certificate CA names sent
Peer signing digest: SHA512
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 4946 bytes and written 415 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
 Protocol : TLSv1.2
<info snipped>
Start Time: 1518495864
 Timeout : 300 (sec)
 Verify return code: 0 (ok)

Hitting enter after that will probably cause the website to an html error message because we didn’t send a valid request. That’s ok, it shows your website’s existing SSL stuff is working so we can move on.

So first edit your /etc/stunnel/stunnel.conf to something like this:

pid = /etc/stunnel/stunnel.pid

#we won't screw with changing this because we don't want to relocate/change permissions on our files right now
#setuid = nobody
#setgid = nobody

sslVersion = all
options = NO_SSLv2

#for testing purposes.. these should be removed later:
output = /etc/stunnel/log.txt
foreground = yes
debug = 7

[websitename1]
accept = 29000
connect = 80
cert = /var/cpanel/ssl/apache_tls/oversi.io/combined

[websitename2]
accept = 30000
connect = 20000
cert = /var/cpanel/ssl/apache_tls/oversi.io/combined

Next, still from the ssh prompt, run stunnel by typing stunnel.

Because we have foreground=yes set above it will run it in the shell, showing us all output directly, instead of in the background like it normally would. (Ctrl-C to cause stunnel to stop and quit)

Look for any issues or errors it reports.  The .conf file I listed aboveshows how to set it up for two or more tunnels at once, you likely only need one of those settings.

The “websitename1” part doesn’t matter or have to match anything.

The SSL cert is the most important setting.  You need to give it your private & public & CA info in  the same file.

Now, initially, you might try to setup your keys using the files in ~/ssl/keys and ~/ssl/certs but they seem to not have everything all in one nice file including the CA certs.  I figured out ‘bundled’ ones already exist in a cpanel directory so I linked straight to them there.  (replace oversi.io with your website name)

If stuff worked, you should be able to test your SSL’ed port with OpenSSL again.  In the example above under “websitename1” I told it to listen at 29000 and send to port 80, for no good reason.

So to test from a remote computer we can do:

(you did open those ports in your firewall so outside people can connect, right?)

C:\Users\Seth>openssl s_client -connect oversi.io:29000
Loading 'screen' into random state - done
CONNECTED(00000270)
depth=2 /C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Certification Authority
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
 0 s:/CN=oversi.io
 i:/C=US/ST=TX/L=Houston/O=cPanel, Inc./CN=cPanel, Inc. Certification Authority
 1 s:/C=US/ST=TX/L=Houston/O=cPanel, Inc./CN=cPanel, Inc. Certification Authority
 i:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Certification Authority
 2 s:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Certification Authority
 i:/C=SE/O=AddTrust AB/OU=AddTrust External TTP Network/CN=AddTrust External CA Root
---
Server certificate
-----BEGIN CERTIFICATE-----
<snipped>
-----END CERTIFICATE-----
subject=/CN=oversi.io
issuer=/C=US/ST=TX/L=Houston/O=cPanel, Inc./CN=cPanel, Inc. Certification Authority
---
No client certificate CA names sent
---
SSL handshake has read 5129 bytes and written 453 bytes
---
New, TLSv1/SSLv3, Cipher is DHE-RSA-AES256-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
 Protocol : TLSv1
<snipped>
 Key-Arg : None
 Start Time: 1518497616
 Timeout : 300 (sec)
 Verify return code: 20 (unable to get local issuer certificate)
read:errno=10093

Despite the errno=11093 and return code 20 errors, it’s working and properly sending our CA info (“cPanel, Inc. Certification Authority”).

Or, easier, let’s just use the browser instead for this one since we’re connecting to port 80 if it works in this case:

https://oversi.io:29000

It worked, see the green padlock?  Oh, ignore the error the website is sending, I assume that’s apache freaking out because the URL request is different from what it’s expecting (http vs https or the port difference?) so it can’t match up the virtual domain.

From here, you should probably remove the debug options in the .conf (including the foreground=yes) and set it up to run automatically.  I just placed “stunnel” in my /etc/rc.d/rc.local file. (this gets run at boot)

Actually connecting using the Unity LLAPI

Congratulations, everything is setup on the server and you’re sure your web socket port is listening and ready to go.

While your server binary doesn’t need to change anything, your webgl client does.

You now need to connect to WSS instead of WS.  Example:

try
 {
   _connectionID = NetworkTransport.Connect(_hostID, "wss://oversi.io", portNum, 0, out error);
 }
 catch (System.Exception ex)
 {
   Debug.Log("RTNetworkClient.Connect> " + ex.Message);
 }

That’s pretty much it.  If someone doesn’t care about https and decides to play over http, it still works fine. (internally the websocket code will still connect via wss)

If you want to see it in action, check out my webgl llapi multiplayer test project https://www.oversi.io