submit
Dependencies: mbed Gamepad N5110
main.cpp@0:b67614a0c4cf, 2019-04-02 (annotated)
- Committer:
- 694617778
- Date:
- Tue Apr 02 06:46:47 2019 +0000
- Revision:
- 0:b67614a0c4cf
- Child:
- 1:b49c36604125
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
694617778 | 0:b67614a0c4cf | 1 | ///////// pre-processor directives //////// |
694617778 | 0:b67614a0c4cf | 2 | #include "mbed.h" |
694617778 | 0:b67614a0c4cf | 3 | #include "Gamepad.h" |
694617778 | 0:b67614a0c4cf | 4 | #include "N5110.h" |
694617778 | 0:b67614a0c4cf | 5 | |
694617778 | 0:b67614a0c4cf | 6 | /////////////// structs ///////////////// |
694617778 | 0:b67614a0c4cf | 7 | |
694617778 | 0:b67614a0c4cf | 8 | struct UserInput { |
694617778 | 0:b67614a0c4cf | 9 | Direction d; |
694617778 | 0:b67614a0c4cf | 10 | float mag; |
694617778 | 0:b67614a0c4cf | 11 | }; |
694617778 | 0:b67614a0c4cf | 12 | /////////////// objects /////////////// |
694617778 | 0:b67614a0c4cf | 13 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
694617778 | 0:b67614a0c4cf | 14 | Gamepad pad; |
694617778 | 0:b67614a0c4cf | 15 | |
694617778 | 0:b67614a0c4cf | 16 | |
694617778 | 0:b67614a0c4cf | 17 | ///////////// prototypes /////////////// |
694617778 | 0:b67614a0c4cf | 18 | void init(); |
694617778 | 0:b67614a0c4cf | 19 | void update_game(UserInput input); |
694617778 | 0:b67614a0c4cf | 20 | void run(); |
694617778 | 0:b67614a0c4cf | 21 | void welcome(); |
694617778 | 0:b67614a0c4cf | 22 | |
694617778 | 0:b67614a0c4cf | 23 | ///////////// functions //////////////// |
694617778 | 0:b67614a0c4cf | 24 | int main() |
694617778 | 0:b67614a0c4cf | 25 | { |
694617778 | 0:b67614a0c4cf | 26 | int fps = 8; // frames per second |
694617778 | 0:b67614a0c4cf | 27 | |
694617778 | 0:b67614a0c4cf | 28 | init(); |
694617778 | 0:b67614a0c4cf | 29 | welcome(); // show welcome display, waiting for the user to start |
694617778 | 0:b67614a0c4cf | 30 | |
694617778 | 0:b67614a0c4cf | 31 | run(); // first draw the initial frame |
694617778 | 0:b67614a0c4cf | 32 | wait(1.0f/fps); // and wait for one frame period |
694617778 | 0:b67614a0c4cf | 33 | |
694617778 | 0:b67614a0c4cf | 34 | // game loop - read input, update the game state and render the display |
694617778 | 0:b67614a0c4cf | 35 | // while (1) { |
694617778 | 0:b67614a0c4cf | 36 | // pong.read_input(pad); |
694617778 | 0:b67614a0c4cf | 37 | // pong.update(pad); |
694617778 | 0:b67614a0c4cf | 38 | // run(); |
694617778 | 0:b67614a0c4cf | 39 | // wait(1.0f/fps); |
694617778 | 0:b67614a0c4cf | 40 | // } |
694617778 | 0:b67614a0c4cf | 41 | } |
694617778 | 0:b67614a0c4cf | 42 | |
694617778 | 0:b67614a0c4cf | 43 | // simple splash screen displayed on start-up |
694617778 | 0:b67614a0c4cf | 44 | void welcome() { |
694617778 | 0:b67614a0c4cf | 45 | |
694617778 | 0:b67614a0c4cf | 46 | lcd.printString(" ###SNAKE### ",0,1); |
694617778 | 0:b67614a0c4cf | 47 | lcd.printString(" Press Start ",0,4); |
694617778 | 0:b67614a0c4cf | 48 | lcd.refresh(); |
694617778 | 0:b67614a0c4cf | 49 | |
694617778 | 0:b67614a0c4cf | 50 | // wait flashing LEDs until start button is pressed |
694617778 | 0:b67614a0c4cf | 51 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
694617778 | 0:b67614a0c4cf | 52 | pad.leds_on(); |
694617778 | 0:b67614a0c4cf | 53 | wait(0.1); |
694617778 | 0:b67614a0c4cf | 54 | pad.leds_off(); |
694617778 | 0:b67614a0c4cf | 55 | wait(0.1); |
694617778 | 0:b67614a0c4cf | 56 | } |
694617778 | 0:b67614a0c4cf | 57 | |
694617778 | 0:b67614a0c4cf | 58 | } |
694617778 | 0:b67614a0c4cf | 59 | |
694617778 | 0:b67614a0c4cf | 60 | // this function draws each frame on the LCD |
694617778 | 0:b67614a0c4cf | 61 | void run() |
694617778 | 0:b67614a0c4cf | 62 | { |
694617778 | 0:b67614a0c4cf | 63 | // clear screen, re-draw and refresh |
694617778 | 0:b67614a0c4cf | 64 | lcd.clear(); |
694617778 | 0:b67614a0c4cf | 65 | // snake.draw(); |
694617778 | 0:b67614a0c4cf | 66 | lcd.refresh(); |
694617778 | 0:b67614a0c4cf | 67 | } |
694617778 | 0:b67614a0c4cf | 68 | |
694617778 | 0:b67614a0c4cf | 69 | // initialies all classes and libraries |
694617778 | 0:b67614a0c4cf | 70 | void init() |
694617778 | 0:b67614a0c4cf | 71 | { |
694617778 | 0:b67614a0c4cf | 72 | // need to initialise LCD and Gamepad |
694617778 | 0:b67614a0c4cf | 73 | lcd.init(); |
694617778 | 0:b67614a0c4cf | 74 | pad.init(); |
694617778 | 0:b67614a0c4cf | 75 | |
694617778 | 0:b67614a0c4cf | 76 | } |
694617778 | 0:b67614a0c4cf | 77 |