Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | None | 0 0
  1. #include <msp430x14x.h>
  2.  
  3. #include "uart.h"
  4. #include "portyLcd.h"
  5. #include "lcd.h"
  6. #include "portyUart.h"
  7.  
  8. struct Note
  9. {
  10.  
  11. int comboValue;
  12. int frequency;
  13. int delay;
  14. int length;
  15.  
  16. Note(int combo, int Freq, int Delay, int Length)
  17. {
  18. comboValue = combo;
  19. frequency = Freq;
  20. delay = Delay;
  21. length = Length;
  22. }
  23.  
  24. Note()
  25. {}
  26. };
  27.  
  28.  
  29. char Bufor[30]; // bufor odczytywanych danych
  30. int low=0; // znacznik począteku danych w buforze
  31. int high=0; // zmacznik końca danych w buforze
  32.  
  33. Note* cycleValues[16];
  34. int cycleCurrentIndex= 0;
  35. int cycleBuforLength;
  36. int cycleTemp[16];
  37. int cycleNotesPushed = 0;
  38.  
  39. int currScore = 0;
  40. int Button1Pressed;
  41. int Button2Pressed;
  42. int Button3Pressed;
  43. int Button4Pressed;
  44.  
  45. int Mario_bf [7]; //bf/10
  46. int Mario_lg [7]; //lg/10
  47. int Mario_del [7]; //del
  48. int mario_key[7];
  49.  
  50.  
  51. int* songLengths;
  52. int* songNotesFrequences;
  53. int* songNotesDelays;
  54. int* answerKeys;
  55. int songLength = 7;
  56. void ShowScreen(int values[]);
  57. Note songNotes[7];
  58.  
  59. void AddScore()
  60. {
  61. currScore += 1;
  62. }
  63.  
  64. int GetScore()
  65. {
  66. return currScore;
  67. }
  68.  
  69. void ShowEndScreen()
  70. {
  71. clearDisplay();
  72.  
  73. char endDisplay[16] = { ' ', 'Y', 'o', 'u', 'r', ' ', 's', 'c', 'o', 'r','e', ':', ' '};
  74. endDisplay[13] = currScore/100 + 48;
  75. currScore /= 100;
  76. endDisplay[14] = currScore/10 + 48;
  77. currScore /= 10;
  78. endDisplay[15] = currScore + 48;
  79.  
  80. SEND_STRING(1, 16, endDisplay);
  81. }
  82.  
  83. int* GetValues()
  84. {
  85. int index = 0;
  86.  
  87. for(int i = cycleCurrentIndex - 1; i >=0; i--)
  88. {
  89. cycleTemp[index++] = cycleValues[i] -> comboValue;
  90. }
  91.  
  92. for(int i = cycleBuforLength-1; i >= cycleCurrentIndex; i--)
  93. {
  94. cycleTemp[index++] = cycleValues[i] ->comboValue;
  95. }
  96.  
  97. return cycleTemp;
  98. }
  99.  
  100.  
  101.  
  102. void CreateSong(int notesSize)
  103. {
  104. songLength = notesSize;
  105. answerKeys = mario_key;
  106.  
  107. songLengths = Mario_lg;
  108. songNotesFrequences = Mario_bf;
  109. songNotesDelays = Mario_del;
  110.  
  111. for(int i = 0; i < notesSize; i++)
  112. {
  113. songNotes[i] = Note(answerKeys[i], songNotesFrequences[i], songNotesDelays[i], songLengths[i]);
  114. }
  115. }
  116. void Update()
  117. {
  118. Button1Pressed = false;
  119. Button2Pressed = false;
  120. Button3Pressed = false;
  121. Button4Pressed = false;
  122. while(high != low) // gdy odebrano dane
  123. {
  124. if(Bufor[low] == 'q')
  125. Button1Pressed = true;
  126. if(Bufor[low] == 'w')
  127. Button2Pressed = true;
  128. if(Bufor[low] == 'e')
  129. Button3Pressed = true;
  130. if(Bufor[low] == 'r')
  131. Button4Pressed = true;
  132. low = (low+1)%30;
  133. }
  134. }
  135.  
  136. void MakeSound(int freq, int czasBuzzowania)
  137. {
  138. for(int i = 0; i < czasBuzzowania; i++)
  139. {
  140. P4OUT &= ~BIT2;
  141. P4OUT |= BIT3;
  142. for(long int i = 0; i < 1 * freq; i++) { }
  143.  
  144. P4OUT |= BIT2;
  145. P4OUT &= ~BIT3;
  146. for(long int i = 0; i < 1 * freq; i++) { }
  147. }
  148. }
  149.  
  150. bool TakeInput(int playerControllerInput, Note *note)
  151. {
  152. if(note -> comboValue == playerControllerInput)
  153. {
  154. MakeSound(note -> frequency, 1000);
  155. AddScore();
  156. return true;
  157. }
  158. else
  159. {
  160. MakeSound((int)(note -> frequency), 1000); // TODO
  161. return false;
  162. }
  163. }
  164.  
  165. void Push(Note *value)
  166. {
  167. if(cycleNotesPushed < 16)
  168. cycleNotesPushed += 1;
  169. cycleValues[cycleCurrentIndex] = value;
  170. cycleCurrentIndex = (cycleCurrentIndex+1) % 16;
  171. }
  172.  
  173. int GetCurrentClickedKeys()
  174. {
  175. int value = 0;
  176. if(Button1Pressed)
  177. value+=1;
  178. if(Button2Pressed)
  179. value+=2;
  180. if(Button3Pressed)
  181. value+=4;
  182. if(Button4Pressed)
  183. value+=8;
  184.  
  185. return value;
  186. }
  187.  
  188. Note* GetLastCheckNote()
  189. {
  190. return cycleValues[(cycleCurrentIndex+1)%16];
  191. }
  192.  
  193. void PushNote(Note *note)
  194. {
  195. Update();
  196. Push(note);
  197. Note* comboValueNeeded = GetLastCheckNote();
  198. ShowScreen(GetValues());
  199. TakeInput(GetCurrentClickedKeys(), comboValueNeeded);
  200. }
  201.  
  202.  
  203. void FillMarioSong()
  204. {
  205. Mario_bf[0]=660; Mario_lg[0]=1;
  206. Mario_del[0]= 2; mario_key[0] = 12;
  207. Mario_bf[1]=660; Mario_lg[1]=1;
  208. Mario_del[1]= 3; mario_key[1] = 8;
  209. Mario_bf[2]=660; Mario_lg[2]=1;
  210. Mario_del[2]= 3; mario_key[2] = 2;
  211. Mario_bf[3]=510; Mario_lg[3]=1;
  212. Mario_del[3]= 1; mario_key[3] = 10;
  213. Mario_bf[4]=660; Mario_lg[4]=1;
  214. Mario_del[4]= 3; mario_key[4] = 4;
  215. Mario_bf[5]=770; Mario_lg[5]=1;
  216. Mario_del[5]= 6; mario_key[5] = 3;
  217. Mario_bf[6]=380; Mario_lg[6]=1;
  218. Mario_del[6]= 6; mario_key[6] = 7;
  219.  
  220.  
  221. songLengths = Mario_lg;
  222. songNotesFrequences = Mario_bf;
  223. songNotesDelays = Mario_del;
  224. answerKeys = mario_key;
  225. }
  226.  
  227.  
  228. // To jest wciskanie buttonow na msp, tego nie uzywamy - kolo ratunkowe
  229. /*void ButtonPress()
  230. {
  231. if(isButton1Pressed) {
  232. UartStringTransmit("q");
  233. }
  234. if(isButton2Pressed) {
  235. UartStringTransmit("w");
  236. }
  237. if(isButton3Pressed) {
  238. UartStringTransmit("e");
  239. }
  240. if(isButton4Pressed) {
  241. UartStringTransmit("r");
  242. }
  243. } */
  244.  
  245.  
  246.  
  247. void ResetScore()
  248. {
  249. currScore = 0;
  250. }
  251.  
  252. void SongPlaying()
  253. {
  254. Note *noteToSend = new Note(0, 0, 0 , 0);
  255. for(int i = 0; i < songLength + 18; i++)
  256. {
  257. Note currentNote;
  258. if(i < songLength)
  259. {
  260. currentNote = songNotes[i];
  261.  
  262. for(int j = 0; j < currentNote.delay; j++)
  263. {
  264. if(j < currentNote.length)
  265. {
  266. noteToSend -> comboValue = currentNote.comboValue;
  267. noteToSend -> frequency = currentNote.frequency;
  268. }
  269. else
  270. {
  271. noteToSend -> comboValue = 0;
  272. noteToSend -> frequency = 0;
  273. }
  274. PushNote(noteToSend);
  275. }
  276. }
  277. else
  278. {
  279. noteToSend -> comboValue = 0;
  280. noteToSend -> frequency = 0;
  281. PushNote(noteToSend);
  282. }
  283.  
  284. }
  285. ShowEndScreen();
  286. }
  287.  
  288.  
  289. void LCDController()
  290. {
  291. char signs[4][8] =
  292. {
  293. { 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00 }, // BOTh EMPTY 0x01
  294. { 0x1F, 0x10, 0x10, 0x1f, 0x1f, 0x10, 0x10, 0x1f }, // BOTh BUSY 0x02
  295. { 0x00, 0x0e, 0x00, 0x00, 0x1f, 0x10, 0x10, 0x1f }, // BOT BUSY 0x03
  296. { 0x1f, 0x10, 0x10, 0x1f, 0x00, 0x0e, 0x00, 0x00 }, // TOP BUSY 0x04
  297.  
  298. };
  299.  
  300.  
  301. SEND_CMD(CG_RAM_ADDR);
  302.  
  303. for(int i = 0; i < 4; i++)
  304. for(int j = 0; j < 8; j++)
  305. SEND_CHAR(signs[i][j]);
  306.  
  307. }
  308.  
  309. void InitBuzzerBits()
  310. {
  311. P4DIR |= BIT2;
  312. P4DIR |= BIT3;
  313.  
  314. P4DIR &= ~BIT4;
  315. P4DIR &= ~BIT5;
  316. P4DIR &= ~BIT6;
  317. P4DIR &= ~BIT7;
  318. }
  319.  
  320. void PrepareCustomChars()
  321. {
  322. char signs[4][8] =
  323. {
  324. { 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00 }, // BOTH EMPTY 0x01
  325. { 0x1F, 0x10, 0x10, 0x1f, 0x1f, 0x10, 0x10, 0x1f }, // BOTH BUSY 0x02
  326. { 0x00, 0x0e, 0x00, 0x00, 0x1f, 0x10, 0x10, 0x1f }, // BOTTOM BUSY 0x03
  327. { 0x1f, 0x10, 0x10, 0x1f, 0x00, 0x0e, 0x00, 0x00 }, // TOPPOM BUSY 0x04
  328.  
  329. };
  330.  
  331. SEND_CMD(CG_RAM_ADDR);
  332. for(int i = 0; i < 4; i++)
  333. for(int j = 0; j < 8; j++)
  334. SEND_CHAR(signs[i][j]);
  335.  
  336. SEND_CMD(DD_RAM_ADDR);
  337. }
  338.  
  339. void WyswitlEkranWstepu()
  340. {
  341. // wyswietlanie napisu MSPHero - Godlike UART, czy cos - miejsce na smieszek
  342. SEND_CMD(DD_RAM_ADDR);
  343. SEND_STRING(1,16," MSPHero ");// TODO
  344. for(int i = 0; i < 10000; i++) ; // wait a moment - show logo
  345. }
  346.  
  347. int WyswietlMenu()
  348. {
  349. int chosenOption = 0;
  350. SEND_CMD(DD_RAM_ADDR);
  351. SEND_STRING(1, 16, " MSPHero - Menu: ");
  352. SEND_CMD(DD_RAM_ADDR2);
  353. SEND_STRING(2, 16, " 1. Mario (q) ");
  354. // wyswitlanie opcji:
  355. // Zagraj piosenke:
  356. // 1. Mario.
  357. // 2. Metallica.
  358. while(high != low) // gdy odebrano dane
  359. {
  360. if(Bufor[low] != 'q'){
  361. low = (++low)%30;
  362. SEND_CMD(DD_RAM_ADDR);
  363. SEND_STRING(1, 16, "Wcisnieto nie-q ");
  364. }
  365. else{
  366. chosenOption = 1;
  367. SEND_CMD(DD_RAM_ADDR);
  368. SEND_STRING(1, 16, "Wcisnieto q ");
  369. break;
  370. }
  371. }
  372. return chosenOption;
  373. }
  374.  
  375. void PlaySong(int index)
  376. {
  377. MakeSound(50, 1000);
  378. if(index == 1)
  379. {
  380. SongPlaying();
  381. }
  382. }
  383.  
  384. int main( void )
  385. {
  386. P4DIR &= ~BIT4;
  387. P4DIR &= ~BIT5;
  388. P4DIR &= ~BIT6;
  389. P4DIR &= ~BIT7;
  390.  
  391. WDTCTL=WDTPW + WDTHOLD; // wyłączenie WDT
  392.  
  393. // Timer_A ustawiamy na 500 kHz
  394. // a przerwanie generujemy co 100 ms
  395. TACTL = TASSEL_1 + MC_1 +ID_3; // Wybieram ACLK, ACLK/8=500kHz,tryb Up
  396. CCTL0 = CCIE; // włączenie przerwań od CCR0
  397. CCR0=50000; // podzielnik 50000: przerwanie co 100 ms
  398.  
  399. InitPortsLcd(); // inicjalizacja portów LCD
  400. InitLCD(); // inicjalizacja LCD
  401. clearDisplay(); // czyszczenie wyświetlacza
  402. initPortyUart(); // inicjalizacja portow UART
  403. initUart(1200); //ma byc 15200
  404. // inicjalizacja UARTa
  405.  
  406. _EINT(); //TODO // włączenie przerwań
  407. SEND_CMD(DD_RAM_ADDR);
  408.  
  409. PrepareCustomChars();
  410. // Buzzer *buzzer = new Buzzer();
  411. // Test buzzera:
  412. InitBuzzerBits();
  413.  
  414. Note emptyNote;
  415. for(int i = 0; i < 16; i++)
  416. cycleValues[i] = emptyNote;
  417.  
  418. FillMarioSong();
  419. CreateSong(7);
  420. // while(1)
  421. // {
  422. // Buzzer::MakeSound(10, 200);
  423. // Buzzer::MakeSound(100, 300);
  424. // }
  425.  
  426. WyswitlEkranWstepu();
  427.  
  428. while(1)
  429. {
  430. int chosenOption = WyswietlMenu();
  431. // ButtonPress();
  432. if(chosenOption != 0)
  433. {
  434. clearDisplay();
  435. PlaySong(chosenOption);
  436. }
  437.  
  438.  
  439. }
  440. }
  441.  
  442. void ShowScreen(int values[])
  443. {
  444. int tab[16];
  445. int tab2[16];
  446.  
  447. for (int k=0;k<16;k++)
  448. {
  449. if (values[k]==3)
  450. {
  451. tab[k]= 0x01;
  452. tab2[k]= 0x02;
  453. }
  454. if (values[k]==2)
  455. {
  456. tab[k]= 0x01;
  457. tab2[k]= 0x04;
  458. }
  459. if (values[k]==1)
  460. {
  461. tab[k]= 0x01;
  462. tab2[k]= 0x03;
  463.  
  464. }
  465. if (values[k]==0)
  466. {
  467. tab[k]= 0x01;
  468. tab2[k]= 0x01;
  469. }
  470. if (values[k]==4)
  471. {
  472. tab[k]= 0x03;
  473. tab2[k]= 0x01;
  474. }
  475. if (values[k]==5)
  476. {
  477. tab[k]= 0x03;
  478. tab2[k]= 0x03;
  479. }
  480. if (values[k]==6)
  481. {
  482. tab[k]= 0x03;
  483. tab2[k]= 0x04;
  484. }
  485. if (values[k]==7)
  486. {
  487. tab[k]= 0x03;
  488. tab2[k]= 0x02;
  489. }
  490. if (values[k]==8)
  491. {
  492. tab[k]= 0x04;
  493. tab2[k]= 0x01;
  494. }
  495. if (values[k]==9)
  496. {
  497. tab[k]= 0x04;
  498. tab2[k]= 0x03;
  499. }
  500. if (values[k]==10)
  501. {
  502. tab[k]= 0x04;
  503. tab2[k]= 0x04;
  504. }
  505. if (values[k]==11)
  506. {
  507. tab[k]= 0x04;
  508. tab2[k]= 0x02;
  509. }
  510. if (values[k]==12)
  511. {
  512. tab[k]= 0x02;
  513. tab2[k]= 0x01;
  514. }
  515. if (values[k]==13)
  516. {
  517. tab[k]= 0x02;
  518. tab2[k]= 0x03;
  519. }
  520. if (values[k]==14)
  521. {
  522. tab[k]= 0x02;
  523. tab2[k]= 0x04;
  524. }
  525. if (values[k]==15)
  526. {
  527. tab[k]= 0x02;
  528. tab2[k]= 0x02;
  529. }
  530. }
  531.  
  532. SEND_CMD(DD_RAM_ADDR);
  533. char whatToSend;
  534. for(int i = 0; i <16; i++)
  535. {
  536. SEND_CHAR(tab[i]);
  537. }
  538. SEND_CMD(DD_RAM_ADDR2);
  539. for(int j = 0; j <16; j++)
  540. {
  541. SEND_CHAR(tab2[j]);
  542. }
  543. }
  544.  
  545.  
  546. #pragma vector = UART0RX_VECTOR // procedura obsługi przerwania UART
  547. __interrupt void usart0_rx (void)
  548. {
  549. Bufor[high] = RXBUF0; // wpisanie odebranych danych do bufora
  550. high = (++high) % 30; // inkrementowanie znacznika końca danych
  551. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement