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

📄 tg.c

📁 网络时间协议NTP 源码 版本v4.2.0b 该源码用于linux平台下
💻 C
📖 第 1 页 / 共 2 页
字号:
		exit(1);	}	rval = ioctl(fd, AUDIO_GETINFO, &info);	if (rval < 0) {		printf("audio control %s\n", strerror(errno));		exit(0);	}	info.play.port = port;	info.play.gain = level;	info.play.sample_rate = SECOND;	info.play.channels = 1;	info.play.precision = 8;	info.play.encoding = AUDIO_ENCODING_ULAW;	printf("port %d gain %d rate %d chan %d prec %d encode %d\n",	    info.play.port, info.play.gain, info.play.sample_rate,	    info.play.channels, info.play.precision,	    info.play.encoding);	ioctl(fd, AUDIO_SETINFO, &info); 	/*	 * Unless specified otherwise, read the system clock and	 * initialize the time.	 */	if (!utc) {		gettimeofday(&tv, NULL);		tm = gmtime(&tv.tv_sec);		minute = tm->tm_min;		hour = tm->tm_hour;		day = tm->tm_yday + 1;		year = tm->tm_year % 100;		second = tm->tm_sec;		/*		 * Delay the first second so the generator is accurately		 * aligned with the system clock within one sample (125		 * microseconds ).		 */		delay(SECOND - tv.tv_usec * 8 / 1000);	}	memset(code, 0, sizeof(code));	switch (encode) {	/*	 * For WWV/H and default time, carefully set the signal	 * generator seconds number to agree with the current time.	 */ 	case WWV:		printf("year %d day %d time %02d:%02d:%02d tone %d\n",		    year, day, hour, minute, second, tone);		sprintf(code, "%01d%03d%02d%02d%01d", year / 10, day,		    hour, minute, year % 10);		printf("%s\n", code);		ptr = 8;		for (i = 0; i <= second; i++) {			if (progx[i].sw == DEC)				ptr--;		}		break;	/*	 * For IRIG the signal generator runs every second, so requires	 * no additional alignment.	 */	case IRIG:		printf("sbs %x year %d day %d time %02d:%02d:%02d\n",		    0, year, day, hour, minute, second);		break;	}	/*	 * Run the signal generator to generate new timecode strings	 * once per minute for WWV/H and once per second for IRIG.	 */	while(1) {		/*		 * Crank the state machine to propagate carries to the		 * year of century. Note that we delayed up to one		 * second for alignment after reading the time, so this		 * is the next second.		 */		second = (second + 1) % 60;		if (second == 0) {			minute++;			if (minute >= 60) {				minute = 0;				hour++;			}			if (hour >= 24) {				hour = 0;				day++;			}			/*			 * At year rollover check for leap second.			 */			if (day >= (year & 0x3 ? 366 : 367)) {				if (leap) {					sec(DATA0);					printf("\nleap!");					leap = 0;				}				day = 1;				year++;			}			if (encode == WWV) {				sprintf(code, "%01d%03d%02d%02d%01d",				    year / 10, day, hour, minute, year %				    10);				printf("\n%s\n", code);				ptr = 8;			}		}		if (encode == IRIG) {			sprintf(code, "%04x%04d%06d%02d%02d%02d", 0,			    year, day, hour, minute, second);			printf("%s\n", code);			ptr = 19;		}		/*		 * Generate data for the second		 */		switch(encode) {		/*		 * The IRIG second consists of 20 BCD digits of width-		 * modulateod pulses at 2, 5 and 8 ms and modulated 50		 * percent on the 1000-Hz carrier.		 */		case IRIG:			for (i = 0; i < 100; i++) {				if (i < 10) {					sw = progz[i].sw;					arg = progz[i].arg;				} else {					sw = progy[i % 10].sw;					arg = progy[i % 10].arg;				}				switch(sw) {				case COEF:	/* send BCD bit */					if (code[ptr] & arg) {						peep(M5, 1000, HIGH);						peep(M5, 1000, LOW);						printf("1");					} else {						peep(M2, 1000, HIGH);						peep(M8, 1000, LOW);						printf("0");					}					break;				case DEC:	/* send IM/PI bit */					ptr--;					printf(" ");					peep(arg, 1000, HIGH);					peep(10 - arg, 1000, LOW);					break;				case MIN:	/* send data bit */					peep(arg, 1000, HIGH);					peep(10 - arg, 1000, LOW);					printf("M ");					break;				}				if (ptr < 0)					break;			}			printf("\n");			break;		/*		 * The WWV/H second consists of 9 BCD digits of width-		 * modulateod pulses 200, 500 and 800 ms at 100-Hz.		 */		case WWV:			sw = progx[second].sw;			arg = progx[second].arg;			switch(sw) {			case DATA:		/* send data bit */				sec(arg);				break;			case COEF:		/* send BCD bit */				if (code[ptr] & arg) {					sec(DATA1);					printf("1");				} else {					sec(DATA0);					printf("0");				}				break;			case LEAP:		/* send leap bit */				if (leap) {					sec(DATA1);					printf("L ");				} else {					sec(DATA0);					printf("  ");				}				break;			case DEC:		/* send data bit */				ptr--;				sec(arg);				printf(" ");				break;			case MIN:		/* send minute sync */				peep(arg, tone, HIGH);				peep(1000 - arg, tone, OFF);				break;			case DUT1:		/* send DUT1 bits */				if (dut1 & arg)					sec(DATA1);				else					sec(DATA0);				break;							case DST1:		/* send DST1 bit */				ptr--;				if (dst)					sec(DATA1);				else					sec(DATA0);				printf(" ");				break;			case DST2:		/* send DST2 bit */				if (dst)					sec(DATA1);				else					sec(DATA0);				break;			}		}	}}/* * Generate WWV/H 0 or 1 data pulse. */void sec(	int	code		/* DATA0, DATA1, PI */	){	/*	 * The WWV data pulse begins with 5 ms of 1000 Hz follwed by a	 * guard time of 25 ms. The data pulse is 170, 570 or 770 ms at	 * 100 Hz corresponding to 0, 1 or position indicator (PI),	 * respectively. Note the 100-Hz data pulses are transmitted 6	 * dB below the 1000-Hz sync pulses. Originally the data pulses	 * were transmited 10 dB below the sync pulses, but the station	 * engineers increased that to 6 dB because the Heath GC-1000	 * WWV/H radio clock worked much better.	 */	peep(5, tone, HIGH);		/* send seconds tick */	peep(25, tone, OFF);	peep(code - 30, 100, LOW);	/* send data */	peep(1000 - code, 100, OFF);}/* * Generate cycles of 100 Hz or any multiple of 100 Hz. */void peep(	int	pulse,		/* pulse length (ms) */	int	freq,		/* frequency (Hz) */	int	amp		/* amplitude */	){	int	increm;		/* phase increment */	int	i, j;	if (amp == OFF || freq == 0)		increm = 10;	else		increm = freq / 100;	j = 0;	for (i = 0 ; i < pulse * 8; i++) {		switch (amp) {		case HIGH:			buffer[bufcnt++] = ~c6000[j];			break;		case LOW:			buffer[bufcnt++] = ~c3000[j];			break;		default:			buffer[bufcnt++] = ~0;		}		if (bufcnt >= BUFLNG) {			write(fd, buffer, BUFLNG);			bufcnt = 0;		}		j = (j + increm) % 80;	}}/* * Delay for initial phasing */void delay (	int	delay		/* delay in samples */	){	int	samples;	/* samples remaining */	samples = delay;	memset(buffer, 0, BUFLNG);	while (samples >= BUFLNG) {		write(fd, buffer, BUFLNG);		samples -= BUFLNG;	}		write(fd, buffer, samples);}

⌨️ 快捷键说明

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