ECE 4180 Final

Dependencies:   mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem

main.cpp

Committer:
ShelbyC22
Date:
2019-12-06
Revision:
17:13e45fdcf0b0
Parent:
16:13b65f6139be
Child:
20:7d56cdcbc9a5

File content as of revision 17:13e45fdcf0b0:

#include "mbed.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#include "uLCD_4DGL.h"
#include "mpr121.h"

//setup some color objects in flash using const's

#include "Small_6.h"
#include "Small_7.h"
#include "Arial_9.h"
#include "stdio.h"
#include "C12832_lcd.h"

#include "bubbles.h"
#include "stacys_mom.h"
#include "the_middle.h"
#include "fireflies.h"
#include "sins.h"
#include "LED.hpp"

 
//Setup RGB led using PWM pins and class
RGBLed myRGBled(p24,p23,p22); //RGB PWM pins

char bred=0;
char bgreen=0;
char bblue=0; 

volatile bool songselect = false;
volatile bool homescreen = true;
volatile bool songchange = false;
volatile bool playing = false;
uLCD_4DGL uLCD(p28,p27,p30);
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
DigitalOut myled(LED1);
DigitalIn pb1(p20);
DigitalIn pb2(p19);
AnalogOut DACout(p18);
// Create the interrupt receiver object on pin 26 for touch pad IRQ
InterruptIn interrupt(p26);
// Setup the i2c bus on pins 9 and 10
I2C i2c(p9, p10);
// Setup the Mpr121:
// constructor(i2c object, i2c address of the mpr121)
Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
 
// Key hit/release interrupt routine
void fallInterrupt() {
  int key_code=0;
  int i=0;
  int value=mpr121.read(0x00);
  value +=mpr121.read(0x01)<<8;
  for (i=0; i<12; i++) {
      if (((value>>i)&0x01)==1) {
          key_code=i+1;
      } 
      }
}

wave_player waver(&DACout);
Thread thread1, thread2, thread3;
Ticker nextsample;
int songNdx = 0;

// mutex to make the lcd lib thread safe
Mutex lcd_mutex;
int songnum = 1;
AnalogIn joy_pot(p16);

// Thread 1
// print homescreen to LCD
//new homescreen that doesn't have the flashing 
void homescreen_thread()
{
    while(true) {       // thread loop
        lcd_mutex.lock();
        if (homescreen){
            uLCD.cls();
            uLCD.text_height(1.3);
            uLCD.text_width(1.9);
            uLCD.color(WHITE);
            uLCD.locate(5,0);
            uLCD.printf("Tap Tap");
            uLCD.locate(4,1);
            uLCD.printf("Revolution");
            uLCD.locate(0,3);
            uLCD.printf("Pick a song!");
            uLCD.text_height(1.3);
            uLCD.text_width(1.9);
            uLCD.locate(3,5);
            uLCD.printf("Fireflies");
            uLCD.locate(3,7);
            uLCD.printf("The Middle");
            uLCD.locate(3,9);
            uLCD.printf("Stacy's Mom");
            uLCD.locate(3,11);
            uLCD.printf("I Write Sins \n \t  Not Tragedies");
            uLCD.filled_circle(6, 43, 4, GREEN); //new selection with circle instead of rectangle
            homescreen = false;}
        if (songchange){
            for (int i = 0; i < 4; i++){
            uLCD.filled_circle(6, i*15+43, 4,BLACK);
            }
            uLCD.filled_circle(6, (songnum-1)*15+43, 4,GREEN);
            songchange = false;
            }
            lcd_mutex.unlock();
            Thread::wait(200);
    }
}

// Thread 2
// joystick control for song selection during homescreen
void joystick_thread()
{
   while(1){
            if ((joy_pot <= (1.4/3.3)) && songnum>1) {
                songnum--;
                songchange = true;
                }
            else if ((joy_pot >= (1.8/3.3)) && songnum<4){
                songnum++;
                songchange = true;
                }
        Thread::wait(250);
    }
}

// Thread 3
//pb1 is to select song
//pb2 is to return to homescreen
void pbcontrol_thread()
{
    pb1.mode(PullUp);
    pb2.mode(PullUp);
    while(true) {       // thread loop
        if (!pb2)
            {
            homescreen = true;
            songselect = false;
            }
        if (!pb1)
            {
            songselect = true;
            }
        Thread::wait(100);   // value of pot1 / 100
    }
}

// Thread 4
// this thread plays music using the ticker class
void playsound() {
    if (playing) {
        //DACout = song[songNdx++];
        //if (i>**songlength**) i 
    } else {
        DACout = 0;
        songNdx = 0;
    }
}

int main() {
    interrupt.fall(&fallInterrupt);
    interrupt.mode(PullUp);  //for touch keypad
    lcd_mutex.lock();
    uLCD.baudrate(3000000);
    uLCD.cls();
    uLCD.media_init();
    uLCD.set_sector_address(0x0077, 0x4002);
    uLCD.display_video(0,0);
    wait(4);
    lcd_mutex.unlock();
    thread1.start(homescreen_thread);
    thread2.start(joystick_thread);
    thread3.start(pbcontrol_thread);
//    nextsample.attach(&playsound, 1.0/8000.0);
    //startup sound
    myRGBled = blue; //tested to make sure led works, we can use whatever color(s) here
    FILE *wave_file;
    wave_file=fopen("/sd/cheer.wav","r");
    waver.play(wave_file);
    fclose(wave_file);
    while(1) 
    {
        
        if (songselect){
                myled = 0;
                homescreen = false;
                lcd_mutex.lock();
                uLCD.cls();
                uLCD.printf("You selected song %2d",songnum);
                lcd_mutex.unlock();
                //add case statement based on songnum or something
                //code for playing song from sd
                switch (songnum)
                {
                case 1:
                {
                FILE *wave_file;
                wave_file =fopen("/sd/4180 final project/Fireflies.wav", "r");
                waver.play(wave_file);
                fclose(wave_file);
                break;
                }
                case 2:
                {
                FILE *wave_file;
                wave_file =fopen("/sd/4180 final project/The_Middle.wav", "r");
                waver.play(wave_file);
                fclose(wave_file);
                break;
                }
                case 3:
                {
                FILE *wave_file;
                wave_file =fopen("/sd/4180 final project/Stacy's_Mom.wav", "r");
                waver.play(wave_file);
                fclose(wave_file);
                break;
                }
                case 4:
                {
                FILE *wave_file;
                wave_file =fopen("/sd/4180 final project/Sins_!_Tragedies.wav", "r");
                waver.play(wave_file);
                fclose(wave_file);
                break;
                }
                default:
                break;
            }
                
                }
        Thread::wait(100);    
        }
}