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: mbed
main.cpp@6:b393cfe4e0a7, 2020-05-28 (annotated)
- Committer:
- ChenZirui
- Date:
- Thu May 28 01:11:47 2020 +0000
- Revision:
- 6:b393cfe4e0a7
- Parent:
- 5:7207c9b70108
- Child:
- 7:f61ac963eb07
not final
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| ChenZirui | 5:7207c9b70108 | 1 | ///////// pre-processor directives //////// | 
| eencae | 0:b7f1f47bb26a | 2 | #include "mbed.h" | 
| eencae | 0:b7f1f47bb26a | 3 | #include "Gamepad.h" | 
| eencae | 0:b7f1f47bb26a | 4 | #include "N5110.h" | 
| ChenZirui | 5:7207c9b70108 | 5 | #include "Touch.h" | 
| ChenZirui | 6:b393cfe4e0a7 | 6 | #include "Bitmap.h" | 
| ChenZirui | 5:7207c9b70108 | 7 | #ifdef WITH_TESTING | 
| ChenZirui | 6:b393cfe4e0a7 | 8 | # include "tests.h" | 
| ChenZirui | 5:7207c9b70108 | 9 | #endif | 
| eencae | 0:b7f1f47bb26a | 10 | |
| ChenZirui | 5:7207c9b70108 | 11 | #define PADDLE_WIDTH 2 | 
| ChenZirui | 5:7207c9b70108 | 12 | #define PADDLE_HEIGHT 8 | 
| ChenZirui | 5:7207c9b70108 | 13 | #define BALL_SIZE 2 | 
| ChenZirui | 5:7207c9b70108 | 14 | #define BALL_SPEED 3 | 
| eencae | 0:b7f1f47bb26a | 15 | |
| ChenZirui | 5:7207c9b70108 | 16 | /////////////// structs ///////////////// | 
| ChenZirui | 5:7207c9b70108 | 17 | struct UserInput { | 
| ChenZirui | 5:7207c9b70108 | 18 | Direction d; | 
| ChenZirui | 5:7207c9b70108 | 19 | float mag; | 
| ChenZirui | 5:7207c9b70108 | 20 | }; | 
| ChenZirui | 5:7207c9b70108 | 21 | /////////////// objects /////////////// | 
| ChenZirui | 5:7207c9b70108 | 22 | N5110 lcd; | 
| eencae | 0:b7f1f47bb26a | 23 | Gamepad pad; | 
| ChenZirui | 5:7207c9b70108 | 24 | Touch touch; | 
| ChenZirui | 5:7207c9b70108 | 25 | ///////////// prototypes /////////////// | 
| ChenZirui | 5:7207c9b70108 | 26 | void init(); | 
| ChenZirui | 5:7207c9b70108 | 27 | void update_game(UserInput input); | 
| ChenZirui | 5:7207c9b70108 | 28 | void render(); | 
| ChenZirui | 5:7207c9b70108 | 29 | void welcome(); | 
| ChenZirui | 5:7207c9b70108 | 30 | |
| ChenZirui | 5:7207c9b70108 | 31 | ///////////// functions //////////////// | 
| eencae | 0:b7f1f47bb26a | 32 | int main() | 
| eencae | 0:b7f1f47bb26a | 33 | { | 
| ChenZirui | 5:7207c9b70108 | 34 | #ifdef WITH_TESTING | 
| ChenZirui | 5:7207c9b70108 | 35 | int number_of_failures = run_all_tests(); | 
| ChenZirui | 5:7207c9b70108 | 36 | |
| ChenZirui | 5:7207c9b70108 | 37 | if(number_of_failures > 0) return number_of_failures; | 
| ChenZirui | 5:7207c9b70108 | 38 | #endif | 
| ChenZirui | 5:7207c9b70108 | 39 | int fps = 6; // frames per second | 
| ChenZirui | 6:b393cfe4e0a7 | 40 | init(); // initialise and then display welcome screen... | 
| ChenZirui | 6:b393cfe4e0a7 | 41 | welcome(); // waiting for the user to start | 
| ChenZirui | 6:b393cfe4e0a7 | 42 | //lcd.drawRect(0,0,84,24,FILL_BLACK); | 
| ChenZirui | 5:7207c9b70108 | 43 | render(); // first draw the initial frame | 
| ChenZirui | 6:b393cfe4e0a7 | 44 | //lcd.drawRect(0,0,84,24,FILL_BLACK); | 
| ChenZirui | 5:7207c9b70108 | 45 | wait(1.0f/fps); // and wait for one frame period | 
| ChenZirui | 5:7207c9b70108 | 46 | |
| ChenZirui | 5:7207c9b70108 | 47 | //lcd.drawRect(0,0,84,24,FILL_BLACK); | 
| ChenZirui | 5:7207c9b70108 | 48 | // game loop - read input, update the game state and render the display | 
| ChenZirui | 5:7207c9b70108 | 49 | while (1) { | 
| ChenZirui | 6:b393cfe4e0a7 | 50 | // pong.read_input(pad); | 
| ChenZirui | 6:b393cfe4e0a7 | 51 | // pong.read_input(pad); | 
| ChenZirui | 5:7207c9b70108 | 52 | touch.read_input(pad); | 
| ChenZirui | 6:b393cfe4e0a7 | 53 | touch.update(pad); | 
| ChenZirui | 5:7207c9b70108 | 54 | render(); | 
| ChenZirui | 5:7207c9b70108 | 55 | //lcd.drawRect(0,0,84,24,FILL_BLACK); | 
| ChenZirui | 5:7207c9b70108 | 56 | wait(1.0f/fps); | 
| ChenZirui | 5:7207c9b70108 | 57 | } | 
| eencae | 0:b7f1f47bb26a | 58 | } | 
| eencae | 0:b7f1f47bb26a | 59 | |
| ChenZirui | 5:7207c9b70108 | 60 | // initialies all classes and libraries | 
| ChenZirui | 5:7207c9b70108 | 61 | void init() | 
| ChenZirui | 5:7207c9b70108 | 62 | { | 
| ChenZirui | 5:7207c9b70108 | 63 | // need to initialise LCD and Gamepad | 
| ChenZirui | 5:7207c9b70108 | 64 | lcd.init(); | 
| ChenZirui | 5:7207c9b70108 | 65 | pad.init(); | 
| ChenZirui | 5:7207c9b70108 | 66 | |
| ChenZirui | 5:7207c9b70108 | 67 | // initialise the game with correct ball and paddle sizes | 
| ChenZirui | 6:b393cfe4e0a7 | 68 | touch.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); | 
| ChenZirui | 5:7207c9b70108 | 69 | |
| ChenZirui | 5:7207c9b70108 | 70 | } | 
| ChenZirui | 5:7207c9b70108 | 71 | |
| ChenZirui | 5:7207c9b70108 | 72 | // this function draws each frame on the LCD | 
| ChenZirui | 5:7207c9b70108 | 73 | void render() | 
| ChenZirui | 5:7207c9b70108 | 74 | { | 
| ChenZirui | 5:7207c9b70108 | 75 | // clear screen, re-draw and refresh | 
| ChenZirui | 5:7207c9b70108 | 76 | lcd.clear(); | 
| ChenZirui | 5:7207c9b70108 | 77 | touch.draw(lcd); | 
| ChenZirui | 5:7207c9b70108 | 78 | lcd.refresh(); | 
| ChenZirui | 5:7207c9b70108 | 79 | } | 
| ChenZirui | 5:7207c9b70108 | 80 | |
| ChenZirui | 5:7207c9b70108 | 81 | // simple splash screen displayed on start-up | 
| ChenZirui | 5:7207c9b70108 | 82 | void welcome() { | 
| ChenZirui | 5:7207c9b70108 | 83 | |
| ChenZirui | 5:7207c9b70108 | 84 | lcd.printString(" touch! ",0,1); | 
| ChenZirui | 5:7207c9b70108 | 85 | lcd.printString(" Press Start ",0,4); | 
| ChenZirui | 5:7207c9b70108 | 86 | lcd.refresh(); | 
| ChenZirui | 5:7207c9b70108 | 87 | |
| ChenZirui | 5:7207c9b70108 | 88 | // wait flashing LEDs until start button is pressed | 
| ChenZirui | 5:7207c9b70108 | 89 | while ( pad.start_pressed() == false) { | 
| ChenZirui | 5:7207c9b70108 | 90 | lcd.setContrast( pad.read_pot1()); | 
| ChenZirui | 5:7207c9b70108 | 91 | pad.leds_on(); | 
| ChenZirui | 5:7207c9b70108 | 92 | wait(0.1); | 
| ChenZirui | 5:7207c9b70108 | 93 | pad.leds_off(); | 
| ChenZirui | 5:7207c9b70108 | 94 | wait(0.1); | 
| ChenZirui | 5:7207c9b70108 | 95 | } | 
| ChenZirui | 5:7207c9b70108 | 96 | |
| ChenZirui | 5:7207c9b70108 | 97 | } |