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 0000000..5b78a63 Binary files /dev/null and b/Resources/boulevard_of_broken_dreams.bin differ diff --git a/Resources/gravity_falls_soundtrack.bin b/Resources/gravity_falls_soundtrack.bin new file mode 100644 index 0000000..60c6198 Binary files /dev/null and b/Resources/gravity_falls_soundtrack.bin differ diff --git a/Resources/imperial_march.bin b/Resources/imperial_march.bin new file mode 100644 index 0000000..49b4c2c Binary files /dev/null and b/Resources/imperial_march.bin differ diff --git a/Resources/main_theme.bin b/Resources/main_theme.bin new file mode 100644 index 0000000..84133c8 Binary files /dev/null and b/Resources/main_theme.bin differ diff --git a/Resources/super_mario.bin b/Resources/super_mario.bin new file mode 100644 index 0000000..ad4781b Binary files /dev/null and b/Resources/super_mario.bin differ diff --git a/Resources/tetris.bin b/Resources/tetris.bin new file mode 100644 index 0000000..bced315 Binary files /dev/null and b/Resources/tetris.bin differ