Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.08 KB | None | 0 0
  1. /****************************************************************************
  2. Module
  3. Audio1.c
  4.  
  5. Description
  6. This service implements a state machine to play audio for player 1
  7.  
  8. Author
  9. Margaret Coad
  10.  
  11. Date
  12. 11/10/15
  13.  
  14. ****************************************************************************/
  15. /*----------------------------- Include Files -----------------------------*/
  16. /* include header files for the framework and this service
  17. */
  18. #include "ES_Configure.h"
  19. #include "ES_Framework.h"
  20. #include "ES_DeferRecall.h"
  21. #include "ES_ShortTimer.h"
  22. #include "Audio1.h"
  23. #include "PWM12Tiva.h"
  24.  
  25. #include "inc/hw_memmap.h"
  26. #include "inc/hw_types.h"
  27. #include "inc/hw_gpio.h"
  28. #include "inc/hw_sysctl.h"
  29. #include "driverlib/sysctl.h"
  30. #include "driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
  31. #include "driverlib/gpio.h"
  32.  
  33. /*----------------------------- Module Defines ----------------------------*/
  34.  
  35. /*---------------------------- Module Functions ---------------------------*/
  36. /* prototypes for private functions for this service. They should be functions
  37. relevant to the behavior of this service
  38. */
  39.  
  40. /*---------------------------- Module Variables ---------------------------*/
  41. // Data private to the module: MyPriority, CurrentState, inARMedMode, // inPipeOut, inCelebration, various note frequencies, various
  42. // frequency and delay arrays for different tunes
  43. static uint8_t MyPriority;
  44. static Audio1State_t CurrentState;
  45. static uint8_t inARMedMode;
  46. static uint8_t inPipeOut;
  47. static uint8_t inCelebration;
  48.  
  49. // Frequencies of various musical notes
  50. static const uint16_t E6 = 1319;
  51. static const uint16_t D6 = 1175;
  52. static const uint16_t Cs6 = 1109;
  53. static const uint16_t B5 = 988;
  54. static const uint16_t As5 = 932;
  55. static const uint16_t A5 = 880;
  56. static const uint16_t Gs5 = 831;
  57. static const uint16_t Fs5 = 740;
  58. static const uint16_t E5 = 660;
  59. static const uint16_t Ds5 = 622;
  60. static const uint16_t D5 = 587;
  61. static const uint16_t Cs5 = 554;
  62. static const uint16_t B4 = 494;
  63. static const uint16_t As4 = 466;
  64. static const uint16_t A4 = 440;
  65. static const uint16_t Gs4 = 415;
  66. static const uint16_t Fs4 = 370;
  67. static const uint16_t E4 = 330;
  68. static const uint16_t Ds4 = 311;
  69. static const uint16_t D4 = 294;
  70. static const uint16_t Cs4 = 277;
  71. static const uint16_t C4 = 262;
  72. static const uint16_t B3 = 247;
  73. static const uint16_t A3 = 220;
  74.  
  75. // Length of a quarter note
  76. static const uint16_t Q = 450;
  77.  
  78. // Tune arrays holding frequencies and delays for each tune
  79. static const uint16_t ARMedModeFreqs[89] = {E5, Cs5, A4, Cs5, E5, A5, Cs6, B5, A5, Cs5, Ds5, E5, 0, E5, 0, E5, Cs6, B5, A5, Gs5, Fs5, Gs5, A5, 0, A5, E5, Cs5, A4, E5, Cs5, A4, Cs5, E5, A5, Cs6, B5, A5, Cs5, Ds5, E5, 0, E5, 0, E5, Cs6, B5, A5, Gs5, Fs5, Gs5, A5, Cs5, Ds5, E5, 0,
  80. E5, A5, 0, A5, 0, A5, Gs5, Fs5, 0, Fs5, 0, Fs5, B5, D6, Cs6, B5, A5, 0, A5, Gs5, E5, 0, E5, A5, B5, Cs6, D6, E6, A5, B5, Cs6, D6, B5, A5};
  81. static const uint16_t ARMedModeDelays[89] = {0.75*Q, 0.25*Q, Q, Q, Q, 2*Q, 0.75*Q, 0.25*Q, Q, Q, Q, 1.9*Q, 0.1*Q, 0.65*Q, 0.1*Q, 0.25*Q, 1.5*Q, 0.5*Q, Q, 2*Q, 0.75*Q, 0.25*Q, 0.9*Q, 0.1*Q, Q, Q, Q, Q, 0.75*Q, 0.25*Q, Q, Q, Q, 2*Q, 0.75*Q, 0.25*Q, Q, Q, Q, 1.9*Q, 0.1*Q, 0.65*Q, 0.1*Q, 0.25*Q, 1.5*Q, 0.5*Q, Q, 2*Q, 0.75*Q, 0.25*Q, Q, Q, Q, 1.9*Q, 0.1*Q, Q, 0.9*Q, 0.1*Q, 0.9*Q, 0.1*Q, 0.5*Q, 0.5*Q, 0.9*Q, 0.1*Q, 0.9*Q, 0.1*Q, Q, Q, 0.5*Q, 0.5*Q, 0.5*Q, 0.4*Q, 0.1*Q, Q, Q, 0.4*Q, 0.1*Q, 0.5*Q, 1.5*Q, 0.5*Q, 0.5*Q, 0.5*Q, 2*Q, 0.5*Q, 0.5*Q, 1.5*Q, 0.5*Q, Q, 2*Q};
  82. static const uint16_t StartButtonFreqs[3] = {A4, E5, A5};
  83. static const uint16_t StartButtonDelays[3] = {0.25*Q, 0.25*Q, 0.25*Q};
  84. static const uint16_t SuccessWrenchFreqs[3] = {Cs5, D5, E5};
  85. static const uint16_t SuccessWrenchDelays[3] = {0.25*Q, 0.25*Q, 0.25*Q};
  86. static const uint16_t SuccessBucketFreqs[3] = {Cs5, E5, A5};
  87. static const uint16_t SuccessBucketDelays[3] = {0.25*Q, 0.25*Q, 0.25*Q};
  88. static const uint16_t FailBucketFreqs[1] = {A3};
  89. static const uint16_t FailBucketDelays[1] = {0.75*Q};
  90. static const uint16_t PipeOutFreqs[3] = {E4, Cs4, C4};
  91. static const uint16_t PipeOutDelays[3] = {0.25*Q, 0.25*Q, 0.25*Q};
  92. static const uint16_t PipeInFreqs[3] = {C4, Cs4, E4};
  93. static const uint16_t PipeInDelays[3] = {0.25*Q, 0.25*Q, 0.25*Q};
  94. static const uint16_t CelebrationModeFreqs[74] = {E5, 0, E5, Cs5, 0, Cs5, E5, 0, E5, B4, 0, B4, Cs5, D5, E5, Fs5, Gs5, E5, 0, E5, 0, E5, Cs5, 0, Cs5, E5, 0, E5, B4, 0, B4, B5, As5, B5, Cs6, Fs5, B5, E5, Cs6, 0, Cs6, B5, A5, 0, A5, Gs5, 0, Gs5, A5, B5, Gs5, Fs5, E5, A5, 0, A5, 0, A5, Fs5, 0, Fs5, A5, 0, A5, E5, 0, E5, 0, E5, Fs5, A5, E5, B5, A5};
  95. static const uint16_t CelebrationModeDelays[74] = {0.9*Q, 0.1*Q, 1.5*Q, 0.4*Q, 0.1*Q, Q, 0.9*Q, 0.1*Q, 1.5*Q, 0.4*Q, 0.1*Q, Q, Q, Q, Q, Q, Q, 2.9*Q, 0.1*Q, 0.9*Q, 0.1*Q, 1.5*Q, 0.4*Q, 0.1*Q, Q, 0.9*Q, 0.1*Q, 1.5*Q, 0.4*Q, 0.1*Q, Q, Q, Q, Q, Q, Q, 3*Q, Q, 1.4*Q, 0.1*Q, 0.5*Q, Q, 0.9*Q, 0.1*Q, 1.5*Q, 0.4*Q, 0.1*Q, Q, Q, Q, Q, Q, Q, 2.9*Q, 0.1*Q, 0.9*Q, 0.1*Q, 1.5*Q, 0.4*Q, 0.1*Q, Q, 0.9*Q, 0.1*Q, 1.5*Q, 0.4*Q, 0.1*Q, 0.9*Q, 0.1*Q, Q, Q, Q, Q, Q, 3*Q};
  96. static const uint16_t FailureModeFreqs[5] = {E4, Ds4, D4, Cs4, C4};
  97. static const uint16_t FailureModeDelays[5] = {Q, Q, Q, Q, 2*Q};
  98.  
  99. /*------------------------------ Module Code ------------------------------*/
  100. /****************************************************************************
  101. Function
  102. InitAudio1
  103.  
  104. Parameters
  105. uint8_t : the priority of this service
  106.  
  107. Returns
  108. bool, false if error in initialization, true otherwise
  109.  
  110. Description
  111. Saves away the priority, and does any
  112. other required initialization for this service
  113. Notes
  114.  
  115. Author
  116. Margaret Coad, 11/10/15
  117. ****************************************************************************/
  118. bool InitAudio1 ( uint8_t Priority )
  119. {
  120. //Initialize the MyPriority variable with the passed in parameter.
  121. MyPriority = Priority;
  122. //Initialize the port line (C4) to write to speaker 1
  123. // Enable GPIO Port C
  124. HWREG(SYSCTL_RCGCGPIO) |= BIT2HI;
  125. // Wait until peripheral reports that clock is ready
  126. while ((HWREG(SYSCTL_PRGPIO) & SYSCTL_PRGPIO_R2) !=
  127. SYSCTL_PRGPIO_R2);
  128. // Set bit 4 on Port C to be a digital I/O line
  129. HWREG(GPIO_PORTC_BASE+GPIO_O_DEN) |= (GPIO_PIN_4);
  130. // Set bit 4 on Port C to be an input
  131. HWREG(GPIO_PORTC_BASE+GPIO_O_DIR) &= (~GPIO_PIN_4);
  132. //Set CurrentState to Waiting2Play
  133. CurrentState = Waiting2Play;
  134. //End of InitAudio1 (return True)
  135. return true;
  136. }
  137.  
  138. /****************************************************************************
  139. Function
  140. PostAudio1
  141.  
  142. Parameters
  143. ES_Event ThisEvent, the event to post to the queue
  144.  
  145. Returns
  146. bool false if the Enqueue operation failed, true otherwise
  147.  
  148. Description
  149. Posts an event to this state machine's queue
  150. Notes
  151.  
  152. Author
  153. Margaret Coad, 11/10/15
  154. ****************************************************************************/
  155. bool PostAudio1( ES_Event ThisEvent )
  156. {
  157. return ES_PostToService( MyPriority, ThisEvent);
  158. }
  159.  
  160. /****************************************************************************
  161. Function
  162. RunAudio1 (implements 2-state state machine for player 1's audio)
  163.  
  164. Parameters
  165. ES_Event : the event to process
  166.  
  167. Returns
  168. ES_Event, ES_NO_EVENT if no error, ES_ERROR otherwise
  169.  
  170. Description
  171. The EventType of ThisEvent will be ES_PLAY_TUNE,
  172. ES_START_BUTTON_DB_DOWN, ES_PIPE_RECOVERED, ES_TIMEOUT,
  173. ES_GAINED_POINT, or ES_GAMEOVER; the EventParam will be ARMedMode,
  174. StartButton, SuccessWrench, SuccessBucket, FailBucket, PipeOut,
  175. PipeIn, CelebrationMode, FailureMode, PIPE_TIMER, or AUDIO1_TIMER
  176.  
  177. Notes
  178.  
  179. Author
  180. Margaret Coad, 11/10/15
  181. ****************************************************************************/
  182. ES_Event RunAudio1( ES_Event ThisEvent )
  183. {
  184. ES_Event ReturnEvent;
  185. ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
  186. //Local Variables: NextState (state)
  187. Audio1State_t NextState;
  188. //Static Local Variable: TuneFreqs (array); create enough space
  189. //for max length tune
  190. static uint16_t TuneFreqs[ARRAY_SIZE(ARMedModeFreqs)];
  191. //Static Local Variable: TuneDelays (array)
  192. static uint16_t TuneDelays[ARRAY_SIZE(ARMedModeFreqs)];
  193. //Static Local Variable: NumberOfNotes (number)
  194. static uint16_t NumberOfNotes;
  195. //Static Local Variable: CurrentNote (number)
  196. static uint16_t CurrentNote;
  197.  
  198. //Set NextState to CurrentState
  199. NextState = CurrentState;
  200. //Based on the state of the CurrentState variable choose one of
  201. //the following blocks of code:
  202. switch (CurrentState){
  203. case Waiting2Play:
  204. //If ThisEvent is ES_PLAY_TUNE
  205. if (ThisEvent.EventType == ES_PLAY_TUNE) {
  206. //Initialize flags to say that we are not in
  207. //ARMedMode, PipeOut, or Celebration
  208. inARMedMode = 0;
  209. inPipeOut = 0;
  210. inCelebration = 0;
  211. //Set TuneFreqs to frequency array //corresponding to Event Parameter
  212. //Set TuneDelays to delay array corresponding
  213. //to Event Parameter
  214. //Set NumberOfNotes to length of TuneFreqs
  215. uint16_t i;
  216. switch (ThisEvent.EventParam) {
  217. case ARMedMode:
  218. NumberOfNotes =
  219. ARRAY_SIZE(ARMedModeFreqs);
  220. for (i = 0; i < NumberOfNotes;
  221. i++){
  222. TuneFreqs[i] =
  223. ARMedModeFreqs[i];
  224. TuneDelays[i] =
  225. ARMedModeDelays[i];
  226. inARMedMode = 1;
  227. }
  228. break;
  229. case StartButton:
  230. NumberOfNotes =
  231. ARRAY_SIZE(StartButtonFreqs);
  232. for (i = 0; i < NumberOfNotes;
  233. i++){
  234. TuneFreqs[i] =
  235. StartButtonFreqs[i];
  236. TuneDelays[i] =
  237. StartButtonDelays[i];
  238. }
  239. break;
  240. case SuccessWrench:
  241. NumberOfNotes =
  242. ARRAY_SIZE(SuccessWrenchFreqs);
  243. for (i = 0; i < NumberOfNotes;
  244. i++){
  245. TuneFreqs[i] =
  246. SuccessWrenchFreqs[i];
  247. TuneDelays[i] =
  248. SuccessWrenchDelays[i];
  249. }
  250. break;
  251. case SuccessBucket:
  252. NumberOfNotes =
  253. ARRAY_SIZE(SuccessBucketFreqs);
  254. for (i = 0; i < NumberOfNotes;
  255. i++){
  256. TuneFreqs[i] =
  257. SuccessBucketFreqs[i];
  258. TuneDelays[i] =
  259. SuccessBucketDelays[i];
  260. }
  261. break;
  262. case FailBucket:
  263. NumberOfNotes =
  264. ARRAY_SIZE(FailBucketFreqs);
  265. for (i = 0; i < NumberOfNotes;
  266. i++){
  267. TuneFreqs[i] =
  268. FailBucketFreqs[i];
  269. TuneDelays[i] =
  270. FailBucketDelays[i];
  271. }
  272. break;
  273. case PipeOut:
  274. NumberOfNotes =
  275. ARRAY_SIZE(PipeOutFreqs);
  276. for (i = 0; i < NumberOfNotes;
  277. i++){
  278. TuneFreqs[i] =
  279. PipeOutFreqs[i];
  280. TuneDelays[i] =
  281. PipeOutDelays[i];
  282. inPipeOut = 1;
  283. }
  284. break;
  285. case PipeIn:
  286. NumberOfNotes =
  287. ARRAY_SIZE(PipeInFreqs);
  288. for (i = 0; i < NumberOfNotes;
  289. i++){
  290. TuneFreqs[i] =
  291. PipeInFreqs[i];
  292. TuneDelays[i] =
  293. PipeInDelays[i];
  294. }
  295. break;
  296. case CelebrationMode:
  297. inCelebration = 1;
  298. NumberOfNotes =
  299. ARRAY_SIZE(CelebrationModeFreqs);
  300. for (i = 0; i < NumberOfNotes;
  301. i++){
  302. TuneFreqs[i] =
  303. CelebrationModeFreqs[i];
  304. TuneDelays[i] =
  305. CelebrationModeDelays[i];
  306. }
  307. break;
  308. case FailureMode:
  309. NumberOfNotes =
  310. ARRAY_SIZE(FailureModeFreqs);
  311. for (i = 0; i < NumberOfNotes;
  312. i++){
  313. TuneFreqs[i] =
  314. FailureModeFreqs[i];
  315. TuneDelays[i] =
  316. FailureModeDelays[i];
  317. }
  318. break;
  319. }
  320. // Play first note of TuneFreqs
  321. PWM_TIVA_SetDuty(50, 6);
  322. PWM_TIVA_SetFreq(TuneFreqs[0], 3);
  323. // Set audio timer for length of first note in // TuneDelays
  324. ES_Timer_InitTimer(AUDIO1_TIMER,TuneDelays[0]);
  325. // Set CurrentNote to 1
  326. CurrentNote = 1;
  327. // Set NextState to Playing
  328. NextState = Playing;
  329. //Endif
  330. }
  331. break;
  332.  
  333. case Playing:
  334. // If statement to interrupt a tune under certain
  335. // circumstances
  336. if (ThisEvent.EventType==ES_START_BUTTON_DBDOWN ||
  337. ThisEvent.EventType == ES_PIPE_RECOVERED ||
  338. (ThisEvent.EventType == ES_TIMEOUT &&
  339. ThisEvent.EventParam == PIPE_TIMER && inCelebration
  340. ==0) || (ThisEvent.EventType == ES_GAINED_POINT) || (ThisEvent.EventType==ES_GAMEOVER)){
  341. NextState = Waiting2Play;
  342. //Play nothing (set duty cycle to 0)
  343. PWM_TIVA_SetDuty(0, 6);
  344. }
  345. else if ((ThisEvent.EventType == ES_TIMEOUT) &&
  346. (ThisEvent.EventParam == AUDIO1_TIMER)){
  347. //If CurrentNote = NumberOfNotes
  348. if (CurrentNote == NumberOfNotes){
  349. if (inARMedMode==1){
  350. ES_Event ThatEvent;
  351. ThatEvent.EventType = ES_PLAY_TUNE;
  352. ThatEvent.EventParam = ARMedMode;
  353. PostAudio1(ThatEvent);
  354. }
  355. if (inPipeOut==1){
  356. ES_Event OtherEvent;
  357. OtherEvent.EventType =ES_PLAY_TUNE;
  358. OtherEvent.EventParam = PipeOut;
  359. PostAudio1(OtherEvent);
  360. }
  361. //Play nothing (set duty cycle to 0)
  362. PWM_TIVA_SetDuty(0, 6);
  363. //Set NextState to Waiting2Play
  364. NextState = Waiting2Play;
  365. //Endif
  366. }
  367. //If CurrentNote < NumberOfNotes
  368. if (CurrentNote < NumberOfNotes){
  369. //Play note of TuneFreqs with index
  370. //CurrentNote
  371. PWM_TIVA_SetFreq(TuneFreqs[CurrentNote],3);
  372. //Set audio timer for length of note in
  373. TuneDelays with index CurrentNote
  374. ES_Timer_InitTimer(AUDIO1_TIMER,
  375. TuneDelays[CurrentNote]);
  376. //Increment CurrentNote
  377. CurrentNote++;
  378. //Endif
  379. }
  380. //Endif
  381. }
  382. break;
  383. }
  384. //Set CurrentState to NextState
  385. CurrentState = NextState;
  386. //Return ES_NO_EVENT
  387. return ReturnEvent;
  388. }
  389.  
  390. /***************************************************************************
  391. private functions
  392. ***************************************************************************/
  393.  
  394. /*------------------------------- Footnotes -------------------------------*/
  395. /*------------------------------ End of file ------------------------------*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement