<commit_before>/**
 ******************************************************************************
 * @file    FreeRTOS/FreeRTOS_ThreadCreation/Src/main.c
 * @author  MCD Application Team
 * @version V1.1.0
 * @date    26-June-2014
 * @brief   Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
 *
 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *        http://www.st.com/software_license_agreement_liberty_v2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "string.h"

#include "stm32f4xx_hal.h"
#include "stm32f429i_discovery.h"
#include "stm32f429i_discovery_lcd.h"
#include "stm32f429i_discovery_ts.h"

#include "cmsis_os.h"

#include "MspInit.h"
#include "helper.h"
#include "setting.h"

#include "base-effect.h"
#include "volume.h"
#include "delay.h"
#include "distortion.h"
#include "reverb.h"


//static void Error_Handler(void);
static void SystemClock_Config(void);

/* Private variables ---------------------------------------------------------*/
DMA_HandleTypeDef hdma_adc1;
DMA_HandleTypeDef hdma_adc2;
DMA_HandleTypeDef hdma_dac2;
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
DAC_HandleTypeDef hdac;
TIM_HandleTypeDef htim2;

struct Volume_t vol;
struct Distortion_t distor;
struct Reverb_t delay;

osThreadId LEDThread1Handle;
static void LED_Thread1(void const *argument);

osThreadId SPUid;
static void SignalProcessingUnit(void const *argument);
volatile uint32_t SPU_Hold = 0;
volatile uint16_t SignalBuffer[BUFFER_NUM][SAMPLE_NUM];
volatile float SignalPipe[STAGE_NUM][SAMPLE_NUM];
struct Effect_t *EffectStages[STAGE_NUM];

osThreadId UIid;
static void UserInterface(void const *argument);

osThreadId InputEventId;
static void InputEvent(void const *argument);

uint32_t controllingStage = 0;
uint8_t values[3];

int main(void){
    /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
       */

    /* Configure the system clock to 180 Mhz */
    SystemClock_Config();

    HAL_Init();

    BSP_LCD_Init();
    BSP_LCD_LayerDefaultInit(LCD_FOREGROUND_LAYER, LCD_FRAME_BUFFER);
    BSP_LCD_LayerDefaultInit(LCD_BACKGROUND_LAYER, LCD_FRAME_BUFFER + BUFFER_OFFSET);
    BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
    BSP_LCD_SetBackColor(LCD_COLOR_BLACK);

    BSP_LCD_SelectLayer(LCD_FOREGROUND_LAYER);
    BSP_LCD_Clear(LCD_COLOR_RED);
    BSP_LCD_FillCircle(150, 150, 40);

    BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());

    BSP_SDRAM_Init();

    BSP_LED_Init(LED3);
    BSP_LED_Init(LED4);

    MX_GPIO_Init();
    MX_DMA_Init();
    MX_TIM2_Init();
    MX_ADC1_Init();
    MX_ADC2_Init();
    MX_DAC_Init();

    osThreadDef(LED3, LED_Thread1, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
    LEDThread1Handle = osThreadCreate (osThread(LED3), NULL);

    osThreadDef(SPU, SignalProcessingUnit, osPriorityNormal, 0, 1024);
    SPUid = osThreadCreate (osThread(SPU), NULL);

    osThreadDef(UI, UserInterface, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
    UIid = osThreadCreate (osThread(UI), NULL);

    osThreadDef(IE, InputEvent, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
    UIid = osThreadCreate (osThread(IE), NULL);

    osKernelStart (NULL, NULL);

    while (1);
}

static void SignalProcessingUnit(void const *argument){
    uint32_t index = 0;
    uint32_t pipeindex = 0;
    uint32_t i;

    for(i = 0; i < STAGE_NUM; i++){
        EffectStages[i] = NULL;
    }

    /* Effect Stage Setting*/

    //EffectStages[0] = new_Volume(&vol);
    //EffectStages[0] = new_Distortion(&distor);
    EffectStages[0] = new_Reverb(&delay);

    /* Init */
    HAL_TIM_Base_Start(&htim2);
    HAL_ADC_Start_DMA_DoubleBuffer(&hadc1, (uint32_t*)SignalBuffer[0], (uint32_t*)SignalBuffer[1], SAMPLE_NUM);
    HAL_DAC_Start_DMA_DoubleBuffer(&hdac, DAC_CHANNEL_2, (uint32_t*) SignalBuffer[1], (uint32_t*) SignalBuffer[2], SAMPLE_NUM, DAC_ALIGN_12B_R);

    /* Process */
    while(1){
        if(SPU_Hold){
            SPU_Hold--;

            for(i = 0; i < STAGE_NUM; i++){
                if(EffectStages[i] != NULL){
                    EffectStages[i]->func(SignalPipe[(pipeindex - i) % STAGE_NUM], EffectStages[i]);
                }
            }

            DenormalizeData(SignalPipe[(pipeindex - STAGE_NUM + 1) % STAGE_NUM], SignalBuffer[(index - 1) % BUFFER_NUM]);

            index+=1;
            if(index >= BUFFER_NUM)
                index = 0;
            pipeindex+=1;
            if(pipeindex >= STAGE_NUM)
                pipeindex = 0;
        }
    }

    while(1);
}

static void UserInterface(void const *argument){
    while (1) {
        BSP_LCD_Clear(LCD_COLOR_WHITE);

        if(EffectStages[controllingStage] == NULL){
            BSP_LCD_DisplayStringAt(0, 0, (uint8_t*) "uROCK", CENTER_MODE);
        }else{
            BSP_LCD_DisplayStringAt(0, 0, (uint8_t*) "uROCK", CENTER_MODE);
            BSP_LCD_DisplayStringAt(0, 1 * 16, (uint8_t*) EffectStages[controllingStage]->name, CENTER_MODE);

            EffectStages[controllingStage]->adj(EffectStages[controllingStage], values);
        }

        osSignalWait(0, 0);
    }
}

static void InputEvent(void const *argument){
    TS_StateTypeDef tp;

    HAL_ADC_Start_DMA(&hadc2, (uint32_t*)values, 3);

    while (1) {
        BSP_TS_GetState(&tp);
        if (tp.TouchDetected == 1) {
            controllingStage++;

            if(controllingStage >= 4)
                controllingStage = 0;

           osSignalSet(UIid, 0);
        }
<commit_msg>Basic double buffer and draft event system done<commit_after>/**
 ******************************************************************************
 * @file    FreeRTOS/FreeRTOS_ThreadCreation/Src/main.c
 * @author  MCD Application Team
 * @version V1.1.0
 * @date    26-June-2014
 * @brief   Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
 *
 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *        http://www.st.com/software_license_agreement_liberty_v2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "string.h"

#include "stm32f4xx_hal.h"
#include "stm32f429i_discovery.h"
#include "stm32f429i_discovery_lcd.h"
#include "stm32f429i_discovery_ts.h"

#include "cmsis_os.h"

#include "MspInit.h"
#include "helper.h"
#include "setting.h"

#include "base-effect.h"
#include "volume.h"
#include "delay.h"
#include "distortion.h"
#include "reverb.h"


//static void Error_Handler(void);
static void SystemClock_Config(void);

/* Private variables ---------------------------------------------------------*/
DMA_HandleTypeDef hdma_adc1;
DMA_HandleTypeDef hdma_adc2;
DMA_HandleTypeDef hdma_dac2;
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
DAC_HandleTypeDef hdac;
TIM_HandleTypeDef htim2;

struct Volume_t vol;
struct Distortion_t distor;
struct Reverb_t delay;

osThreadId LEDThread1Handle;
static void LED_Thread1(void const *argument);

osThreadId SPUid;
static void SignalProcessingUnit(void const *argument);
volatile uint32_t SPU_Hold = 0;
volatile uint16_t SignalBuffer[BUFFER_NUM][SAMPLE_NUM];
volatile float SignalPipe[STAGE_NUM][SAMPLE_NUM];
struct Effect_t *EffectStages[STAGE_NUM];

osThreadId UIid;
static void UserInterface(void const *argument);

osThreadId InputEventId;
static void InputEvent(void const *argument);

enum EventType{
    EVENT_TP_PRESSED = 0x00,
    EVENT_TP_RELEASED,
};
enum EventType eventType;
uint32_t controllingStage = 0;
uint8_t values[3];
uint16_t touchX, touchY;

int main(void){
    /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
       */

    /* Configure the system clock to 180 Mhz */
    SystemClock_Config();

    HAL_Init();

    BSP_LCD_Init();
    BSP_LCD_LayerDefaultInit(LCD_FOREGROUND_LAYER, LCD_FRAME_BUFFER);
    BSP_LCD_LayerDefaultInit(LCD_BACKGROUND_LAYER, LCD_FRAME_BUFFER + BUFFER_OFFSET);

    BSP_LCD_SelectLayer(LCD_BACKGROUND_LAYER);
    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);

    BSP_LCD_SelectLayer(LCD_FOREGROUND_LAYER);
    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);

    BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());

    BSP_SDRAM_Init();

    BSP_LED_Init(LED3);
    BSP_LED_Init(LED4);

    MX_GPIO_Init();
    MX_DMA_Init();
    MX_TIM2_Init();
    MX_ADC1_Init();
    MX_ADC2_Init();
    MX_DAC_Init();

    osThreadDef(LED3, LED_Thread1, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
    LEDThread1Handle = osThreadCreate (osThread(LED3), NULL);

    osThreadDef(SPU, SignalProcessingUnit, osPriorityNormal, 0, 1024);
    SPUid = osThreadCreate (osThread(SPU), NULL);

    osThreadDef(UI, UserInterface, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
    UIid = osThreadCreate (osThread(UI), NULL);

    osThreadDef(IE, InputEvent, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
    InputEventId = osThreadCreate (osThread(IE), NULL);

    osKernelStart (NULL, NULL);

    while (1);
}

static void SignalProcessingUnit(void const *argument){
    uint32_t index = 0;
    uint32_t pipeindex = 0;
    uint32_t i;

    for(i = 0; i < STAGE_NUM; i++){
        EffectStages[i] = NULL;
    }

    /* Effect Stage Setting*/

    //EffectStages[0] = new_Volume(&vol);
    //EffectStages[0] = new_Distortion(&distor);
    EffectStages[0] = new_Reverb(&delay);

    /* Init */
    HAL_TIM_Base_Start(&htim2);
    HAL_ADC_Start_DMA_DoubleBuffer(&hadc1, (uint32_t*)SignalBuffer[0], (uint32_t*)SignalBuffer[1], SAMPLE_NUM);
    HAL_DAC_Start_DMA_DoubleBuffer(&hdac, DAC_CHANNEL_2, (uint32_t*) SignalBuffer[1], (uint32_t*) SignalBuffer[2], SAMPLE_NUM, DAC_ALIGN_12B_R);

    /* Process */
    while(1){
        if(SPU_Hold){
            SPU_Hold--;

            for(i = 0; i < STAGE_NUM; i++){
                if(EffectStages[i] != NULL){
                    EffectStages[i]->func(SignalPipe[(pipeindex - i) % STAGE_NUM], EffectStages[i]);
                }
            }

            DenormalizeData(SignalPipe[(pipeindex - STAGE_NUM + 1) % STAGE_NUM], SignalBuffer[(index - 1) % BUFFER_NUM]);

            index+=1;
            if(index >= BUFFER_NUM)
                index = 0;
            pipeindex+=1;
            if(pipeindex >= STAGE_NUM)
                pipeindex = 0;
        }
    }

    while(1);
}

static void present(){
    static uint32_t currentDrawLayer = LCD_BACKGROUND_LAYER;

    if(currentDrawLayer == LCD_BACKGROUND_LAYER){
        BSP_LCD_SetTransparency(LCD_FOREGROUND_LAYER, 0);
        BSP_LCD_SelectLayer(LCD_FOREGROUND_LAYER);
        currentDrawLayer = LCD_FOREGROUND_LAYER;
    }else{
        BSP_LCD_SetTransparency(LCD_FOREGROUND_LAYER, 255);
        BSP_LCD_SelectLayer(LCD_BACKGROUND_LAYER);
        currentDrawLayer = LCD_BACKGROUND_LAYER;
    }
}

static void UserInterface(void const *argument){
    BSP_LCD_SetTransparency(LCD_FOREGROUND_LAYER, 0);
    BSP_LCD_SetTransparency(LCD_BACKGROUND_LAYER, 255);
    BSP_LCD_SelectLayer(LCD_BACKGROUND_LAYER);

    while(1){
        /* Event part */
        switch(eventType){
        case EVENT_TP_PRESSED:
            break;
        case EVENT_TP_RELEASED:
            controllingStage++;

            if(controllingStage >= STAGE_NUM)
                controllingStage = 0;
            break;
        }

        /* Render part */
        BSP_LCD_Clear(LCD_COLOR_WHITE);

        if(EffectStages[controllingStage] == NULL){
            BSP_LCD_DisplayStringAt(0, 0, (uint8_t*) "uROCK", CENTER_MODE);
        }else{
            BSP_LCD_DisplayStringAt(0, 0, (uint8_t*) "uROCK", CENTER_MODE);
            BSP_LCD_DisplayStringAt(0, 1 * 16, (uint8_t*) EffectStages[controllingStage]->name, CENTER_MODE);

            EffectStages[controllingStage]->adj(EffectStages[controllingStage], values);
        }

        present();

        osThreadResume(InputEventId);
        osThreadSuspend(UIid);
    }
}

static void InputEvent(void const *argument){
    TS_StateTypeDef tp;
    uint8_t lastTouchState = 0;

    HAL_ADC_Start_DMA(&hadc2, (uint32_t*)values, 3);

    while(1){
        BSP_TS_GetState(&tp);
        if(tp.TouchDetected != lastTouchState){
            if(tp.TouchDetected == 1){
                lastTouchState = 1;
                eventType = EVENT_TP_PRESSED;
                touchX = tp.X;
                touchY = tp.Y;
            }else if(tp.TouchDetected == 0){
                lastTouchState = 0;
                eventType = EVENT_TP_RELEASED;
                touchX = tp.X;
                touchY = tp.Y;
            }

            osThreadResume(UIid);
            osThreadSuspend(InputEventId);
        }

{'fraction_non_alphanumeric': 0.14950911640953718, 'fraction_numerical': 0.021318373071528753, 'mean_word_length': 3.271039233303384, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1812249', 'n_tokens_mistral': 4996, 'n_tokens_neox': 4618, 'n_words': 965}