OSDN Git Service

e7e6bda4485b344dc02bdb2e15a9b9b8aaf2396f
[scilog/scilog.git] / scilog.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4 #include <math.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <stdarg.h>
9 //#include <getopt.h>
10 #include <stdlib.h>
11 #include <assert.h>
12 #include <signal.h>
13 #include <pthread.h>
14 #include <syslog.h>
15
16 #include <stdint.h>
17 #include <sys/ioctl.h>
18 #include <fcntl.h>
19 #include <linux/i2c-dev.h> /* for I2C_SLAVE */
20
21 #include <sys/stat.h>
22
23 #include "my_thread.h"
24 #include "spi.h"
25 #include "ad_ring.h"
26 #include "sts.h"
27 #include "thread_rec.h"
28 #include "conf.h"
29 #include "ad_file.h"
30 #include "lcd.h"
31
32 // debug_print.h内で#define   DEBUG_PRINTしているので
33 // リリース時は、debug_print.hでコメントする
34 #include "debug_print.h"
35
36 #define MES_PRINT
37 #include        "mes_print.h"
38
39 // ダミーデータを自分で発生するときに定義
40 //#define       DUMMY
41
42 #define VERSION "1.0.1"
43
44 #define DEV_SPI "/dev/spike-ad"
45
46 /**** 設定デフォルト値
47 */
48 #define CONF_FREQ_DEF   50
49 #define CONF_GAIN_DEF   SPI_CMD_GAIN_1P4
50 #define CONF_LINUX_TIME_SET_DEF 1       // OFF
51
52 //
53 /**** signal ***********************************************************
54 */
55 void cleanup(void)
56 {
57         spi_close();
58 }
59 void sig_handler(int sig)
60 {
61         char    szBuf[128];
62         static int      busy = 0;
63
64         if (busy) return;
65         busy = 1;
66
67         sprintf(szBuf, "signal trap. signal=%d\n", sig);
68         PDEBUG("%s", szBuf);
69         fflush(stdout);
70         fflush(stderr);
71
72         cleanup();
73         exit(0);
74 }
75 //
76 /**** main
77 */
78 int main (int argc, char *argv[])
79 {
80         pthread_t       tid_rcv, tid_disp;
81 //      char    buf[512];
82         
83         signal(SIGINT, sig_handler);
84         signal(SIGQUIT, sig_handler);
85         signal(SIGKILL, sig_handler);
86         signal(SIGTERM, sig_handler);
87
88         lcd_init();
89         lcd_pos(6, 1);
90         lcd_print("sciLogger");
91         lcd_pos(0, 3);
92
93         ad_ring_init();
94         sts_init();
95
96 //printf("%d\n", sizeof(UbloxNavTimeUtcRecType));
97 //printf("%d\n", sizeof(HighSampleRecType));
98 //goto END;
99
100         // デフォルト設定
101         conf_freq_set(CONF_FREQ_DEF);
102         conf_gain_set(CONF_GAIN_DEF);
103         conf_linux_time_set_set(CONF_LINUX_TIME_SET_DEF);
104         // 設定ファイル読み込み
105         conf_read();
106         PDEBUG("freq=%d\n", conf_freq_get());
107         PDEBUG("gain=%d\n", conf_gain_get());
108         lcd_print("*");
109
110         PDEBUG("sciLogger %s START\n", VERSION);
111         sleep(1);
112 //
113 /**** デバイスOPEN
114 */
115         if (spi_open(DEV_SPI) < 0) {
116                 perror("spi_open() ERROR!");
117                 goto END;
118         }
119         lcd_print("*");
120
121 //
122 /**** PGA設定
123 */
124         spi_cmd_send_gain(conf_gain_get());
125         lcd_print("*");
126 //
127 /**** スレッド生成
128 */
129         // SPI RCVと平均
130         if (pthread_create(&tid_rcv, NULL, thread_rcv, NULL) != 0) {
131                 perror("pthread_create(SPI RCV)");
132                 goto END;
133         } else {
134                 PDEBUG("SPI RCV thread create\n");
135         }
136         lcd_print("*");
137         // 表示
138         if (pthread_create(&tid_disp, NULL, thread_disp, NULL) != 0) {
139                 perror("pthread_create(DISP)");
140                 goto END;
141         } else {
142                 PDEBUG("DISP thread create\n");
143         }
144         lcd_print("*");
145
146 //
147 /**** メインループ 記録 ************************
148 */
149         // 記録スレッド
150         thread_rec(NULL);
151 END:
152         sig_handler(0);
153
154         return 0;
155 }