Example #1
0
 private void SocketConn_Click(object sender, RoutedEventArgs e)
 {
     clrLog(UpdateType.Information);
     // Make sure hostname and port are given.
     if (txtHostName.Equals("Host") || txtHostName.Equals("") || txtPort.Equals("Port") || txtPort.Equals(""))
     {
         // Release message and return.
         Log("Invalid hostname or port", UpdateType.MessageBox);
     }
     else
     {
         if (app_comsocket == null)
         {
             Log("Attempting connection.", UpdateType.Information);
             app_comsocket = new ComSocket();
             string result = app_comsocket.Connect(txtHostName.Text, int.Parse(txtPort.Text));
             Log("Connection status: " + result, UpdateType.Information);
             if (result.Equals("Success"))
             {
                 // Good.
                 Log("Connection Established.", UpdateType.Information);
                 app_comsocket.Send("STRT\n");
                 app_comsocket.Send("ACKR\n");
                 // Change button content
                 SocketConn.Content = "Disconnect";
             }
             else
             {
                 Log("Connection Failure", UpdateType.Information);
                 app_comsocket = null;
             }
         }
         else if (app_comsocket != null)
         {
             app_comsocket.Send("NCKR"); // End of transmission
             Log("Connection closed", UpdateType.Information);
             app_comsocket.Close();      // Close the connection
             app_comsocket      = null;
             SocketConn.Content = "Connect";
         }
     }
 }
Example #2
0
        // The accelerometer timer not only displays the acceleration data periodically, but also
        // logs the acceleration values when the shutter is open.
        void accelerometer_timer(object sender, EventArgs e)
        {
            // Set the viewfinderBrush source
            if (app_camera.cam_open_busy == false && app_camera.source_set == false)
            {
                viewfinderBrush.SetSource(app_camera._camera);
                app_camera.source_set = true;
            }
            // If we are doing a focus sweep, it needs to be done once every 10 ms, not faster
            if (man_focus == true)
            {
                // A 100 is a complete sweep done.
                if (focus_counter >= 99)
                {
                    man_focus = false;
                    sweep_button.IsEnabled = true;
                    if (app_comsocket != null)
                    {
                        app_comsocket.Send("EDFS\n");
                    }
                }
                if (focus_counter == 0)
                {
                    if (app_comsocket != null)
                    {
                        app_comsocket.Send("STFS\n");
                    }
                }
                // Keep getting a new image with changed focus value
                byte[] preview_im = new byte[app_camera.imheight * app_camera.imwidth];
                // increment the counter
                focus_counter += 1;
                Log(focus_counter.ToString(), UpdateType.DebugSection);
                // Focus the camera
                app_camera.set_focus(focus_counter);
                // Get the Y buffer
                app_camera._camera.GetPreviewBufferY(preview_im);
                // Send the image over tcp
                if (app_comsocket != null)
                {
                    app_comsocket.Send("STFR" + focus_counter.ToString() + "\n");
                    app_comsocket.Send(preview_im);
                    app_comsocket.Send("EDFR" + focus_counter.ToString() + "\n");
                }
            }
            // Focus the camera only if it is not in manual mode
            if (app_camera.focus_busy == false && app_camera.cam_busy == false && man_focus == false)
            {
                app_camera.set_focus(focus_slider.Value, int.Parse(txtExpTime.Text));
                Log(focus_slider.Value.ToString(), UpdateType.DebugSection);
            }
            Vector3 accel = accelerometer.getvalue();

            // If logging is enabled, send the data to the com socket
            if (accel_log == true)
            {
                if (app_comsocket != null)
                {
                    if (imlog == true)
                    {
                        app_comsocket.Send("STFR" + im_counter.ToString() + "\n");
                        app_comsocket.Send("STAC" + im_counter.ToString() + "\n");
                        app_comsocket.Send(accel.X.ToString() + ";" + accel.Y.ToString() + ";" + accel.Z.ToString() + ";;");
                        app_comsocket.Send("EDAC" + im_counter.ToString() + "\n");
                        // Log image frames also.
                        byte[] byte_preview = new byte[app_camera.imheight * app_camera.imwidth];
                        app_camera._camera.GetPreviewBufferY(byte_preview);
                        app_comsocket.Send("STIM" + im_counter.ToString() + "\n");
                        app_comsocket.Send(byte_preview);
                        app_comsocket.Send("EDIM" + im_counter.ToString() + "\n");
                        app_comsocket.Send("EDFR" + im_counter.ToString() + "\n");
                        im_counter += 1;
                    }
                    else
                    {
                        app_comsocket.Send(accel.X.ToString() + ";" + accel.Y.ToString() + ";" + accel.Z.ToString() + ";;");
                    }
                }
            }
            if (app_camera.cam_busy == true)
            {
                focus_slider.IsEnabled = false;
                accelX.Add(accel.X);
                accelY.Add(accel.Y);
                accelZ.Add(accel.Z);

                gX.Add(gx);
                gY.Add(gy);
                gZ.Add(gz);
            }
            if ((app_camera.cam_busy == false) && (app_camera.transmit == true))
            {
                Log("Camera capture complete", UpdateType.DebugSection);
                if (app_comsocket != null)
                {
                    // Send gravity vector data
                    app_comsocket.Send("STGR\n");
                    app_comsocket.Send(gx.ToString() + ";" + gy.ToString() + ";" + gz.ToString() + "\n");
                    app_comsocket.Send("EDGR\n");
                    // Send acceleration data
                    app_comsocket.Send("STAC\n");
                    string accel_string = "";
                    for (int i = 0; i < accelX.Count; i++)
                    {
                        accel_string += accelX[i].ToString() + ";" + accelY[i].ToString() + ";" + accelZ[i].ToString() + ";"
                                        + gX[i].ToString() + ";" + gY[i].ToString() + ";" + gZ[i].ToString() + ";;";
                    }
                    accel_string += "\n";
                    app_comsocket.Send(accel_string);
                    app_comsocket.Send("EDAC\n");

                    // Send image data length
                    byte[] imarray = app_camera.imstream.ToArray();
                    app_comsocket.Send("STIL\n");
                    app_comsocket.Send(imarray.Length.ToString() + '\n');
                    app_comsocket.Send("EDIL\n");
                    // Send focus details
                    app_comsocket.Send("STFC\n");
                    app_comsocket.Send(focus_slider.Value.ToString() + "\n");
                    app_comsocket.Send("EDFC\n");
                    // Send preview image data
                    if (get_preview_image == true)
                    {
                        app_comsocket.Send("STIP\n");
                        byte[] byte_preview = new byte[app_camera.preview_image.Length * sizeof(int)];
                        System.Buffer.BlockCopy(app_camera.preview_image, 0, byte_preview, 0, byte_preview.Length);
                        app_comsocket.Send(byte_preview);
                        app_comsocket.Send("\nEDIP\n");
                    }
                    // Send image data
                    app_comsocket.Send("STIM\n");
                    app_comsocket.Send(imarray);
                    app_comsocket.Send("\n");
                    Log("Image size is " + app_camera.imheight.ToString() + ";" + app_camera.imwidth.ToString(), UpdateType.DebugSection);

                    app_comsocket.Send("\n");

                    app_comsocket.Send("EDIM\n");
                    // Done.
                    app_comsocket.Send("ENDT\n");
                    app_comsocket.Close();
                    app_comsocket      = null;
                    SocketConn.Content = "Connect";
                }
                Log("Total readings: " + accelX.Count.ToString(), UpdateType.Information);
                app_camera.transmit    = false;
                focus_slider.IsEnabled = true;
            }
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                // Estimate gravity vector from static data
                gx            = alpha * gx + (1 - alpha) * accel.X;
                gy            = alpha * gy + (1 - alpha) * accel.Y;
                gz            = alpha * gz + (1 - alpha) * accel.Z;
                txtAccel.Text = string.Format("x:{0}\n,y:{1}\n,z:{2}", accel.X.ToString("0.00"), accel.Y.ToString("0.00"), accel.Z.ToString("0.00"));
            });
        }