single player mbedKart

Dependencies:   Motor

(notes)

main.cpp

Committer:
sli425
Date:
2018-12-09
Revision:
3:6c749bff51aa
Parent:
2:b57d7156830c
Child:
5:839f7f3e31d6

File content as of revision 3:6c749bff51aa:

#include "mbed.h"
#include "rtos.h"
#include "Motor.h"
#include "rgbled.h"
#include "rgbSensor.h"
#include "SDFileSystem.h"
#include "wave_player.h"

// Define devices
Motor left(p22, p16, p15); // pwm, fwd, rev
Motor right(p23, p19, p20); // pwm, fwd, rev
BusOut myled(LED1,LED2,LED3,LED4);
RGBLed myRGBled(p26, p25, p24); // red, green, blue
rgbSensor rgbsensor(p28, p27);
Serial blue(p13, p14); // serial tx, serial rx
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
AnalogOut DACout(p18);
wave_player waver(&DACout);

//Game state variables
int progress = 0;
int current_item;
int speed;
int max_speed;
int acceleration;
int cstate;
bool colide = false;

// Global game actions
bool game_paused = false;
void pause_game() {
    game_paused = true;
    // code to tell other mbeds game is paused
}

// Thread for detecting item boxes
int sensor_addr = 41 << 1;
void item_thread() {
    // Read data from color sensor (Clear/Red/Green/Blue)
    while (true) { 
        myRGBled.write(1.0,0.0,0.0); //red
        wait(2.0);
        myRGBled.write(0.0,1.0,0.0); //green
        wait(2.0);
        myRGBled.write(0.0,0.0,1.0); //blue
        wait(2.0);
        myRGBled.write(1.0,0.2,0.0); //yellow = red + some green
        wait(2.0);
        //white with a slow fade to black dimming effect
        for (float x=1.0; x>=0.0001; x=x*0.99) {
            myRGBled.write(x, x, x);
            Thread::wait(0.005);
        }
        Thread::wait(100);
    }
}    

// Thread for checking rgb sensor values and updating game variables
// Change later depending on behavior of RGB sensor and colors used
int thresh = 150;   //change this depending on RGB values
void check_RGB() {
    rgbsensor.update();
    int C_value = rgbsensor.get_C();
    int R_value = rgbsensor.get_R();
    int G_value = rgbsensor.get_G();
    int B_value = rgbsensor.get_B();
    if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh && current_item == 0) { //Check for if cart runs over an item box
        current_item = rand() % 4 + 1;      //change this depending on items implemented and random item alg
        cstate = 0;
    }
    else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh) {     //Check for if cart runs over a speed boost panel
        max_speed = 1;      //change this depending on speed up panel algs
        cstate = 0;
    }
    else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh) {     //Check if cart collides with wall
        colide = true;
    }
    else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh) {     //Check if cart reaches next checkpoint. cstate is set to 1 upon seeing first color of checkpoint
        cstate = 1;
    }
    else if(C_value > thresh && R_value > thresh && G_value > thresh && B_value > thresh && cstate == 1) {   
    //Second part of checking if cart reaches next checkpoint. If the previous color was passed through previously, updates checkpoint progress
        cstate = 0;
        progress++;
    }
    else {
        cstate = 0;
    }
}

// Define threads
Thread thread1(item_thread);
Thread thread2(sound_thread);

int main() {
    // Start threads
    thread1.start(item_thread);
    
    // Bluetooth controller code
    char bnum=0;
    char bhit=0;
    while(1) {
        if (blue.getc()=='!') {
            if (blue.getc()=='B') { //button data packet
                bnum = blue.getc(); //button number
                bhit = blue.getc(); //1=hit, 0=release
                if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
                    switch (bnum) {
                        case '1': //number button 1, pause
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                //pause_game();
                            }
                            break;
                        case '2': //number button 2, accelerate
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                left.speed(1.0);
                                right.speed(1.0);
                            } else {
                                left.speed(0.0);
                                right.speed(0.0);
                            }
                            break;
                        case '3': //number button 3
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                //add hit code here
                            } else {
                                //add release code here
                            }
                            break;
                        case '4': //number button 4, brakes
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                //add hit code here
                            } else {
                                //add release code here
                            }
                            break;
                        case '5': //button 5 up arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                //throw_item();
                                //item_box();
                            } else {
                                
                            }
                            break;
                        case '6': //button 6 down arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                left.speed(-1.0);
                                right.speed(-1.0);
                            } else {
                                left.speed(0.0);
                                right.speed(0.0);
                            }
                            break;
                        case '7': //button 7 left arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                left.speed(0.3);
                                right.speed(1.0);
                            } else {
                                left.speed(0.0);
                                right.speed(0.0);
                            }
                            break;
                        case '8': //button 8 right arrow
                            if (bhit=='1') {
                                //myled = bnum - '0'; //current button number will appear on LEDs
                                left.speed(1.0);
                                right.speed(0.3);
                            } else {
                                left.speed(0.0);
                                right.speed(0.0);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}