OSDN Git Service

記録パス設定 記録CH設定 ファイル書込タイミング
[scilog/scilog.git] / scilog.c
1 /*
2  * scilog.c
3  * sciLogger Logger Program
4  * NT System Design
5  * 
6  **** Ver1.0.1
7  * conf_linux_time_set_XXX() add?
8  * thread_disp.cでGPS時刻をlinuxにset
9  * 
10  **** Ver1.0.2
11  * 2015/09/22
12  * lcd.c i2clcd_init() i2cLCD基板のConfig変更 LCD自動初期化ON
13  *
14  **** Ver1.0.3
15  * Kernel 3.18.18 対応
16  * i2cデバイス名変更
17  * spiデバイス名変更 ->/dev/spikead spikeadデバイス側で変更したため
18  * ad_ring.h AdDataのADデータ型をlong->int32_tに変更
19  *      data1sec[]
20  *      data[][]
21  *      avg[][]
22  * thread_rcv.c b3_to_long32() 返り値long->int32_tにした
23  * 
24  * LCDがリセットされて表示が更新されなくなる問題に対応
25  * thread_disp.c 毎分1回 LCD初期化する
26  * i2cLCD基板の自動初期化機能は、2行設定で初期化されるので使用しない
27  * 
28  **** Ver1.0.4
29  * 2017/1
30  * 設定ファイルに記録先パス設定を追加
31  * rec_path= Default /home/data
32  * 
33  * 記録するCH数設定を追加
34  * sec/high両方のファイルで有効
35  * rec_chn= Default 6
36  * CH1から指定した番号のCHまでを記録する
37  * highファイルのヘッダにCH数を入れてないので、デコード時に指定する必要あり
38  * 
39  * sec/highファイルの書込タイミングを制御するようにした
40  * secファイルは毎分00secに書込
41  * highファイルは、00,10,20,30,40,50secに書込
42  * 
43  * ●コンパイル方法
44  * . environment-setup-cortexa8hf-vfp-neon-poky-linux-gnueabi
45  * make
46  * 
47  */
48 #include <stdio.h>
49 #include <string.h>
50 #include <time.h>
51 #include <math.h>
52 #include <sys/types.h>
53 #include <unistd.h>
54 #include <errno.h>
55 #include <stdarg.h>
56 //#include <getopt.h>
57 #include <stdlib.h>
58 #include <assert.h>
59 #include <signal.h>
60 #include <pthread.h>
61 #include <syslog.h>
62
63 #include <stdint.h>
64 #include <sys/ioctl.h>
65 #include <fcntl.h>
66 #include <linux/i2c-dev.h> /* for I2C_SLAVE */
67
68 #include <sys/stat.h>
69
70 #include "my_thread.h"
71 #include "spi.h"
72 #include "ad_ring.h"
73 #include "sts.h"
74 #include "thread_rec.h"
75 #include "conf.h"
76 #include "ad_file.h"
77 #include "lcd.h"
78
79 // debug_print.h内で#define   DEBUG_PRINTしているので
80 // リリース時は、debug_print.hでコメントする
81 #include "debug_print.h"
82
83 #define MES_PRINT
84 #include        "mes_print.h"
85
86 // ダミーデータを自分で発生するときに定義
87 //#define       DUMMY
88
89 #define VERSION "1.0.4"
90
91 #define DEV_SPI "/dev/spikead"
92
93 /**** 設定デフォルト値
94 */
95 #define CONF_FREQ_DEF   50
96 #define CONF_GAIN_DEF   SPI_CMD_GAIN_1P4
97 #define CONF_LINUX_TIME_SET_DEF 1       // ON
98 // データ記録用dir 末尾に/付けない
99 #define CONF_RECPATH_DEF        "/home/data"
100 #define CONF_RECCHN_DEF 6
101
102 //
103 /**** signal ***********************************************************
104 */
105 void cleanup(void)
106 {
107         spi_close();
108 }
109 void sig_handler(int sig)
110 {
111         char    szBuf[128];
112         static int      busy = 0;
113
114         if (busy) return;
115         busy = 1;
116
117         sprintf(szBuf, "signal trap. signal=%d\n", sig);
118         PDEBUG("%s", szBuf);
119         fflush(stdout);
120         fflush(stderr);
121
122         cleanup();
123         exit(0);
124 }
125 //
126 /**** main
127 */
128 int main (int argc, char *argv[])
129 {
130         pthread_t       tid_rcv, tid_disp;
131 //      char    buf[512];
132         
133         signal(SIGINT, sig_handler);
134         signal(SIGQUIT, sig_handler);
135         signal(SIGKILL, sig_handler);
136         signal(SIGTERM, sig_handler);
137
138         lcd_init();
139         lcd_pos(0, 1);
140         lcd_print("sciLogger ");
141         lcd_print(VERSION);
142         lcd_pos(0, 3);
143
144         ad_ring_init();
145         sts_init();
146
147 //printf("%d\n", sizeof(UbloxNavTimeUtcRecType));
148 //printf("%d\n", sizeof(HighSampleRecType));
149 //goto END;
150
151         // デフォルト設定
152         conf_freq_set(CONF_FREQ_DEF);
153         conf_gain_set(CONF_GAIN_DEF);
154         conf_linux_time_set_set(CONF_LINUX_TIME_SET_DEF);
155         conf_recpath_set(CONF_RECPATH_DEF);
156         conf_recchn_set(CONF_RECCHN_DEF);
157         // 設定ファイル読み込み
158         conf_read();
159         PDEBUG("freq=%d\n", conf_freq_get());
160         PDEBUG("gain=%d\n", conf_gain_get());
161         PDEBUG("linux_time_set=%d\n", conf_linux_time_set_get());
162         PDEBUG("rec_path=%s\n", conf_recpath_getp());
163         PDEBUG("rec_chn=%d\n", conf_recchn_get());
164         lcd_print("*");
165
166         PDEBUG("sciLogger %s START\n", VERSION);
167         sleep(1);
168 //
169 /**** デバイスOPEN
170 */
171         if (spi_open(DEV_SPI) < 0) {
172                 perror("spi_open() ERROR!");
173                 goto END;
174         }
175         lcd_print("*");
176
177 //
178 /**** PGA設定
179 */
180         spi_cmd_send_gain(conf_gain_get());
181         lcd_print("*");
182 //
183 /**** スレッド生成
184 */
185         // SPI RCVと平均
186         if (pthread_create(&tid_rcv, NULL, thread_rcv, NULL) != 0) {
187                 perror("pthread_create(SPI RCV)");
188                 goto END;
189         } else {
190                 PDEBUG("SPI RCV thread create\n");
191         }
192         lcd_print("*");
193         // 表示
194         if (pthread_create(&tid_disp, NULL, thread_disp, NULL) != 0) {
195                 perror("pthread_create(DISP)");
196                 goto END;
197         } else {
198                 PDEBUG("DISP thread create\n");
199         }
200         lcd_print("*");
201
202 //
203 /**** メインループ 記録 ************************
204 */
205         // 記録スレッド
206         thread_rec(NULL);
207 END:
208         sig_handler(0);
209
210         return 0;
211 }