Tutorial 0: Java Game Development: Creating Fun With Your ODROID #1
Dev Environment Setup, Java, IDE, GPIO Basics
-
Author: Brian A. Ree
Sections
Software and Scripts
The software and scripts used in this tutorial can be downloaded at the following links. There are also links provided
in the tutorial at the step where they are needed. I've placed a set of links here for convenience.
Link: GPIO Scripts
Link: Netbeans IDE Project v0.5.0
-- 0: Things You'll Need --
- An ODROID-N2 ~ $60 - $70:
2GB version $60
4GB version $70
- An ODROID-N2 Case:
link $4
- An ODROID-N2 Power Supply:
link $6
- If you choose to use eMMC (Recommended for the operating system) ~ $27 - $40:
32GB eMMC Memory Module $27
64GB eMMC Memory Module $40
- If you choose to use micro SD (Recommended for storage but can be used for the operating system) ~ $8 - $14:
16GB Micro SD Card $14
32GB Micro SD Card $8
64GB Micro SD Card $12
- A Micro SD to USB Adapter:
link $11
- An eMMC to USB Adapter:
link $10
- WIFI Modules (If you don't have a wired internet connection) ~ $5 - $8:
WIFI Module 0 $5
WIFI Module 5A $5
WIFI Module 4 $8
- Tinkering Kit (Breakout breadboard, LEDs, Jumpers, Buttons, Resistors):
link $16
- USB Mouse and Keyboard
- HDMI Monitor or TV
- HDMI Cable
- Internet Connection
-- 1: Introduction and Tutorial Goals --
Have you ever wanted to make your own video games? Well you've come to the right place. This tutorial series will show you how to make your own
games at a very low cost to you. You can develop them and play them on your own little computer, the awesome ODROID-N2. But wait there's more! You'll
also gain experience working with Linux, setting up a single board computer, writing code in Java, working with IDE's, and accessing general purpose IO
pins to actually control your game. Yes your heard right, this tutorial series will show you how to control your own game using a breadboard, jumpers, and some
switches.
Now I know there are a lot of different game APIs out there, and powerful game development tools like Unity but often times beginners have to not only
learn about video game programming, but also how to program in general, and then also learn how to use the game development tool. That's a lot to learn and can
be a bit daunting. Fortunately there are many aspects of game development that are almost universal, game loops, managing graphics and game resources.
Even IDEs are becoming more an more ubiquitous and offer many of the same features and very similar interfaces for software development.
This tutorial series will assume you know nothing and show you how to start from the ground up building your own development box, configuring it, and writing your
own code. Now that's a lot to take in, and a lot to cover so we'll be doing a little bit each tutorial. We won't go into a lot of depth on certain subjects. For instance
I'll be showing you some code and showing you how to use it and run it but I won't go into depth on the Java programming language, that could take up a whole book
on it's own. I will however give you some starting knowledge and you can go off on your own and research certain topics as you see fit.
Well that's the gist of it, let's get started.
-- 2: Review of the Things You'll Need --
Now there are a lot of options for configuring your ODROID-N2 so we'll go over what we recommend and also what other configurations are acceptable.
The low end configuration we recommend will cost around $120 not including shipping. The higher end configuration will cost around $140. You'll need
an HDMI monitor or TV to use with your ODROID-N2 development box and a mouse and keyboard. You can use any old USB mouse and keyboard.
Configuration 1: This is the recommended configuration because of the stability and speed offered by eMMC modules. We recommend using a decent sized eMMC module
as the operating system boot memory module and using a micro SD card for file backups and storage. There are links above to different sized eMMC modules and micro SD cards.
You can also choose between an ODROID-N2 with 2GB or 4GB of RAM. The 2GB version will run fine but if you want to go all out the 4GB version can only be an improvement.
We recommend using at least a 32GB eMMC module as the OS boot memory module and having anywhere from 16GB to 64GB of backup storage in the form of a micro SD.
Configuration 2: This configuration will work fine but doesn't have the performance and stability benefits offered by using an eMMC module. In this setup you would
use one micro SD card as both the boot memory module and for all file storage. If you go this route we recommend you buy a second micro SD card to backup your files to and get a
micro SD to USB converter to that you can access the files on any computer and the ODROID-N2 itself.
There is also a link at the top of the page for a really great ODROID-N2 case. I would think about getting 2 of them. We will be accessing the GPIO pins on the ODROID-N2
board and you have the option to open an access port on the case for easy access to the pins. However, you may want to keep the device completely closed in certain situations
and having a second case will allow you to easily change tops so you can use one for GPIO and the other to keep the device fully closed and protected.
So for less than $150 you will have a fully functional Linux based computer to use as a development box for games, if you have an HDMI screen/TV
and a USB mouse and keyboard. If you need to buy those remaining parts the price will vary but you can certainly be developing for under $350 if you
shop around for a great price on a screen/TV.
Ok so now that we have that part out of the way let's get started setting up the operating system on the eMMC module or micro SD card. I'll provide instructions
for Window, Mac, and Linux for each one. It's actually very simple and the process is almost exactly the same for both. You will need access to another computer to do this step
but I'll also show you how to use your ODROID-N2 to setup a new eMMC module or micro SD card for the ODROID-N2.
If you don't have access to another computer to prep your eMMC module or micro SD card you'll have to buy one of the pre-installed eMMC modules or micro SD
cards from Hard Kernel, listed above. We recommend that you get the 16GB ODROID-N2 micro SD card and use it to setup a custom eMMC module that we will use for this
tutorial series. You can also get the Hard Kernel 16GB micro SD card and use it to setup a larger micro SD card as the boot memory module. With these options you should
be able to get your dev environment up and running without access to another computer.
-- 3: Build your ODROID-N2 Computer --
In this step we'll put together the ODROID-N2 and get it ready to create our custom boot eMMC or micro SD memory module.
Seating the single board computer and assembling the case is very simple. This is one of the best case designs I've seen in a while.
You won't be pushing and pulling and hoping you don't crack some plastic catches at all. Simply seat the ODROID-N2 on the metal base of the case.
This also acts as a heat sink, ingenious! Gently tighten up the screws until they are firmly tight, do not over tighten them.
A good firm setup is what we're looking for here. Let's take a look at the case and board.
Next slide on the case front. Make sure you have the case front plastic guides lined up with the little metal ridge on the case bottom.
Now if you've opted to purchase two ODROID-N2 cases then you'll want to break out the small GPIO panel on the top of the case's back.
This is the longer case top that has a long "door" on it. This will allow you to work on the GPIO section of this tutorial with a protected
ODROID-N2. If not, don't worry, we'll just leave the remaining case top off for now. Be sure not to have any drinks or cats around your ODROID-N2
while it's exposed.
We recommend that you purchase a bootable micro SD card from Hard Kernel, links listed above, and use it to create the custom Linux OS we'll
be using in this tutorial series. When I say custom I only mean that we can choose which version of Linux OS images available for the ODROID-N2
we want to use and we'll also get experience building bootable eMMC modules and micro SD cards.
The picture above shows the ODROID-N2 with a Hard Kernel bootable micro SD card and the boot device switch set to the MMC setting.
Note that the MMC setting is used for both eMMC modules and micro SD card. The board gives higher priority to eMMC modules than micro SD cards.
In this way if you have both setup it will always try to boot the eMMC module first. I'd like to reiterate that we're purposely creating our own
bootable eMMC module or micro SD card because we want to be able to do so any time we want. Obviously we already have a bootable micro SD card,
the one from Hard Kernel, but we're only using it as a jumping off point to using the ODROID-N2 for creating our own bootable media.
Ok so now that we have the computer all setup let's fire it up and get into our Linux desktop. We'll be locating and downloading
a specific Ubuntu Linux image to write to our eMMC module or micro SD card, whichever path you decide to take. If you have access
to a computer and aren't building your custom boot memory module on the ODROID-N2 just follow the steps using your favorite browser and I'll be
sure to show you how to write to your storage device of choice on Windows or Mac.
You'll need an internet connection for this part. If you don't have access to an internet cable to plug into your ODROID-N2 then purchase
a WIFI module, links above, and you should be all set. If you're not sure which one to get just load up the page for that product and
see what types of WIFI each module supports. I've found that the cheapest, WIFI Module 0, works just fine and doesn't take up a lot of space around
the ODROID-N2's USB ports.
Simply plug the module into an available USB port while you have your system booted up and running Ubuntu MATE.
You should be able to select the WIFI icon in the top right corner of the screen. Doing so will give you a list of
available WIFI networks in your local environment.
Select your WIFI network and you'll be prompted to enter in any authentication information required by the WIFI end point.
The screen shot below depicts a similar prompt you'll encounter when connecting to WIFI.
Now that you've got your internet connection configured open up a browser and make sure you can access the internet.
Click the wiki link at the top of the page.
Or you can jump right to the destination by navigating your browser to the ODROID Wiki.
Select the ODROID-N2 option from the side navigation bar. Select os_images then select ubuntu.
You should see a list of dates, click the 20190812. Choose a mirror from the list of mirror sites, I use the
East Coast Mirror because it's closer to me.
Once on the mirror site you'll have to navigate to the N2 OS image, click the N2 link.
Next click the ubuntu-18.04.3-4.9-mate-odroid-n2-20190812.img.xz link. It may take a few minutes for it to download because
it's around 1GB in size so get up stretch, walk the dog, feed the cat, or do whatever it is you want to do to kill a few minutes.
Also, like any good cooking show we have the meal we're making already prepared so a direct link to the east coast mirror OS image we want is,
ubuntu-18.04.3-4.9-mate-odroid-n2-20190812.img.xz.
Once your download has completed you may receive a window popup like the one shown below. This has something to do with Ubuntu MATE detecting the file type and thinking you want to update
your current eMMC module or micro SD card.
Cancel out of the prompt and close the window behind it so that you're back at the browser.
Now that you've got a copy of the OS image downloaded it's time to move onto the next step.
Read below to choose your path, the recommended path has the best performance and reliability but may be slightly more expensive.
Choose wisely.
-
If you have a bootable Hard Kernel micro SD and plan to use an eMMC module (recommended) then proceed to Section 4.
-
If you have a bootable Hard Kernel micro SD and plan to use a micro SD card then proceed to Section 5.
-
If you have a computer and are building an eMMC module or micro SD card:
Follow along with Section 4 for eMMC modules, or Section 5 for micro SD cards
and you'll find information on how to build the boot memory module using Window or Mac at
the bottom of that section.
-- 4: Setup your ODROID-N2 with an eMMC Module --
To prepare for this step you'll need your eMMC module and a Hard Kernel eMMC to USB adapter. Lay your eMMC module down on a dry static free
surface so that the chip side is facing up.
Note the white notch in the corner of eMMC module. This will line up on the same side, left or right, as the white circle (or notch)
on the eMMC to USB adapter. It doesn't line up with the exact corner just the side.
Carefully place the eMMC module's little black connector onto the eMMC to USB adapter's little black connector.
You can kind of roll the eMMC module onto the adapter and line up the connectors. Gently press down and the two connectors
will gently and slightly snap together. That's it, you have successfully mounted the eMMC module onto the adapter.
Now plug your eMMC to USB adapter into your ODROID-N2, as shown below.
You are now ready to start writing an OS image to the eMMC module, which we'll cover in the next few steps.
Feel free to skip any micro SD steps that do not apply to you. You can always come back and look up any info you need.
I'll cover a few different ways to write the OS image to the memory module using different environments. The process is the same
for eMMC module or micro SD card.
-- 5: Setup your ODROID-N2 with a micro SD Card --
To prepare for this step you'll need your target, new, bootable micro SD card and a micro SD to USB adapter.
Simply plug your micro SD card into the USB adapter and then plug the adapter into your ODROID-N2.
That's it! Now you're ready to start writing an OS image to your micro SD card. I'll cover a few different ways to write the OS
image to the memory module using different environments. The process is the same for eMMC module or micro SD card.
-- 6: Writing to your Memory Module using Linux, Windows, or Mac --
This step will cover writing to your eMMC module or your micro SD card. At this point the process is that same for both.
If you've connected your eMMC module or micro SD card, please eject it. We're going to get the device in Linux for the memory module
and we want to be able to detect that new device so we don't accidentally write to our Hard Kernel bootable micro SD card.
For this step you're going to need to open up a terminal. On the top left hand side of the screen bring up the list of applications, and utilities.
Click on System Tools, then locate the MATE Terminal program entry as depicted below. Click it and you'll have a terminal window
to work with.
In the terminal type the following command:
sudo fdisk -l
Note the list of drive devices that are shown. Now connect your memory module and run the command again. Note the new entry in the list and write it down or copy
and paste it into a text document. This step verifies that we'll be writing to the proper memory module. For the purposes of writing this tutorial
let's assume that the device is dev/sdx. Next we will unmount the device by using some sweet terminal commands.
Unmount the partitions by running:
sudo umount /dev/sdx*
It may give an error saying the disk isn't mounted - that's fine. Now we'll write the contents of the image file onto the SD card by running
the following command.
sudo dd bs=1M if=/path/to/file/your_image_file_name.img of=/dev/sdx
If your file has a .img.xz extension instead of a .img extension you can right-click the file in MATE using normal file operations
and extract it to a .img file. You can also use the following command to do both operations in one step.
sudo xz -d < /path/to/file/your_image_file_name.img.xz - | dd of=/dev/sdx
Remember in this case the /path/to/file/your_image_file_name.img.xz or /path/to/file/your_image_file_name.img is the
path to the ubuntu-18.04.3-4.9-mate-odroid-n2-20190812.img.xz file we want to write and /dev/sdx is the device name we figured out earlier.
You can navigate to where the file is located using the MATE desktop. Right-click the window and select the Open Terminal command to open a terminal at the location of the target
file.
In this case your terminal commands would be:
sudo dd bs=1M if=your_image_file_name.img of=/dev/sdx
or
sudo xz -d < your_image_file_name.img.xz - | dd of=/dev/sdx
Let the operation run to completion and you'll have a fresh new bootable memory module to use on your ODROID-N2. What is more you
can use these steps to try different ODROID-N2 operating system images. Just look around the wiki page for other OS options
to try. For our purposes we are ready to move onto the next step of this tutorial. I will take a moment to demonstrate other ways to
write the image to the memory module. It is important to note that the eMMC module and the micro SD card behave the same at this point.
Linux just views them as a memory device to write to.
If you are working on your ODROID-N2 you can use a gui method to write the image to your memory module of choice.
Once the image file has finished downloading find the file in your Downloads folder and right-click on it.
Select the Open With Disk Image Writer option. You should see a window pop-up similar to what's shown below.
Select the proper destination to write the image. ALERT: Be very careful here, make sure you are choosing the target memory module
and not the boot SD card!!!
Writing an OS Image on a Mac
To write the OS image file on a Mac we recommend getting a great piece of free software, Balena Etcher.
You could also probably run the Linux commands listed above in a terminal on a Mac but let's try something new. Download and install Balena Etcher.
The software handles writing .img.xz files so you don't have to worry about decompressing the OS image. Locate your OS image file.
Note: The screen shots below show a different file being written then the one we are working with. The process is the same, no worries.
Next, fire up Balena Etcher and answer any prompts for higher privileges that might pop up.
Double check that you're indeed flashing the correct device and that it is the correct approximate size!!
Start flashing the device and wait for the process to complete.
Writing an OS Image on Windows
To write the OS image file on a Mac we recommend getting a great piece of free software, Win32 Disk Imager.
Now if you are running Ubuntu Linux under Windows 10 you could also probably run the Linux commands listed above in a terminal.
You could also install the Windows version of Balena Etcher and follow the directions for that software listed above.
I want to show you a new way so that you have a bunch of options and tools you can use to make your own bootable memory modules.
You'll have to decompress the .img.xz file before we can write it to the memory module. Get a free copy of 7-Zip.
Install it and use it to decompress the .img.xz file you should have a nice fresh .img file in a few minutes.
Once your .img file is decompressed and ready to use open up Win32 Disk Imager and navigate to the .img file you want to
write to your micro SD card. ALERT: Be sure to select the proper drive letter to write to!! If you have any doubts just eject the micro SD card
and take note of which drive letters go away. Some card, especially if they have a bootable OS on them, will mount as two drive letters. It is ok
to simply choose one of them. The screen shot below shows Win32 Disk Imager in action, it is being used to write a different .img
file, just ignore that part.
Once you have everything setup click the Write button at the bottom of the window and let the software do its thing.
Be careful that you click the correct button. It can become confusing at times which direction the read or write operation is going,
especially if you've done a few card in a row using different operations. There will be some hint text that will explain
what the button you're about to click does. I always read it and double check that it is indeed what I want to do.
-- 7: Configuring Your Dev Environment --
Now that we've created a new custom bootable eMMC or micro SD memory module, whichever you chose, we have to update the OS
and install some software before we can begin developing. Boot up the ODROID-N2 with your new memory module and put the Hard Kernel
bootable micro SD card in a safe place.
First let's update the operating system and software. We'll do this two different ways. The first will use the MATE desktop system
tools. Find the Software Updater tool under Menu -> Administration -> Software Updater.
Click the Update button and let the process complete. Sometimes an update may require you to hit a button so it's a good
idea to keep the window visible and also have the details visible. You should see a window like the following while the update is running.
Once that is complete we'll run an update from the command line.
This step is more or less that same thing as what the system update tool is doing but it is good to know how to run an update from
a terminal so let's do it. Open up the terminal like we did earlier in the tutorial and run the following commands. The default superuser
password is odroid. Copy and paste the following command in the terminal window and hit enter.
sudo apt-get update -y; sudo apt-get upgrade -y; sudo apt-get autoremove -y;
Once that's done we'll want to install some software and we can get most of it done right from the terminal. We're going to install
gparted, for partition and drive management, gimp, for image management, default-jre, so we can use the Java runtime
environment, and chromium-browser, to add a Chrome browser to the system. Copy and paste the following command in the terminal window and hit enter.
And also the default-jdk so we can develop some programs in Java.
sudo apt-get install gparted -y; sudo apt-get install gimp -y; sudo apt-get install default-jre -y; sudo apt-get install default-jdk -y; sudo apt-get install chromium-browser -y;
The next piece of software we need to install is a great Java IDE, https://www.netbeans.org. Fire up your browser and navigate to the
netbeans website.
Click the download button and you'll be brought to a screen with a list of versions. Go to the latest stable version and click the download button
as shown below.
This will take you to a page with a few different links for the version of netbeans you selected. We'll want to download a zipped version
of the binary files. Click the binaries link and let the IDE download.
Go to the Downloads folder and copy and paste the zip file
into a new folder named install_zips, create this folder in your home directory. Once the file is done copying
over, unzip it by right-clicking the compressed file and selecting the unzip menu option. Create another new folder in your
home directory named applications. Move the uncompressed version of netbeans into this directory. Open the folder, find the
bin folder, open it, and right-click on the file named netbeans. Choose the pin to desktop option. Now we have a link to open
our netbeans IDE. But wait, it looks, well rough. Let's grab a good looking icon for it. Navigate your browser to
https://commons.wikimedia.org/wiki/File:Apache_NetBeans_Logo.svg.
Save the netbeans icon file from the page that loads up.
Right-click on the desktop shortcut for netbeans. Click on the icon and find the new netbeans icon you just downloaded. Choose it to be
the icon for the desktop shortcut. Now we have a proper looking IDE shortcut. Open up the netbeans IDE and allow the IDE to install
any modules it needs. You may also have to do this when opening a new project for the first time, simply allow the IDE to install the modules it
needs.
-- 8: Setting up a Swap Partition --
In this step we'll turn on a swap partition and setup our ODROID-N2 to always enable swap space on boot. This can help us with the
IDE and other memory needs we'll have when writing games for the ODROID-N2 on the ODROID-N2. First we'll check if we have any swap space
enabled. Run the following command in the MATE Terminal, Menu -> System Tools -> MATE Terminal. You may want to right-click
the menu option and click pin to desktop so that you have a quick shortcut to the terminal when you need it.
sudo swapon --show
If there is no text displayed you don't have swap space enabled. If you do see some information printed then you have swap space enabled and you
can skip the rest of this step.
Create a new swap file on the root of the boot drive by running the next command.
sudo fallocate -l 1G /swapfile
Set the permissions on the swap file to be secure.
sudo chmod 600 /swapfile
Enable the file as a new swap file.
sudo mkswap /swapfile
Turn on the swap file.
sudo swapon /swapfile
To enable the swap file on every boot edit the fstab file and add the following entry, like so.
sudo nano /etc/fstab
Copy and paste the following line at the bottom of the file's current contents.
/swapfile swap swap defaults 0 0
Give the system a reboot by running the following command, sudo shutdown -r now, and bam! We now have
an active swap partition.
When the system comes back up you can check that the swap file was enabled by running, sudo swapon --show.
You should see an entry print out after running the command. If not, go through the process again and make sure you
run the correct commands in order.
-- 9: Mapping Specific GPIO Pins --
In this step we'll use the terminal and our breadboard to figure exactly what number the GPIO pins, as they are expressed on the breadboard, are.
Now sometimes you can check the documentation to see where each pin is, but depending on the OS and how it works you might find yourself
in a situation where the pin numbers aren't matching the documentation. Plus it's fun to hook up an LED to the ODROID-N2 and get the light to blink.
Now you'll have to connect your breakout breadboard to the ODROID-N2. Simply plug the connector onto the GPIO expansion
pins on the board. Make sure it is facing the correct way, the red stripe on the ribbon cable should line up with the side
of the GPIO pins that have labels 1, and 2.
We're going to setup a simple circuit using 1 resistor, 1 LED, and 3s jumpers using the breakout breadboard tinker kit from
Hard Kernel. We're going to setup the following simple circuit. Follow the image and diagram below. Your pin may be different.
I'm using pin number 488 which maps to position 7 on the breadboard.
A simple circuit diagram is as follows. The circuit serves two purposes. One, it allows for the pin voltage to drive the LED.
This is the handled by the first branch of the circuit. Two, it allows for the pin voltage to be driven by a button. This is used
to mimic input from a gamepad.
Follow the images and diagram and setup a similar circuit on your breadboard. Once it's ready we're going to run a script to locate
some pins to work with.
Download the following zip file it has multiple scripts for working with the GPIO pins.
Link: GPIO Scripts
Once the file download completes, decompress the zip file and move the btn_test, btn_prep, and pin_finder scripts
into the home directory. Clean up the download folder by moving all the zip files into the install_zips folder we
created earlier. This will give you a backup of all the files you need for this tutorial.
Next we're going to query the GPIO pin files to get an idea of what numbers our Linux OS has assigned to the pins.
This will give us a range to work with. We're going to be running some tests to figure out which pin is associated with
what GPIO number. Run the following command to see what GPIO pin numbers we're working with.
sudo ls /sys/class/gpio/
You should see certain numbers next to the GPIO chips found. These indicate starting pin numbers and can be an indication of a pin range.
/sys/class/gpio/gpiochip64/
/sys/class/gpio/gpiochip128/
You'll see different numbers but what they indicate is a range of pin numbers from 64 to 128 and possibly from 128 up to some unknown number.
This wil give you an indication of what GPIO numbers to use when you start looking for matching pins.
Now, open a terminal and run the following command from the home directory, nano pin_finder. All the files are setup to work with
pin number 488 by default. This may work for you but you should still complete these steps so you know how to find more of your own pins.
Edit the text at the top of the file, TMP=488, and change the number to one of the numbers in the range you identified previously.
Or you could just move up one to 489 and try that. Write and save the file, Ctrl + O, followed by Ctrl + X. Run the script with the
following command.
sudo ./pin_finder
The script will toggle the high and low value of the target pin. Move connector #1 from pin to pin, waiting for 2 seconds at each spot to see
if the LED flashes. If it does you've found the pin location that matches the Linux GPIO pin number.
When you find one blinking pin write down the position on the breadboard and the pin number used to find it.
Make sure to exit the pin_finder script with Ctrl + C when the LED is off. If you miss the timing just start the script
and try again. Open the btn_test with the command, nano ./btn_test. Edit the line at the top of the file and enter in the
number of the pin you've found. Write the file and exit with Ctrl + O followed by Ctrl + X. Run the btn_test
script like so, sudo ./btn_test.
While the script is running toggle the push button and you should see the output change from 0 to 1 and the LED turn on and off.
The image below shows the terminal output during this process.
-- 10: Setting up a Service for GPIO Permissions --
In this step we'll quickly setup a service in Linux that will open up the GPIO pin you've located to the odroid user so that
we don't have to type sudo every time we want to use it, and so that our Java program can access them also
without being required to run the sudo command.
Edit the btn_prep script using nano as we've done before. Change the number at the top of the script to the GPIO
number you located with the pin_finder script.
Open a terminal and run the following commands to set the permissions for the GPIO button prep files. You should have a file
named btn_prep and custgpiosvc.service in your home directory.
sudo chmod 755 ./btn_prep
sudo +x 755 ./btn_prep
sudo chmod 755 ./custgpiosvc.service
Next we'll copy the btn_prep script to the /usr/bin/ directory. Run the following command in the terminal. Make sure
you are in the home directory. If you need to get back to the home directory run this command, cd ~.
sudo cp ./btn_prep /usr/bin/
Now we'll install the systemd service so that our system will set permissions to the GPIO files so that the odroid user can access
them without running the sudo command. This will make things easy for us to access the pins from Java.
sudo cp custgpiosvc.service /etc/systemd/system && sudo systemctl start custgpiosvc
Check to see if the service is up and running with the following command, systemctl is-active custgpiosvc.
You should see the word, active, as output.
To run the service each time the system boots up you'll have to run, systemctl enable custgpiosvc
Now give the system a reboot, sudo shutdown -r now.
-- 11: Setting Up Projects in Netbeans --
In this step we'll load up the Java project we need into the Netbeans IDE and make some slight configuration changes.
First download the version of the project we'll need for this tutorial. This project will be updated and different as
the tutorial series proceed so expect to download a similar project file in the future if you follow along.
Link: Netbeans IDE Project v0.5.0
The project includes a lot of Java files and we'll go over them in a future tutorial but we're going to focus on a small
project for interacting with the ODROID-N2's GPIO pins. I'll do a more in depth review of code in an upcoming tutorial.
And we'll go over the game API, also in an upcoming tutorial, because we're going to be working with it to build some games!
Unzip the project after it finishes downloading. Copy the original zip file to the install_zips folder.
Move the resulting project directory to the a new folder in your home directory. Name the folder netbeans_projects.
Now fire up netbeans and open the project by clicking the File -> Open Project menu option. Navigate to the netbeans_projects
folder you created and choose the MmgGameApiJava_v0-5-0 folder. The project will load up in netbeans, allow netbeans to install any necessary
modules if it pops up and asks to do so.
We're going to make sure the project has one slight configuration change. Right-click on the project and select the properties option
similar to what is shown below.
Make sure that the Build -> Packaging -> JAR File property is set to the following, dist/MmgGameApiJava.jar
as shown below.
We're all ready to run our Java program and demonstrate Java connection to Linux GPIO pins! This was a long detailed
tutorial to setup our environment. I hope you learned a lot going through it, you've certainly accomplished a ton.
We're going to be using this setup to create some Java games for the ODROID-N2 on the ODROID-N2 and use the GPIO
pins to control the game with a simple game pad of sorts. This tutorial was the first step.
-- 12: Reading GPIO Pins with Java --
In this step we'll tie everything together and read the status of a GPIO pin using Java, very cool. Go to the project you
just loaded in netbeans, MmgGameApiJava. Expand the Source Packages section, select the com.middlemind.OdroidGpio package
and expand it. Select the OdroidGpio.java file and open it.
Change the GPIO pin number next to the new GpioPin lines. There are six of them to handle a 4 direction D-Pad and 2 input buttons.
For now though set the first number, 488, to the GPIO pin number you located yourself with pin_finder. Once that is done right-click
on the MmgGameApiJava project and select clean and build. Open a terminal and navigate to the JAR output directory using the following command.
cd ~/netbeans_projects/MmgGameApiJava_0-5-0/dist
Make sure there is a new MmgGameApiJava.jar file in the directory by running the command, ls -al ./MmgGameApiJava.jar, in the terminal.
Get ready to run the program, while it's running we're going to toggle the LED and see what the Java program does.
Make sure your circuit is setup to work with the pin number you chose, check your notes from the pin_finder step.
When you are ready right-click on the OdroidGpio.java file and select run file from the menu.
While the program is running press the button and hold it for a second or two, you should see ProcessAPress in the output window when the button is pressed
as shown below.
Well that wraps up our first tutorial in this series. We covered a lot and got a lot accomplished.
Reading the status of GPIO pins in Java running on your own personal ODROID-N2 Linux computer.
Not bad at all. This series of tutorials will show you how to write your own game and control it
with a keyboard, mouse, and a GPIO based gamepad. Stay tuned!