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: 4DGL-uLCD-SE LSM9DS1_Library SDFileSystem mbed-rtos mbed wave_player
main.cpp@4:5c7d990d79ac, 2016-03-14 (annotated)
- Committer:
- Dogstopper
- Date:
- Mon Mar 14 18:53:11 2016 +0000
- Revision:
- 4:5c7d990d79ac
- Parent:
- 3:27889fffc2f7
Final
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Dogstopper | 0:6a49493943be | 1 | #include "mbed.h" |
| Dogstopper | 0:6a49493943be | 2 | #include "InputHandler.h" |
| Dogstopper | 2:623f29bad35c | 3 | #include "Invader.h" |
| Dogstopper | 2:623f29bad35c | 4 | #include "Player.h" |
| Dogstopper | 3:27889fffc2f7 | 5 | #include "SDFileSystem.h" |
| Dogstopper | 3:27889fffc2f7 | 6 | |
| Dogstopper | 3:27889fffc2f7 | 7 | #include <wave_player.h> |
| Dogstopper | 3:27889fffc2f7 | 8 | #include <vector> |
| Dogstopper | 3:27889fffc2f7 | 9 | |
| Dogstopper | 3:27889fffc2f7 | 10 | #define SIGNAL_KILL 200 |
| Dogstopper | 0:6a49493943be | 11 | |
| Dogstopper | 0:6a49493943be | 12 | #define WIDTH 128 |
| Dogstopper | 0:6a49493943be | 13 | #define HEIGHT 128 |
| Dogstopper | 0:6a49493943be | 14 | |
| Dogstopper | 3:27889fffc2f7 | 15 | #define NUM_INVADERS 2 |
| Dogstopper | 0:6a49493943be | 16 | |
| Dogstopper | 3:27889fffc2f7 | 17 | DigitalOut led(LED1); |
| Dogstopper | 3:27889fffc2f7 | 18 | |
| Dogstopper | 3:27889fffc2f7 | 19 | AnalogOut DACout(p18); |
| Dogstopper | 3:27889fffc2f7 | 20 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
| Dogstopper | 2:623f29bad35c | 21 | |
| Dogstopper | 3:27889fffc2f7 | 22 | void updatePlayer(const void* player) { |
| Dogstopper | 3:27889fffc2f7 | 23 | while(true) { |
| Dogstopper | 3:27889fffc2f7 | 24 | Player *p = (Player*)player; |
| Dogstopper | 3:27889fffc2f7 | 25 | p->update(WIDTH, HEIGHT); |
| Dogstopper | 4:5c7d990d79ac | 26 | Thread::wait(45); |
| Dogstopper | 3:27889fffc2f7 | 27 | } |
| Dogstopper | 3:27889fffc2f7 | 28 | } |
| Dogstopper | 0:6a49493943be | 29 | |
| Dogstopper | 3:27889fffc2f7 | 30 | void updateInvader(const void* invaders) { |
| Dogstopper | 3:27889fffc2f7 | 31 | std::vector<Invader*> invaderVector = *((std::vector<Invader*>*)invaders); |
| Dogstopper | 3:27889fffc2f7 | 32 | while(true) { |
| Dogstopper | 3:27889fffc2f7 | 33 | for (std::vector<Invader*>::iterator it = invaderVector.begin(); it != invaderVector.end(); ++it) { |
| Dogstopper | 3:27889fffc2f7 | 34 | (*it)->update(); |
| Dogstopper | 3:27889fffc2f7 | 35 | } |
| Dogstopper | 4:5c7d990d79ac | 36 | Thread::wait(45); |
| Dogstopper | 3:27889fffc2f7 | 37 | } |
| Dogstopper | 3:27889fffc2f7 | 38 | } |
| Dogstopper | 3:27889fffc2f7 | 39 | |
| Dogstopper | 3:27889fffc2f7 | 40 | FILE *wave_file; |
| Dogstopper | 0:6a49493943be | 41 | |
| Dogstopper | 3:27889fffc2f7 | 42 | void playBackgroundMusic(const void* waver) { |
| Dogstopper | 3:27889fffc2f7 | 43 | wave_player* playMe = (wave_player*)waver; |
| Dogstopper | 3:27889fffc2f7 | 44 | playMe->play(wave_file); |
| Dogstopper | 3:27889fffc2f7 | 45 | Thread::wait(50); |
| Dogstopper | 0:6a49493943be | 46 | } |
| Dogstopper | 0:6a49493943be | 47 | |
| Dogstopper | 0:6a49493943be | 48 | int main() { |
| Dogstopper | 3:27889fffc2f7 | 49 | InputHandler input(p9, p10, 0xD6, 0x3C, p15); |
| Dogstopper | 3:27889fffc2f7 | 50 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; |
| Dogstopper | 3:27889fffc2f7 | 51 | |
| Dogstopper | 3:27889fffc2f7 | 52 | wave_player waver(&DACout); |
| Dogstopper | 3:27889fffc2f7 | 53 | wave_file=fopen("/sd/beethoven.wav","r"); |
| Dogstopper | 3:27889fffc2f7 | 54 | |
| Dogstopper | 2:623f29bad35c | 55 | Thread* inputThread = input.start(); |
| Dogstopper | 2:623f29bad35c | 56 | uLCD.background_color(BLACK); |
| Dogstopper | 4:5c7d990d79ac | 57 | uLCD.baudrate(300000); |
| Dogstopper | 0:6a49493943be | 58 | |
| Dogstopper | 3:27889fffc2f7 | 59 | bool alive = false; |
| Dogstopper | 3:27889fffc2f7 | 60 | while(true) { |
| Dogstopper | 4:5c7d990d79ac | 61 | uLCD.media_init(); |
| Dogstopper | 4:5c7d990d79ac | 62 | uLCD.set_sector_address(0x003B, 0x5001); |
| Dogstopper | 4:5c7d990d79ac | 63 | uLCD.display_image(0,0); |
| Dogstopper | 4:5c7d990d79ac | 64 | while(!input.getPushed()) {} |
| Dogstopper | 4:5c7d990d79ac | 65 | |
| Dogstopper | 4:5c7d990d79ac | 66 | Player player(&uLCD, &input, WIDTH/2, HEIGHT/2); |
| Dogstopper | 4:5c7d990d79ac | 67 | |
| Dogstopper | 4:5c7d990d79ac | 68 | std::vector<Invader *> invaders; |
| Dogstopper | 4:5c7d990d79ac | 69 | for (int i = 0; i < NUM_INVADERS; i++) { |
| Dogstopper | 4:5c7d990d79ac | 70 | invaders.push_back(new Invader(&uLCD, WIDTH, HEIGHT)); |
| Dogstopper | 3:27889fffc2f7 | 71 | } |
| Dogstopper | 4:5c7d990d79ac | 72 | |
| Dogstopper | 4:5c7d990d79ac | 73 | alive = true; |
| Dogstopper | 4:5c7d990d79ac | 74 | uLCD.cls(); |
| Dogstopper | 4:5c7d990d79ac | 75 | uLCD.background_color(BLACK); |
| Dogstopper | 4:5c7d990d79ac | 76 | |
| Dogstopper | 4:5c7d990d79ac | 77 | // Start the music and the two updater threads (different speeds) |
| Dogstopper | 3:27889fffc2f7 | 78 | Thread* playerThread = new Thread(updatePlayer, &player); |
| Dogstopper | 3:27889fffc2f7 | 79 | Thread* invaderThread = new Thread(updateInvader, &invaders); |
| Dogstopper | 4:5c7d990d79ac | 80 | Thread* wavplayerThread = new Thread(playBackgroundMusic, &waver); |
| Dogstopper | 4:5c7d990d79ac | 81 | |
| Dogstopper | 3:27889fffc2f7 | 82 | while (alive) { |
| Dogstopper | 3:27889fffc2f7 | 83 | player.draw(); |
| Dogstopper | 4:5c7d990d79ac | 84 | int count = 1; |
| Dogstopper | 3:27889fffc2f7 | 85 | for (std::vector<Invader*>::iterator it = invaders.begin(); it != invaders.end(); ++it) { |
| Dogstopper | 4:5c7d990d79ac | 86 | printf("count: %d", count); |
| Dogstopper | 3:27889fffc2f7 | 87 | (*it)->draw(); |
| Dogstopper | 3:27889fffc2f7 | 88 | alive = !(*it)->intersects(player.getX(), player.getY(), player.getWidth(), player.getHeight()); |
| Dogstopper | 3:27889fffc2f7 | 89 | led = alive; |
| Dogstopper | 4:5c7d990d79ac | 90 | count++; |
| Dogstopper | 3:27889fffc2f7 | 91 | } |
| Dogstopper | 4:5c7d990d79ac | 92 | wait_ms(40); |
| Dogstopper | 3:27889fffc2f7 | 93 | } |
| Dogstopper | 4:5c7d990d79ac | 94 | uLCD.locate(2, 2); |
| Dogstopper | 4:5c7d990d79ac | 95 | uLCD.printf("GAME OVER! >:)"); |
| Dogstopper | 4:5c7d990d79ac | 96 | |
| Dogstopper | 3:27889fffc2f7 | 97 | playerThread->terminate(); |
| Dogstopper | 3:27889fffc2f7 | 98 | invaderThread->terminate(); |
| Dogstopper | 4:5c7d990d79ac | 99 | wait_ms(5000); |
| Dogstopper | 4:5c7d990d79ac | 100 | break; |
| Dogstopper | 0:6a49493943be | 101 | } |
| Dogstopper | 0:6a49493943be | 102 | } |