From b8da0dabe58ebca3e185b5f75058160da376e0c0 Mon Sep 17 00:00:00 2001 From: Dmitry Maylarov Date: Wed, 10 Jul 2019 22:18:18 +0300 Subject: [PATCH] looks like music is here; compiled but not tested --- .vscode/tasks.json | 8 + .vscode/uberdevice.code-workspace | 9 +- Core/Inc/game.h | 2 - Core/Inc/main.h | 6 - Core/Inc/music.h | 19 +- Core/Src/game.c | 2 + Core/Src/main.c | 38 +++- Core/Src/music.c | 218 ++++++++++++++++++++++- Makefile | 3 +- Resources/boulevard_of_broken_dreams.bin | Bin 0 -> 12303 bytes Resources/gravity_falls_soundtrack.bin | Bin 0 -> 932 bytes Resources/imperial_march.bin | Bin 0 -> 944 bytes Resources/main_theme.bin | Bin 0 -> 2585 bytes Resources/super_mario.bin | Bin 0 -> 2418 bytes Resources/tetris.bin | Bin 0 -> 1987 bytes 15 files changed, 286 insertions(+), 19 deletions(-) create mode 100644 Resources/boulevard_of_broken_dreams.bin create mode 100644 Resources/gravity_falls_soundtrack.bin create mode 100644 Resources/imperial_march.bin create mode 100644 Resources/main_theme.bin create mode 100644 Resources/super_mario.bin create mode 100644 Resources/tetris.bin diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 38357a6..fcec01f 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -39,6 +39,14 @@ "kind": "build", "isDefault": true }, + "presentation": { + "echo": true, + "reveal": "always", + "focus": false, + "panel": "shared", + "showReuseMessage": true, + "clear": true + }, "problemMatcher": [] }, { diff --git a/.vscode/uberdevice.code-workspace b/.vscode/uberdevice.code-workspace index 3b10996..0643704 100644 --- a/.vscode/uberdevice.code-workspace +++ b/.vscode/uberdevice.code-workspace @@ -3,5 +3,12 @@ { "path": "D:\\Folders\\Documents\\STM32\\Uberdevice" } - ] + ], + "settings": { + "files.associations": { + "task.h": "c", + "music.h": "c", + "usb_device.h": "c" + } + } } \ No newline at end of file diff --git a/Core/Inc/game.h b/Core/Inc/game.h index 322cc9d..1a9f53e 100644 --- a/Core/Inc/game.h +++ b/Core/Inc/game.h @@ -16,8 +16,6 @@ typedef struct { uint32_t scores[MAX_PLAYERS]; } GameEngine; -extern GameEngine game; - void InitGameEngine(); void IncrementTurnTime(); void DecrementTurnTime(); diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 3f57875..ad74305 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -32,13 +32,7 @@ extern "C" { /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ -#include "stm32f1xx_hal_gpio.h" -#include -#include "lcd.h" -#include "game.h" #include "FreeRTOS.h" -#include "task.h" -#include "queue.h" #include "timers.h" /* USER CODE END Includes */ diff --git a/Core/Inc/music.h b/Core/Inc/music.h index fa92f23..64fe50f 100644 --- a/Core/Inc/music.h +++ b/Core/Inc/music.h @@ -2,8 +2,23 @@ #define __MUSIC_H #include -#include -void MusicPlay(void); +#define MAX_TONEGENS 6 +#define MAX_CHANNELS 2 + +typedef enum returnCodes { + OK, + STOPPED, + WRONG_HEADER, + OUT_OF_LOOP, + WRONG_BYTE +} RET_CODE; + +typedef struct { + uint8_t *begin; + uint32_t size; +} Track; + +RET_CODE MusicPlay(Track *track); void MusicStop(void); #endif \ No newline at end of file diff --git a/Core/Src/game.c b/Core/Src/game.c index f71e155..280b377 100644 --- a/Core/Src/game.c +++ b/Core/Src/game.c @@ -1,5 +1,7 @@ #include "game.h" +extern GameEngine game; + void InitGameEngine() { game.timerValue = game.turnTime; game.countScores = false; diff --git a/Core/Src/main.c b/Core/Src/main.c index 10311b8..0f3cf68 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -25,6 +25,14 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include +#include "stm32f1xx_hal_gpio.h" +#include "lcd.h" +#include "game.h" +#include "task.h" +#include "queue.h" +#include "music.h" + /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -42,7 +50,6 @@ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ - /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ @@ -68,8 +75,25 @@ bool plusButton = false; bool minusButton = false; bool bigButton = false; -static TimerHandle_t secondsTimerHandle = NULL; -static TaskHandle_t xMusicHandle = NULL; +TimerHandle_t secondsTimerHandle = NULL; +TaskHandle_t xMusicHandle = NULL; + +asm( +"tetris:\n\t" +".incbin \"../../Resources/tetris.bin\"\n\t" // используем директиву .incbin +"tetris_len:\n\t" +".long .-tetris\n\t" // вставляем значение .long с вычисленной длиной файла +); +extern uint8_t *tetris; +extern uint32_t tetris_len; +asm( +"super_mario:\n\t" +".incbin \"../../Resources/super_mario.bin\"\n\t" // используем директиву .incbin +"super_mario_len:\n\t" +".long .-super_mario\n\t" // вставляем значение .long с вычисленной длиной файла +); +extern uint8_t *super_mario; +extern uint32_t super_mario_len; /* USER CODE END PV */ @@ -479,7 +503,7 @@ void vTaskTimerSetup(void *parameter) { vTimerCallback // function to call after timer expires ); - xTaskCreate(vTaskPlayerSetup, "Setup", configMINIMAL_STACK_SIZE, NULL, 1, NULL); + xTaskCreate(vTaskConfig, "Config", configMINIMAL_STACK_SIZE, NULL, 1, NULL); vTaskDelete(NULL); } @@ -565,10 +589,14 @@ void vTaskTurnEnd(void *parameter) { } void vTaskOvertime(void *parameter) { + Track music[2] = { + { .begin = tetris, .size = tetris_len}, + { .begin = super_mario, .size = super_mario_len} + }; while (1) { HAL_GPIO_WritePin(LED2_GPIO_Port, LED1_Pin, GPIO_PIN_SET); - MusicPlay(); + MusicPlay(&music[0]); HAL_GPIO_WritePin(LED2_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); vTaskDelay(1000); } diff --git a/Core/Src/music.c b/Core/Src/music.c index e6ff30f..278ff90 100644 --- a/Core/Src/music.c +++ b/Core/Src/music.c @@ -1,9 +1,223 @@ #include "music.h" +#include "FreeRTOS.h" +#include "task.h" +#include -void MusicPlay(void) { +#define NOTE_C0 16 +#define NOTE_CS0 17 +#define NOTE_D0 18 +#define NOTE_DS0 19 +#define NOTE_E0 21 +#define NOTE_F0 22 +#define NOTE_FS0 23 +#define NOTE_G0 24 +#define NOTE_GS0 26 +#define NOTE_A0 28 +#define NOTE_AS0 29 +#define NOTE_B0 31 +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4435 +#define NOTE_D8 4699 +#define NOTE_DS8 4978 -} +bool playing; +uint16_t pitches[110]; + +uint8_t tonegens[MAX_TONEGENS] = { 0, 0, 0, 0, 0, 0 }; +uint8_t channelOut[MAX_TONEGENS] = { 0, 0, 0, 0, 0, 0 }; void MusicStop(void) { + playing = false; + for (int i = 0; i < MAX_TONEGENS; i++) { + // disable PWM + tonegens[i] = 0; + channelOut[i] = 0; + } +} +RET_CODE MusicPlay(Track *track) { + MusicStop(); + uint8_t *melody = track->begin; + uint32_t melodySize = track->size; + playing = true; + TickType_t xLastWakeTime; + + xLastWakeTime = xTaskGetTickCount(); + if (melody[0] != 'P' || melody[1] != 't') + { + MusicStop(); + return WRONG_HEADER; + } + for (int i = melody[2]; i < melodySize; i++) { + if (playing == true) + { + //melody[2] stores the length of header + /* If the high-order bit of the byte is 0, it is a command to delay for a while until + the next note change. The other 7 bits and the 8 bits of the following byte are + interpreted as a 15-bit big-endian integer that is the number of milliseconds to + wait before processing the next command. For example, 07 D0 + would cause a delay of 0x07d0 = 2000 decimal millisconds, or 2 seconds. Any tones + that were playing before the delay command will continue to play.*/ + uint8_t byte = melody[i]; + if ((melody[i] >> 7) == 0) { + uint32_t delay = (melody[i] << 8) + melody[i + 1]; + vTaskDelayUntil(&xLastWakeTime, delay); + i++; + } + else { + switch (melody[i] >> 4) { + case 0xF || 0xE : { + MusicStop(); + return OK; + } + case 0x8 : { + // 8t Stop playing the note on tone generator t + uint8_t generator = melody[i] & 0x0F; + tonegens[generator] = 0; + break; + } + /* 9t nn [vv] + Start playing note nn on tone generator t, replacing any previous note. + Generators are numbered starting with 0. The note numbers are the MIDI + numbers for the chromatic scale, with decimal 69 being Middle A (440 Hz). + If the -v option was given, the third byte specifies the note volume.*/ + case 0x9 : { + uint8_t generator = melody[i] & 0x0F; + tonegens[generator] = pitches[melody[i + 1]]; + i++; + break; + } + /* Ct ii Change tone generator t to play instrument ii from now on. This will only + be generated if the -i option was given.*/ + case 0xC : { + //not impemented + i++; + break; + } + default: { + MusicStop(); + return WRONG_BYTE; + } + } + } + + //placeholder (тут так и было, хз) + for (uint8_t i = 0; i < MAX_TONEGENS; i++) { + channelOut[i] = 0; + } + + uint8_t activeChannels = 0; + uint8_t currentChannel = 0; + for (uint8_t i = 0; i < MAX_TONEGENS; i++) { + if (tonegens[i] && (activeChannels < MAX_CHANNELS)) { + activeChannels++; + channelOut[currentChannel++] = tonegens[i]; + } + } + + for (uint8_t i = 0; i < MAX_CHANNELS; i++) { + channelOut[i] = 0; + } + /* + for (auto i = 0; i < channels.size(); i++) { + channels[i].PWM_SetFrequency(channelOut[i] ? channelOut[i] : Periph::Timer::PWM_MAX); + } + */ + } else { + MusicStop(); + return STOPPED; + } + } + return OUT_OF_LOOP; } + +/* +MusicPlayer() { + for (auto &channel : channels) + channel.PWM_Init(); + channels[1].PWM_SetParam(1000, 250); +}*/ diff --git a/Makefile b/Makefile index f8d8081..e18595a 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,7 @@ Middlewares/FreeRTOS/Src/croutine.c \ Middlewares/FreeRTOS/Src/queue.c \ Middlewares/FreeRTOS/Src/timers.c \ Core/Src/main.c \ +Core/Src/music.c \ Core/Src/lcd.c \ Core/Src/game.c \ Core/Src/stm32f1xx_it.c \ @@ -133,6 +134,7 @@ AS_INCLUDES = # C includes C_INCLUDES = \ +-IMiddlewares/FreeRTOS/Inc \ -IUSB_DEVICE/App \ -IUSB_DEVICE/Target \ -ICore/Inc \ @@ -141,7 +143,6 @@ C_INCLUDES = \ -IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc \ -IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc \ -IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ --IMiddlewares/FreeRTOS/Inc \ -IDrivers/CMSIS/Include diff --git a/Resources/boulevard_of_broken_dreams.bin b/Resources/boulevard_of_broken_dreams.bin new file mode 100644 index 0000000000000000000000000000000000000000..5b78a63cb23c8bbad30ee5da883ffcbcad378424 GIT binary patch literal 12303 zcmeI2-D)E@6vv%X+Lx%ogltoTnf2B+>li1&*bIiW(3j{N6!xl+@4JFVqnVdUo@wdz zUiEjR`H+-&z1b$&EcHdA^FQazNcvk}M`wQj^SfT>d)3dzdZ_xH8~$$BmV>i%JO+3S z)|4akBkb$S1bc#gUAeIZYwE4rv##D+zpma|-_PcHmLHgL;jxj;snznL=Gjai2HqXPL<8ha&dWhZsJRpn8P%0ag3+?24%A0_Cv7^ze*@?$d< z>Fm05lZ|*mHd0FwMamk7ER_nxu0|Pl{-;7M9`pQ&SBzD1hT8HibvcO|Ds!1o2k%X; zd2wQ^z&Gn-pP7SVTnKRBq1AU@; z0z}SgpOIdPmgd|4C0)rE=D3)Or1lwhdc@P3j;ehDBJ0LS<0CbQPd1`Hd?-^;g4f&} z@uo3-+~rN8vWAt~6(lm%)lR8((x`PpZMf(ppP%6s5Tm;Cs*mu}FyW1<(sF3IYQ~C; z1fQRZoxjUv=e028MwIhvM@+-+NcH&HVNRH8#?HP!b}SjRKXA}8XrZwPwTvUHtr#h% zg(FQTj5CcabXaQqVT86sTY6)U!BuPTCmN+* zr)*41G_B=OPt>c7|M{ECtLvS2POMcmHox7`oOqgv9_6h0nJq7Q$O?vS(n+-@#%7sK zM3)boAMHxb)J$_(k=>zTUms`Fd~|umJB~GySr!wuFWa!goew6}Q_Z@5-TBL@7>Cxi z^WpsRs#e~4?uPzC4d=wXZ_;9_y6mZm!%~|wtcn?inI1YU3SRN0u?pRwm^*8fz=R#w)_S==ALT;@dxnFPj6N>X*a| zE}m*7Ke7^S633bRaE*C!Uz{bJYlTf1=&Wm}&+QeS@%T9>eps}IRC7fq(G97~4KCMzUUo$yW@vm?)3>b0B_$M5mRY*tj) z>&4r~R9&xE7Vdi0D?Pusv{&m~@o@30=W@M#Sb>EnFJCPjZNH=s@UiW8JDJpC zH?bGmezo8tk4CDd&8Bw>oRDT8O+%CS1hAuUp&Z1(8YZrx|Ul>C{W{UNETFx zu0>znY9YFI;6s5!_)bBscb?9hLYzQguh_-E~qg zOc{h4|ZNdX5XQ5U}4?QnNs*i5lF9!ISgej+%@`C0w0+i^|Y{3FOHGG7^ zBv#CRRC9fVin%WyQi~tf*vIUvnpz{t}KJ1dz==EAXLZ9TT8);wNiRA zY?snpr<326EVyR2+idmHOjOs9%O+E9^VobF*p)MZvHQJnf@gh_u_uHFQ9`8PA8zcC z#*$T#%d?g$k7w&?l67(Njy6<&t;D8xS9zb#ujWzZ>Imhiz5M2VeWr3HO$i*p)#=KW z_y;()gR4`XEAbC-YzH6lrRUG;M_1w>;MfkXj&H8SKftja{EL2_|D=9!CH?`9?ckvQ z_y;()g9ASB4{&S;2Ylck;Mfih_`pBFu^k-nfq#HwJ2>D2{{Y8!aKH!t0gmn9fDilw z9NWPGANU72wu1vc@DFfo2M2uMAK=&y4*0-7z_A@1@PU7TV>>wD1OEWWc5uK4{sE5d z;D8VO1037I0U!7WIJScWKJX84YzGH?;2+@F4i5OhKftja9PojEfMYv2-~;~v$9C|8 N_`pBnL!yv>{spA7Qf&YL literal 0 HcmV?d00001 diff --git a/Resources/gravity_falls_soundtrack.bin b/Resources/gravity_falls_soundtrack.bin new file mode 100644 index 0000000000000000000000000000000000000000..60c6198170819336b12bd2ec2cf8679648ec97f6 GIT binary patch literal 932 zcmb7?%?*M;5QVu&9k!uS1My;v1R-K279bt$)T5$3D{{3eA9{vv7ET@K(_pi3hKc$ZFLp(LzDZ;tVlK+;K*xnXMZ# tAzLTG;f`{mL3D8cqnpudt9`YauxZ+tbOjgbRF&^hP=29@>wWR+-ycN*Y&ZY_ literal 0 HcmV?d00001 diff --git a/Resources/imperial_march.bin b/Resources/imperial_march.bin new file mode 100644 index 0000000000000000000000000000000000000000..49b4c2c1882acf642167a9ae1e6bea14fd758869 GIT binary patch literal 944 zcma)5%T>cL5R}7v)Q}5#bijf4Vn`q&wu#N3D<_(u0J{Nuw-0D1X@dg=FuPjI#Bku? zc(N;vX0+qyx5HyMp`6p?y>47LLLXZ5e3~37+VH|vK&7seZcEag#cl~yQOY@`uB_co zUIC&Cn4z0VM_-hIOIF?T$^-}s#}2hS_M;;}#B*EM3utMO7X(zb+tSLt;;HoQfjt~R zh{v;rR$21IxpB!w1gk)1wePm{$eHPQ;5CGc)qB1&1eN=>(Vn{&?QXAajwRlx z%SbWr6fpplKnhE&hKNcx!z_b7MfzVd7bYyJp4O9PAkd6Upe^b@_yW705H}#e18x`W-xVjo)_NEyvIM1AA7? zA4aDR-}u&1dvvZ=yTP8fkK>7>HgPS-C+3TGLyS=y?}8Kb=9!Gn52Mo_Yehc&^T@@h z?U`wD#;I$_b?c~2tXSmo>luhD(I(70CU#HvGClBKbwI-SWP2k8Nv&jA3);{$c&_LUNiwdYll7 zF)(?<<;4ydl)d66+5)HVyc9r7EiaTHORlh;O1i#*)^LqYNhEPE4c+Q9ARJgBnO7)qjLR%&?9v67&Ox#{od-1qwQ?HMB{UM%pvK{{U^2sEhyr literal 0 HcmV?d00001 diff --git a/Resources/tetris.bin b/Resources/tetris.bin new file mode 100644 index 0000000000000000000000000000000000000000..bced31531598eefbece2067b45a768840289179c GIT binary patch literal 1987 zcmbVML2d#u3@lRBUXvg6SP3Ceg#>6CLWWBLHv7bLutC)8s*cCuXr^svAVi9H^B z{Q33t^6;E?`E?S~@_Q0fm*@1^=OqDKf}_Aa!yUt$JS#A+;cTQ32I5+QI}b58(BcNV zhIE{{FA+l8_$^Bo5>77Yc>^{2M#A@uUx&;X`0qK2z`MMlqbtBudiuPgSAbE#ZFaaJ zIwAmwszgRKGEqg1XiO|7C4sD5T&QG?E-OSrQIHj}RKY+2gn`Ja2}Ctf8*CLW?UJ|( znyg4+jWJoFu%RMrAXzB{Ju`~zvJzHgEpVmal+Hp{5LH<*HbqvHW)ieSRTm?Qf{~d= z!H9|~igF}Q)t}_(54{Nr8XH0pwnn#keL~_E?FJ|%pfke=f>uhEzOKvAN~dslndgst z5&_I4oBt+O3bPh%dRWcW!FyOl_$bV52#-uln;`Z^JaDdTc+(uBE+YJ| zIUH~Al{pl92e$X!bBOwUJnx)CE%n+QnhAYtimYfLPZVZvWNV7Ov5d@yE-SW$HEd*S z!fdxLqk3x+HEJIc4(uJSZB4PauF|2swbo+qG9WB~*}IuxleOu8aaV038{-S+tnKZF ztL^RRxD8=%tw2l^6YSkU^zz5vmlqG6W^=WRN6?yCU7Jn;2EsaT_VyCY-Uc1dzorfc Af&c&j literal 0 HcmV?d00001