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()
- {}
- };
- 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;
- int cycleTemp[16];
- int cycleNotesPushed = 0;
- int currScore = 0;
- int Button1Pressed;
- int Button2Pressed;
- int Button3Pressed;
- int Button4Pressed;
- int Mario_bf [7]; //bf/10
- int Mario_lg [7]; //lg/10
- int Mario_del [7]; //del
- int mario_key[7];
- int* songLengths;
- int* songNotesFrequences;
- int* songNotesDelays;
- int* answerKeys;
- int songLength = 7;
- void ShowScreen(int values[]);
- Note songNotes[7];
- void AddScore()
- {
- currScore += 1;
- }
- int GetScore()
- {
- return currScore;
- }
- void ShowEndScreen()
- {
- clearDisplay();
- char endDisplay[16] = { ' ', 'Y', 'o', 'u', 'r', ' ', 's', 'c', 'o', 'r','e', ':', ' '};
- endDisplay[13] = currScore/100 + 48;
- currScore /= 100;
- endDisplay[14] = currScore/10 + 48;
- currScore /= 10;
- endDisplay[15] = currScore + 48;
- SEND_STRING(1, 16, endDisplay);
- }
- 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;
- }
- void CreateSong(int notesSize)
- {
- songLength = notesSize;
- answerKeys = mario_key;
- songLengths = Mario_lg;
- songNotesFrequences = Mario_bf;
- songNotesDelays = Mario_del;
- for(int i = 0; i < notesSize; i++)
- {
- songNotes[i] = Note(answerKeys[i], songNotesFrequences[i], songNotesDelays[i], songLengths[i]);
- }
- }
- void Update()
- {
- 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(int freq, int czasBuzzowania)
- {
- for(int i = 0; i < czasBuzzowania; i++)
- {
- P4OUT &= ~BIT2;
- P4OUT |= BIT3;
- for(long int i = 0; i < 1 * freq; i++) { }
- P4OUT |= BIT2;
- P4OUT &= ~BIT3;
- for(long int i = 0; i < 1 * freq; i++) { }
- }
- }
- bool TakeInput(int playerControllerInput, Note *note)
- {
- if(note -> comboValue == playerControllerInput)
- {
- MakeSound(note -> frequency, 1000);
- AddScore();
- return true;
- }
- else
- {
- MakeSound((int)(note -> frequency), 1000); // TODO
- return false;
- }
- }
- void Push(Note *value)
- {
- if(cycleNotesPushed < 16)
- cycleNotesPushed += 1;
- cycleValues[cycleCurrentIndex] = value;
- cycleCurrentIndex = (cycleCurrentIndex+1) % 16;
- }
- 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+1)%16];
- }
- void PushNote(Note *note)
- {
- Update();
- Push(note);
- Note* comboValueNeeded = GetLastCheckNote();
- ShowScreen(GetValues());
- 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;
- songLengths = Mario_lg;
- songNotesFrequences = Mario_bf;
- songNotesDelays = Mario_del;
- answerKeys = mario_key;
- }
- // To jest wciskanie buttonow na msp, tego nie uzywamy - kolo ratunkowe
- /*void ButtonPress()
- {
- if(isButton1Pressed) {
- UartStringTransmit("q");
- }
- if(isButton2Pressed) {
- UartStringTransmit("w");
- }
- if(isButton3Pressed) {
- UartStringTransmit("e");
- }
- if(isButton4Pressed) {
- UartStringTransmit("r");
- }
- } */
- void ResetScore()
- {
- currScore = 0;
- }
- void SongPlaying()
- {
- Note *noteToSend = new Note(0, 0, 0 , 0);
- for(int i = 0; i < songLength + 18; i++)
- {
- Note currentNote;
- if(i < songLength)
- {
- currentNote = songNotes[i];
- for(int j = 0; j < currentNote.delay; j++)
- {
- if(j < currentNote.length)
- {
- noteToSend -> comboValue = currentNote.comboValue;
- noteToSend -> frequency = currentNote.frequency;
- }
- else
- {
- noteToSend -> comboValue = 0;
- noteToSend -> frequency = 0;
- }
- PushNote(noteToSend);
- }
- }
- else
- {
- noteToSend -> comboValue = 0;
- noteToSend -> frequency = 0;
- PushNote(noteToSend);
- }
- }
- ShowEndScreen();
- }
- 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, 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 }, // BOTTOM BUSY 0x03
- { 0x1f, 0x10, 0x10, 0x1f, 0x00, 0x0e, 0x00, 0x00 }, // TOPPOM 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]);
- 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
- for(int i = 0; i < 10000; i++) ; // wait a moment - show logo
- }
- 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, " 1. Mario (q) ");
- // wyswitlanie opcji:
- // Zagraj piosenke:
- // 1. Mario.
- // 2. Metallica.
- while(high != low) // gdy odebrano dane
- {
- if(Bufor[low] != 'q'){
- low = (++low)%30;
- SEND_CMD(DD_RAM_ADDR);
- SEND_STRING(1, 16, "Wcisnieto nie-q ");
- }
- else{
- chosenOption = 1;
- SEND_CMD(DD_RAM_ADDR);
- SEND_STRING(1, 16, "Wcisnieto q ");
- break;
- }
- }
- return chosenOption;
- }
- void PlaySong(int index)
- {
- MakeSound(50, 1000);
- if(index == 1)
- {
- SongPlaying();
- }
- }
- int main( void )
- {
- 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();
- // Buzzer *buzzer = new Buzzer();
- // Test buzzera:
- InitBuzzerBits();
- Note emptyNote;
- for(int i = 0; i < 16; i++)
- cycleValues[i] = emptyNote;
- FillMarioSong();
- CreateSong(7);
- // while(1)
- // {
- // Buzzer::MakeSound(10, 200);
- // Buzzer::MakeSound(100, 300);
- // }
- WyswitlEkranWstepu();
- while(1)
- {
- int chosenOption = WyswietlMenu();
- // ButtonPress();
- if(chosenOption != 0)
- {
- clearDisplay();
- PlaySong(chosenOption);
- }
- }
- }
- void ShowScreen(int values[])
- {
- int tab[16];
- int tab2[16];
- for (int k=0;k<16;k++)
- {
- if (values[k]==3)
- {
- tab[k]= 0x01;
- tab2[k]= 0x02;
- }
- if (values[k]==2)
- {
- tab[k]= 0x01;
- tab2[k]= 0x04;
- }
- if (values[k]==1)
- {
- tab[k]= 0x01;
- tab2[k]= 0x03;
- }
- if (values[k]==0)
- {
- tab[k]= 0x01;
- tab2[k]= 0x01;
- }
- if (values[k]==4)
- {
- tab[k]= 0x03;
- tab2[k]= 0x01;
- }
- if (values[k]==5)
- {
- tab[k]= 0x03;
- tab2[k]= 0x03;
- }
- if (values[k]==6)
- {
- tab[k]= 0x03;
- tab2[k]= 0x04;
- }
- if (values[k]==7)
- {
- tab[k]= 0x03;
- tab2[k]= 0x02;
- }
- if (values[k]==8)
- {
- tab[k]= 0x04;
- tab2[k]= 0x01;
- }
- if (values[k]==9)
- {
- tab[k]= 0x04;
- tab2[k]= 0x03;
- }
- if (values[k]==10)
- {
- tab[k]= 0x04;
- tab2[k]= 0x04;
- }
- if (values[k]==11)
- {
- tab[k]= 0x04;
- tab2[k]= 0x02;
- }
- if (values[k]==12)
- {
- tab[k]= 0x02;
- tab2[k]= 0x01;
- }
- if (values[k]==13)
- {
- tab[k]= 0x02;
- tab2[k]= 0x03;
- }
- if (values[k]==14)
- {
- tab[k]= 0x02;
- tab2[k]= 0x04;
- }
- if (values[k]==15)
- {
- tab[k]= 0x02;
- tab2[k]= 0x02;
- }
- }
- SEND_CMD(DD_RAM_ADDR);
- char whatToSend;
- for(int i = 0; i <16; i++)
- {
- SEND_CHAR(tab[i]);
- }
- SEND_CMD(DD_RAM_ADDR2);
- for(int j = 0; j <16; j++)
- {
- SEND_CHAR(tab2[j]);
- }
- }
- #pragma vector = UART0RX_VECTOR // procedura obsługi przerwania UART
- __interrupt void usart0_rx (void)
- {
- 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