⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dtmffft.c

📁 VC写的对DTMF信号的识别程序
💻 C
📖 第 1 页 / 共 3 页
字号:
		{
			pdfts->ddps[n].dOffset=adOffsetDefault[n];
			sprintf(szVal,"%G",pdfts->ddps[n].dOffset);
			RegSetString(szKey,szVal);
		}

		sprintf(szKey,"ParameterMultiplier%d",n+1);
		if (RegGetString(szKey,szVal,sizeof(szVal)))
		{
			pdfts->ddps[n].dMultiplier=atof(szVal);
		}
		else
		{
			pdfts->ddps[n].dMultiplier=adMultiplierDefault[n];
			sprintf(szVal,"%G",pdfts->ddps[n].dMultiplier);
			RegSetString(szKey,szVal);
		}

		strcpy(pdfts->ddps[n].szUnits,aszUnitsDefault[n]);
		sprintf(szKey,"ParameterUnits%d",n+1);
		if (!RegGetString(szKey,pdfts->ddps[n].szUnits,DF_DLGSTRINGLEN))
		{
			RegSetString(szKey,pdfts->ddps[n].szUnits);
		}
		SetDlgItemText(pdfts->hdlg,pdfts->ddps[n].nDlgIDUnits,pdfts->ddps[n].szUnits);
	}

	// Show/hide fields...
	{
		int n;

		for (n=0;n<DF_MAXDLGPARAMETERS;n++)
		{
			static struct
			{
				int nNameID;
				int nEditID;
				int nUnitsID;
			} afs[DF_MAXDLGPARAMETERS]=
			{
				{IDC_DF_PARAMETER_NAME1,IDC_DF_PARAMETER_VALUE1,IDC_DF_PARAMETER_UNITS1},
				{IDC_DF_PARAMETER_NAME2,IDC_DF_PARAMETER_VALUE2,IDC_DF_PARAMETER_UNITS2},
				{IDC_DF_PARAMETER_NAME3,IDC_DF_PARAMETER_VALUE3,IDC_DF_PARAMETER_UNITS3},
				{IDC_DF_PARAMETER_NAME4,IDC_DF_PARAMETER_VALUE4,IDC_DF_PARAMETER_UNITS4},
				{IDC_DF_PARAMETER_NAME5,IDC_DF_PARAMETER_VALUE5,IDC_DF_PARAMETER_UNITS5}
			};

			ShowWindow(GetDlgItem(pdfts->hdlg,afs[n].nNameID),n<pdfts->nNumDlgParms ? SW_SHOW : SW_HIDE);
			ShowWindow(GetDlgItem(pdfts->hdlg,afs[n].nEditID),n<pdfts->nNumDlgParms ? SW_SHOW : SW_HIDE);
			ShowWindow(GetDlgItem(pdfts->hdlg,afs[n].nUnitsID),n<pdfts->nNumDlgParms ? SW_SHOW : SW_HIDE);
		}
	}

}

static BOOL DFInit(PDFTHREADSTRUCT pdfts,HWND hdlg)
{
	pdfts->hdlg=hdlg;
	{
		char szFileName[30];
		SYSTEMTIME st;

		pdfts->pf=NULL;
		GetSystemTime(&st);
		sprintf(szFileName,"DTMFFFT%04d%02d%02d.csv",st.wYear,st.wMonth,st.wDay);
		if ((pdfts->pf=fopen(szFileName,"at"))==NULL)
		{
			MessageBox(pdfts->hdlg,"Could not open log file","DTMF FFT",MB_OK | MB_ICONEXCLAMATION);
		}

		if (pdfts->pf!=NULL)
		{
			fprintf(pdfts->pf,"DTMFFFT,%04d%02d%02d %02d:%02d:%02d.%03d,UTC,Open   \n",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
		}
	}

	DFInitSetParameterFields(pdfts);

	// Create a monospace font for the window edit control
	pdfts->hf=CreateFont(0,0,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,
		DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,FIXED_PITCH | FF_DONTCARE,NULL);

	if (pdfts->hf!=NULL)
	{
		SendDlgItemMessage(pdfts->hdlg,IDC_EDIT_WINDOW,WM_SETFONT,(WPARAM)pdfts->hf,MAKELPARAM(FALSE,0));
	}

	// Let's initialize the inter-thread communications
	{
		char sz[10];
		pdfts->nThreshold=8;
		sprintf(sz,"%d",pdfts->nThreshold);
		SetDlgItemText(pdfts->hdlg,IDC_EDIT_THRESHOLD,sz);
	}

	InitializeCriticalSection(&pdfts->cs);
	pdfts->hevent=CreateEvent(NULL,FALSE,FALSE,NULL);
	pdfts->hwi=NULL;
	DFTSSetWaveBuffers(pdfts,0);
	DFTSSetStarting(pdfts,FALSE);
	DFTSSetStarted(pdfts,FALSE);
	DFTSSetFinish(pdfts,FALSE);
	DFTSSetFinished(pdfts,TRUE);

	// Now let's populate the combo box...
	{
		UINT uNum=waveInGetNumDevs();
		UINT u;
		int nItemID=0;

		for (u=0;u<uNum;u++)
		{
			WAVEINCAPS wic;

			waveInGetDevCaps(u,&wic,sizeof(wic));
			if ((wic.dwFormats & WAVE_FORMAT_1M16)!=0)
			{
				SendDlgItemMessage(pdfts->hdlg,IDC_COMBO_DEVICE,CB_ADDSTRING,0,(LPARAM)wic.szPname);
				// Set the device ID to the 32 bit combo box item data...
				SendDlgItemMessage(pdfts->hdlg,IDC_COMBO_DEVICE,CB_SETITEMDATA,(WPARAM)nItemID,(LPARAM)u);
				nItemID++;
			}
		}
		// Select the first item...
		SendDlgItemMessage(pdfts->hdlg,IDC_COMBO_DEVICE,CB_SETCURSEL,0,0);
	}
	return TRUE;
}

static LRESULT CALLBACK DFDlg(HWND hdlg,UINT message,WPARAM wParam,LPARAM lParam)
{
	static DFTHREADSTRUCT dfts;

	switch (message) 
	{
		case WM_INITDIALOG:
			DFInit(&dfts,hdlg);
			break;
		case WM_DESTROY:
			DFTerm(&dfts);
			break;
		case DF_WM_USER_NEWCHAR: // wParam has the character, lParam has the ticker time that the character was decoded
			{
				char sz[2];
				char c=(char)wParam;
				static char szDTMFDecode[]="D84#195A206B3*7C";
				static char szHex[]="0123456789ABCDEF";
				static char szWindow[DF_MAXDLGPARAMETERS*2+1]=""; // last DF_MAXDLGPARAMETERS*2 hex characters
				static DWORD adwWindowTicks[DF_MAXDLGPARAMETERS*2+1]; // last DF_MAXDLGPARAMETERS*2+1 character's ticker counts
				static BOOL bFirstTime=TRUE;
				int n;

				sz[0]=c;
				sz[1]='\0';

				SendDlgItemMessage(hdlg,IDC_EDIT_DTMF,EM_REPLACESEL,(WPARAM)FALSE,(LPARAM)sz);

				sz[0]=szHex[strchr(szDTMFDecode,c)-szDTMFDecode];
				SendDlgItemMessage(hdlg,IDC_EDIT_DTMF_DECODE,EM_REPLACESEL,(WPARAM)FALSE,(LPARAM)sz);

				n=strlen(szWindow);

				if (n>=dfts.nNumDlgParms*2-1) // We only want the last nNumDlgParms*2-1 characters
				{
					strcpy(szWindow,szWindow+n-(dfts.nNumDlgParms*2-1));
				}
				strcat(szWindow,sz); // append last decoded character to make nNumDlgParms*2 characters
				SetDlgItemText(hdlg,IDC_EDIT_WINDOW,szWindow);

				for (n=0;n<dfts.nNumDlgParms*2;n++) // move the ticks down
				{
					adwWindowTicks[n]=adwWindowTicks[n+1];
				}
				adwWindowTicks[dfts.nNumDlgParms*2]=(DWORD)lParam; // Save this last character's ticker time

				// If the total time for the last nNumDlgParms*2 stored DTMF codes is under 5 seconds AND
				// the time between the nNumDlgParms*2th and nNumDlgParms*2+1th last is over 5 seconds...
				if (adwWindowTicks[dfts.nNumDlgParms*2]-adwWindowTicks[1]<5000 && (bFirstTime || adwWindowTicks[1]-adwWindowTicks[0]>5000)) 
				{
					int anVal[DF_MAXDLGPARAMETERS];
					double adVal[DF_MAXDLGPARAMETERS];
					bFirstTime=FALSE;
					SendDlgItemMessage(hdlg,IDC_LIST_DECODE,LB_ADDSTRING,0,(LPARAM)szWindow);

					// Select the last item inserted...
					n=(int)SendDlgItemMessage(hdlg,IDC_LIST_DECODE,LB_GETCOUNT,0,0);
					SendDlgItemMessage(hdlg,IDC_LIST_DECODE,LB_SETCURSEL,(WPARAM)(n-1),0);

					// Now disect and present the data in fields...
					for (n=0;n<dfts.nNumDlgParms;n++)
					{
						char szVal[20];
						int nVal=strchr(szHex,szWindow[n*2])-szHex + 16*(strchr(szHex,szWindow[n*2+1])-szHex);
						double dVal=nVal*dfts.ddps[n].dMultiplier+dfts.ddps[n].dOffset;

						anVal[n]=nVal;
						adVal[n]=dVal;

						sprintf(szVal,"%.4G",dVal);
						SetDlgItemText(hdlg,dfts.ddps[n].nDlgIDValue,szVal);
					}

					for (;n<DF_MAXDLGPARAMETERS;n++)
					{
						anVal[n]=0;
						adVal[n]=0.0;
					}

					// Log to file...
					if (dfts.pf!=NULL)
					{
						SYSTEMTIME st;

						GetSystemTime(&st);
						fprintf
						(
							dfts.pf,
							"DTMFFFT,%04d%02d%02d %02d:%02d:%02d.%03d,UTC,Data   ,%s,%3d,%3d,%3d,%3d,%3d,%.4G,%.4G,%.4G,%.4G,%.4G\n",
							st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds,szWindow,
							anVal[0],anVal[1],anVal[2],anVal[3],anVal[4],
							adVal[0],adVal[1],adVal[2],adVal[3],adVal[4]
						);
					}
				}
			}
			break;
		case DF_WM_USER_REDRAW: // Redraw the spectrum display
			if (DFTSGetStarted(&dfts))
			{
				HWND hwnd=GetDlgItem(hdlg,IDC_STATIC_PICTURE);
				HDC hdc=GetDC(hwnd);
				RECT rc;
				S32 as32Buffer[DF_FFTLEN/2];
				S32 s32Max;
				S32 s32ThresholdAGC=DFTSGetThresholdAGC(&dfts);

				EnterCriticalSection(&dfts.cs);
				memcpy(as32Buffer,dfts.as32Buffer,sizeof(as32Buffer));
				LeaveCriticalSection(&dfts.cs);
				s32Max=FFTGetMax(&dfts.fs);

				if (s32ThresholdAGC>s32Max)
				{
					s32Max=s32ThresholdAGC;
				}
				
				if (s32Max<10000)
				{
					s32Max=10000;
				}
				GetWindowRect(hwnd,&rc);

				rc.right-=rc.left;
				rc.bottom-=rc.top;
				rc.left=0;
				rc.top=0;

				FillRect(hdc,&rc,(HBRUSH)(COLOR_WINDOW+1)); // Clear the space out.

				{
					int n;
					int nMaxy=rc.bottom;
					int nMaxx=rc.right;
					double dScaley=(double)s32Max/nMaxy;
					int nIncx=nMaxx/(DF_FFTLEN/2);

					for (n=0;n<DF_FFTLEN/2;n++)
					{
						int m;
						int y=nMaxy-(int)(as32Buffer[n]/dScaley);

						for (m=n*nMaxx/(DF_FFTLEN/2);m<(n+1)*nMaxx/(DF_FFTLEN/2);m++)
						{
							MoveToEx(hdc,m,nMaxy,NULL);
							LineTo(hdc,m,y);
						}
					}

					// Draw the threshold...
					MoveToEx(hdc,0,nMaxy-(int)(s32ThresholdAGC/dScaley),NULL);
					LineTo(hdc,nMaxx,nMaxy-(int)(s32ThresholdAGC/dScaley));
				}
				ReleaseDC(hwnd,hdc);
				hdc=NULL;
			}
			break;
		case WM_VSCROLL:
			{
				int nChange=0;
				int nThreshold=DFTSGetThreshold(&dfts);
				char sz[10];

				switch (wParam)
				{
					case SB_LINEUP:
						nChange=1;
						break;
					case SB_LINEDOWN:
						nChange=-1;
						break;
					default:
						break;
				}
				nThreshold+=nChange;
				if (nThreshold<1)
				{
					nThreshold=1;
				}
				if (nThreshold>20)
				{
					nThreshold=20;
				}
				DFTSSetThreshold(&dfts,nThreshold);
				sprintf(sz,"%d",nThreshold);
				SetDlgItemText(hdlg,IDC_EDIT_THRESHOLD,sz);
			}
			break;
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDOK:
				case IDCANCEL:
				case IDM_EXIT:
					EndDialog(hdlg, TRUE);
					break;
				case IDC_BUTTON_STARTSTOP:
					{
						char sz[10];
						
						GetDlgItemText(hdlg,IDC_BUTTON_STARTSTOP,sz,sizeof(sz));
						if (strcmp(sz,"&Start")==0)
						{
							LONG l=SendDlgItemMessage(hdlg,IDC_COMBO_DEVICE,CB_GETCURSEL,0,0);
							LONG lDeviceID=SendDlgItemMessage(hdlg,IDC_COMBO_DEVICE,CB_GETITEMDATA,(WPARAM)l,0);

							if (DFThreadInit(&dfts,lDeviceID))
							{
								if (dfts.pf!=NULL)
								{
									SYSTEMTIME st;

									GetSystemTime(&st);
									fprintf(dfts.pf,"DTMFFFT,%04d%02d%02d %02d:%02d:%02d.%03d,UTC,Start  \n",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
								}
								SetDlgItemText(hdlg,IDC_BUTTON_STARTSTOP,"&Stop");
								SendDlgItemMessage(hdlg,IDC_COMBO_DEVICE,WM_ENABLE,(WPARAM)FALSE,0);
							}
						}
						else
						{
							DFThreadTerm(&dfts);
							DFWaveInTerm(&dfts);
							ResetEvent(dfts.hevent); // Do this or next time the thread starts it'll think something's happened.
							if (dfts.pf!=NULL)
							{
								SYSTEMTIME st;

								GetSystemTime(&st);
								fprintf(dfts.pf,"DTMFFFT,%04d%02d%02d %02d:%02d:%02d.%03d,UTC,Stop  \n",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
							}
							SetDlgItemText(hdlg,IDC_BUTTON_STARTSTOP,"&Start");
							SendDlgItemMessage(hdlg,IDC_COMBO_DEVICE,WM_ENABLE,(WPARAM)TRUE,0);
						}
					}
					break;
				case IDM_ABOUT:
					About(hdlg,_hinst);
					break;
				case IDM_PARAMETERS:
					if (Parameter(hdlg,_hinst))
					{
						DFInitSetParameterFields(&dfts);
					}
					break;
				default:
					break;
			}
            return TRUE;
		default:
			break;
	}
    return FALSE;
}

int APIENTRY WinMain(HINSTANCE hinst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow)
{
	_hinst=hinst;
	DialogBox(hinst,MAKEINTRESOURCE(IDD_DTMFFFT),NULL,DFDlg);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -