1 #include "lms7002_dlgGFIR_Coefficients.h"
2 #include <wx/filedlg.h>
3 #include <wx/msgdlg.h>
4 #include "CoefficientFileParser.h"
5 
lms7002_dlgGFIR_Coefficients(wxWindow * parent)6 lms7002_dlgGFIR_Coefficients::lms7002_dlgGFIR_Coefficients( wxWindow* parent )
7 :
8 dlgGFIR_Coefficients( parent )
9 {
10 
11 }
12 
OnLoadFromFile(wxCommandEvent & event)13 void lms7002_dlgGFIR_Coefficients::OnLoadFromFile( wxCommandEvent& event )
14 {
15     wxFileDialog dlg(this, _("Open coefficients file"), "", "", "FIR Coeffs (*.fir)|*.fir", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
16     if (dlg.ShowModal() == wxID_CANCEL)
17         return;
18 
19     float cbuf[200];
20     int iVal = Parser::getcoeffs((const char*)dlg.GetPath().ToStdString().c_str(), cbuf, 200);
21 
22     switch (iVal)
23     {
24     case -2:
25         wxMessageDialog(this, "syntax error within the file", "Warning");
26         break;
27     case -3:
28         wxMessageDialog(this, "filename is empty string", "Warning");
29         break;
30     case -4:
31         wxMessageDialog(this, "can not open the file", "Warning");
32         break;
33     case -5:
34         wxMessageDialog(this, "too many coefficients in the file", "Warning");
35         break;
36     }
37     if (iVal < 0)
38         return;
39 
40     spinCoefCount->SetValue(iVal);
41     if (gridCoef->GetTable()->GetRowsCount() > 0)
42         gridCoef->GetTable()->DeleteRows(0, gridCoef->GetTable()->GetRowsCount());
43     gridCoef->GetTable()->AppendRows(spinCoefCount->GetValue());
44     for (int i = 0; i<iVal; ++i)
45     {
46         gridCoef->SetCellValue(i, 0, wxString::Format("%f", cbuf[i]));
47     }
48 }
49 
OnSaveToFile(wxCommandEvent & event)50 void lms7002_dlgGFIR_Coefficients::OnSaveToFile( wxCommandEvent& event )
51 {
52     wxFileDialog dlg(this, _("Save coefficients file"), "", "", "FIR Coeffs (*.fir)|*.fir", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
53     if (dlg.ShowModal() == wxID_CANCEL)
54         return;
55     float coefficients[200];
56     memset(coefficients, 0, sizeof(unsigned short) * 200);
57     double ltemp;
58     for (int i = 0; i<spinCoefCount->GetValue(); ++i)
59     {
60         ltemp = 0;
61         gridCoef->GetCellValue(i, 0).ToDouble(&ltemp);
62         coefficients[i] = ltemp;
63     }
64     Parser::saveToFile((const char*)dlg.GetPath().ToStdString().c_str(), coefficients, spinCoefCount->GetValue());
65 }
66 
OnClearTable(wxCommandEvent & event)67 void lms7002_dlgGFIR_Coefficients::OnClearTable( wxCommandEvent& event )
68 {
69     if (gridCoef->GetTable()->GetRowsCount() > 0)
70         gridCoef->GetTable()->DeleteRows(0, gridCoef->GetTable()->GetRowsCount());
71     gridCoef->GetTable()->AppendRows(spinCoefCount->GetValue());
72     for (int i = 0; i<spinCoefCount->GetValue(); ++i)
73     {
74         gridCoef->SetCellValue(i, 0, wxString::Format("%i", 0));
75     }
76 }
77 
OnspinCoefCountChange(wxSpinEvent & event)78 void lms7002_dlgGFIR_Coefficients::OnspinCoefCountChange(wxSpinEvent& event)
79 {
80     if (spinCoefCount->GetValue() < gridCoef->GetTable()->GetRowsCount())
81         gridCoef->GetTable()->DeleteRows(spinCoefCount->GetValue(), gridCoef->GetTable()->GetRowsCount() - spinCoefCount->GetValue());
82     else
83         gridCoef->GetTable()->AppendRows(spinCoefCount->GetValue() - gridCoef->GetTable()->GetRowsCount());
84 }
85 
SetCoefficients(const std::vector<double> & coefficients)86 void lms7002_dlgGFIR_Coefficients::SetCoefficients(const std::vector<double> &coefficients)
87 {
88     spinCoefCount->SetValue(coefficients.size());
89     if (gridCoef->GetTable()->GetRowsCount() > 0)
90         gridCoef->GetTable()->DeleteRows(0, gridCoef->GetTable()->GetRowsCount());
91     gridCoef->GetTable()->AppendRows(coefficients.size());
92     for (unsigned i = 0; i<coefficients.size(); ++i)
93         gridCoef->SetCellValue(i, 0, wxString::Format("%.6f", coefficients[i]));
94 }
95 
GetCoefficients()96 std::vector<double> lms7002_dlgGFIR_Coefficients::GetCoefficients()
97 {
98     std::vector<double> coefficients;
99     coefficients.resize(spinCoefCount->GetValue(), 0);
100     for (int i = 0; i<spinCoefCount->GetValue(); ++i)
101     {
102         double dtemp = 0;
103         gridCoef->GetCellValue(i, 0).ToDouble(&dtemp);
104         coefficients[i] = dtemp;
105     }
106     return coefficients;
107 }
108 
OnBtnOkClick(wxCommandEvent & event)109 void lms7002_dlgGFIR_Coefficients::OnBtnOkClick(wxCommandEvent& event)
110 {
111     EndModal(wxID_OK);
112 }
113 
OnBtnCancelClick(wxCommandEvent & event)114 void lms7002_dlgGFIR_Coefficients::OnBtnCancelClick(wxCommandEvent& event)
115 {
116     EndModal(wxID_CANCEL);
117 }