diff --git a/ISCamRecorder/CvCamera.cs b/ISCamRecorder/CvCamera.cs index 14fa066..516e961 100644 --- a/ISCamRecorder/CvCamera.cs +++ b/ISCamRecorder/CvCamera.cs @@ -104,15 +104,15 @@ /// /// /// - public void SaveToFile(string outDir, string imageType, string recTime) { + public void SaveToFile(string imageType, string recTime) { if (_RecFrames.Count < 1) return; var CamID = "CvCam"; Debug.WriteLine($"{CamID} starts saving with 0.0fps."); // 保存先確保 - var outDir2 = Path.Combine(outDir, CamID); + var outDir2 = Path.Combine(_MainForm.OutputDir, CamID); Directory.CreateDirectory(outDir2); // 動画保存準備 - var movieFile = Path.Combine(outDir, $"{CamID}_{recTime}.mp4"); + var movieFile = Path.Combine(_MainForm.OutputDir, $"{CamID}_{recTime}.mp4"); var writer = new VideoWriter(movieFile, FourCC.H264, _RecFrameRate, _RecFrames[0].FrameImage.Size()); for (int i = 0; i < _RecFrames.Count; i++) { @@ -145,22 +145,6 @@ } /// - /// 撮影画像の取得 - /// - /// - public Bitmap GetImage() { - return _Buffer[1 - _WriteBufferID]; - } - - /// - /// 停止 - /// - //public void Stop() { - // _ExitSignal = true; - // Debug.WriteLine("cvcamera stop"); - //} - - /// /// カメラ情報文字列 /// /// diff --git a/ISCamRecorder/ISCamRecorder.csproj b/ISCamRecorder/ISCamRecorder.csproj index 2ce2726..965f723 100644 --- a/ISCamRecorder/ISCamRecorder.csproj +++ b/ISCamRecorder/ISCamRecorder.csproj @@ -159,7 +159,9 @@ - copy $(ProjectDir)*.iccf $(TargetDir) + copy $(ProjectDir)*.iccf $(TargetDir) +copy $(ProjectDir)media\*.wav $(TargetDir) + diff --git a/ISCamRecorder/MainForm.Designer.cs b/ISCamRecorder/MainForm.Designer.cs index b745b99..4deb50d 100644 --- a/ISCamRecorder/MainForm.Designer.cs +++ b/ISCamRecorder/MainForm.Designer.cs @@ -477,7 +477,7 @@ this.BtnSnapImage.Name = "BtnSnapImage"; this.BtnSnapImage.Size = new System.Drawing.Size(74, 27); this.BtnSnapImage.TabIndex = 18; - this.BtnSnapImage.Text = "静止画撮影"; + this.BtnSnapImage.Text = "終了"; this.BtnSnapImage.UseVisualStyleBackColor = true; this.BtnSnapImage.Click += new System.EventHandler(this.BtnSnapImage_Click); // @@ -574,7 +574,6 @@ this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); this.Load += new System.EventHandler(this.MainForm_Load); this.SizeChanged += new System.EventHandler(this.MainForm_SizeChanged); - this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint); this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); diff --git a/ISCamRecorder/MainForm.cs b/ISCamRecorder/MainForm.cs index c839d29..3001149 100644 --- a/ISCamRecorder/MainForm.cs +++ b/ISCamRecorder/MainForm.cs @@ -24,6 +24,7 @@ Init, // 初期化中 Idle, // 待機中 Recoding, // 録画中 + Saving, // 保存中 Exit // 終了 } @@ -49,11 +50,16 @@ Task _RecodingThread; // 録画スレッド float _TriggerFrameRate = 30.0F; // トリガーフレームレート private ulong _availablePhysicalMemory; //合計物理メモリ - DateTime _LastUpdate; // 前回のUI更新時間 + //DateTime _LastUpdate; // 前回のUI更新時間 + public Chart SensorChart { get { return chart1; } } // 外部からのアクセス用 public PictureBox CvCameraPic { get { return PicCvCamera; } } // 外部からのアクセス用 - public STATE State { get; private set; } = STATE.Init; // ソフトウェアの状態 + public DateTime RecodingTime { get; private set; } = DateTime.Now; // 録画開始時間 + public string RecodingTimeStr { get { return RecodingTime.ToString("yyyyMMdd_HHmmss"); } } + public string OutputDir { get { return Path.Combine(TxtOutputDir.Text, $"rec{RecodingTimeStr}"); } } + + /// /// コンストラクタ @@ -61,7 +67,7 @@ public MainForm() { MediaFoundationApi.Startup(); InitializeComponent(); - _Sensor = new SensorData(this); + _Sensor = new SensorData(this, PLOT_LENGTH); _CvCamera = new CvCamera(this); } @@ -85,7 +91,7 @@ _TriggerThread = Task.Run(TriggerThread); _SerialThread = Task.Run(SerialThread); _CvCameraThread = Task.Run(CvCameraThread); - _LastUpdate = DateTime.Now; + //_LastUpdate = DateTime.Now; State = STATE.Idle; } @@ -119,10 +125,13 @@ /// シリアル通信スレッド /// private void SerialThread() { - if (!_Sensor.Connect(PLOT_LENGTH)) return; + if (!_Sensor.Connect()) return; _Sensor.Loop(); } + /// + /// 録画終了 + /// void EndRecoding() { if (_RecodingThread != null && _RecodingThread.IsCompleted) { _RecodingThread.Dispose(); @@ -135,8 +144,6 @@ /// /// static void UITimerCB(object obj) { - //Debug.WriteLine("TUI:" + DateTime.Now.ToString("ss.ff")); - //((MainForm)obj).Invalidate(); if (((MainForm)obj).State == STATE.Exit) return; ((MainForm)obj).UpdateForm(); ((MainForm)obj).EndRecoding(); @@ -150,10 +157,11 @@ this.Invoke((MethodInvoker)delegate { UpdateForm(); }); return; } - var now = DateTime.Now; - var elapsed = now - _LastUpdate; - if (elapsed.TotalMilliseconds > UI_UPDATE_INTERVAL) { - _LastUpdate = now; + //var now = DateTime.Now; + //var elapsed = now - _LastUpdate; + //if (elapsed.TotalMilliseconds > UI_UPDATE_INTERVAL) { + // _LastUpdate = now; + if (State != STATE.Exit) { TxtTop.Text = $"上方カメラ {_Cameras[0].CameraInfo()}"; TxtLeft.Text = $"左方カメラ {_Cameras[1].CameraInfo()}"; TxtFront.Text = $"前方カメラ {_Cameras[2].CameraInfo()}"; @@ -164,8 +172,9 @@ MemoryToUse(); _TriggerFrameRate = float.Parse(TxtTriggerFPS.Text); } + //} //PicCvCamera.Image = _CvCamera.GetImage(); // 追加カメラ表示 - if (ChkStartSW.Checked && _Sensor.IsStartButtonPressed) StartRecoding(); + //if (ChkStartSW.Checked && _Sensor.IsStartButtonPressed) StartRecoding(); } /// @@ -197,15 +206,7 @@ /// /// private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { - //_Sensor.Stop(); - //_SerialThread.Dispose(); - - //_UITimer.Dispose(); - - //_CvCamera.Stop(); - //_ExitSignal = true; - //_TriggerThread.Wait(); - //_CvCameraThread.Wait(); + //e.Cancel = true; } /// @@ -281,6 +282,9 @@ //_Cameras.ForEach(c => c.StopRecoding()); Debug.WriteLine("recoding"); } else { + var player = new SoundPlayer(@"start.wav"); + player.Play(); + _RecodingThread = Task.Run(RecodingThread); } } @@ -312,12 +316,15 @@ var frameRate = _Cameras.Select(c => c.FrameRate).Average(); var framesToCapture = (int)(float.Parse(recodingDulationTxt) * frameRate + 1.0F); var movieRate = int.Parse(movieRateTxt); - var recTime = DateTime.Now.ToString("yyyyMMdd_HHmmss"); - var outputDir = Path.Combine(TxtOutputDir.Text, $"rec{recTime}"); - Directory.CreateDirectory(outputDir); + //var recTime = DateTime.Now.ToString("yyyyMMdd_HHmmss"); + //var outputDir = Path.Combine(TxtOutputDir.Text, $"rec{recTime}"); var imageType = imageTypeTxt; - var sensorFile = Path.Combine(outputDir, $"Sensor_{recTime}.csv"); - _Sensor.StartRecoding(sensorFile); + //var sensorFile = Path.Combine(outputDir, $"Sensor_{recTime}.csv"); + + RecodingTime = DateTime.Now; + Directory.CreateDirectory(OutputDir); + State = STATE.Recoding; + //_Sensor.StartRecoding(sensorFile); _CvCamera.StartRecoding(); // 録画 Task[] tasks = new Task[4]; @@ -326,7 +333,9 @@ tasks[i] = Task.Run(() => cam.RecordToMemory(framesToCapture)); } Task.WaitAll(tasks); - _Sensor.StopRecoding(); + + State = STATE.Saving; + //_Sensor.StopRecoding(); _CvCamera.StopRecoding(); // 保存 this.Invoke((MethodInvoker)delegate { @@ -337,10 +346,10 @@ { var cam = _Cameras[i]; tasks[i] = Task.Run(() => cam.SaveToFile( - outputDir, frameRate, movieRate, imageType, recTime)); + OutputDir, frameRate, movieRate, imageType, RecodingTimeStr)); } Task.WaitAll(tasks); - _CvCamera.SaveToFile(outputDir, imageType, recTime); + _CvCamera.SaveToFile(imageType, RecodingTimeStr); // プレビューモードへ変更 this.Invoke((MethodInvoker)delegate { @@ -353,15 +362,6 @@ } /// - /// フォーム再描画 - /// - /// - /// - private void MainForm_Paint(object sender, PaintEventArgs e) { - UpdateForm(); - } - - /// /// 静止画撮影ボタン /// /// @@ -378,8 +378,6 @@ _TriggerThread.Wait(timeout); Debug.WriteLine("_TriggerThread stop"); - //var player = new SoundPlayer(@"D:\prog\ISCamRecorder\ISCamRecorder\media\start.wav"); - //player.Play(); this.Close(); } } diff --git a/ISCamRecorder/SensorData.cs b/ISCamRecorder/SensorData.cs index ab71d74..16d65fd 100644 --- a/ISCamRecorder/SensorData.cs +++ b/ISCamRecorder/SensorData.cs @@ -18,38 +18,33 @@ readonly string SERIAL_PORT_NAME = "USB シリアル"; - // センサー値 - //public Queue Values { get; private set; } = new Queue(); - // サンプル時間 - //public Queue Times { get; private set; } = new Queue(); - public float FrameRate { get { return _Fps.FrameRate; } } - public bool IsStartButtonPressed { get { return _StartButtonState; } } - SerialPort _Serial = null; // シリアル通信オブジェクト Queue Values = new Queue(); // センサー値 Queue Times = new Queue(); // サンプル時間 - int _QueueLength = 1; // グラフ表示するサンプル数 - bool _RecodingStopSignal = false; // 記録終了シグナル + int _QueueLength; // グラフ表示するサンプル数 + //bool _RecodingStopSignal = false; // 記録終了シグナル StreamWriter _CsvWriter = null; // ファイル保存オブジェクト - DateTime _RecordBeginTime; // 記録開始時間 + //DateTime _RecordBeginTime; // 記録開始時間 FrameRateCounter _Fps = new FrameRateCounter(10); - bool _StartButtonState = false; // スタートボタンの状態 + bool _LastButtonState = false; // スタートボタンの状態 MainForm _MainForm = null; // メインフォームインスタンス + public float FrameRate { get { return _Fps.FrameRate; } } // FPS値 + /// /// コンストラクタ /// /// - public SensorData(MainForm mf) { + public SensorData(MainForm mf, int queueLength) { _MainForm = mf; + _QueueLength = queueLength; } /// /// 接続 /// /// - public bool Connect(int queueLength) { - _QueueLength = queueLength; + public bool Connect() { // シリアルポート接続 var portname = GetSerialPort(); @@ -91,21 +86,35 @@ var values = str.Split(','); if (values.Length < 5) continue; - // 受信データ解析 - _StartButtonState = values[4].Equals("0"); + // ボタン状態 + bool buttonState = values[4].StartsWith("0"); + if (_LastButtonState == false && buttonState == true) { + _MainForm.StartRecoding(); + } + _LastButtonState = buttonState; + + // センサー値 var photoSensor = int.Parse(values[1]); var dt = DateTime.Now; - var elapsed = (dt - _RecordBeginTime).TotalMilliseconds; + var elapsed = (dt - _MainForm.RecodingTime).TotalMilliseconds; // CSV保存 - if (_CsvWriter != null) { - if (_RecodingStopSignal) { + if (_MainForm.State == STATE.Recoding) { + if (_CsvWriter == null) { + var sensorFile = Path.Combine(_MainForm.OutputDir, $"Sensor_{_MainForm.RecodingTimeStr}.csv"); + _CsvWriter = new StreamWriter(sensorFile); + if (_CsvWriter != null) { + _CsvWriter.WriteLine($"Date,Time,elapsed,sensor,switch"); + } + } + if (_CsvWriter != null) { + _CsvWriter.WriteLine( + $"{dt.ToString("yyyy/MM/dd,HH:mm:ss.fff")},{elapsed:0.00},{photoSensor},{buttonState}"); + } + } else { + if (_CsvWriter != null) { _CsvWriter.Close(); _CsvWriter = null; - _RecodingStopSignal = false; - } else { - _CsvWriter.WriteLine( - $"{dt.ToString("yyyy/MM/dd,HH:mm:ss.fff")},{elapsed:0.00},{photoSensor}"); } } @@ -147,23 +156,20 @@ return portname; } - /// - /// 記録開始 - /// - /// - public void StartRecoding(string filename) { - _CsvWriter = new StreamWriter(filename); - if (_CsvWriter == null) return; - _CsvWriter.WriteLine($"Date,Time,elapsed,sensor,switch"); - _RecordBeginTime = DateTime.Now; - _RecodingStopSignal = false; - } + ///// + ///// 記録開始 + ///// + ///// + //private void StartRecoding() { + // _RecordBeginTime = DateTime.Now; + // _RecodingStopSignal = false; + //} - /// - /// 記録終了 - /// - public void StopRecoding() { - _RecodingStopSignal = true; - } + ///// + ///// 記録終了 + ///// + //public void StopRecoding() { + // _RecodingStopSignal = true; + //} } }