【Altera SoC体验之旅】高速数据采集之数据显示(2)
作者:chenzhufly QQ:36886052
研究了几天终于可以在web上显示曲线了,需要优化的工作还很多,继续努力吧
1、 硬件环境
硬件平台: Embest SoC --LarkBoard
软件平台:开发板-linux-3.10.31
Quartus 14.0
2、Web服务器
我这次使用的是Lighttpd
Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web server环境。具有非常低的内存开销、cpu占用率低、效能好以及丰富的模块等特点。
Lighttpd是众多OpenSource轻量级的web server中较为优秀的一个。支持FastCGI,CGI,Auth,输出压缩(output compress),URL重写,Alias等重要功能;而Apache之所以流行,很大程度也是因为功能丰富,在lighttpd上很多功能都有相应的实现了,这点对于apache的用户是非常重要的,因为迁移到lighttpd就必须面对这些问题。
debian系统就是这点好,直接安装就可以了
配置文件在/etc/lighttpd/lighttpd.conf,可自行更具需要修改
3、曲线显示控件
我使用的是Highcharts
4、数据的获取
Highchart支持多种方式的数据获取,由于能力有限,就没继续研究了,我使用了比较笨的办法,把数据存在一个文件中,然后从文件中数据读出并展示出来;数据都存入到adcdata.js这个文件,分别是原始数据adcdata、FFT后的数据fftdata,信号频率freq
数据获取的代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <math.h>
-
- #define PI (3.14159f)
- #define TWOPI (6.2831853f)
- #define POINT (1024)
-
- struct complex{
- double real;
- double imag;
- }complex ;
-
- static float gFft_res_ch0[POINT];
- static float gFft_res_ch1[POINT];
- static struct complex gFft_sr_ch0[POINT];
- static struct complex gFft_sr_ch1[POINT];
- static unsigned int gSource_buff[POINT];
- static int gChanel0_buff[POINT];
- static int gChanel1_buff[POINT];
-
- struct complex EE(struct complex c1,struct complex c2)
- {
- struct complex c3;
-
- c3.real = c1.real*c2.real - c1.imag*c2.imag;
- c3.imag = c1.real*c2.imag + c1.imag*c2.real;
-
- return (c3);
- }
-
- void fft(struct complex *xin,int N)
- {
- int f, m, LH, nm, i, k, j, L;
- int le, B, ip;
- double p , ps;
- struct complex w, t;
-
- LH = N/2;
- f = N;
-
- for(m = 1;(f = f/2) != 1;m++);
-
- for(L = m;L >= 1;L--)
- {
- le = pow((double)2,(int)L);
- B = le/2;
- for(j = 0;j <= B - 1;j++)
- {
- p = pow((double)2,(int)(m-L))*j;
- ps = TWOPI/N*p;
- w.real = cos(ps);
- w.imag = -sin(ps);
- for(i = j;i <= N - 1;i = i + le)
- {
- ip = i + B;
- t = xin[i];
- xin[i].real = xin[i].real + xin[ip].real;
- xin[i].imag = xin[i].imag + xin[ip].imag;
- xin[ip].real = xin[ip].real - t.real;
- xin[ip].imag = xin[ip].imag - t.imag;
- xin[ip] = EE(xin[ip],w);
- }
- }
- }
-
- nm = N - 2;
- j = N / 2;
- for(i = 1;i <= nm;i++)
- {
- if(i < j)
- {
- t = xin[j];
- xin[j] = xin[i];
- xin[i] = t;
- }
-
- k = LH;
- while(j >= k)
- {
- j = j - k;
- k = k/2;
- }
- j = j + k;
- }
- }
-
- int main(int argc, char* argv[])
- {
- int i = 0;
- int val;
- size_t write_len = 0;
- FILE * srFp;
- FILE * adcFp;
- double freq_ch0 = 0;
- double freq_ch1 = 0;
-
- int max_ch0 = 0;
- int max_num_ch0 = 0;
- int max_ch1 = 0;
- int max_num_ch1 = 0;
- unsigned int tmp = 0;
-
- int fd;
-
-
- adcFp = fopen("/www/pages/adcdata.js", "wb");
- if(!adcFp){
- perror("adcdata.js open failed\n");
- return 1;
- }
-
- fd = open("/dev/adc/adc", O_RDONLY);
- if (fd == -1) {
- perror("device open error\n");
- return 1;
- }
-
- read(fd,gSource_buff,POINT*4);
- fprintf(adcFp,"%s","var adcdata = [");
-
- for(i = 0;i < POINT; i++ ){
-
- tmp = (gSource_buff[i]&0xffff0000)>>16; //chanel 1
- if( (tmp&0x800) != 0){ //negative
- tmp = (tmp | 0xfffff000);
- }
- gChanel1_buff[i] = (int)tmp;
- fprintf(adcFp,"%d, ",tmp);
-
-
- gFft_sr_ch1[i].real = gChanel1_buff[i];
- }
- fprintf(adcFp,"%s\n"," ];");
-
-
- fft(gFft_sr_ch1,POINT);
-
- fprintf(adcFp,"%s","var fftdata = [");
- for(i = 0;i < POINT/2 ;i++){
-
- gFft_res_ch1[i] = sqrt( gFft_sr_ch1[i].real*gFft_sr_ch1[i].real + gFft_sr_ch1[i].imag*gFft_sr_ch1[i].imag ) * 2 / POINT; /* caculate the amplitude ch1*/
- fprintf(adcFp,"%f, ",gFft_res_ch1[i]);
- if((int)gFft_res_ch1[i] > max_ch1){
- max_ch1 = (int)gFft_res_ch1[i];
- max_num_ch1 = i;
- }
- }
- fprintf(adcFp,"%s\n"," ];");
-
- close(fd);
- fprintf(adcFp,"%s","freq = ");
- freq_ch1 = 105.0 / POINT*max_num_ch1;
- printf("\n%d:chanel1 frequency = %f Mhz, amplitude=%d\n", max_num_ch1, freq_ch1, max_ch1);
- fprintf(adcFp,"%d;",freq_ch1);
-
-
- return 0;
- }
5、网页代码
利用highcharts的例程,自己写了一个index.html,放在/var/www文件夹下面。
- <!DOCTYPE HTML>
- <html>
- <meta http-equiv="refresh" content="5">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Data Acquisition System</title>
-
- <script type="text/javascript" src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>
- <style type="text/css">
- ${demo.css}
- </style>
- <script src="adcdata.js"></script>
- <script type="text/javascript">
-
- $(function () {
- $('#container').highcharts({
- title: {
- text: '原始数据',
- x: -20 //center
- },
- subtitle: {
- text: '作者:chenzhufly 来源:www.eeworld.com.cn',
- x: -20
- },
- tooltip: {
- valueSuffix: ''
- },
- legend: {
- layout: 'vertical',
- align: 'right',
- verticalAlign: 'middle',
- borderWidth: 0
- },
- series: [{
- name: '原始数据',
- data: adcdata
-
- // data : [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
- }
- ]
- });
- });
-
- $(function () {
- $('#container1').highcharts({
- title: {
- text: 'FFT数据分析',
- x: -20 //center
- },
- subtitle: {
- text: '作者:chenzhufly 来源:www.eeworld.com.cn',
- x: -20
- },
- tooltip: {
- valueSuffix: ''
- },
- legend: {
- layout: 'vertical',
- align: 'right',
- verticalAlign: 'middle',
- borderWidth: 0
- },
- series: [ {
- name: 'FFT数据',
- data: fftdata
-
- // data : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
- }
- ]
- });
- });
-
- </script>
-
- </head>
-
- <body>
- <script src="http://cdn.hcharts.cn/highcharts/highcharts.js"°></script>
- <script src="http://cdn.hcharts.cn/highcharts/exporting.js"></script>
-
- <div id="container" style="min-width: 250px; height: 300px; margin: 0 auto"></div>
- <div id="container1" style="min-width: 250px; height: 300px; margin: 0 auto"></div>
-
- <FORM >
- <p>Frequency(Mhz):
- <script>document.write(freq);</script>
- </FORM>
-
- </body>
- </html>
网页5秒钟自动刷新一次,使用如下的代码
6、测试结果
本帖最后由 chenzhufly 于 2015-5-7 15:58 编辑