So, it was all plain sailing getting the Pi up and running with NOOBS following the instructions there, and quickly got it all setup with wifi connectivity and ssh access (which is essential for me, as I don't have a HDMI monitor other than my TV in the living room - so until I could get ssh access from my normal workstation it meant having to take over the living room to configure the Pi on the TV, which my wife was not best pleased about! Also a much cheaper option than buying a new HDMI monitor just for the Pi).
I had been looking around for how to get started with some hardware hacking with the Pi, and must admit the last time I did anything vaguely electronics was when I was in school at 14yrs old (actually I did a robotics module in uni, but that was still mostly programming stuff rather than actual chips/resistors/wires etc). Some of blogs were writing about how great/essential the Adafruit Pi Cobbler was, and again, I had to go and google and read up exactly what a breakout "cobbler" would be useful for etc but decided that enough people were going for it to invest
(So yeah, that's the Amazon affiliates link - it means I get some money from amazon if you buy this thing from here - it doesn't cost you anything, but is nice for me, so by all means click & buy!)
That lead me on to looking more extensivley at the adafruit site, which has some nice tutorials and all recommend using the Occidentalis OS if you are planning some hardware hacking.
Setting up Occidentalis OS
Occidentalis doesn't come in as nice a package as NOOBS - You basically need to get the image, flash it to an SD card, then boot up your Pi. I'm on Windows 7 setting up the SD card, so here are the steps/software I used:
- SD Formatter (from the SD association - this is one of the standard tools recommended in normal Pi setup tutorials and is a pretty convenient way to format SD cards) - https://www.sdcard.org/downloads/formatter_4/
- Fedora ARM installer - This can be used to install OS images on to an SD card - you can't just copy & paste downloaded images on to an SD card and this just makes it pretty easy - https://fedoraproject.org/wiki/Fedora_ARM_Installer
- Occidentalis distro image - This is Adafruit's mod of the Raspian Wheezy OS and is modified specifically with hardware hacking in mind - The latest version is currently at v0.2 - http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2
Getting the Occidentalis image and flashing it to an SD card is pretty straight forward if you have the above software - the steps are as follows:
- Format an SD card - it really needs to be 4gb - I am using an 8gb samsung card that is working fine (disclaimer: as with any formatting or writing images to drives - make sure you are targeting the correct drive for your appropriate SD card - as all data will be wiped!)
- Assuming you have downloaded the Occidentalis image from the above link, extract the zip file locally
- Right-click and select "Run as administrator" on the Fedora ARM installer executable. For the source option, select "Browse" and navigate to the .img file you have just extracted from the zip. For the destination triple-check that you have your SD card drive selected (again, this drive will be wiped and all data overwritten when you hit the install button). Then press install. This will correctly setup the SD card with the OS image.
- Jam your newly setup SD card in your Raspberry Pi and startup as usual. For most of the boiler plate setup, this will work just like Wheezy/NOOBS setup.
Uh.. Wait.. My Pi isn't Booting..
So yeah. This happened to me. I setup the SD card and jammed it into my Pi and nada. The red power light flickered on, but no signs of activity and no output on the HDMI. I did some research, and it turns out that there is a problem with some OS booting if they are using the Hyinx/Samsung RAM chip on the newer models of the Pi. However, my Pi used neither of those RAM chips, mine was fitted with the Micron RAM chip, but still suffered the same problem.
Thankfully with a little tinkering, the same solution generally seemed to resolve this problem for me. If you are experiencing this problem, here is what fixed it for me:
- Download and flash and standard OS distrubution to the SD card - I just grabbed the standard Wheezy distro from the Pi download site and flashed that to my SD card as per above instructions
- Once on the SD card, I backed up the files written (only ~35mb) locally
- I then installed the Occidentalis image to my SD card (the image that wasn't booting previously, again using the steps above)
- I then copied the bootloader files from Wheezy backup on to my SD card - this was just overwriting the bootcode.bin and start.elf files from my SD card with those I had backed up from the standard Wheezy distro.
- Jam the SD card in and power up..
So.. I'm off. I will now get the normal wifi stuff setup, plus set up the ssh keys for my github account so I can stick all my code on there (I'm not relying on some flaky SD card for my code backups), the next Pi post will likely be about the Cobbler and basic stuff connecting the Pi to a bread board and a few LEDs, maybe a motor or two.. but the first step for that is soldering my Adafruit Cobbler.
Yep. I'm gonna have to use my hands..
About a year ago I released my first app on to the Android market. I had developed the market primarily as a learning tool, and at the end decided that putting it on the market would be a good step to understand the process and get some feedback.
The application was a simple quiz app based on the popular NBC series Chuck, I released it for free (no-ads) and no real advertising other than one or two target tweets. The app currently has about 4,000 – 5,000 downloads with a 4 star rating – both of which it achieved pretty quickly in the first month or two.
DISCLAIMER: I believe this to be purely down to the fact that at the time there weren’t really any other Chuck related apps, and it has a nerd-ish, cult like following, so I think the rating and popularity is more of a reflection on the show’s popularity than the quality of the app!
Anyway, I thought it might be interesting/helpful to release the source code here and walkthrough the different aspects of the code in a tutorial “my first app” kind of way.
I won’t go in to the setting up Eclipse/Android plug-ins stuff here, so will assume familiarity with getting around the IDE.
Android Directory Structure
Creating a new Android project in Eclipse (using the Android plug-ins) creates the required project structure automatically for you. Lets have a look at that now:
Src : as you would expect, this is the main java source directory, so this is where all your source code lives
Gen: this is the generated code that Eclipse handles for you – the files in here will be generated from your config. You needn’t really worry about what happens in here as Eclipse takes care of it for you
Assets: this can be used to contain any resources or libraries you want to bundle with the app – such as third party jars and data sets (you will see later we use this directory to store the data that we will load to populate all the questions in the DB when the app is first launched on a new device)
Res: this is where the configuration for the layout/UI aspects live:
- Drawable – keep you image files in here (.png/jpg/etc)
- Layout – this is where you keep the xml config for UI layout
- Values – this is where you keep the config with application code, such as application name or set text. This means you can easily change text through out the application without having to change hardcoded strings in the java code and re-compile
Getting Started
Ok, lets get started – the first thing we need is a welcome screen that is displayed when a user first launches the app. In Android, a screen is modelled by an “Activity” class, so to create our welcome screen we need to extend Activity.
public class SplashActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
Every class that overrides the Activity class must implement the onCreate() method – this is called when the activity is launched. As our welcome screen will just be a simple screen with a few buttons on it (play, rules, exit) we don’t need to do much at this point other than set up the buttons to have a listener so we know when one of them is pressed. For convenience here, we will set our current activity class to implement “OnClickListener” – this means we can define the onClick() method directly in our activity to handle the button events.
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.playBtn :
List questions = getQuestionSetFromDb();
GamePlay c = new GamePlay();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((ChuckApplication)getApplication()).setCurrentGame(c);
//Start Game Now.. //
i = new Intent(this, QuestionActivity.class);
startActivityForResult(i, Constants.PLAYBUTTON);
break;
case R.id.rulesBtn :
i = new Intent(this, RulesActivity.class);
startActivityForResult(i, Constants.RULESBUTTON);
break;
case R.id.settingsBtn :
i = new Intent(this, SettingsActivity.class);
startActivityForResult(i, Constants.SETTINGSBUTTON);
break;
case R.id.exitBtn :
finish();
break;
}
}
As you can see, we have one single method that handles any button press, and we manage this with a switch statement checking the id of the view (we will go into this later). Mostly, the cases are simple, “exit” finishes the application whilst “rules” and “settings” have code like this:
i = new Intent(this, RulesActivity.class);
startActivityForResult(i, Constants.RULESBUTTON);
Intents are objects in Android that are used to communicate between activities, and as you can probably spot from the above, this simply creates a new Intent and then uses it to start another Activity class, in this case, our RulesActivity class.
The “Play” case is slightly more complicated – as this needs to initialise the game and load the questions to be used from the DB. Once it has done this, it loads the information in to our “Application” class (we will cover this later also) and then just kicks of the QuestionsActivity with a new Intent as per above.
So that is our welcome page pretty much done, we have a single screen that has a few buttons and we have setup listeners to perform appropriate actions on pressing them. The only remaining issue is the layout of the screen.
Activity Design
In the onCreate() method of our Activity you will notice the second line is
setContentView(R.layout.welcome);
This call tells Android which layout xml file should be used to configure the design – to find the relevant file is quite intuitive, its simply going to be located at /layout/welcome.xml
There are several layouts available to use, each of which are applicable in different circumstances, but as we are just going to have a straight list of buttons, we will use the simple “LinearLayout”. You can nest layouts, as you will see in this example, but lets walk through some of the config options we are using for our layout:
android:orientation="vertical" – This defines our layout as vertical, so new elements will be added on a new row below (if it were horizontal it would add as a new column)
android:layout_width="fill_parent" – this tells the activity to fill the whole width of the screen with the contents
android:layout_height="fill_parent" – this tells the activity to fill the whole width of the screen with the contents
android:gravity="center_horizontal" – this centers all elements that are contained in this layout
android:background="@drawable/background – this defines a background image to use for the activity (this image should exist at /drawable/background.png)
Now, if we look inside the layout you will see some simple components. The first one being a simple ImageView, which points to an image that we want to use as a header. The following elements are the buttons – you will notice that the ID of each button corresponds to the onCreate() method of our Activity where we inflate the button to set the onClickListener.
If you press the “Graphical Layout” tab in eclipse, you will see what the welcome screen will look like.
Ok, so we have our welcome Activty, we have designed a nice UI to control the app and have an onClick handler - the next part will cover intergrating with the DB to pull out the questions and then we are all there!
Grab complete source code for the project here