Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <msp430x14x.h>
- #include "uart.h"
- #include "portyLcd.h"
- #include "lcd.h"
- #include "portyUart.h"
- struct Note
- {
- int comboValue;
- int frequency;
- int delay;
- int length;
- Note(int combo, int Freq, int Delay, int Length)
- {
- comboValue = combo;
- frequency = Freq;
- delay = Delay;
- length = Length;
- }
- Note()
- {
- comboValue = 0;
- frequency = 0;
- delay = 1;
- length = 1;
- }
- };
- int zeroFrequencesInProgram = 1;
- char Bufor[30]; // bufor odczytywanych danych
- int low=0; // znacznik począteku danych w buforze
- int high=0; // zmacznik końca danych w buforze
- Note cycleValues[16];
- int cycleCurrentIndex= 0;
- int cycleBuforLength = 16;
- int cycleTemp[16];
- int currScore = 0;
- int maxScore = 0;
- int Button1Pressed;
- int Button2Pressed;
- int Button3Pressed;
- int Button4Pressed;
- const int songLength = 50;
- int Mario_bf [songLength]; //bf/10
- int Mario_lg [songLength]; //lg/10
- int Mario_del [songLength]; //del
- int mario_key[songLength];
- int* songLengths;
- int* songNotesFrequences;
- int* songNotesDelays;
- int* answerKeys;
- int keyboardPressed = 0;
- int notePlay = 1;
- int isPlaying = 0;
- int demoLength = 7;
- void ShowScreen(int values[]);
- Note songNotes[songLength];
- void MakeSound(long freq, long czasBuzzowania);
- void AddScore()
- {
- currScore += 1;
- }
- int GetScore()
- {
- return currScore;
- }
- // To jest wciskanie buttonow na msp, tego nie uzywamy - kolo ratunkowe
- void ButtonPress()
- {
- if(!(P4IN & BIT4)) {
- Bufor[high] = 'q';
- high = (++high) % 30;
- }
- if(!(P4IN & BIT5)) {
- Bufor[high] = 'w';
- high = (++high) % 30;
- }
- if(!(P4IN & BIT6)) {
- Bufor[high] = 'e';
- high = (++high) % 30;
- }
- if(!(P4IN & BIT7)) {
- Bufor[high] = 'r';
- high = (++high) % 30;
- }
- }
- void ShowEndScreen()
- {
- clearDisplay();
- char endDisplay[16] = { 'S', 'c', 'o', 'r','e', ':'};
- endDisplay[7] = currScore/100 + 48;
- currScore %= 100;
- endDisplay[8] = currScore/10 + 48;
- currScore %= 10;
- endDisplay[9] = currScore + 48;
- endDisplay[10] = '/';
- endDisplay[11] = maxScore/100 + 48;
- maxScore %= 100;
- endDisplay[12] = maxScore/10 + 48;
- maxScore %= 10;
- endDisplay[13] = maxScore + 48;
- SEND_STRING(1, 16, endDisplay);
- MakeSound(100, 5000);
- }
- void CreateSong()
- {
- answerKeys = mario_key;
- songLengths = Mario_lg;
- songNotesFrequences = Mario_bf;
- songNotesDelays = Mario_del;
- for(int i = 0; i < songLength; i++)
- {
- songNotes[i] = Note(answerKeys[i], songNotesFrequences[i], songNotesDelays[i], songLengths[i]);
- }
- }
- void Update()
- {
- if(keyboardPressed>0)
- {
- low=high;
- keyboardPressed--;
- }
- ButtonPress();
- Button1Pressed = false;
- Button2Pressed = false;
- Button3Pressed = false;
- Button4Pressed = false;
- while(high != low) // gdy odebrano dane
- {
- if(Bufor[low] == 'q')
- Button1Pressed = true;
- if(Bufor[low] == 'w')
- Button2Pressed = true;
- if(Bufor[low] == 'e')
- Button3Pressed = true;
- if(Bufor[low] == 'r')
- Button4Pressed = true;
- low = (low+1)%30;
- }
- }
- void MakeSound(long freq, long czasBuzzowania)
- {
- if(freq != 0)
- {
- freq = 750000/freq;
- czasBuzzowania = (long)(czasBuzzowania * (100 / (float)freq));
- //czasBuzzowania /= 4;
- for(long i = 0; i < czasBuzzowania; i++)
- {
- P4OUT &= ~BIT2;
- P4OUT |= BIT3;
- for(long int j = 0; j < 1 * freq; j++) { }
- P4OUT |= BIT2;
- P4OUT &= ~BIT3;
- for(long int j = 0; j < 1 * freq; j++) { }
- }
- }
- else
- {
- int bcaa = 0;
- for(long x = 0; x < czasBuzzowania; x++)
- {
- for(long u = 0; u < 600; u++)
- {
- for(long f = 0; f < 600; f++)
- {
- bcaa += 1; // dodatkowe obliczenia
- bcaa %= 100;// ktore daja pewniejsze opoznienie
- }
- }
- }
- }
- }
- bool TakeInput(int playerControllerInput, Note note)
- {
- if(note . comboValue == playerControllerInput && note.comboValue != 0)
- {
- AddScore();
- return true;
- }
- else
- {
- return false;
- }
- }
- void Push(Note value)
- {
- cycleValues[cycleCurrentIndex] = value;
- cycleCurrentIndex = (cycleCurrentIndex+1) % 16;
- }
- int* GetValues()
- {
- int index = 0;
- for(int i = cycleCurrentIndex - 1; i >=0; i--)
- {
- cycleTemp[index++] = cycleValues[i] . comboValue;
- }
- for(int i = cycleBuforLength-1; i >= cycleCurrentIndex; i--)
- {
- cycleTemp[index++] = cycleValues[i] . comboValue;
- }
- return cycleTemp;
- }
- int GetCurrentClickedKeys()
- {
- int value = 0;
- if(Button1Pressed)
- value+=1;
- if(Button2Pressed)
- value+=2;
- if(Button3Pressed)
- value+=4;
- if(Button4Pressed)
- value+=8;
- return value;
- }
- Note GetLastCheckNote()
- {
- return cycleValues[(cycleCurrentIndex)%16];
- }
- void PushNote(Note note)
- {
- Push(note);
- if(isPlaying == 1)
- {
- ShowScreen(GetValues());
- }
- Note comboValueNeeded = GetLastCheckNote();
- MakeSound(comboValueNeeded . frequency, 200);
- if(isPlaying == 1)
- {
- Update();
- TakeInput(GetCurrentClickedKeys(), comboValueNeeded);
- }
- }
- void FillMarioSong()
- { Mario_bf[0]=660; Mario_lg[0]=1;
- Mario_del[0]= 2; mario_key[0] = 12;
- Mario_bf[1]=660; Mario_lg[1]=1;
- Mario_del[1]= 3; mario_key[1] = 8;
- Mario_bf[2]=660; Mario_lg[2]=1;
- Mario_del[2]= 3; mario_key[2] = 2;
- Mario_bf[3]=510; Mario_lg[3]=1;
- Mario_del[3]= 1; mario_key[3] = 10;
- Mario_bf[4]=660; Mario_lg[4]=1;
- Mario_del[4]= 3; mario_key[4] = 4;
- Mario_bf[5]=770; Mario_lg[5]=1;
- Mario_del[5]= 6; mario_key[5] = 3;
- Mario_bf[6]=380; Mario_lg[6]=1;
- Mario_del[6]= 6; mario_key[6] = 7;
- Mario_bf[7]=510; Mario_lg[7]=1;
- Mario_del[7]= 5; mario_key[7] = 7;
- Mario_bf[8]=380; Mario_lg[8]=1;
- Mario_del[8]= 4; mario_key[8] = 11;
- Mario_bf[9]=320; Mario_lg[9]=1;
- Mario_del[9]= 5; mario_key[9] = 2;
- Mario_bf[10]=440; Mario_lg[10]=1;
- Mario_del[10]= 3; mario_key[10] = 1;
- Mario_bf[11]=480; Mario_lg[11]=1;
- Mario_del[11]= 3; mario_key[11] = 11;
- Mario_bf[12]=450; Mario_lg[12]=1;
- Mario_del[12]= 2; mario_key[12] = 10;
- Mario_bf[13]=430; Mario_lg[13]=1;
- Mario_del[13]= 3; mario_key[13] = 15;
- Mario_bf[14]=380; Mario_lg[14]=1;
- Mario_del[14]= 2; mario_key[14] = 6;
- Mario_bf[15]=660; Mario_lg[15]=1;
- Mario_del[15]= 2; mario_key[15] = 13;
- Mario_bf[16]=760; Mario_lg[16]=1;
- Mario_del[16]= 2; mario_key[16] = 12;
- Mario_bf[17]=860; Mario_lg[17]=1;
- Mario_del[17]= 3; mario_key[17] = 12;
- Mario_bf[18]=700; Mario_lg[18]=1; mario_key[18] = 11;
- Mario_del[18]= 2;
- Mario_bf[19]=760; Mario_lg[19]=1; mario_key[19] = 1;
- Mario_del[19]= 4;
- Mario_bf[20]=660; Mario_lg[20]=1; mario_key[20] = 6;
- Mario_del[20]= 3;
- Mario_bf[21]=520; Mario_lg[21]=1; mario_key[21] = 15;
- Mario_del[21]= 2;
- Mario_bf[22]=580; Mario_lg[22]=1; mario_key[22] = 2;
- Mario_del[22]= 2;
- Mario_bf[23]=480; Mario_lg[23]=1; mario_key[23] = 10;
- Mario_del[23]= 5;
- Mario_bf[24]=510; Mario_lg[24]=1; mario_key[24] = 14;
- Mario_del[24]= 5;
- Mario_bf[25]=380; Mario_lg[25]=1; mario_key[25] = 13;
- Mario_del[25]= 4;
- Mario_bf[26]=320; Mario_lg[26]=1; mario_key[26] = 13;
- Mario_del[26]= 5;
- Mario_bf[27]=440; Mario_lg[27]=1; mario_key[27] = 11;
- Mario_del[27]= 3;
- Mario_bf[28]=480; Mario_lg[28]=1; mario_key[28] = 1;
- Mario_del[28]= 3;
- Mario_bf[29]=450; Mario_lg[29]=1; mario_key[29] = 4;
- Mario_del[29]= 2;
- Mario_bf[30]=430; Mario_lg[30]=1; mario_key[30] = 7;
- Mario_del[30]= 3;
- Mario_bf[31]=380; Mario_lg[31]=1; mario_key[31] = 8;
- Mario_del[31]= 2;
- Mario_bf[32]=660; Mario_lg[32]=1; mario_key[32] = 6;
- Mario_del[32]= 2;
- Mario_bf[33]=760; Mario_lg[33]=1; mario_key[33] = 15;
- Mario_del[33]= 2;
- Mario_bf[34]=860; Mario_lg[34]=1; mario_key[34] = 1;
- Mario_del[34]= 3;
- Mario_bf[35]=700; mario_key[35] = 13; Mario_lg[35]=1;
- Mario_del[35]= 2;
- Mario_bf[36]=760; mario_key[36] = 1; Mario_lg[36]=1;
- Mario_del[36]= 4;
- Mario_bf[37]=660; mario_key[37] = 7; Mario_lg[37]=1;
- Mario_del[37]= 3;
- Mario_bf[38]=520; mario_key[38] = 5; Mario_lg[38]=1;
- Mario_del[38]= 2;
- Mario_bf[39]=580; mario_key[39] = 14; Mario_lg[39]=1;
- Mario_del[39]= 2;
- Mario_bf[40]=480; mario_key[40] = 9; Mario_lg[40]=1;
- Mario_del[40]= 5;
- Mario_bf[41]=500; mario_key[41] = 9;0; Mario_lg[41]=1;
- Mario_del[41]= 3; mario_key[42] = 11;
- Mario_bf[42]=760; Mario_lg[42]=1;
- Mario_del[42]= 1; mario_key[43] = 11;
- Mario_bf[43]=720; Mario_lg[43]=1;
- Mario_del[43]= 2; mario_key[44] = 2;
- Mario_bf[44]=680; Mario_lg[44]=1;
- Mario_del[44]= 2; mario_key[45] = 5;
- Mario_bf[45]=620; Mario_lg[45]=2;
- Mario_del[45]= 3; mario_key[46] = 13;
- Mario_bf[46]=650; Mario_lg[46]=2;
- Mario_del[46]= 3; mario_key[47] = 3;
- Mario_bf[47]=380; Mario_lg[47]=1;
- Mario_del[47]= 2; mario_key[48] = 8;
- Mario_bf[48]=430; Mario_lg[48]=1;
- Mario_del[48]= 2; mario_key[49] = 13;
- Mario_bf[49]=500; Mario_lg[49]=1;
- Mario_del[49]= 3;
- songLengths = Mario_lg;
- songNotesFrequences = Mario_bf;
- songNotesDelays = Mario_del;
- answerKeys = mario_key;
- }
- void ResetScore()
- {
- currScore = 0;
- }
- void SongPlaying(int length)
- {
- int noteIndex = 0;
- for(int i = 0; i < length + 18; i++)
- {
- Note noteToSend = Note(0, 0, 0 , 0);
- Note currentNote;
- if(i < length)
- {
- currentNote = songNotes[i];
- for(int j = 0; j < currentNote.delay; j++)
- {
- if(j < currentNote.length)
- {
- if(noteIndex == 0)
- {
- noteToSend . comboValue = currentNote.comboValue;
- maxScore += 1;
- }
- else
- noteToSend . comboValue = 0;
- noteIndex = (noteIndex + 1) % notePlay;
- noteToSend . frequency = currentNote.frequency;
- }
- else
- {
- noteToSend . comboValue = 0;
- noteToSend . frequency = 0;
- }
- PushNote(noteToSend);
- }
- }
- else
- {
- noteToSend . comboValue = 0;
- noteToSend . frequency = 0;
- PushNote(noteToSend);
- }
- }
- if(isPlaying == 1)
- {
- ShowEndScreen();
- }
- currScore = 0;
- maxScore = 0;
- low=high;
- isPlaying = 0;
- }
- void LCDController()
- {
- char signs[4][8] =
- {
- { 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00 }, // BOTh EMPTY 0x01
- { 0x1F, 0x10, 0x10, 0x1f, 0x1f, 0x10, 0x10, 0x1f }, // BOTh BUSY 0x02
- { 0x00, 0x0e, 0x00, 0x00, 0x1f, 0x10, 0x10, 0x1f }, // BOT BUSY 0x03
- { 0x1f, 0x10, 0x10, 0x1f, 0x00, 0x0e, 0x00, 0x00 }, // TOP BUSY 0x04
- };
- SEND_CMD(CG_RAM_ADDR);
- for(int i = 0; i < 4; i++)
- for(int j = 0; j < 8; j++)
- SEND_CHAR(signs[i][j]);
- }
- void InitBuzzerBits()
- {
- P4DIR |= BIT2;
- P4DIR |= BIT3;
- P4DIR &= ~BIT4;
- P4DIR &= ~BIT5;
- P4DIR &= ~BIT6;
- P4DIR &= ~BIT7;
- }
- void PrepareCustomChars()
- {
- char signs[4][8] =
- {
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
- {0x00,0x1f,0x1f,0x00,0x00,0x1f,0x1f,0x00},
- {0x00,0x00,0x00,0x00,0x00,0x1f,0x1f,0x00},
- {0x00,0x1f,0x1f,0x00,0x00,0x00,0x00,0x00}
- };
- SEND_CMD(CG_RAM_ADDR);
- for(int i = 0; i < 4; i++)
- for(int j = 0; j < 8; j++)
- SEND_CHAR(signs[i][j]);
- SEND_CMD(DD_RAM_ADDR);
- }
- void WyswitlEkranWstepu()
- {
- // wyswietlanie napisu MSPHero - Godlike UART, czy cos - miejsce na smieszek
- SEND_CMD(DD_RAM_ADDR);
- SEND_STRING(1,16," MSPHero ");// TODO
- SongPlaying(demoLength);
- }
- int WyswietlMenu()
- {
- int chosenOption = 0;
- SEND_CMD(DD_RAM_ADDR);
- SEND_STRING(1, 16, " MSPHero - Menu: ");
- SEND_CMD(DD_RAM_ADDR2);
- SEND_STRING(2, 16, "Lvl 1-4: q,w,e,r");
- // wyswitlanie opcji:
- // Zagraj piosenke:
- // 1. Mario.
- // 2. Metallica.
- while(high != low) // gdy odebrano dane
- {
- if(Bufor[low] == 'q'){
- chosenOption = 1;
- SEND_CMD(DD_RAM_ADDR);
- low=high;
- break;
- }
- else if(Bufor[low] == 'w'){
- chosenOption = 2;
- SEND_CMD(DD_RAM_ADDR);
- low=high;
- break;
- }
- else if(Bufor[low] == 'e'){
- chosenOption = 3;
- SEND_CMD(DD_RAM_ADDR);
- low=high;
- break;
- }
- else if(Bufor[low] == 'r'){
- chosenOption = 4;
- SEND_CMD(DD_RAM_ADDR);
- low=high;
- break;
- }
- else{
- low = (++low)%30;
- chosenOption = 0;
- }
- }
- return chosenOption;
- }
- void PlaySong(int index)
- {
- MakeSound(10, 1000);
- MakeSound(50, 1000);
- MakeSound(100, 1000);
- MakeSound(400, 1000);
- MakeSound(1000, 1000);
- MakeSound(10, 1000);
- MakeSound(100, 1000);
- MakeSound(1000, 1000);
- ResetScore();
- if(index != 0)
- {
- SongPlaying(songLength);
- }
- }
- int main( void )
- {
- P4DIR |= BIT2;
- P4DIR |= BIT3;
- P4DIR &= ~BIT4;
- P4DIR &= ~BIT5;
- P4DIR &= ~BIT6;
- P4DIR &= ~BIT7;
- WDTCTL=WDTPW + WDTHOLD; // wyłączenie WDT
- // Timer_A ustawiamy na 500 kHz
- // a przerwanie generujemy co 100 ms
- TACTL = TASSEL_1 + MC_1 +ID_3; // Wybieram ACLK, ACLK/8=500kHz,tryb Up
- CCTL0 = CCIE; // włączenie przerwań od CCR0
- CCR0=50000; // podzielnik 50000: przerwanie co 100 ms
- InitPortsLcd(); // inicjalizacja portów LCD
- InitLCD(); // inicjalizacja LCD
- clearDisplay(); // czyszczenie wyświetlacza
- initPortyUart(); // inicjalizacja portow UART
- initUart(1200); //ma byc 15200
- // inicjalizacja UARTa
- _EINT(); //TODO // włączenie przerwań
- SEND_CMD(DD_RAM_ADDR);
- PrepareCustomChars();
- InitBuzzerBits();
- for(int i = 0; i < 16; i++)
- {
- Note emptyNote;
- cycleValues[i] = emptyNote;
- }
- FillMarioSong();
- CreateSong();
- WyswitlEkranWstepu();
- while(1)
- {
- int chosenOption = WyswietlMenu();
- ButtonPress();
- if(chosenOption != 0)
- {
- switch(chosenOption)
- {
- case 1:
- notePlay = 7;
- break;
- case 2:
- notePlay = 5;
- break;
- case 3:
- notePlay = 3;
- break;
- case 4:
- notePlay = 1;
- break;
- }
- clearDisplay();
- isPlaying = true;
- PlaySong(chosenOption);
- }
- }
- }
- void ShowScreen(int values[])
- {
- int tab[16][2];
- for(int y = 0; y < 16; y++)
- {
- int wart[2] = {0, 0};
- int sprawdzanyBit = 8;
- int sprawdzanaWartosc = values[y];
- while(sprawdzanyBit != 0)
- {
- if(sprawdzanaWartosc >= sprawdzanyBit)
- {
- if(sprawdzanyBit == 8)
- wart[0] += 1;
- if(sprawdzanyBit == 4)
- wart[0] += 2;
- if(sprawdzanyBit == 2)
- wart[1] += 1;
- if(sprawdzanyBit == 1)
- wart[1] += 2;
- sprawdzanaWartosc -= sprawdzanyBit;
- }
- sprawdzanyBit /= 2;
- }
- for(int o = 0; o < 2; o++)
- {
- // BOTh EMPTY 0x00
- // BOTh BUSY 0x01
- // BOT BUSY 0x02
- // TOP BUSY 0x03
- if(wart[o] == 0)
- tab[y][o] = 0x00;
- else if(wart[o] == 1)
- tab[y][o] = 0x03;
- else if(wart[o] == 2)
- tab[y][o] = 0x02;
- else if(wart[o] == 3)
- tab[y][o] = 0x01;
- }
- }
- for(int j = 0; j < 2; j++)
- {
- if(j == 0)
- SEND_CMD(DD_RAM_ADDR);
- else
- SEND_CMD(DD_RAM_ADDR2);
- for(int i = 0; i < 16; i++)
- {
- SEND_CHAR(tab[i][j]);
- }
- }
- }
- #pragma vector = UART0RX_VECTOR // procedura obsługi przerwania UART
- __interrupt void usart0_rx (void)
- {
- keyboardPressed = 8;
- Bufor[high] = RXBUF0; // wpisanie odebranych danych do bufora
- high = (++high) % 30; // inkrementowanie znacznika końca danych
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement