CRTC Electronics

Click here to edit subtitle

Chaser

Images

Code

package ioio.crtc.target;

import java.io.IOException;
import java.util.Random;
import ioio.crtc.target.R;
import ioio.lib.api.DigitalInput;
import ioio.lib.api.DigitalInput.Spec.Mode;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIO.VersionType;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.Toast;
import android.widget.ToggleButton;

/**
 * This is the main activity of the HelloIOIO example application.
 *
 * It displays a toggle button on the screen, which enables control of the
 * on-board LED. This example shows a very simple usage of the IOIO, by using
 * the {@link IOIOActivity} class. For a more advanced use case, see the
 * HelloIOIOPower example.
 */
public class MainActivity extends IOIOActivity {
private ToggleButton button_;
private RadioButton   rb1;
private RadioButton   rb2;
private RadioButton   rb3;
private RadioButton   rb4;
private RadioButton   rb5;
private RadioButton   rb6;
private RadioButton   rb7;
private RadioButton   rb8;
private RadioButton   rb9;
private RadioButton  rb10;
private RatingBar     db1;
private ImageButton  ibGo;
public int stage;
public int phase;
boolean hit_  = false;
MediaPlayer introSong;
MediaPlayer   endSong;
MediaPlayer   hitSong;
MediaPlayer  failSong;   
Random random1 = new Random();
public int          randomINT;
int                 round = 1;
private static final int IDLE    = 1;
private static final int INTRO   = 2;
private static final int PLAYING = 3;  
private static final int LIGHT   = 4;
private static final int DARK    = 5;
private static final int END     = 6;
CountDownTimer easyTimer = new CountDownTimer(2000,2000){public void onTick(long millisUntilFinished)
{}public void onFinish() 
{
phase = DARK;
darkTimer.start();
if (!hit_) failSong.start();
}};
CountDownTimer mediumTimer = new CountDownTimer(700,2000){public void onTick(long millisUntilFinished)
{}public void onFinish() 
{
phase = DARK;
darkTimer.start();
if (!hit_) failSong.start(); 
}};
CountDownTimer hardTimer = new CountDownTimer(500,2000){public void onTick(long millisUntilFinished)
{}public void onFinish() 
{
phase = DARK;
darkTimer.start();
if (!hit_) failSong.start(); 
}};
CountDownTimer darkTimer = new CountDownTimer(1000,2000){public void onTick(long millisUntilFinished)
{}public void onFinish() 
{  
db1.getRating();
randomINT = random1.nextInt(4) + 1;
phase = LIGHT;
hit_ = false;
round ++;
if (round > 10)
{
stage = END;
}
else
{
if(db1.getRating() == 1.0)easyTimer.start();
if(db1.getRating() == 2.0)mediumTimer.start();
if(db1.getRating() == 3.0)hardTimer.start();
}    
}};
private void makeEndSong()
{endSong   =  MediaPlayer.create(this,R.raw.end);}
/**
* Called when the activity is first created. Here we normally initialize
* our GUI.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button_    = (ToggleButton)   findViewById(R.id.button);
introSong  =  MediaPlayer.create(this, R.raw.intro);
endSong    =    MediaPlayer.create(this, R.raw.end);
hitSong    =    MediaPlayer.create(this, R.raw.hit);
failSong   =   MediaPlayer.create(this, R.raw.fail);
rb1 = (RadioButton)    findViewById(R.id.radioButton1);
rb2 = (RadioButton)    findViewById(R.id.radioButton2);
rb3 = (RadioButton)    findViewById(R.id.radioButton3);
rb4 = (RadioButton)    findViewById(R.id.radioButton4);
rb5 = (RadioButton)    findViewById(R.id.radioButton5);
rb6 = (RadioButton)    findViewById(R.id.radioButton6);
rb7 = (RadioButton)    findViewById(R.id.radioButton7);
rb8 = (RadioButton)    findViewById(R.id.radioButton8);
rb9 = (RadioButton)    findViewById(R.id.radioButton9);
   rb10 = (RadioButton)   findViewById(R.id.radioButton10);
db1 = (RatingBar)      findViewById(R.id.ratingBar1);
phase = LIGHT;
ibGo    = (ImageButton) findViewById(R.id.ibGo);
ibGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ibGo.setEnabled(false);
introSong.start();
if( endSong.isPlaying())
{endSong.stop();makeEndSong();}
round = 1;
rb1.setChecked(false);
rb2.setChecked(false);
rb3.setChecked(false);
rb4.setChecked(false);
rb5.setChecked(false);
rb6.setChecked(false);
rb7.setChecked(false);
rb8.setChecked(false);
rb9.setChecked(false);
  rb10.setChecked(false);
stage = INTRO; 
}});}
/**
* This is the thread on which all the IOIO activity happens. It will be run
* every time the application is resumed and aborted when it is paused. The
* method setup() will be called right after a connection with the IOIO has
* been established (which might happen several times!). Then, loop() will
* be called repetitively until the IOIO gets disconnected.
*/
class Looper extends BaseIOIOLooper {
/** The on-board LED. */
  private DigitalOutput led_;
private DigitalOutput L1;
private DigitalOutput L2;
private DigitalOutput L3;
private DigitalOutput L4;
private DigitalInput  Sensor1;
private DigitalInput  Sensor2;
private DigitalInput  Sensor3;
private DigitalInput  Sensor4;
/**
* Called every time a connection with IOIO has been established.
* Typically used to open pins.
*
* @throws ConnectionLostException
*             When IOIO connection is lost.
*
* @see ioio.lib.util.IOIOLooper#setup()
*/
@Override
protected void setup() throws ConnectionLostException {
showVersions(ioio_, "IOIO connected!");
 led_ = ioio_.openDigitalOutput(0, true);
L1 = ioio_.openDigitalOutput(35, false);
L2 = ioio_.openDigitalOutput(36, false);
L3 = ioio_.openDigitalOutput(37, false);
L4 = ioio_.openDigitalOutput(38, false);
Sensor1 = ioio_.openDigitalInput(40,Mode.PULL_DOWN);
Sensor2 = ioio_.openDigitalInput(41,Mode.PULL_DOWN);
Sensor3 = ioio_.openDigitalInput(42,Mode.PULL_DOWN);
Sensor4 = ioio_.openDigitalInput(43,Mode.PULL_DOWN);
enableUi(true);
}

/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException
*             When IOIO connection is lost.
* @throws InterruptedException
* When the IOIO thread has been interrupted.
*
* @see ioio.lib.util.IOIOLooper#loop()
*/
@Override
public void loop() throws ConnectionLostException, InterruptedException {
led_.write(!button_.isChecked());
switch(stage)
{
case IDLE:
//wait for start of game
//goes to intro when button is pressed
runOnUiThread(new Runnable() {
@Override
public void run() {ibGo.setEnabled(true);db1.setEnabled(true);}});
break;
case INTRO:
//flash lights one color then play a sound
if (!introSong.isPlaying())
{
stage = PLAYING;
if(db1.getRating() == 1.0)easyTimer.start();
if(db1.getRating() == 2.0)mediumTimer.start();
if(db1.getRating() == 3.0)hardTimer.start();
randomINT = random1.nextInt(4) + 1;
}
break;
case PLAYING:
//select light turn on for 1 second then repeat ten times
//turn on light then check for hit              
switch(phase)   
{   
case LIGHT:  
boolean button1 = Sensor1.read();
boolean button2 = Sensor2.read();
boolean button3 = Sensor3.read();
boolean button4 = Sensor4.read();
Thread.sleep(10);
if (button1 && Sensor1.read()) button1 = true; else button1 = false;
if (button2 && Sensor2.read()) button2 = true; else button2 = false;
if (button3 && Sensor3.read()) button3 = true; else button3 = false;
if (button4 && Sensor4.read()) button4 = true; else button4 = false;
if (randomINT == 1) L1.write(true);
if (randomINT == 2) L2.write(true);
if (randomINT == 3) L3.write(true);
if (randomINT == 4) L4.write(true);
if (button1|| button2|| button3|| button4)
{
hit_ = true;
if (button1&& randomINT == 1)
{
hitSong.start();
phase = DARK;
score(round, true);
}
else if (button2&& randomINT == 2)
{
hitSong.start();
phase = DARK;
score(round, true);
}
else if (button3 && randomINT == 3)
{
hitSong.start();
phase = DARK;
score(round, true);
}
else if (button4 && randomINT == 4)
{
hitSong.start();
phase = DARK;
score(round, true);
}
else 
{    
score(round, false);
failSong.start();
runOnUiThread(new Runnable() {
@Override
public void run(){db1.setEnabled(false); }}
 );
phase = DARK;
}
}
break;
case DARK:
L1.write(false);
L2.write(false);
L3.write(false);
L4.write(false);
runOnUiThread(new Runnable() {
@Override
public void run() {db1.setEnabled(false);}}
);
break;
}
break;
case END: 
//flash lights  play gameover sound reset enabled button
endSong.start();
{
L1.write(false);
L2.write(false);
L3.write(false);
L4.write(false);
stage = IDLE;
}   
break;
};
};
private void score(final int score_round, final boolean score_hit) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable(){
@Override
public void run(){
if (score_hit){
if (score_round == 1) rb1.setChecked(true);
if (score_round == 2) rb2.setChecked(true);
if (score_round == 3) rb3.setChecked(true);
if (score_round == 4) rb4.setChecked(true);
if (score_round == 5) rb5.setChecked(true);
if (score_round == 6) rb6.setChecked(true);
if (score_round == 7) rb7.setChecked(true);
if (score_round == 8) rb8.setChecked(true);
if (score_round == 9) rb9.setChecked(true);
if (score_round == 10)rb10.setChecked(true);
}else{
if (score_round == 1) rb1.setChecked(false);
if (score_round == 2) rb2.setChecked(false);
if (score_round == 3) rb3.setChecked(false);
if (score_round == 4) rb4.setChecked(false);
if (score_round == 5) rb5.setChecked(false);
if (score_round == 6) rb6.setChecked(false);
if (score_round == 7) rb7.setChecked(false);
if (score_round == 8) rb8.setChecked(false);
if (score_round == 9) rb9.setChecked(false);
if (score_round == 10)rb10.setChecked(false);
}
}});
}

/**
* Called when the IOIO is disconnected.
*
* @see ioio.lib.util.IOIOLooper#disconnected()
*/
@Override
public void disconnected() {
enableUi(false);
toast("IOIO disconnected");
}
/**
* Called when the IOIO is connected, but has an incompatible firmware version.
*
* @see ioio.lib.util.IOIOLooper#incompatible(IOIO)
*/
@Override
public void incompatible() {
showVersions(ioio_, "Incompatible firmware version!");
}
}
/**
* A method to create our IOIO thread.
*
* @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
*/
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
private void showVersions(IOIO ioio, String title) {
toast(String.format("%s\n" +
"IOIOLib: %s\n" +
"Application firmware: %s\n" +
"Bootloader firmware: %s\n" +
"Hardware: %s",
title,
ioio.getImplVersion(VersionType.IOIOLIB_VER),
ioio.getImplVersion(VersionType.APP_FIRMWARE_VER),
ioio.getImplVersion(VersionType.BOOTLOADER_VER),
ioio.getImplVersion(VersionType.HARDWARE_VER)));
}

private void toast(final String message) {
final Context context = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
});
}

private int numConnected_ = 0;

private void enableUi(final boolean enable) {
// This is slightly trickier than expected to support a multi-IOIO use-case.
runOnUiThread(new Runnable() {
@Override
public void run() {
if (enable) {
if (numConnected_++ == 0) {
button_.setEnabled(true);
}
} else {
if (--numConnected_ == 0) {
button_.setEnabled(false);
}
}
}
});
}
}