
ece 4180 project 2019
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem FATFileSystem
main.cpp
- Committer:
- rhuang77
- Date:
- 2019-04-15
- Revision:
- 1:549f56cb4740
- Parent:
- 0:567492543056
- Child:
- 2:270569d33ef1
File content as of revision 1:549f56cb4740:
#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 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; 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; } void song1(void const *args) { } int main() { // SongPlayer mySpeaker(p25); Speaker mySpeaker(p25); interrupt.fall(&fallInterrupt); interrupt.mode(PullUp); uLCD.printf("welcome :)"); wait(2.0); //uLCD.locate(3,3); //uLCD.text_width(4); //uLCD.text_height(4); 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; while (1) { if(true) { //uLCD.printf("starting song"); wait(1.0); while(i <14) { // uLCD.cls(); uLCD.locate(3,3); uLCD.text_width(4); uLCD.text_height(4); uLCD.printf("%s",note[i]); switch(key_code) //uses a case statement for each individual touch sensor { case 1: mySpeaker.PlayNote(261.6256, 0.5, 0.5); break; case 2: mySpeaker.PlayNote(277.1826, 0.5, 0.5); break; case 3: mySpeaker.PlayNote(293.6648, 0.5, 0.5); break; case 4: mySpeaker.PlayNote(311.1270, 0.5, 0.5); break; case 5: mySpeaker.PlayNote(329.6276, 0.5, 0.5); break; case 6: mySpeaker.PlayNote(349.2282, 0.5, 0.5); break; case 7: mySpeaker.PlayNote(369.9944, 0.5, 0.5); break; case 8: mySpeaker.PlayNote(391.9954, 0.5, 0.5); break; case 9: mySpeaker.PlayNote(415.3047, 0.5, 0.5); break; case 10: mySpeaker.PlayNote(440.0, 0.5, 0.5); break; case 11: mySpeaker.PlayNote(466.1638, 0.5, 0.5); break; case 12: mySpeaker.PlayNote(493.8833, 0.5, 0.5); break; } if(song[i]==key_code){ myRGBled.write(0.0,1.0,0.0 ); i++; } else if (song[i] != key_code) { myRGBled.write(1.0,0.0,0.0 ); } else if (key_code == 0) { myRGBled.write(0.0,1.0,0.0 ); } } i = 0; } 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/wavfiles/imperial_march.wav","r"); // waver.play(wave_file); // fclose(wave_file); mySpeaker.PlayNote(261.6256, 0.5, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); 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, 0.5); break; } } } }