Lab 4 for ECE4180, final verison
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Fork of Lab4-Reversi-v2 by
Overview
For our project, we created the 2 player game “Reversi”, otherwise known as Othello using the uLCD-144-G2, a Sparkfun 5-way Tactile Switch Joystick, a speaker with a Class D audio amp breakout board, and SD card reader. The program begins with the dark player’s turn, and the joystick is used to move the red cursor box around. Pressing down on the joystick will attempt to put a piece down, resulting in a flipping sound, and will show an X on the screen for invalid moves. The game will finish when the board is filled, and go to a result screen, playing a victory sound. The result screen allows for a play again option.
Components
- /users/4180_1/notebook/using-a-navigation-switch-digital-joystick/
- /users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/
- /users/4180_1/notebook/using-a-speaker-for-audio-output/
- /cookbook/SD-Card-File-System
Wiring
mbed | Cable | uLCD |
---|---|---|
P28 | TX | RX |
P27 | RX | TX |
P29 | RES | RESET |
VU | +5V | +5V |
GND | GND | GND |
mbed | SD Card Reader |
---|---|
P5 | DI |
P6 | DO |
P7 | SCK |
P8 | CS |
GND | GND |
Vout | VCC |
mbed | Amplifer | Speaker |
---|---|---|
P18 | In + | |
GND | GND | |
VU | PWR + | |
Out + | + | |
Out - | - | |
GND | PWR - |
mbed | Joystick |
---|---|
P14 | Up |
P13 | Center |
P12 | Left |
P11 | Down |
P10 | Right |
GND | - |
Vout | + |
Program
Import programLab4-Reversi-Final
Lab 4 for ECE4180, final verison
SongPlayer.h@3:b1fe86f61f2f, 2016-11-01 (annotated)
- Committer:
- aolmenki
- Date:
- Tue Nov 01 23:30:44 2016 +0000
- Revision:
- 3:b1fe86f61f2f
- Parent:
- 1:cc72ad58982b
Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aolmenki | 1:cc72ad58982b | 1 | #include "mbed.h" |
aolmenki | 1:cc72ad58982b | 2 | // new class to play a note on Speaker based on PwmOut class |
aolmenki | 1:cc72ad58982b | 3 | class SongPlayer |
aolmenki | 1:cc72ad58982b | 4 | { |
aolmenki | 1:cc72ad58982b | 5 | public: |
aolmenki | 1:cc72ad58982b | 6 | SongPlayer(PinName pin) : _pin(pin) { |
aolmenki | 1:cc72ad58982b | 7 | // _pin(pin) means pass pin to the constructor |
aolmenki | 1:cc72ad58982b | 8 | } |
aolmenki | 1:cc72ad58982b | 9 | // class method to play a note based on PwmOut class |
aolmenki | 1:cc72ad58982b | 10 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
aolmenki | 1:cc72ad58982b | 11 | vol = volume; |
aolmenki | 1:cc72ad58982b | 12 | notecount = 0; |
aolmenki | 1:cc72ad58982b | 13 | _pin.period(1.0/frequency[notecount]); |
aolmenki | 1:cc72ad58982b | 14 | _pin = volume/2.0; |
aolmenki | 1:cc72ad58982b | 15 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
aolmenki | 1:cc72ad58982b | 16 | // setup timer to interrupt for next note to play |
aolmenki | 1:cc72ad58982b | 17 | frequencyptr = frequency; |
aolmenki | 1:cc72ad58982b | 18 | durationptr = duration; |
aolmenki | 1:cc72ad58982b | 19 | //returns after first note starts to play |
aolmenki | 1:cc72ad58982b | 20 | } |
aolmenki | 1:cc72ad58982b | 21 | void nextnote(); |
aolmenki | 1:cc72ad58982b | 22 | private: |
aolmenki | 1:cc72ad58982b | 23 | Timeout noteduration; |
aolmenki | 1:cc72ad58982b | 24 | PwmOut _pin; |
aolmenki | 1:cc72ad58982b | 25 | int notecount; |
aolmenki | 1:cc72ad58982b | 26 | float vol; |
aolmenki | 1:cc72ad58982b | 27 | float * frequencyptr; |
aolmenki | 1:cc72ad58982b | 28 | float * durationptr; |
aolmenki | 1:cc72ad58982b | 29 | }; |
aolmenki | 1:cc72ad58982b | 30 | //Interrupt Routine to play next note |
aolmenki | 1:cc72ad58982b | 31 | void SongPlayer::nextnote() |
aolmenki | 1:cc72ad58982b | 32 | { |
aolmenki | 1:cc72ad58982b | 33 | _pin = 0.0; |
aolmenki | 1:cc72ad58982b | 34 | notecount++; //setup next note in song |
aolmenki | 1:cc72ad58982b | 35 | if (durationptr[notecount]!=0.0) { |
aolmenki | 1:cc72ad58982b | 36 | _pin.period(1.0/frequencyptr[notecount]); |
aolmenki | 1:cc72ad58982b | 37 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
aolmenki | 1:cc72ad58982b | 38 | _pin = vol/2.0; |
aolmenki | 1:cc72ad58982b | 39 | } else |
aolmenki | 1:cc72ad58982b | 40 | _pin = 0.0; //turn off on last note |
aolmenki | 1:cc72ad58982b | 41 | } |