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 4DGL-uLCD-SE SDFileSystem PinDetect
Diff: main.cpp
- Revision:
- 5:cf8ae4ca6f2b
- Parent:
- 4:9a4d22a279b3
diff -r 9a4d22a279b3 -r cf8ae4ca6f2b main.cpp
--- a/main.cpp Thu Jan 23 16:47:05 2014 +0000
+++ b/main.cpp Mon Apr 25 01:52:31 2022 +0000
@@ -1,141 +1,176 @@
-// skeleton code for ECE 2036 thermostat lab
-// code must be added by students
+//Michael McDonald-LAME
+
#include "mbed.h"
-#include "TMP36.h"
-#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "PinDetect.h"
#include "Speaker.h"
-// must add your new class code to the project file Shiftbrite.h
-#include "Shiftbrite.h"
-
-// use class to setup temperature sensor pins
-TMP36 myTMP36(p15); //Analog in
+#include "MMA8452.h"
+#include "lame.h"
+#include <cstdlib>
+#include <ctime>
-// use class to setup microSD card filesystem
-SDFileSystem sd(p5, p6, p7, p8, "sd");
-
-// use class to setup the Color LCD
-uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object
+PinDetect pbFire(p22);
+Serial pc(USBTX,USBRX);
+uLCD_4DGL uLCD(p9, p10, p11);
+MMA8452 acc(p28, p27, 40000);
+Speaker soundOut(p25);
-// use class to setup pushbuttons pins
-PinDetect pb1(p23);
-PinDetect pb2(p24);
-PinDetect pb3(p25);
-
-// use class to setup speaker pin
-Speaker mySpeaker(p21); //PWM out
+//Create my objects
+Timer timeObject;
+Ship shipObject;
+Bullet bulletObject;
-// use class to setup Shiftbrite pins
-Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci
-
-// use class to setup Mbed's four on-board LEDs
-DigitalOut myLED1(LED1);
-DigitalOut myLED2(LED2);
-DigitalOut myLED3(LED3);
-DigitalOut myLED4(LED4);
-
-
+// Pushbutton Function
+void pbFire_hit_callback(void) {
+ soundOut.PlayNote(100, 0.05, 0.1);
+ bulletObject.setBullet(true);
+}
+// Accelerometer Functions
+void setupAccelerometer(){
+ acc.setBitDepth(MMA8452::BIT_DEPTH_12);
+ acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
+ acc.setDataRate(MMA8452::RATE_100);
+}
+ double x = .01;
+ double y = .01;
+ double z = .01;
-//also setting any unused analog input pins to digital outputs reduces A/D noise a bit
-//see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/
-DigitalOut P16(p16);
-DigitalOut P17(p17);
-DigitalOut P18(p18);
-DigitalOut P19(p19);
-DigitalOut P20(p20);
+void accInput(){
+ acc.readXYZGravity(&x,&y,&z);
+ if ((y>0.1)){
+ shipObject.moveLeft();}
+ if ((y<-0.1)){
+ shipObject.moveRight();}
-
-
+};
-// Global variables used in callbacks and main program
-// C variables in interrupt routines should use volatile keyword
-int volatile heat_setting=78; // heat to temp
-int volatile cool_setting=68; // cool to temp
-bool volatile mode=false; // heat or cool mode
+int main() {
+
+ setupAccelerometer();
+ std::srand(time(0)); //seeded with time
-// Callback routine is interrupt activated by a debounced pb1 hit
-void pb1_hit_callback (void)
-{
-// ADD CODE HERE
-}
-// Callback routine is interrupt activated by a debounced pb2 hit
-void pb2_hit_callback (void)
-{
-// ADD CODE HERE
-}
-// Callback routine is interrupt activated by a debounced pb3 hit
-void pb3_hit_callback (void)
-{
-// ADD CODE HERE
-}
+ uLCD.display_control(LANDSCAPE_R);
+ uLCD.cls();
+ uLCD.baudrate(3000000);
+ wait(0.3);
+ uLCD.background_color(BLACK);
+
+ // Intro Screen Message
+ uLCD.text_width(2);
+ uLCD.text_height(2);
+ uLCD.printf("HELP!\nTHEY ARE\nATTACKING");
+ soundOut.PlayNote(100.0,0.15,0.2);
+ soundOut.PlayNote(200.0,0.15,0.2);
+ soundOut.PlayNote(300.0,0.15,0.2);
+ soundOut.PlayNote(400.0,0.15,0.2);
+ soundOut.PlayNote(300.0,0.15,0.2);
+ soundOut.PlayNote(200.0,0.15,0.2);
+ soundOut.PlayNote(100.0,0.15,0.2);
+ wait(2.0);
+
+ pbFire.mode(PullUp);
+ pbFire.attach_deasserted(&pbFire_hit_callback);
+ pbFire.setSampleFrequency();
+ wait(.01);
-int main()
-{
- float Current_temp=0.0;
-
- // Use internal pullups for the three pushbuttons
- pb1.mode(PullUp);
- pb2.mode(PullUp);
- pb3.mode(PullUp);
- // Delay for initial pullup to take effect
- wait(.01);
- // Setup Interrupt callback functions for a pb hit
- pb1.attach_deasserted(&pb1_hit_callback);
- pb2.attach_deasserted(&pb2_hit_callback);
- pb3.attach_deasserted(&pb3_hit_callback);
- // Start sampling pb inputs using interrupts
- pb1.setSampleFrequency();
- pb2.setSampleFrequency();
- pb3.setSampleFrequency();
- // pushbuttons now setup and running
-
+
+ uLCD.cls();
+ ScreenObject *alienObject[6];
+ alienObject[0] = new AlienBob(1);
+ alienObject[1] = new AlienMike(2);
+ alienObject[2] = new AlienJeff(3);
+ alienObject[3] = new AlienBob(4);
+ alienObject[4] = new AlienMike(5);
+ alienObject[5] = new AlienAlice(6);
+ for (int i = 0; i < 6; i++)
+ {
+ alienObject[i]->draw(uLCD);
+ alienObject[i]->setHit(false);
+ }
+
+
+ shipObject.draw(uLCD);
+ timeObject.start();
+ bool endGame = false;
+ int numberOfAliens = 6;
+ bool isHit[6] = {false};
+ while (endGame == false) {
+ accInput();
+ if (bulletObject.getBullet() == true) {
+ bulletObject.fireBullet(shipObject,uLCD);
+ soundOut.PlayNote(100, 0.05, 0.1);
+ soundOut.PlayNote(50, 0.05, 0.3);
+ }
+ //If the ship has a bullet, run this code
+ while (bulletObject.getBullet() == true) {
+ accInput();
+ shipObject.draw(uLCD);
+ bulletObject.update(uLCD);
+
+ for (int i = 0; i < 6; i++) {
+ if (numberOfAliens < 1) {
+ endGame = true;
+ break;
+ }
- // start I/O examples - DELETE THIS IN YOUR CODE..BUT WILL USE THESE I/O IDEAS ELSEWHERE
- // since all this compiles - the needed *.h files for these are in the project
- //
- Current_temp = myTMP36; //Read temp sensor
- printf("Hello PC World\n\r"); // need terminal application running on PC to see this output
- uLCD.printf("\n\rHello LCD World\n\r"); // LCD
- mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz
- myShiftbrite.write( 0, 50 ,0); // Green RGB LED
- // SD card write file example - prints error message on PC when running until SD card hooked up
- // Delete to avoid run time error
- mkdir("/sd/mydir", 0777); // set up directory and permissions
- FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD
- if(fp == NULL) {
- error("Could not open file for write\n");
- }
- fprintf(fp, "Hello SD Card World!"); // write SD
- fclose(fp); // close SD card
- //
- // end I/O examples
-
-
-
-
- // State machine code below will need changes and additions
- while (1) {
- {
- enum Statetype { Heat_off = 0, Heat_on };
- Statetype state = Heat_off;
- while(1) {
- switch (state) {
- case Heat_off:
- myLED4 = 0;
- state = Heat_on;
- break;
- case Heat_on:
- myLED4 = 1;
- state = Heat_off;
- break;
+ isHit[i] = (bulletObject.hitTest(*alienObject[i]));
+
+ if (isHit[i] == true){
+ alienObject[i]->setHit(true);
+ }
+ if (isHit[i] == true) {
+ --numberOfAliens;
+ alienObject[i]->clearScreen(uLCD);
+ soundOut.PlayNote(200, 0.1, 0.1);
+ soundOut.PlayNote(350, 0.1, 0.1);
+
+ uLCD.filled_circle(bulletObject.getxBullet(), bulletObject.getyBullet(), 8, BLACK);
+ bulletObject.setBullet(false);
+ }
+ else
+ if ((alienObject[i]->getHit()) == false) {
+ alienObject[i]->update(uLCD);
}
- wait(0.33);
- // heartbeat LED - common debug tool
- // blinks as long as code is running and not locked up
- myLED1=!myLED1;
}
+ wait(0.01);
+ }
+
+ while (bulletObject.getBullet() == false) {
+ accInput();
+ if (numberOfAliens < 1) {
+ endGame = true;
+ break;
+ }
+
+ shipObject.draw(uLCD);
+ for (int i = 0; i < 6; i++) {
+ if ((alienObject[i]->getHit()) == false){
+ alienObject[i]->update(uLCD);
+ }
+ }
+ wait(0.01);
}
}
-}
\ No newline at end of file
+
+ delete[] alienObject;
+ timeObject.stop();
+ float total = timeObject.read();
+
+ //Winning sound
+ soundOut.PlayNote(1200, 0.1, 0.1);
+ soundOut.PlayNote(900, 0.1, 0.1);
+ soundOut.PlayNote(600, 0.1, 0.1);
+ soundOut.PlayNote(300, 0.1, 0.1);
+ soundOut.PlayNote(100, 0.1, 0.1);
+ soundOut.PlayNote(300, 0.1, 0.1);
+ soundOut.PlayNote(600, 0.1, 0.1);
+ soundOut.PlayNote(900, 0.1, 0.1);
+ soundOut.PlayNote(1200, 0.1, 0.1);
+
+ //Win Screen
+ uLCD.cls();
+ uLCD.printf("\n\n\n\n Congratulations!!\n\n Earth Saved!\n\n\n (%f\n Seconds)",total);
+ wait(4.0);
+ exit(0);
+}//end main