Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PowerControl mbed
Revision 0:4686e853d105, committed 2015-05-12
- Comitter:
- j_ogus
- Date:
- Tue May 12 22:05:06 2015 +0000
- Commit message:
- test
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/N5110.lib Tue May 12 22:05:06 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/j_ogus/code/N5110/#80bdef8bfd48
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PowerControl.lib Tue May 12 22:05:06 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/JST2011/code/PowerControl/#d0fa2aeb02a4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue May 12 22:05:06 2015 +0000
@@ -0,0 +1,374 @@
+
+
+#include "main.h"
+/**
+* @file main.cpp
+*
+*
+* @brief code information
+* The code is written with numerous start and end positions of code class.
+* This made debugging much easier and will be used as a standard of practice
+* for future work.
+*
+* @author Jeremy Ogus
+*
+* @version 1.2.2
+*
+* @date 10/05/2015
+*
+*
+*v1.0 - broken code in separate loops
+*v1.1 - main game in working fashion, no bugs
+*v1.2 - power saving and multi directional joystick included, 8 way
+*v1.2.1 - pause system now flawless
+*v1.2.2 - harder game, borders more efficient
+*
+*____________________________________________________Definitions and inputs START_____________________________________________________________________
+*/
+
+/*
+*___________________________________________________Pause game interrupt END______________________________________________________________________
+*
+*
+*
+*______________________________________________________Main code loop START_____________________________________________________________________
+*/
+
+int main(){
+
+//turns off ethernet ports
+PHY_PowerDown();
+
+
+//pause button interrupt
+buttonB.fall(&enable);
+
+
+
+/*
+*_______________________________________________________Intro sequence START_____________________________________________________________________
+*/
+
+ wait(0.3);
+ led1 = 1; //oount up LED sequence
+ wait(0.2);
+ led2 = 1;
+ wait(0.2);
+ led3 = 1;
+ wait(0.2);
+ led4 = 1;
+ wait(0.5);
+
+ led1 = 0;//LED flashing
+ led2 = 0;
+ led3 = 0;
+ led4 = 0;
+ wait(0.1);
+ led1 = 1;
+ led2 = 1;
+ led3 = 1;
+ led4 = 1;
+ wait(0.1);
+ led1 = 0;
+ led2 = 0;
+ led3 = 0;
+ led4 = 0;
+ wait(0.1);
+ led1 = 1;
+ led2 = 1;
+ led3 = 1;
+ led4 = 1;
+ wait(0.1);
+ led1 = 0;
+ led2 = 0;
+ led3 = 0;
+ led4 = 0;
+
+
+
+ //LCD initialisation + power
+ lcd.init();
+ void clearBuffer();
+buzzer.period(0.05); //pwm period
+buzzer = 0.8; //duty cyle
+
+wait(0.2);
+buzzer.period(0.05);
+buzzer = 0;
+
+ wait(0.1);
+ lcd.normalMode(); // normal colour mode
+ lcd.setBrightness(1); // put LED backlight on 100%
+ lcd.printString("Let's Play..",8,3); //intro
+ wait(1.25);
+
+ lcd.clear(); //clear current display
+
+ lcd.inverseMode(); //white on black mode
+ lcd.printString("METEOR",26,2); //game title
+ lcd.printString("DODGER",26,4);
+ wait(2.25);
+
+ lcd.clear(); //clear display again
+ lcd.normalMode();
+
+/*
+*______________________________________________Replay cuts into code here => getTo theChopper;___________________________________________
+*/
+
+ theChopper:
+ wait(0.01);
+
+
+ buttonA.mode(PullUp);
+ buttonB.mode(PullUp);
+
+
+ lcd.printString("Press button A",0,2);
+ lcd.printString("to start!",16,4);
+
+
+/*____________________________________________________Intro sequence END_____________________________________________________________________
+*
+*
+*
+*____________________________________________________Game loop START_________________________________________________________________________
+*/
+
+//wait until A is pushed
+
+while(buttonA==1){
+ }
+
+ lcd.clear();
+
+
+ // character spawn
+ pxpos = 42;
+ pypos = 32;
+ lcd.drawCircle(pxpos,pypos,5,0);
+ lcd.refresh();
+
+
+ calibrateJoystick(); // get centred values of joystick
+
+ pollJoystick.attach(&updateJoystick,1.0/20.0); // read joystick 20 times per second
+
+//Border rules START
+ while(1){
+
+ for(int i=0;i<=47;i++){
+ lcd.setPixel(0,i);
+ lcd.setPixel(83,i);
+ }
+ for(int j=0;j<=83;j++){
+ lcd.setPixel(j,0);
+ lcd.setPixel(j,47);
+ }
+
+ lcd.refresh();
+
+//Border rules END
+
+ if (printFlag) { // if flag set, clear flag and print joystick values to serial port
+ printFlag = 0;
+/*
+* middle "if" loop enables border rules, only move if character pixels are outside the border limit
+* main if loop moves position 2 pixels in relevant direction
+*/
+
+ // check joystick direction
+ if (joystick.direction == UP)
+ {if(pypos>7){
+ pypos = pypos-2;}}
+
+ if (joystick.direction == DOWN)
+ {if(pypos<40){
+ pypos = pypos+2;}}
+
+ if (joystick.direction == LEFT)
+ {if(pxpos>7){
+ pxpos = pxpos-2;}}
+
+ if (joystick.direction == RIGHT)
+ {if(pxpos<76){
+ pxpos = pxpos+2;}}
+
+ if (joystick.direction == UP_RIGHT)
+ {if((pypos>7)&&(pxpos<76)){
+ pxpos = pxpos+2;
+ pypos = pypos-2;}}
+
+ if (joystick.direction == UP_LEFT)
+ {if((pxpos>7)&&(pypos>7)){
+ pxpos = pxpos-2;
+ pypos = pypos-2;}}
+
+ if (joystick.direction == DOWN_RIGHT)
+ {if((pxpos<76)&&(pypos<40)){
+ pxpos = pxpos+2;
+ pypos = pypos+2;}}
+
+ if (joystick.direction == DOWN_LEFT)
+ {if((pxpos>7)&&(pypos<40)){
+ pxpos = pxpos-2;
+ pypos = pypos+2;}}
+
+/*
+UNNEEDED FOR CURRENT VERSION
+ if (joystick.direction == CENTRE)
+ {}
+
+ if (joystick.direction == UNKNOWN)
+
+ {}
+*/
+
+ }
+
+ //user drawing
+ lcd.drawCircle(pxpos,pypos,5,0); //USER
+
+ lcd.refresh();
+
+
+/*
+*____________________________________________________Hostile AI START_____________________________________________________________________
+*/
+
+
+ if(meteorstart ==1){
+
+ meteorstart = 0;
+ srand(time(NULL));
+
+ for(int q=0;q<=4;q++){ //do for all 5 AI
+
+ mxpos[q]=(rand()%83); //random generation
+
+
+ mypos[q]=0;
+ lcd.drawCircle(mxpos[q],mypos[q],2,1); // x,y,radius,black fill
+
+
+ }
+ }
+ else{
+
+ for(int q=0;q<=4;q++){ //for all 5 AI
+
+ if (mypos[q] <=45){ //continue downward, if less than 45
+
+ mypos[q]=mypos[q]+3; //drop speed
+ lcd.drawCircle(mxpos[q],mypos[q],2,1); // x,y,radius,black fill
+ }
+ else{
+ meteorstart = 1;
+ }
+
+
+ }
+ }
+
+
+ for(int k = 0; k<=4;k++){ //if collision has occured
+ if((abs(pxpos - mxpos[k])<7)){
+ if((abs(pypos - mypos[k]))<7){
+ magchange = 1;
+ }
+ }
+
+ }
+
+ if(magchange == 1){
+ break;
+
+ }
+
+
+
+
+ lcd.refresh();
+ wait(0.1);//wait before spawn MUST be less than 0.1, affects refresh rate of anim + movement
+ lcd.clear();
+
+
+
+//____________________________________________________Hostile AI END_____________________________________________________________________
+}
+
+
+/*
+ESSENTIAL!
+following two statements enable initial code states, allowing the code to
+execute game loop properly
+*/
+ meteorstart = 1;
+ magchange = 0;
+
+
+
+ lcd.clear();
+ lcd.printString("GAME OVER!",14,2);
+
+buzzer.period(0.05); //pwm period
+buzzer = 0.8; //duty cyle
+
+
+ wait(0.5);
+ lcd.inverseMode();
+ wait(0.25);
+ lcd.normalMode();
+
+buzzer.period(0.05);
+buzzer = 0;
+
+ wait(0.25);
+ lcd.inverseMode();
+ wait(0.25);
+ lcd.normalMode();
+ wait(0.25);
+ lcd.inverseMode();
+ wait(0.25);
+ lcd.normalMode();
+ wait(0.5);
+ lcd.clear();
+ wait(0.1);
+
+
+
+//inactivity timer, put to sleep if not replaying within 15 seconds
+
+ goneFishing.start();
+
+ lcd.printString("Press button A",0,2);
+ lcd.printString("to replay..",10,4);
+
+ lcd.refresh();
+
+
+ while(buttonA==1){
+
+ if(goneFishing.read()>10){
+ lcd.setBrightness(0.25);
+ }
+
+ if(goneFishing.read()> 11.25){
+ DeepPowerDown();
+ }
+ }
+
+ goneFishing.stop();
+ goneFishing.reset();
+
+
+ lcd.clear();
+ lcd.setBrightness(1);
+ getTo theChopper;
+
+
+//____________________________________________________Game loop END_____________________________________________________________________
+
+
+}
+//____________________________________________________Main code loop END_____________________________________________________________________
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h Tue May 12 22:05:06 2015 +0000
@@ -0,0 +1,247 @@
+/**
+*@file main.h
+*
+*@brief Library for enabling the main.cpp of the METEOR DODGER game to work.
+*@brief So that the code will work, the definitions, variables, parameters.
+*@brief and other libraries will need to be included and stated.
+*
+*@date 10/05/2015
+*
+*/
+
+
+#ifndef MAIN_H
+#define MAIN_H
+
+/**
+*@brief List of require libraries that enable lines of code to function.
+*@brief Some lines of code have words that execute integers of other functions.
+*@brief These are substituted in the definitions section.
+*@author Jeremy Ogus
+*@date 10/05/2015
+*/
+
+//libraries included
+#include "mbed.h"
+#include "N5110.h"
+#include "PowerControl/PowerControl.h"
+#include "PowerControl/EthernetPowerControl.h"
+
+//stated definitions
+#define DIRECTION_TOLERANCE 0.15
+#define getTo goto
+#define zipTo goto
+
+/**
+*@namespace lcd
+*@brief output pins for the LCD displace.
+*/
+//Nokia LCD inputs, VCC,SCE,RST,D/C,MOSI,SCLK,LED
+N5110 lcd(p7,p8,p9,p10,p11,p13,p21);
+
+/**
+*@namespace leds, led1, led3, led4
+*@brief internal mbed leds used in the intro to signify powerup.
+*/
+//LED inputs on mbed
+BusOut leds(LED4, LED3, LED2, LED1);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+/**
+*@namespace buttonA, buttonB
+*@brief inputs for the two push buttons.
+*/
+//push buttons A & B inputs
+DigitalIn buttonA(p16);
+InterruptIn buttonB(p17);
+
+/**
+*@namespace button, xPot, yPot
+*@brief inputs to enable the joystick to funciton.
+*/
+//joystick inputs
+DigitalIn button(p18);
+AnalogIn xPot(p19);
+AnalogIn yPot(p20);
+
+/**
+*@namespace buzzer
+*@brief input on PWM for the buzzer.
+*/
+//buzzer input
+PwmOut buzzer(p22);
+
+/**
+*@param meteorstart - interger to check state whilst spawning the AI.
+*@param magchange - interger to check whether to break out of game loop when the charcer
+*@param pxpos - interger to control position for both the character and AI in the horizontal direction
+*@param pypos - interger to control position for both the character and AI in the vertical direction
+*@param mxpos - interger to create array to limit hostile AI on display
+*@param mypos - interger to create array to limit hostile AI on display
+*@param goneFishing - timer to turn enter deep power down 10 seconds after game end, with no user input to replay
+*@param enable - loop for pause system, interrupt by button B
+*/
+
+//intergers for AI loop system
+int meteorstart = 1;
+int magchange;
+
+//integers for user position
+int pxpos;
+int pypos;
+
+// int for AI array
+int mxpos[5], mypos[5];
+
+
+Timer goneFishing;
+
+void enable(void);
+
+
+//int joyCont(); RIP, was used in an earlier version. still here for personal reference
+
+
+/*
+*____________________________________________________Definitions and inputs END_____________________________________________________________________
+*
+*
+*
+*____________________________________________________Joystick (variables, definition, sensor) START___________________________________________________
+*/
+
+/**
+*@param pollJosytick - timer to constantly read joystick sensor 20 times/second
+*@param DirectionName - ironically, defines direction names when listed below in the if direction loop
+*@brief the remaining code is self explanatory, reading it will tell you what it does, however trivial it seems
+*/
+
+// timer to regularly read the joystick
+Ticker pollJoystick;
+
+
+// create enumerated type (0,1,2,3 etc. for direction)
+enum DirectionName {
+ UP,
+ DOWN,
+ LEFT,
+ RIGHT,
+ UP_RIGHT,
+ UP_LEFT,
+ DOWN_RIGHT,
+ DOWN_LEFT,
+ CENTRE,
+ UNKNOWN
+};
+
+// struct for Joystick
+typedef struct JoyStick Joystick;
+struct JoyStick {
+ float x; // current x value
+ float x0; // 'centred' x value
+ float y; // current y value
+ float y0; // 'centred' y value
+ int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
+ DirectionName direction; // current direction
+};
+// create struct variable
+Joystick joystick;
+
+int printFlag = 0;
+
+// function prototypes
+void calibrateJoystick();
+void updateJoystick();
+
+
+// read default positions of the joystick to calibrate later readings
+void calibrateJoystick()
+{
+ button.mode(PullUp);
+ // must not move during calibration
+ joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
+ joystick.y0 = yPot;
+}
+void updateJoystick()
+{
+ // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
+ joystick.x = xPot - joystick.x0;
+ joystick.y = yPot - joystick.y0;
+ // read button state
+ joystick.button = button;
+
+ // calculate direction depending on x,y values
+ // tolerance allows a little lee-way in case joystick not exactly in the stated direction
+ if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+ joystick.direction = CENTRE;
+ }
+ else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+ joystick.direction = UP;
+ }
+ else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+ joystick.direction = DOWN;
+ }
+ else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+ joystick.direction = LEFT;
+ }
+ else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+ joystick.direction = RIGHT;
+ }
+ else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y > DIRECTION_TOLERANCE) {
+ joystick.direction = UP_RIGHT;
+ }
+ else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y > DIRECTION_TOLERANCE) {
+ joystick.direction = UP_LEFT;
+ }
+ else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
+ joystick.direction = DOWN_RIGHT;
+ }
+ else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
+ joystick.direction = DOWN_LEFT;
+ }
+
+ else {
+ joystick.direction = UNKNOWN;
+ }
+
+ // set flag for printing
+ printFlag = 1;
+}
+
+/*
+*____________________________________________________Joystick (variables, definition, sensor) END___________________________________________________
+*
+*
+*____________________________________________________Pause game interrupt START______________________________________________________________________
+*/
+
+/**
+*@brief this section of code is a pause section.
+*@brief when B is pushed, code is paused.
+*@brief when B is pushed again, code is resumed.
+*@author Jeremy Ogus
+*@date 10/05/2015
+*@param buttonB - Push button, can have a state of either 0 or 1
+*/
+
+//when enabled, short delay. This is so you don't switch the state
+//of the button by the time code has a chance to loop. Should take
+//less than 0.2 seconds for the user to press buttonB and let go.
+void enable(){
+ wait(0.2);
+ //if buttonB is pushed, wait 0.5s. This is for release time, as above
+ if(buttonB==1){
+ wait(0.5);
+ //wait forever until buttonB is pushed again
+ while(buttonB==1){
+ }
+ wait(0.2); //timer for release of the button to resume
+
+ }
+
+}
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue May 12 22:05:06 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8ab26030e058 \ No newline at end of file