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@2:9e791f33c49f, 2016-04-27 (annotated)
- Committer:
- el15s3p
- Date:
- Wed Apr 27 22:47:43 2016 +0000
- Revision:
- 2:9e791f33c49f
- Parent:
- 1:9d69901e18d0
FULLY WORKING MENU, ALHAMDULILLAH!
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 | 2:9e791f33c49f | 61 | while (1) { |
| 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 | 2:9e791f33c49f | 74 | sleep(); |
| el15s3p | 2:9e791f33c49f | 75 | switch (menuSelect) { |
| el15s3p | 2:9e791f33c49f | 76 | case 2: |
| el15s3p | 2:9e791f33c49f | 77 | if (joystick.direction == DOWN) { |
| el15s3p | 2:9e791f33c49f | 78 | menuSelect = 3; |
| el15s3p | 2:9e791f33c49f | 79 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 80 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 81 | sleep(); |
| el15s3p | 2:9e791f33c49f | 82 | } else if (joystick.direction == UP) { |
| el15s3p | 2:9e791f33c49f | 83 | menuSelect = 5; |
| el15s3p | 2:9e791f33c49f | 84 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 85 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 86 | sleep(); |
| el15s3p | 2:9e791f33c49f | 87 | } |
| el15s3p | 2:9e791f33c49f | 88 | break; |
| el15s3p | 2:9e791f33c49f | 89 | case 3: |
| el15s3p | 2:9e791f33c49f | 90 | if (joystick.direction == DOWN) { |
| el15s3p | 2:9e791f33c49f | 91 | menuSelect = 4; |
| el15s3p | 2:9e791f33c49f | 92 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 93 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 94 | sleep(); |
| el15s3p | 2:9e791f33c49f | 95 | } else if (joystick.direction == UP) { |
| el15s3p | 2:9e791f33c49f | 96 | menuSelect = 2; |
| el15s3p | 2:9e791f33c49f | 97 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 98 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 99 | sleep(); |
| el15s3p | 2:9e791f33c49f | 100 | } |
| el15s3p | 2:9e791f33c49f | 101 | break; |
| el15s3p | 2:9e791f33c49f | 102 | case 4: |
| el15s3p | 2:9e791f33c49f | 103 | if (joystick.direction == DOWN) { |
| el15s3p | 2:9e791f33c49f | 104 | menuSelect = 5; |
| el15s3p | 2:9e791f33c49f | 105 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 106 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 107 | sleep(); |
| el15s3p | 2:9e791f33c49f | 108 | } else if (joystick.direction == UP) { |
| el15s3p | 2:9e791f33c49f | 109 | menuSelect = 3; |
| el15s3p | 2:9e791f33c49f | 110 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 111 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 112 | sleep(); |
| el15s3p | 2:9e791f33c49f | 113 | } |
| el15s3p | 2:9e791f33c49f | 114 | break; |
| el15s3p | 2:9e791f33c49f | 115 | case 5: |
| el15s3p | 2:9e791f33c49f | 116 | if (joystick.direction == DOWN) { |
| el15s3p | 2:9e791f33c49f | 117 | menuSelect = 2; |
| el15s3p | 2:9e791f33c49f | 118 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 119 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 120 | sleep(); |
| el15s3p | 2:9e791f33c49f | 121 | } else if (joystick.direction == UP) { |
| el15s3p | 2:9e791f33c49f | 122 | menuSelect = 4; |
| el15s3p | 2:9e791f33c49f | 123 | lcd.refresh(); |
| el15s3p | 2:9e791f33c49f | 124 | wait (0.5); |
| el15s3p | 2:9e791f33c49f | 125 | sleep(); |
| el15s3p | 2:9e791f33c49f | 126 | } |
| el15s3p | 2:9e791f33c49f | 127 | break; |
| el15s3p | 1:9d69901e18d0 | 128 | } |
| el15s3p | 0:d3a050703801 | 129 | } |
| el15s3p | 0:d3a050703801 | 130 | } |
| el15s3p | 1:9d69901e18d0 | 131 | void game() |
| el15s3p | 0:d3a050703801 | 132 | { |
| el15s3p | 0:d3a050703801 | 133 | } |
| el15s3p | 0:d3a050703801 | 134 | |
| el15s3p | 1:9d69901e18d0 | 135 | void difficultySet() |
| el15s3p | 1:9d69901e18d0 | 136 | { |
| el15s3p | 1:9d69901e18d0 | 137 | } |
| el15s3p | 1:9d69901e18d0 | 138 | |
| el15s3p | 1:9d69901e18d0 | 139 | void highScore() |
| el15s3p | 1:9d69901e18d0 | 140 | { |
| el15s3p | 1:9d69901e18d0 | 141 | } |
| el15s3p | 1:9d69901e18d0 | 142 | |
| el15s3p | 1:9d69901e18d0 | 143 | void instructions() |
| el15s3p | 0:d3a050703801 | 144 | { |
| el15s3p | 0:d3a050703801 | 145 | } |
| el15s3p | 0:d3a050703801 | 146 | |
| el15s3p | 1:9d69901e18d0 | 147 | void calibrateJoystick() |
| el15s3p | 0:d3a050703801 | 148 | { |
| el15s3p | 1:9d69901e18d0 | 149 | button.mode(PullDown); |
| el15s3p | 1:9d69901e18d0 | 150 | // must not move during calibration |
| el15s3p | 1:9d69901e18d0 | 151 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
| el15s3p | 1:9d69901e18d0 | 152 | joystick.y0 = yPot; |
| el15s3p | 0:d3a050703801 | 153 | } |
| el15s3p | 1:9d69901e18d0 | 154 | void updateJoystick() |
| el15s3p | 1:9d69901e18d0 | 155 | { |
| el15s3p | 1:9d69901e18d0 | 156 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
| el15s3p | 1:9d69901e18d0 | 157 | joystick.x = xPot - joystick.x0; |
| el15s3p | 1:9d69901e18d0 | 158 | joystick.y = yPot - joystick.y0; |
| el15s3p | 1:9d69901e18d0 | 159 | // read button state |
| el15s3p | 1:9d69901e18d0 | 160 | joystick.button = button; |
| el15s3p | 0:d3a050703801 | 161 | |
| el15s3p | 1:9d69901e18d0 | 162 | // calculate direction depending on x,y values |
| el15s3p | 1:9d69901e18d0 | 163 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
| el15s3p | 1:9d69901e18d0 | 164 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 165 | joystick.direction = CENTRE; |
| el15s3p | 1:9d69901e18d0 | 166 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 167 | joystick.direction = DOWN; |
| el15s3p | 1:9d69901e18d0 | 168 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 169 | joystick.direction = UP; |
| el15s3p | 1:9d69901e18d0 | 170 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 171 | joystick.direction = LEFT; |
| el15s3p | 1:9d69901e18d0 | 172 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| el15s3p | 1:9d69901e18d0 | 173 | joystick.direction = RIGHT; |
| el15s3p | 1:9d69901e18d0 | 174 | } else { |
| el15s3p | 1:9d69901e18d0 | 175 | joystick.direction = UNKNOWN; |
| el15s3p | 1:9d69901e18d0 | 176 | } |
| el15s3p | 1:9d69901e18d0 | 177 | |
| el15s3p | 1:9d69901e18d0 | 178 | // set flag for printing |
| el15s3p | 1:9d69901e18d0 | 179 | printFlag = 1; |
| el15s3p | 1:9d69901e18d0 | 180 | } |