cancel
Showing results for 
Search instead for 
Did you mean: 

sine wave generation using touchgfx

Sharan
Senior

will this code work?

#include <gui/screen3_screen/Screen3View.hpp>

//#include <math.h>

#define PI 3.1415926

// Declare the DAC handle (assuming it's defined in another file)

extern DAC_HandleTypeDef hdac1;



static bool generateSineWave = false; // Flag to control sine wave generation



Screen3View::Screen3View()

: end1(172), end2(141), max1(480), max2(200) // Initialize sine wave parameters

{

}



void Screen3View::setupScreen()

{

Screen3ViewBase::setupScreen();

}



void Screen3View::tearDownScreen()

{

Screen3ViewBase::tearDownScreen();

}



void Screen3View::get_sineval()

{

// Generate sine wave values for both DAC channels

for (int i = 0; i < end1; i++)

{

value1[i] = ((sin(i * 2 * PI / end1) + 1) * (max1 / 2)); // Sine values for Channel 1

}

for (int i = 0; i < end2; i++)

{

value2[i] = ((sin(i * 2 * PI / end2) + 1) * (max2 / 2)); // Sine values for Channel 2

}

}



void Screen3View::buttonClicked()

{

generateSineWave = !generateSineWave; // Toggle the sine wave generation on/off



if (generateSineWave)

{

get_sineval(); // Generate the sine wave values



// Start the DAC for both channels

HAL_DAC_Start(&hdac1, DAC_CHANNEL_1); // Start DAC for Channel 1

HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); // Start DAC for Channel 2

}

else

{

// Stop the DAC when the button is pressed again

HAL_DAC_Stop(&hdac1, DAC_CHANNEL_1);

HAL_DAC_Stop(&hdac1, DAC_CHANNEL_2);

}

}



void Screen3View::tickEvent()

{

Screen3ViewBase::tickEvent(); // Call the base class tickEvent

if (generateSineWave)

{

HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value1[time1]);

time1+=1;

if (time1>end1-1)

time1=0;

HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value2[time2]);

time2+=1;

if (time2>end2-1)

time2=0;

}

}

 

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello @Sharan ,

In next time kindly use </> button to share your code. I'm editing your post then ..

Please review our recommendation in this link on how to post a thread.

 


@Sharan wrote:

will this code work?

Did you test it? 

From my point of view, you don't have to call HAL drivers from GUI (View), you need to pass the command of DAC On/Off over GUI layers: view -> presenter -> model -> HAL

Thank you.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3
SofLit
ST Employee

Hello @Sharan ,

In next time kindly use </> button to share your code. I'm editing your post then ..

Please review our recommendation in this link on how to post a thread.

 


@Sharan wrote:

will this code work?

Did you test it? 

From my point of view, you don't have to call HAL drivers from GUI (View), you need to pass the command of DAC On/Off over GUI layers: view -> presenter -> model -> HAL

Thank you.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

hi i tried the code it did not work 

where do i have to write my code ,in presenter.cpp or model.cpp or view.cpp

Hello,

You need to write code in view.cpp, presenter.cpp and model.cpp according to Model-View-Presenter Design Pattern.

Watch this video.

In model you start to interact with HAL/Hardware.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.