#include #include #include #include #include #include #include #include #include #include #include "cusb.h" #define RGIO_CPIPE 0 //コマンド用パイプ(1OUT)への番号 #define RGIO_RPIPE 7 //リードデータパイプ(1IN)への番号 #define PIPE_AD7739 4 //AD7739のデータダンプ用パイプ #define PIPE_GPS 5 //GPSのデータダンプ用パイプ #define SFR_WRITE 0x00 #define SFR_READ 0x01 #define EZR_WRITE 0x02 #define EZR_READ 0x03 #ifdef DEBUG_FW #define FW_VERSION "XXXX" #define FW_FILE "usb_adc.bix" #else #define FW_VERSION "V100" #endif #define FW_SIZE CUSB_DWLSIZE #ifndef DEBUG_FW #ifdef __cplusplus extern "C"{ #endif extern u8 fw_bin[FW_SIZE]; #ifdef __cplusplus } #endif #endif using namespace std; namespace DAQ{ static unsigned target_device(0); void init(){ #ifdef DEBUG_FW //デバッグ時は直接FWのバイナリを読み込む cout << "Debug mode!!" << endl; cout << "Firmware is " << FW_FILE << "." << endl; cout << "Target device is " << target_device << "." << endl; u8 fw_bin[FW_SIZE]; FILE *fp; if(!(fp = fopen(FW_FILE, "rb"))){ cout << boost::format("can't find %s!!") % FW_FILE << endl; exit(-1); } fread(fw_bin,1,FW_SIZE,fp); fclose(fp); #endif //カメレオンUSBライブラリの初期化 HANDLE dev_handle; if(cusb_init(target_device, &dev_handle, fw_bin, (s8 *)"RGIO", (s8 *)FW_VERSION)){ cout << "Can't find Chameleon USB." << endl; exit(-1); } usb_close(&dev_handle); } typedef boost::mutex::scoped_lock lock; boost::condition waiting_until_log_queue_valid; queue log_queue; boost::mutex mutex_for_log_queue; volatile bool m_end_flag = false; void handle_log(){ boost::mutex monitor; while(!m_end_flag){ while(!log_queue.empty()){ string *log = log_queue.front(); { lock lk(mutex_for_log_queue); log_queue.pop(); } cout << *log << endl; delete(log); } lock lk(monitor); waiting_until_log_queue_valid.wait(lk); } } void handle_adc(){ HANDLE dev_handle; u8 buf[64]; //ドライバを開く if(usb_open(target_device, &dev_handle)){ cout << "Could not open driver from ADC thread!!" << endl; return; } while(!m_end_flag){ int index = 0; string *log = new string("A "); usb_bulk_read(&dev_handle, PIPE_AD7739, buf, sizeof(buf)); long global_ms = 0; (global_ms |= buf[index++]); global_ms <<= 8; (global_ms |= buf[index++]); global_ms <<= 8; (global_ms |= buf[index++]); global_ms <<= 8; (global_ms |= buf[index++]); log->append(boost::io::str(boost::format("%10d ") % global_ms)); for(int ch = 0; ch < 8; ch++){ long value = 0; (value |= buf[index++]); value <<= 8; (value |= buf[index++]); value <<= 8; (value |= buf[index++]); log->append(boost::io::str(boost::format("%8d ") % value)); } lock lk(mutex_for_log_queue); log_queue.push(log); waiting_until_log_queue_valid.notify_one(); } usb_close(&dev_handle); } void handle_gps(){ HANDLE dev_handle; u8 buf[64]; //ドライバを開く if(usb_open(target_device, &dev_handle)){ cout << "Could not open driver from GPS thread!!" << endl; return; } string *log(NULL), *append_target(NULL); int temp_index = 0; while(!m_end_flag){ usb_bulk_read(&dev_handle, PIPE_GPS, buf, sizeof(buf)); for(int i = 0; i < sizeof(buf); i++){ //cout << boost::format("%02X %d") % (int)buf[i] % index << endl; //cout << "LOG:" << (log ? *log : "(null)") << endl; //cout << "APPEND:" << (append_target ? *append_target : "(null)") << endl; while(true){ if(1 == temp_index){ if(0x62 == buf[i]){ temp_index++; break; } }else if(2 == temp_index){ if(0x01 == buf[i] || 0x02 == buf[i]){ temp_index++; if(log){ string *log2 = new string("G "); log2->append(*log); delete(log); lock lk(mutex_for_log_queue); log_queue.push(log2); waiting_until_log_queue_valid.notify_one(); } log = append_target; temp_index = 0; break; } } if(0 < temp_index){ if(log && append_target){log->append(*append_target);} delete(append_target); append_target = log; temp_index = 0; } if(0xB5 == buf[i]){ temp_index = 1; append_target = new string(""); } break; } if(append_target){ //cout << boost::format("%02X %d") % (int)buf[i] % temp_index << endl; append_target->append(boost::io::str(boost::format("%02X ") % (int)buf[i])); } } } usb_close(&dev_handle); } void start(){ new boost::thread(DAQ::handle_adc); new boost::thread(DAQ::handle_gps); new boost::thread(DAQ::handle_log); } void stop(){ m_end_flag = true; cout << "Stop signal is sent!!" << endl; } }; int main(int argc, char *argv[]){ long start_time = time(NULL); long end_time = start_time + 8 * 60 * 60; // 8時間後 cout << "start from " << start_time << endl; cout << "end to " << end_time << endl; try{ if(argc > 1){ string arg1(argv[1]); using boost::lexical_cast; DAQ::target_device = lexical_cast(arg1); } }catch(exception &e){} DAQ::init(); DAQ::start(); while(end_time > time(NULL)){Sleep(10);} DAQ::stop(); }