Sound update

Dependencies:   4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed

main.cpp

Committer:
dangulo7
Date:
22 months ago
Revision:
24:3b29ae4d6171
Parent:
23:214c0c828d98
Child:
25:c3eb6c1a2dfb

File content as of revision 24:3b29ae4d6171:

#include "mbed.h"
#include "rtos.h"

#include "globals.h"
#include "hockey.h"
#include "tetris.h"

#include "Speaker.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"



///////////////////////////
// GLOBALS.H DEFINITIONS //
///////////////////////////
// The following variables are declared as extern in "globals.h", and we define
// them here:
volatile bool game1 = false;
volatile bool game2 = false;

Serial pc(USBTX, USBRX);
Mutex pc_mutex;

//uLCD_4DGL uLCD(p28, p27, p29);
uLCD_4DGL uLCD(p13,p14,p30);
Mutex uLCD_mutex;

BluefruitController blue(p28,p27);
//BusIn navSwitch(p15, p16, p17, p19, p20);
Nav_Switch myNav(p20, p16, p17, p15, p19); // pins(up, down, left, right, fire)

SDFileSystem sd(p5, p6, p7, p8, "sd");
PwmOut Rgb(p23);
PwmOut rGb(p24);
PwmOut rgB(p25);
AnalogOut DACout(p18);
wave_player waver(&DACout);



////////////////////////////////
// MAIN.CPP LOCAL DEFINITIONS //
////////////////////////////////
// The following variables are *not* from "globals.h" and are not meant for
// reference outside of main.cpp
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);



//////////////////////
// MAIN.CPP THREADS //
//////////////////////
void audioThread()
{
    while (true) {
        FILE *wave_file;
        while (game1 == false && game2 == false) {
            led2 = true;
            wave_file=fopen("/sd/MiiMenu.wav","r");
            waver.play(wave_file);
            fclose(wave_file);
            led2 = false;
        }
        while (game1 == true && game2 == false) {
            led2 = true;
            wave_file=fopen("/sd/tetris.wav","r");
            waver.play(wave_file);
            fclose(wave_file);
            led2 = false;
        }
        while (game2 == true && game1 == false) {
            led2 = true;
            wave_file=fopen("/sd/WiiPlayAirHockey.wav","r");
            waver.play(wave_file);
            fclose(wave_file);
            led2 = false;
        }
    }
}

void ledThread()
{
    float x = 0.0;
    while(1) {
        //get a new random number for PWM
        x = rand() / float(RAND_MAX);
        //add some exponential brightness scaling
        //for more of a fast flash effect
        x = x*x*x;
        Rgb = x;
        rGb = x;
        rgB = x;
        //fast update rate for welding flashes
        Thread::wait(20);
        //add a random pause between welds
        if (rand() / float(RAND_MAX) > 0.95) {
            Rgb = 0;
            rGb = 0;
            rgB = 0;
            Thread::wait(4000.0 * rand() / float(RAND_MAX));
        }
    }
}

int main()
{
    // Launch four threads
    Thread thread1(tetrisGame, osPriorityHigh);
    Thread thread2(hockeyGame, osPriorityHigh);
    //Thread thread3(audioThread, osPriorityLow);
    Thread thread4(ledThread, osPriorityLow);
    int x = 0;
    // The main thread goes on to blink LEDs
    while (true) {
        led1 = 1;
        Thread::wait(500);
        led1 = 0;
        Thread::wait(500);
        if(!game1 && !game2 && x==0){
            uLCD.color(WHITE);
            uLCD.text_height(1);
            uLCD.text_width(1);
            uLCD.printf("UP:\n");
            uLCD.printf("Tetris \n");
            uLCD.printf("\n");
            uLCD.printf("DOWN:\n");
            uLCD.printf("Air\n");
            uLCD.printf("Hockey");
            x=1;
        }
        
        PRINTF("[MAIN] Thread stacks: %lu %lu %lu %lu\r\n",
            thread1.used_stack(),
            thread2.used_stack(),
            //thread3.used_stack(),
            thread4.used_stack());
        
        // Launch into the air hockey game
        if (game1 != true && myNav.up()){
            uLCD.cls();
            game1 = true;
            }
        if (game2 != true && myNav.down()){
            uLCD.cls();
            game2 = true;
            }
    }
}