ece 4180 project 2019
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem FATFileSystem
main.cpp
- Committer:
- rhuang77
- Date:
- 2019-04-20
- Revision:
- 2:270569d33ef1
- Parent:
- 1:549f56cb4740
- Child:
- 3:221fb009c1ac
File content as of revision 2:270569d33ef1:
#include <mbed.h> #include <mpr121.h> #include "mbed.h" #include "uLCD_4DGL.h" #include "SDFileSystem.h" #include "FATFileSystem.h" #include "wave_player.h" #include <stdio.h> #include "Speaker.h" #include "rtos.h" //Class to control an RGB LED using three PWM pins class RGBLed { public: RGBLed(PinName redpin, PinName greenpin, PinName bluepin); void write(float red,float green, float blue); private: PwmOut _redpin; PwmOut _greenpin; PwmOut _bluepin; }; RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin) : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin) { //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker) _redpin.period(0.0005); } void RGBLed::write(float red,float green, float blue) { _redpin = red; _greenpin = green; _bluepin = blue; } //Setup RGB led using PWM pins and class RGBLed myRGBled(p21,p22,p23); //RGB PWM pins //Bluetooth Serial blue(p28,p27); Serial pc(USBTX, USBRX); DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); // Create the interrupt receiver object on pin 26 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); SDFileSystem sd(p5, p6, p7, p8, "sd"); uLCD_4DGL uLCD(p13,p14,p11); AnalogOut DACout(p18); wave_player waver(&DACout); FILE *wave_file; Mutex stdio_mutex; int key_code; // Key hit/release interrupt routine void fallInterrupt() { key_code=0; int i=0; int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; // LED demo mod i=0; // puts key number out to LEDs for demo for (i=0; i<12; i++) { if (((value>>i)&0x01)==1) key_code=i+1; } led4=key_code & 0x01; led3=(key_code>>1) & 0x01; led2=(key_code>>2) & 0x01; led1=(key_code>>3) & 0x01; } int songcode = -1; int isplay = 0; int islearn = 0; float volume = 0.25; int octave = 3; void readblue() { while(1) { char bnum = 0; if (blue.readable()) { if (blue.getc()=='!') { if (blue.getc() == 'I') { bnum = blue.getc(); //button mode if (bnum == 'B') { bnum = blue.getc(); //play song if (bnum == '0') { islearn = 0; isplay = 1; songcode = blue.getc() - '0'; } //learn song if (bnum == '1') { islearn = 1; isplay = 0; songcode = blue.getc() - '0'; } } //seekbar mode if (bnum == 'S') { islearn = 0; isplay = 0; bnum = blue.getc(); //octave change if (bnum == '0') { octave = blue.getc() - '0'; } //volume change if (bnum == '1') { //get tens digit first int actualVol = blue.getc() - '0'; actualVol *= 10; //then get ones digit actualVol += blue.getc() - '0'; volume = (float)actualVol/(float)100; } } } } } pc.printf("bye\n"); Thread::wait(200); } } int main() { Speaker mySpeaker(p25); interrupt.fall(&fallInterrupt); interrupt.mode(PullUp); uLCD.printf("welcome :)\n"); wait(1.0); uLCD.printf("Initializing...\n"); wait(1.0); uLCD.cls(); int song[14] = {1, 1,8,8,10,10,8,6,6,5,5,3,3,1}; char note[14] = {'C','C','G','G','A','A','G','F','F','E','E','D','D','C'}; int i= 0; Thread thread; //thread.start(callback(readblue, (void *)"foo")); thread.start(readblue); while (1) { // check blutooth input //readblue(); //char bnum = 0; // char bnum1 = 0; // char bnum // bnum = blue.getc(); if(false) { uLCD.printf("starting \nTwinkle Twinkle \nLittle Star"); wait(4.0); uLCD.cls(); while(i <14) { uLCD.text_width(3); uLCD.text_height(3); uLCD.locate(2,2); uLCD.printf("%c",note[i]); if(song[i]==key_code) { myRGBled.write(0.0,1.0,0.0 ); uLCD.cls(); i++; } else if (song[i] != key_code && key_code != 0) { myRGBled.write(1.0,0.0,0.0 ); } else if (key_code == 0) { myRGBled.write(0.0,0.0,0.0 ); } switch(key_code) { //uses a case statement for each individual touch sensor case 1: mySpeaker.PlayNote(261.6256, 0.5, volume); break; case 2: mySpeaker.PlayNote(277.1826, 0.5, volume); break; case 3: mySpeaker.PlayNote(293.6648, 0.5, volume); break; case 4: mySpeaker.PlayNote(311.1270, 0.5, volume); break; case 5: mySpeaker.PlayNote(329.6276, 0.5, volume); break; case 6: mySpeaker.PlayNote(349.2282, 0.5, volume); break; case 7: mySpeaker.PlayNote(369.9944, 0.5, volume); break; case 8: mySpeaker.PlayNote(391.9954, 0.5, volume); break; case 9: mySpeaker.PlayNote(415.3047, 0.5, volume); break; case 10: mySpeaker.PlayNote(440.0, 0.5, volume); break; case 11: mySpeaker.PlayNote(466.1638, 0.5, volume); break; case 12: mySpeaker.PlayNote(493.8833, 0.5, volume); break; } } i = 0; uLCD.printf("Congratulations! \nYou learned \nTwinkle Twinkle \nLittle Star"); wait(4.0); uLCD.cls(); } else if(false) { uLCD.printf("Playing song"); wait(2.0); wave_file=fopen("/sd/songs/mary.wav","r"); waver.play(wave_file); fclose(wave_file); // wave_file=fopen("/sd/wavfiles/imperial_march.wav","r"); // waver.play(wave_file); // fclose(wave_file); } else { switch(key_code) { //uses a case statement for each individual touch sensor case 1: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("C"); //printf("%d", key_code); myRGBled.write(0.0,0.0,1.0 ); // wave_file=fopen("/sd/songs/mary.wav","r"); // if(wave_file==NULL) printf("file open error!\n\n\r"); // printf("test"); // waver.play(wave_file); // fclose(wave_file); mySpeaker.PlayNote(261.6256, 0.5, volume); pc.printf("hello\n"); break; case 2: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("CS"); myRGBled.write(1.0,0.0,0.0 ); mySpeaker.PlayNote(277.1826, 0.5, volume); break; case 3: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("D"); myRGBled.write(0.0,1.0,0.0 ); mySpeaker.PlayNote(293.6648, 0.5, volume); break; case 4: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("DS"); mySpeaker.PlayNote(311.1270, 0.5, volume); break; case 5: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("E"); mySpeaker.PlayNote(329.6276, 0.5, volume); break; case 6: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("F"); mySpeaker.PlayNote(349.2282, 0.5, volume); break; case 7: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("FS"); mySpeaker.PlayNote(369.9944, 0.5, volume); break; case 8: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("G"); mySpeaker.PlayNote(391.9954, 0.5, volume); break; case 9: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("GS"); mySpeaker.PlayNote(415.3047, 0.5, volume); break; case 10: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("A"); mySpeaker.PlayNote(440.0, 0.5, volume); break; case 11: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("AS"); mySpeaker.PlayNote(466.1638, 0.5, volume); break; case 12: uLCD.cls(); uLCD.locate(6,6); uLCD.text_width(5); uLCD.text_height(5); uLCD.printf("B"); mySpeaker.PlayNote(493.8833, 0.5, volume); break; } } Thread::wait(200); } }