Yossi_Students / Mbed 2 deprecated TAU_ZOOLOG_WAVE_Generator

Dependencies:   mbed

Fork of TAU_ZOOLOG_Chirp_Generator by Yossi_Students

predefined_output.h

Committer:
TauZoolog
Date:
2017-06-27
Revision:
3:25cd717ad782
Child:
9:6d4136d9c074

File content as of revision 3:25cd717ad782:

// Predefined output procedure

inline void predefined_output(void)
{
    int i;
    uint32_t ADCValue;

    // Read ADC input
    // ADCFloat=((uint16_t)(hadc1.Instance->DR) * ADC2Float)-1.0f;
    ADCValue = (uint32_t)hadc1.Instance->DR;

    // Read ADC input
    ADCFloat=(ADCValue * ADC2Float)-1.0f;
    // ADCFloat=(float)(hadc1.Instance->DR);

    // toggle pin PA_6 -- D12, Loop frequency indicator
    mytoggle = ! mytoggle;

    if( ADCValue > ADCValueMax) {
        ADCValueMax = ADCValue;
        // printf("ADCValueMin = %d; ADCValueMax = %d; ADCValueDelta = %d\n", ADCValueMin, ADCValueMax, ADCValueMax - ADCValueMin);
    }
    if( ADCValue < ADCValueMin) {
        ADCValueMin = ADCValue;
        // printf("ADCValueMin = %d; ADCValueMax = %d; ADCValueDelta = %d\n", ADCValueMin, ADCValueMax, ADCValueMax - ADCValueMin);
    }

    //////////////////////////////////////////////////////
    // Apply Filter to input                            //
    //////////////////////////////////////////////////////
    if (use_filter_with_predefined_output) {
        LLastInput[0] = LastInput[0];
        LastInput[0] = CurrInput[0];
        CurrInput[0] = ADCFloat;
        for (i=1; i <= NumSectionsHP; i++) {
            LLastInput[i] = LastInput[i];
            LastInput[i] = CurrInput[i];
            CurrInput[i] = SOSMatHP[i-1][0]*CurrInput[i-1] + SOSMatHP[i-1][1]*LastInput[i-1] +
                           SOSMatHP[i-1][2]*LLastInput[i-1] -
                           SOSMatHP[i-1][4]*LastInput[i] - SOSMatHP[i-1][5]*LLastInput[i];
            ADCFloatFiltered = CurrInput[i];
        }

        ADCFloatFiltered = ADCFloatFiltered * GscaleHP;
    } else {
        ADCFloatFiltered = ADCFloat;
    }

    if((ADCValueMax - ADCValueMin) > signal_low_threshold) {
        if (num_back_to_normal == 0) {
            num_threshold_crossing++;

        } else {
            num_back_to_normal--;
            num_threshold_crossing = 0;
        }
        if (num_threshold_crossing > num_of_cycles_to_detect_threshold_crossing) {
            // start transmission of predefined output signal
            startPredefinedOutput = 1;
            // printf("ADCFloatFiltered = %f\n", ADCFloatFiltered);
        }
    } else {
        num_threshold_crossing = 0;
    }

    if (startPredefinedOutput == 0 ) {
        *(__IO uint32_t *) Dac_Reg = (uint16_t)Float2ADC;

    } else {
        //  Predefined Output
        // printf("ADCValueMin = %d; ADCValueMax = %d; ADCValueDelta = %d\n", ADCValueMin, ADCValueMax, ADCValueMax - ADCValueMin);

        //printf("play predefined sound\n");
        for (i=0; i < AMOUNT_OF_SIGNIFICANT_SAMPLES; i++ ) {
            // toggle pin PA_6 -- D12, Loop frequency indicator
            mytoggle = ! mytoggle;
            //OutputADCFloat  = scaled_sounddata[i];
            //OutputADCFloat  = (sounddata[i] * scale_for_predefined_output);
            // ADCValueOut=(uint16_t)(((sounddata[i] * scale_for_predefined_output) + 1.0f) * Float2ADC);
            ADCValueOut=(uint16_t)((scaled_sounddata[i] + 1.0f) * Float2ADC);
            // Output value using DAC
            // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut);
            *(__IO uint32_t *) Dac_Reg = ADCValueOut;

            // Delay processor to achieve SAMPLING_FREQUENCY (500KHz)
            wait_us(sampling_frequency_delay);  // delay in microseconds
            NOP();
            NOP();
            NOP();
            NOP();
            NOP();
            NOP();
            NOP();
            NOP();
#if 1
            NumCycles++;
            if((NUM_CYCLES_TO_CHECK_KEYBOARD_INPUT > 0)  && (NumCycles % NUM_CYCLES_TO_CHECK_KEYBOARD_INPUT == 0)) {
                // printf("Check keyboard input. Cycle: %d\n", NumCycles);
                UserButtonPressed = checkKeyboardInput();
            }
#endif
        }
        wait_us(delay_after_signal_transmission);
        num_back_to_normal = NUM_OF_CYCLES_BACK_TO_NORMAL;
        startPredefinedOutput = 0;
        ADCValueMax = 0;
        ADCValueMin = 0xfff;
    }

}

inline void test_output()
{
    int i;

    for (i=0; i < AMOUNT_OF_SIGNIFICANT_SAMPLES; i++ ) {
        // toggle pin PA_6 -- D12, Loop frequency indicator
        mytoggle = ! mytoggle;
        //OutputADCFloat  = scaled_sounddata[i];
        OutputADCFloat  = sounddata[i] * scale_for_predefined_output;
        ADCValueOut=(uint16_t)((OutputADCFloat + 1.0f) * Float2ADC);
        // Output value using DAC
        // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut);
        *(__IO uint32_t *) Dac_Reg = ADCValueOut;

        // Delay processor to achieve SAMPLING_FREQUENCY (500KHz)
        wait_us(sampling_frequency_delay);  // delay in microseconds
        // NOP();
        // NOP();
        // NOP();
        // NOP();
        // NOP();
        // NOP();
        // NOP();
        // NOP();
#if 1
        NumCycles++;
        if((NUM_CYCLES_TO_CHECK_KEYBOARD_INPUT > 0)  && (NumCycles % NUM_CYCLES_TO_CHECK_KEYBOARD_INPUT == 0)) {
            // printf("Check keyboard input. Cycle: %d\n", NumCycles);
            UserButtonPressed = checkKeyboardInput();
        }
#endif
    }
    wait_us(delay_after_signal_transmission);
}