sine wave generation using touchgfx
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;
}
}
