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@1:9d69901e18d0, 2016-04-27 (annotated)
- Committer:
- el15s3p
- Date:
- Wed Apr 27 22:27:56 2016 +0000
- Revision:
- 1:9d69901e18d0
- Parent:
- 0:d3a050703801
- Child:
- 2:9e791f33c49f
semi-working menu with moving cursor, still very glitchy. To do: game, instructions, difficulty, scores
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el15s3p | 0:d3a050703801 | 1 | #include "mbed.h" |
| el15s3p | 0:d3a050703801 | 2 | #include "N5110.h" |
| el15s3p | 0:d3a050703801 | 3 | |
| el15s3p | 0:d3a050703801 | 4 | #define DIRECTION_TOLERANCE 0.05 |
| el15s3p | 0:d3a050703801 | 5 | |
| el15s3p | 0:d3a050703801 | 6 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
| el15s3p | 0:d3a050703801 | 7 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
| el15s3p | 0:d3a050703801 | 8 | |
| el15s3p | 0:d3a050703801 | 9 | // connections for joystick |
| el15s3p | 1:9d69901e18d0 | 10 | DigitalIn button(PTB11); |
| el15s3p | 0:d3a050703801 | 11 | AnalogIn xPot(PTB2); |
| el15s3p | 0:d3a050703801 | 12 | AnalogIn yPot(PTB3); |
| el15s3p | 0:d3a050703801 | 13 | |
| el15s3p | 0:d3a050703801 | 14 | AnalogIn Pot1(PTB10); |
| el15s3p | 0:d3a050703801 | 15 | |
| el15s3p | 0:d3a050703801 | 16 | BusOut leds(LED1,LED2); |
| el15s3p | 0:d3a050703801 | 17 | DigitalOut led1(LED1); |
| el15s3p | 0:d3a050703801 | 18 | DigitalOut led2(LED2); |
| el15s3p | 0:d3a050703801 | 19 | |
| el15s3p | 0:d3a050703801 | 20 | |
| el15s3p | 0:d3a050703801 | 21 | InterruptIn sw2 (SW2); |
| el15s3p | 0:d3a050703801 | 22 | DigitalIn buttonA(PTB18); |
| el15s3p | 0:d3a050703801 | 23 | DigitalIn buttonB(PTB19); |
| el15s3p | 0:d3a050703801 | 24 | PwmOut buzzer(PTA2); |
| el15s3p | 0:d3a050703801 | 25 | |
| el15s3p | 0:d3a050703801 | 26 | // timer to regularly read the joystick |
| el15s3p | 0:d3a050703801 | 27 | Ticker pollJoystick; |
| el15s3p | 0:d3a050703801 | 28 | |
| el15s3p | 0:d3a050703801 | 29 | enum DirectionName { |
| el15s3p | 0:d3a050703801 | 30 | UP, |
| el15s3p | 0:d3a050703801 | 31 | DOWN, |
| el15s3p | 0:d3a050703801 | 32 | LEFT, |
| el15s3p | 0:d3a050703801 | 33 | RIGHT, |
| el15s3p | 0:d3a050703801 | 34 | CENTRE, |
| el15s3p | 0:d3a050703801 | 35 | UNKNOWN |
| el15s3p | 0:d3a050703801 | 36 | }; |
| el15s3p | 0:d3a050703801 | 37 | |
| el15s3p | 0:d3a050703801 | 38 | // struct for Joystick |
| el15s3p | 0:d3a050703801 | 39 | typedef struct JoyStick Joystick; |
| el15s3p | 0:d3a050703801 | 40 | struct JoyStick { |
| el15s3p | 0:d3a050703801 | 41 | float x; // current x value |
| el15s3p | 0:d3a050703801 | 42 | float x0; // 'centred' x value |
| el15s3p | 0:d3a050703801 | 43 | float y; // current y value |
| el15s3p | 0:d3a050703801 | 44 | float y0; // 'centred' y value |
| el15s3p | 0:d3a050703801 | 45 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
| el15s3p | 0:d3a050703801 | 46 | DirectionName direction; // current direction |
| el15s3p | 0:d3a050703801 | 47 | }; |
| el15s3p | 0:d3a050703801 | 48 | // create struct variable |
| el15s3p | 0:d3a050703801 | 49 | Joystick joystick; |
| el15s3p | 0:d3a050703801 | 50 | |
| el15s3p | 0:d3a050703801 | 51 | int printFlag = 0; |
| el15s3p | 0:d3a050703801 | 52 | |
| el15s3p | 1:9d69901e18d0 | 53 | // function prototypes |
| el15s3p | 1:9d69901e18d0 | 54 | void calibrateJoystick(); |
| el15s3p | 1:9d69901e18d0 | 55 | void updateJoystick(); |
| el15s3p | 1:9d69901e18d0 | 56 | |
| el15s3p | 1:9d69901e18d0 | 57 | int menuSelect = 2; |
| el15s3p | 1:9d69901e18d0 | 58 | |
| el15s3p | 0:d3a050703801 | 59 | int main() |
| el15s3p | 0:d3a050703801 | 60 | { |
| el15s3p | 1:9d69901e18d0 | 61 | for (; 2 > menuSelect < 5;) { |
| el15s3p | 1:9d69901e18d0 | 62 | calibrateJoystick(); // get centred values of joystick |
| el15s3p | 1:9d69901e18d0 | 63 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
| el15s3p | 1:9d69901e18d0 | 64 | lcd.init(); |
| el15s3p | 1:9d69901e18d0 | 65 | lcd.clear(); |
| el15s3p | 1:9d69901e18d0 | 66 | lcd.normalMode(); |
| el15s3p | 1:9d69901e18d0 | 67 | lcd.printString("Comet Crusher!",0,0); |
| el15s3p | 1:9d69901e18d0 | 68 | lcd.printString("Start",10,2); |
| el15s3p | 1:9d69901e18d0 | 69 | lcd.printString("Instructions",10,3); |
| el15s3p | 1:9d69901e18d0 | 70 | lcd.printString("Difficulty",10,4); |
| el15s3p | 1:9d69901e18d0 | 71 | lcd.printString("Scores",10,5); |
| el15s3p | 1:9d69901e18d0 | 72 | lcd.printString(">",5,menuSelect); |
| el15s3p | 1:9d69901e18d0 | 73 | lcd.refresh(); |
| el15s3p | 1:9d69901e18d0 | 74 | |
| el15s3p | 1:9d69901e18d0 | 75 | if (menuSelect == 2) { |
| el15s3p | 1:9d69901e18d0 | 76 | if (joystick.direction == DOWN) { |
| el15s3p | 1:9d69901e18d0 | 77 | menuSelect = 3; |
| el15s3p | 1:9d69901e18d0 | 78 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 79 | } else if (joystick.direction == UP) { |
| el15s3p | 1:9d69901e18d0 | 80 | menuSelect = 5; |
| el15s3p | 1:9d69901e18d0 | 81 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 82 | } |
| el15s3p | 1:9d69901e18d0 | 83 | lcd.refresh(); |
| el15s3p | 1:9d69901e18d0 | 84 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 85 | } |
| el15s3p | 1:9d69901e18d0 | 86 | if (menuSelect == 3) { |
| el15s3p | 1:9d69901e18d0 | 87 | if (joystick.direction == DOWN) { |
| el15s3p | 1:9d69901e18d0 | 88 | menuSelect = 4; |
| el15s3p | 1:9d69901e18d0 | 89 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 90 | } else if (joystick.direction == UP) { |
| el15s3p | 1:9d69901e18d0 | 91 | menuSelect = 2; |
| el15s3p | 1:9d69901e18d0 | 92 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 93 | } |
| el15s3p | 1:9d69901e18d0 | 94 | lcd.refresh(); |
| el15s3p | 1:9d69901e18d0 | 95 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 96 | } |
| el15s3p | 1:9d69901e18d0 | 97 | if (menuSelect == 4) { |
| el15s3p | 1:9d69901e18d0 | 98 | if (joystick.direction == DOWN) { |
| el15s3p | 1:9d69901e18d0 | 99 | menuSelect = 5; |
| el15s3p | 1:9d69901e18d0 | 100 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 101 | } else if (joystick.direction == UP) { |
| el15s3p | 1:9d69901e18d0 | 102 | menuSelect = 3; |
| el15s3p | 1:9d69901e18d0 | 103 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 104 | } |
| el15s3p | 1:9d69901e18d0 | 105 | lcd.refresh(); |
| el15s3p | 1:9d69901e18d0 | 106 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 107 | } |
| el15s3p | 1:9d69901e18d0 | 108 | if (menuSelect == 5) { |
| el15s3p | 1:9d69901e18d0 | 109 | if (joystick.direction == DOWN) { |
| el15s3p | 1:9d69901e18d0 | 110 | menuSelect = 2; |
| el15s3p | 1:9d69901e18d0 | 111 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 112 | } else if (joystick.direction == UP) { |
| el15s3p | 1:9d69901e18d0 | 113 | menuSelect = 4; |
| el15s3p | 1:9d69901e18d0 | 114 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 115 | } |
| el15s3p | 1:9d69901e18d0 | 116 | lcd.refresh(); |
| el15s3p | 1:9d69901e18d0 | 117 | sleep(); |
| el15s3p | 1:9d69901e18d0 | 118 | } |
| el15s3p | 1:9d69901e18d0 | 119 | sleep(); |
| el15s3p | 0:d3a050703801 | 120 | } |
| el15s3p | 0:d3a050703801 | 121 | } |
| el15s3p | 1:9d69901e18d0 | 122 | void game() |
| el15s3p | 0:d3a050703801 | 123 | { |
| el15s3p | 0:d3a050703801 | 124 | } |
| el15s3p | 0:d3a050703801 | 125 | |
| el15s3p | 1:9d69901e18d0 | 126 | void difficultySet() |
| el15s3p | 1:9d69901e18d0 | 127 | { |
| el15s3p | 1:9d69901e18d0 | 128 | } |
| el15s3p | 1:9d69901e18d0 | 129 | |
| el15s3p | 1:9d69901e18d0 | 130 | void highScore() |
| el15s3p | 1:9d69901e18d0 | 131 | { |
| el15s3p | 1:9d69901e18d0 | 132 | } |
| el15s3p | 1:9d69901e18d0 | 133 | |
| el15s3p | 1:9d69901e18d0 | 134 | void instructions() |
| el15s3p | 0:d3a050703801 | 135 | { |
| el15s3p | 0:d3a050703801 | 136 | } |
| el15s3p | 0:d3a050703801 | 137 | |
| el15s3p | 1:9d69901e18d0 | 138 | void calibrateJoystick() |
| el15s3p | 0:d3a050703801 | 139 | { |
| el15s3p | 1:9d69901e18d0 | 140 | button.mode(PullDown); |
| el15s3p | 1:9d69901e18d0 | 141 | // must not move during calibration |
| el15s3p | 1:9d69901e18d0 | 142 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
| el15s3p | 1:9d69901e18d0 | 143 | joystick.y0 = yPot; |
| el15s3p | 0:d3a050703801 | 144 | } |
| el15s3p | 1:9d69901e18d0 | 145 | void updateJoystick() |
| el15s3p | 1:9d69901e18d0 | 146 | { |
| el15s3p | 1:9d69901e18d0 | 147 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
| el15s3p | 1:9d69901e18d0 | 148 | joystick.x = xPot - joystick.x0; |
| el15s3p | 1:9d69901e18d0 | 149 | joystick.y = yPot - joystick.y0; |
| el15s3p | 1:9d69901e18d0 | 150 | // read button state |
| el15s3p | 1:9d69901e18d0 | 151 | joystick.button = button; |
| el15s3p | 0:d3a050703801 | 152 | |
| el15s3p | 1:9d69901e18d0 | 153 | // calculate direction depending on x,y values |
| el15s3p | 1:9d69901e18d0 | 154 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
| el15s3p | 1:9d69901e18d0 | 155 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 156 | joystick.direction = CENTRE; |
| el15s3p | 1:9d69901e18d0 | 157 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 158 | joystick.direction = DOWN; |
| el15s3p | 1:9d69901e18d0 | 159 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 160 | joystick.direction = UP; |
| el15s3p | 1:9d69901e18d0 | 161 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 162 | joystick.direction = LEFT; |
| el15s3p | 1:9d69901e18d0 | 163 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 164 | joystick.direction = RIGHT; |
| el15s3p | 1:9d69901e18d0 | 165 | } else { |
| el15s3p | 1:9d69901e18d0 | 166 | joystick.direction = UNKNOWN; |
| el15s3p | 1:9d69901e18d0 | 167 | } |
| el15s3p | 1:9d69901e18d0 | 168 | |
| el15s3p | 1:9d69901e18d0 | 169 | // set flag for printing |
| el15s3p | 1:9d69901e18d0 | 170 | printFlag = 1; |
| el15s3p | 1:9d69901e18d0 | 171 | } |