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.
main.cpp
- Committer:
- el15s3p
- Date:
- 2016-04-27
- Revision:
- 2:9e791f33c49f
- Parent:
- 1:9d69901e18d0
File content as of revision 2:9e791f33c49f:
#include "mbed.h"
#include "N5110.h"
#define DIRECTION_TOLERANCE 0.05
// VCC, SCE, RST, D/C, MOSI, SCLK, LED
N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
// connections for joystick
DigitalIn button(PTB11);
AnalogIn xPot(PTB2);
AnalogIn yPot(PTB3);
AnalogIn Pot1(PTB10);
BusOut leds(LED1,LED2);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
InterruptIn sw2 (SW2);
DigitalIn buttonA(PTB18);
DigitalIn buttonB(PTB19);
PwmOut buzzer(PTA2);
// timer to regularly read the joystick
Ticker pollJoystick;
enum DirectionName {
UP,
DOWN,
LEFT,
RIGHT,
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();
int menuSelect = 2;
int main()
{
while (1) {
calibrateJoystick(); // get centred values of joystick
pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
lcd.init();
lcd.clear();
lcd.normalMode();
lcd.printString("Comet Crusher!",0,0);
lcd.printString("Start",10,2);
lcd.printString("Instructions",10,3);
lcd.printString("Difficulty",10,4);
lcd.printString("Scores",10,5);
lcd.printString(">",5,menuSelect);
lcd.refresh();
sleep();
switch (menuSelect) {
case 2:
if (joystick.direction == DOWN) {
menuSelect = 3;
lcd.refresh();
wait (0.5);
sleep();
} else if (joystick.direction == UP) {
menuSelect = 5;
lcd.refresh();
wait (0.5);
sleep();
}
break;
case 3:
if (joystick.direction == DOWN) {
menuSelect = 4;
lcd.refresh();
wait (0.5);
sleep();
} else if (joystick.direction == UP) {
menuSelect = 2;
lcd.refresh();
wait (0.5);
sleep();
}
break;
case 4:
if (joystick.direction == DOWN) {
menuSelect = 5;
lcd.refresh();
wait (0.5);
sleep();
} else if (joystick.direction == UP) {
menuSelect = 3;
lcd.refresh();
wait (0.5);
sleep();
}
break;
case 5:
if (joystick.direction == DOWN) {
menuSelect = 2;
lcd.refresh();
wait (0.5);
sleep();
} else if (joystick.direction == UP) {
menuSelect = 4;
lcd.refresh();
wait (0.5);
sleep();
}
break;
}
}
}
void game()
{
}
void difficultySet()
{
}
void highScore()
{
}
void instructions()
{
}
void calibrateJoystick()
{
button.mode(PullDown);
// 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 = DOWN;
} else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
joystick.direction = UP;
} 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 {
joystick.direction = UNKNOWN;
}
// set flag for printing
printFlag = 1;
}