Saturday, August 5, 2017

PiGRRL Zero Plus Tutorial


This past summer, my 12-year-old son and I decided to build a handheld video game system called a PiGRRL Zero Plus that runs on a Raspberry Pi Zero.  Considering we had little experience with soldering and small electrical parts, this was a challenging yet rewarding project.  This tutorial is a compilation of the many hours of work and research we did that hopefully someone else will find useful.

Resources
-          There are some helpful videos and downloads on Thingiverse by JPete24 found here.  He also sells cases on Etsy if you don’t have a 3D printer.  You will also need the pdf download of separate instructions found here.
-          There is also a helpful tutorial for the PiGrrl Zero (not Plus) found here.  It is not quite what is needed for the Plus, though, but close enough to be helpful.  (Don’t download the software support packages, though.)

Initial Setup
-          Write the PiGrrl Zero Plus image file (download from here or here) onto the SD card using Win32DiskImager
 
-          For troubleshooting the screen, you can also use the Jessie Lite-based PiTFT 2.8” Resistive Image (download from here) which should work “out of the box”
-          When re-writing an image over the SD card, clear the partitions and format the card first by following the steps listed on the second suggestion found here (the suggestion that uses diskpart in the command prompt):
-           The command line procedure is not simple, but it is the most likely thing to work.
-           When re-formatting the "drive" you're actually only formatting a partition on the drive. You need to use the diskpart utility to remove the partitions and create 1 single partition covering the full volume.
-           diskpart can be a bit dangerous, because if you pick the wrong disk or partition, you can remove data or partitions that are extremely, EXTREMELY important and lose all data on your machine.
-           Proceed with extreme caution!
-           Open up a command prompt as administrator (open the start menu, type cmd and press Enter.)
-           Type diskpart and press Enter. You'll end up with a new prompt that looks like this:
-           DISKPART>
-           Type
-           list disk <ENTER>
-           and you'll get a listing of all drives on your machine.
-           Type
-           select disk # <ENTER>
-           to select a disk, where # is the number of the disk Windows screw-balled. Usually, the device is disk 1, but it's always best to make sure.
-           Then type
-           list partition <ENTER>
-           to get a listing of all partitions on the disk.
-           If you're sure you have the right disk/partition, you can then repeat typing
-           select partition # <ENTER>
-           delete partition <ENTER>
-           until all partitions are removed from that drive.
-           Once the drive is empty, exit diskpart and remove the drive. Wait at least 5 minutes, then re-insert the drive. DO NOT CLICK FORMAT – this will most likely re-create the problem. Instead, enter diskpart again, and select the disk. Then type
-           create partition primary <ENTER>
-           Once you have created the partition, exit diskpart and, in the command promt, type
-           diskmgmt.msc <ENTER>
-           This will provide a reliable way to create a FAT32 partition. On the drive you would like to format (NEVER TOUCH DISK 0!), right click and select format. The allocation unit size option should be default, and the File System option should be set to FAT32. The format may take a while, depending on the disk size, be patient.
-           This should fix partition troubles on any readable USB flash drive.
-          Insert the card in the Pi Zero, use the dual male header to connect the 3.2” PiTFT screen to the Pi Zero (use some poster putty to wedge the Pi Zero and header for a temporary solderless connection), connect a USB hub with a keyboard and wireless connector, and connect the power supply and plug it in to turn it on
-          Don’t put the Pi Zero in upside down!  When the PiTFT screen is facing away from you, the mostly green side of the Pi should be facing towards you.
-          If all goes well, a green light will flash on the Pi Zero, the screen will start out white for about 10 seconds, then turn black, and eventually load Retropie.  This usually takes about 30 seconds.
-          The PiGrrl Zero (not Plus) tutorial pdf instructs you to download PiTFT Support and other things.  There is no need to do this!  The PiGrrl Zero image will already work without PiTFT support.
-          Use the left/right arrows to find RetroPie, hit “X” to select it, use the up/down arrows to find “config wifi”, hit “X” to select it, and follow the on-screen instructions to connect your Pi Zero to your wi-fi
-          Use PuTTY (download from here) to conveniently SSH into your Pi Zero from a computer on the same wifi network.  After opening PuTTY, type in the hostname of “retropie.local” and click “Open”.  A new window should open up and ask for credentials.  After you input the user name of “pi” and a password of “raspberry”, you will gain access to the Raspberry Pi command line.
 
-          In the Raspberry Pi command line (via PuTTY), type “sudo nano /boot/config.txt” to open up the config file.  On the last line of the file, add “display_rotate=2”, press “Ctrl+X” to exit, and save changes on the way out.  This will turn the screen display 180° which is necessary for the PiGrrl Zero Plus.  In the Raspberry Pi command line, type “sudo reboot” to reboot the Pi to test this.
-          To use all the memory on your SD card, expand the file system by typing “sudo raspi-config” in the Raspberry Pi command line, choose “Expand File System”, follow any directions, and reboot.
 
-          To upload roms (and remove pre-installed roms), turn on the Pi Zero Plus, make sure it is connected wirelessly, go to a computer with Windows, open up File Manager, and type “\\RETROPIE” in the address bar.  Choose the “ROMS” folder and copy and paste the new ROM in the appropriate system folder.
-          Sometimes I would get the error message “Windows cannot access Retropie”.  On the Pi Zero Plus, go to “RetroPie” and then “Config WiFi”, get the IP address, and type that in the windows address bar in place of “RETROPIE”.  For example, “\\192.168.1.67”.
-          The Pi Zero is not powerful enough to run N64 (or higher) roms.
-          The Gameboy Advance emulator needs an additional file called “gba_bios.bin” copied in the RetroPie/BIOS folder for its games to work.  The file can be downloaded from many websites (such as here) and one way to copy it over is to go to File Manager in Windows, type “\\RETROPIE\bios” in the address bar, and paste the file there.
-          To change the look of the Retropie menu, go to Retropie, and choose “install emulation station theme” and follow the instructions to download and install.  (My favorite is “Simple Dark”.)  Then restart EmulationStation, go to “UI Settings” on the main menu, and scroll down to “Themes” and pick your theme.
-          Sometimes the font size for a theme is too small for the PiTFT screens (like “Simple Dark” on a 3.2” PiTFT).  To make the font size bigger on a theme, go to the command line, and type “sudo nano /etc/emulationstation/themes/carbon/carbon.xml” (or whatever the name of the theme is) and change all font-size to 0.05 (so that each line with “fontSize” it looks like “<fontSize>0.05</fontSize>”).

Screen
-          Now that everything is working, unhook the PiTFT and use some heavy duty wire cutters to clip off the 2x20 female pins and the other set of 2x16 pins, and solder all 40 points of the dual male header to where the 2x20 female pins used to be.  Place some masking tape on the other set of unused pins to prevent short circuits.  Use a multimeter to test your soldering connections.
-          Cut trace Pin #18 on the PiTFT circuit board.  Use a multimeter to make sure it is cut.
-          Solder the Pi to the other side of the male dual header.  Once again, make sure it is put in the right way: when the PiTFT screen is facing away from you, the mostly green side of the Pi should be facing towards you.  Use a multimeter to test your soldering connections.
-          Turn on the Pi to make sure the screen is working before moving on to the next step

Battery
-          Solder wires to the battery circuit board, switch and Pi Zero GPIO pins exactly as described here.  You will need to snap off the female USB jack off of the battery circuit board and save it for the USB step.
-          Connect the battery to the circuit board and test everything out by turning on the Pi with the battery before moving on to the next step.

Buttons
-          Solder all buttons on the button gamepad (described here). 
-          Also solder the four buttons for the PiTFT that are on the front beside the screen.
-          Solder wires from the two button gamepads (B1, B2, B3, B4, ground) and the two shoulder buttons (but not the TFT buttons) to the appropriate GPIO pin on the Pi Zero.  Use the diagram found here.
-          Since the Pi Zero is upside down and rotated inside the case, I used this modified image to help me (battery ground, battery 5V added; start, select, and TFT buttons not needed):

-          I also used some of the available “ground” pins for grounding the button and audio components
-          It’s also helpful to use different color wires and write down which colors went where to help troubleshoot for later on
-          Use a multimeter to test all your connections.
-          Once soldered, wrap the metal parts of the shoulder buttons with some masking or painter’s tape to prevent accidentally short circuits.
-          Turn on the Pi to make sure the buttons are working before moving on to the next step.  You will probably need to play a game or two (or three or ten) to test this out properly.  (The top TFT button is “Select” and the second TFT button is “start”)

USB
-          Solder wires from the USB OTG adapter to the female USB jack (from the battery circuit board) exactly as described here
-          I could never get this step to work out properly.  The keyboard would work but not the USB hub or wireless (maybe a power issue?)  I bought a $5 micro USB to female USB adapter from Amazon, cut the plastic off the female USB and melted some of the plastic on the case to make it fit in the right spot.
-          Connect a USB device (the hub with keyboard and wireless connector) and turn on the Pi to test your USB wire out before moving on to the next step.

Audio
-          Solder wires from the speaker, potentiometer, and audio circuit board as pictured in the 2nd page of this pdf file
-          I did not need to connect SD to 5V (Vin to 5V was enough)
-          I have no idea how to “bridge between gain and GND” so I did not do that, either
-          Turn on the Pi, load up a game (or two or twenty) to test out the sound before moving on to the next step
-          Maybe it was just my poor soldering skills, but I found the audio to be very finicky and the potentiometer to die out if the volume was maxed out for extended periods of time.
-          Also, the volume knob only works from 90% to 100% (there were several other online reports of this, too)
-          Expect retro sound quality.

Putting Everything Together
-          Tap each screw hole of the case with a tap set.  Screw the tap bit in the hole until it “bites” (a few turns), and then alternate between doing ¾ of a turn right and ¼ of a turn back.  When you have gone far enough, unscrew the tap bit until it comes out of the hole.
-          Screen: Put the four 3D printed TFT buttons in the appropriate holes and balance the case on top of two equally wide books on each side so the buttons can hang down.  Peel the plastic protector off of the screen and place the screen in the appropriate place, lining the screw holes up.  Screw the four screws in to hold the screen in place.
-          I needed a little more wiggle room to install these, so I actually took the soldering gun and melted some of the inside of the case to make more room
-          Buttons: Put the 3D printed D-Pad and 3D printed A, B, X, Y button pad in the appropriate holes and balance the case on top of two equally wide books on each side so the buttons can hang down.  Line the button pads with their two screw holes and screw those in place.
-          I needed a little more wiggle room to install these, so I actually took the soldering gun and melted some of the inside of the case to make more room
-          Don’t screw in the screws all the way or the buttons will stay in the down position.  The button pads will probably not be level.
-          Audio: Place the speaker in the appropriate hole and use a little bit of tape to hold it in place.  Place the potentiometer in the appropriate place and use a little bit of poster putty to hold it in place (or if you are feeling confident use a hot glue gun).  Whatever you use, make sure that you can still spin the potentiometer after you put it in!  Then place the audio circuit board in the top right of the same case and use some poster putty to hold it in place.
-          Battery: Place the battery circuit board in the appropriate place (there are four protrusions to hold it in place), the battery switch in the appropriate place, and the battery in the top middle of the same case.  Use poster putty to hold each part in place.
-          USB: Place the female USB jack in the appropriate place and use some poster putty to hold it in place.
-          Turn on the Pi and test everything out before screwing the two sides of the case together.  If everything works, use some masking or painter’s tape to cover any putty (otherwise wires from the other side stick to them) and any metal parts (so nothing short circuits), place the shoulder buttons and 3D printed shoulder buttons in the appropriate place, put the two sides of the case together, and screw the four screws in place.
-          Enjoy your Pi Zero Plus!
  

Wednesday, May 24, 2017

Tile Patterns of Regular Polygons

A regular polygon is a shape with all congruent sides and all congruent angles.  For example, an equilateral triangle is a regular polygon because it has 3 congruent sides and 3 congruent angles, and a square is a regular polygon because it has 4 congruent sides and 4 congruent angles.

Regular Tessellations

Some regular polygons can be used to make a tile pattern (or a tessellation), and others cannot.  In order for a regular polygon to be tileable, its interior angle must divide evenly into 360°, and the interior angle of a regular polygon with n sides is θ = 1/n(n – 2)180°.  A regular triangle, which has n = 3 sides, has an interior angle θ = 1/3(3 – 2)180° = 60°, which divides evenly into 360°, which means it can be used to make a tile pattern.  A square, which has n = 4 sides, has an interior angle θ = 1/4(4 – 2)180° = 90°, which divides evenly into 360°, which means it also can be used to make a tile pattern.  However, a regular pentagon, which has n = 5 sides, has an interior angle θ = 1/5(5 – 2)180° = 108°, which does not divide evenly into 360°, which means it cannot be used to make a tile pattern.  A regular hexagon, which has n = 6 sides, has an interior angle θ = 1/6(6 – 2)180° = 120°, which divides evenly into 360°, which means it can be used to make a tile pattern. 

Interior Angles of Regular Polygons

Since the next biggest factor of 360° after 120° is 180°, which is too big to be an interior angle of a regular polygon, there are no other regular polygons that can be used to make a tile pattern.  Therefore, the equilateral triangle, the square, and the hexagon are the only regular polygons that can be used to make a tile pattern.

Regular Polygons That Can Be Tiled

Semi-Regular Tessellations

If the requirement that all the regular polygons in the tile pattern must have the same number of sides is removed, then there are a few more tile patterns that can be obtained called semi-regular tessellations.  Semi-regular tessellations (or Archimedean tessellations) are tile patterns that contain two or more regular polygons with the same order around each vertex.  For example, at my house growing up we had a brick driveway consisting of octagons and squares.  This was a semi-regular tessellation because it was a tile pattern containing regular polygons in which each vertex was surrounded by 2 regular octagons and 1 square.

Brick Driveway Pattern Consisting of
Octagons and Squares

One way to find all the possible semi-regular tessellations is to examine which combinations of regular polygons fit snugly around a single vertex.  In order for this to happen, each interior angle, which for a regular polygon with n sides is θ = 1/n(n – 2)180°, must add up to 360°.  If there are 3 regular polygons around one vertex with sides n1, n2, and n3; then 1/n1(n1 – 2)180° + 1/n2(n2 – 2)180° + 1/n3(n3 – 2)180° = 360°.  After distributing, the equation becomes 180° – 360°/n1 + 180° – 360°/n2 + 180° – 360°/n3 = 360°; after combining like terms, the equation becomes -360°/n1360°/n2 360°/n3 = -180°; and after dividing by -360°, the equation simplifies to

1/n1 + 1/n2 + 1/n3 = 1/2

This holds true for the known three hexagons example (1/6 + 1/6 + 1/6 = 1/2 or (6, 6, 6)), and for the brick driveway example consisting of 1 square and 2 octagons (1/4 + 1/8 + 1/8 = 1/2 or (4, 8, 8)).  Using trial and error and the fact that n is an integer and n ≥ 3, the only possible solutions are (3, 7, 42), (3, 8, 24), (3, 9, 18), (3, 10, 15), (3, 12, 12), (4, 5, 20), (4, 6, 12), (4, 8, 8), (5, 5, 10), and (6, 6, 6).

All Combinations of 3 Polygons Fitting
Snugly around a Single Vertex

However, even though all of these solutions represent all the possible ways for 3 regular polygons to fit snugly around a single vertex, not all of them can be used to make a tessellation.  One final requirement for a tessellation is that the number of different sides must divide evenly into the third side.  For example, the solution (5, 5, 10), which represents a pentagon, pentagon, and decagon, fits snugly around a single vertex but cannot be tessellated.  The first pentagon, with 5 sides, would need to alternately share sides with the other pentagon and decagon, 2 different shapes, but 2 does not divide evenly into 5, and so these 3 shapes cannot be tessellated.  (See here for more details.)

Attempt at a (5, 5, 10) Tessellation Results in Some Gaps

This requirement eliminates all the above solutions except for (3, 12, 12), (4, 6, 12), (4, 8, 8), and (6, 6, 6).

(3, 12, 12)

(4, 6, 12)

(4, 8, 8)

The same logic can be applied to find semi-regular tessellations with 4 or more regular polygons.  In general, if there are k regular polygons around one vertex with sides with sides n1, n2, … nk; then ∑ k p=1 1/np(np – 2)180° = 360°.  After distributing, the equation becomes ∑ k p=1180° – ∑ k p=1360°/np = 360°, which is 180°k – ∑ k p=1360°/np = 360°; after some rearranging, the equation becomes -∑ k p=1360°/np = -180k + 360°; and after dividing by -360°, the equation simplifies to ∑ k p=11/np = 1/2(k – 2) or

1/n1 + 1/n2 + … +  1/nk= 1/2(k – 2)

For k = 3, 1/n1 + 1/n2 + 1/n3 = 1/2 (as proved above); for k = 4,

1/n1 + 1/n2 + 1/n3 + 1/n4 = 1

for k = 5,

1/n1 + 1/n2 + 1/n3 + 1/n4 + 1/n5 = 3/2

and for k = 6,

1/n1 + 1/n2 + 1/n3 + 1/n4 + 1/n5 + 1/n6 = 2

Since n ≥ 3, there are no integer solutions when k ≥ 7 (geometrically, 7 or more equilateral triangles cannot fit around a single vertex). 

Once again using trial and error and the fact that n is an integer and n ≥ 3, the only possible solutions for k ≥ 4 are (3, 3, 4, 12), (3, 3, 6, 6), (3, 4, 4, 6), (4, 4, 4, 4), (3, 3, 3, 3, 6), (3, 3, 3, 4, 4), and (3, 3, 3, 3, 3, 3).  For some of these solutions, a new ordering produces a new unique pattern, and so (3, 4, 3, 12), (3, 6, 3, 6), (3, 4, 6, 4), and (3, 3, 4, 3, 4) can be added to the list. 

All Combinations of More Than 3 Polygons
Fitting Snugly around a Single Vertex

However, the requirement for semi-regular tessellations that there is the same order around each vertex eliminates (3, 3, 4, 12), (3, 3, 6, 6), (3, 4, 4, 6), and (3, 4, 3, 12); leaving only the solutions (3, 4, 6, 4), (3, 6, 3, 6), (4, 4, 4, 4), (3, 3, 3, 3, 6), (3, 3, 3, 4, 4), (3, 3, 4, 3, 4), and (3, 3, 3, 3, 3, 3).   

(3, 4, 6, 4)

(3, 6, 3, 6)

(3, 3, 3, 3, 6)

(3, 3, 3, 4, 4)

(3, 3, 4, 3, 4)

Since (6, 6, 6), (4, 4, 4, 4), and (3, 3, 3, 3, 3, 3) are regular tessellations, that leaves 8 possible semi-regular tessellation patterns: 1 triangle and 2 dodecagons (3, 12, 12); 1 square, 1 hexagon, and 1 dodecagon (4, 6, 12); 1 square and 2 octagons (4, 8, 8); 1 triangle, 2 squares, and 1 hexagon (3, 4, 6, 4); 2 triangles and 2 hexagons (3, 6, 3, 6); 4 triangles and 1 hexagon (3, 3, 3, 3, 6); and 3 triangles and 2 squares (3, 3, 3, 4, 4) and (3, 3, 4, 3, 4).  (See here for more details.)

The Eight Semi-Regular Tessellations

Quasi-Regular Tessellations

If the semi-regular tessellation requirement that there is the same order around each vertex is removed, then there are countless other variations of tile patterns that can be obtained called quasi-regular tessellations.  Many quasi-regular tessellations can be formed by modifying an existing semi-regular tessellation.  For example, since 1 regular hexagon can be formed from 6 equilateral triangles, the semi-regular tessellation of (6, 6, 6) can be transformed to a quasi-regular tessellation in which some of the vertices are (3, 3, 6, 6) and others are (3, 3, 3, 3, 3, 3); and the semi-regular tessellation of (4, 6, 12) can be transformed to a quasi-regular tessellation in which some of the vertices are (3, 3, 4, 12) and others are (3, 3, 3, 3, 3, 3). 

A regular hexagon formed by
6 equilateral triangles

A regular dodecagon formed by
1 regular hexagon, 6 squares,
and 6 equilateral triangles

In addition, 1 regular dodecagon can be formed from 1 regular hexagon, 6 squares, and 6 equilateral triangles, so the semi-regular tessellation of (3, 12, 12) can be transformed to a quasi-regular tessellation in which some of the vertices are (3, 3, 3, 4, 4) and some are (3, 4, 6, 4).  There are countless other variations of quasi-regular tessellations, but all of them use the same vertex combinations found in regular tessellations and semi-regular tessellations.

Some (of Many) Quasi-Regular Tessellations

Conclusion

There are many ways to tile an area with shapes all consisting of segments of the same length.  Three of those ways are by regular tessellations, in which all the shapes are the same.  These include 3 hexagons (6, 6, 6), 4 squares (4, 4, 4, 4), and 6 triangles (3, 3, 3, 3, 3, 3).  Eight other ways are by semi-regular tessellations, in which some of the shapes are different, but all of the shapes follow the same order around each vertex used in the tessellation.  These include 1 triangle and 2 dodecagons (3, 12, 12); 1 square, 1 hexagon, and 1 dodecagon (4, 6, 12); 1 square and 2 octagons (4, 8, 8); 1 triangle, 2 squares, and 1 hexagon (3, 4, 6, 4); 2 triangles and 2 hexagons (3, 6, 3, 6); 4 triangles and 1 hexagon (3, 3, 3, 3, 6); and 3 triangles and 2 squares (3, 3, 3, 4, 4) and (3, 3, 4, 3, 4).  Finally, there are several more ways to tile an area with shapes all consisting of segments of the same length using quasi-regular tessellations, in which some of the shapes are different and in which some of the vertices have different orders of shapes around them.