예제 #1
0
        public static YCurrentProxy FindCurrent(string name)
        {
            // cases to handle:
            // name =""  no matching unknwn
            // name =""  unknown exists
            // name != "" no  matching unknown
            // name !="" unknown exists
            YCurrent      func = null;
            YCurrentProxy res  = (YCurrentProxy)YFunctionProxy.FindSimilarUnknownFunction("YCurrentProxy");

            if (name == "")
            {
                if (res != null)
                {
                    return(res);
                }
                res = (YCurrentProxy)YFunctionProxy.FindSimilarKnownFunction("YCurrentProxy");
                if (res != null)
                {
                    return(res);
                }
                func = YCurrent.FirstCurrent();
                if (func != null)
                {
                    name = func.get_hardwareId();
                    if (func.get_userData() != null)
                    {
                        return((YCurrentProxy)func.get_userData());
                    }
                }
            }
            else
            {
                func = YCurrent.FindCurrent(name);
                if (func.get_userData() != null)
                {
                    return((YCurrentProxy)func.get_userData());
                }
            }
            if (res == null)
            {
                res = new YCurrentProxy(func, name);
            }
            if (func != null)
            {
                res.linkToHardware(name);
                if (func.isOnline())
                {
                    res.arrival();
                }
            }
            return(res);
        }
예제 #2
0
        public override async Task <int> Run()
        {
            try {
                await YAPI.RegisterHub(HubURL);

                YCurrent sensor;
                YCurrent sensorDC = null;
                YCurrent sensorAC = null;

                if (Target.ToLower() == "any")
                {
                    // retreive any voltage sensor (can be AC or DC)
                    sensor = YCurrent.FirstCurrent();
                    if (sensor == null)
                    {
                        WriteLine("No module connected");
                        return(-1);
                    }
                    Target = await(await sensor.get_module()).get_serialNumber();
                }
                WriteLine("using " + Target);
                // we need to retreive both DC and AC from the device.
                sensorDC = YCurrent.FindCurrent(Target + ".current1");
                sensorAC = YCurrent.FindCurrent(Target + ".current2");

                while (await sensorDC.isOnline())
                {
                    Write("DC: " + await sensorDC.get_currentValue() + " mA / ");
                    WriteLine("AC: " + await sensorAC.get_currentValue() + " mA ");
                    await YAPI.Sleep(1000);
                }

                WriteLine("Module not connected (check identification and USB cable)");
            } catch (YAPI_Exception ex) {
                WriteLine("error: " + ex.Message);
            }

            YAPI.FreeAPI();
            return(0);
        }
예제 #3
0
        private void refreshUI()
        { // draw  the dial.
            double value = -5;
            bool   on    = false;

            if (comboBox1.Enabled)
            { // if a yocto-amp device is connected, lets check it value
                YModule  m  = (YModule)comboBox1.Items[comboBox1.SelectedIndex];
                YCurrent DC = YCurrent.FindCurrent(m.get_serialNumber() + ".current1");
                YCurrent AC = YCurrent.FindCurrent(m.get_serialNumber() + ".current2");
                if (DC.isOnline())
                { // read DC or AC value, according to ACDCcheckBox
                    if (ACDCcheckBox.Checked)
                    {
                        value = 100 * (AC.get_currentValue() / maxvalue);
                    }
                    else
                    {
                        value = 100 * (DC.get_currentValue() / maxvalue);
                    }
                    on = true;
                }
            }

            // lets use a double buffering technique to avoid flickering
            Bitmap   BackBuffer = new Bitmap(on ? Properties.Resources.bg : Properties.Resources.bgoff);
            Graphics buffer     = Graphics.FromImage(BackBuffer);

            buffer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            int DialWidth  = BackBuffer.Width;
            int DialHeight = BackBuffer.Height;

            // add inertia to the needle
            needleposition = needleposition + (value - needleposition) / 10;

            // make sure une needle won't go off chart
            if (needleposition < -5)
            {
                needleposition = -5;
            }
            if (needleposition > 105)
            {
                needleposition = 105;
            }

            double angle = 3.1416 * (180 - 30 - 120 * (needleposition / 100)) / 180;
            int    x     = Convert.ToInt32(DialWidth / 2 + Math.Cos(angle) * (DialHeight - 15));
            int    y     = Convert.ToInt32(DialHeight * 1.066 - Math.Sin(angle) * (DialHeight - 15));

            // draw the needle shadow
            Pen   shadow = new Pen(Color.FromArgb(16, 0, 0, 00), 3);
            Point point1 = new Point(DialWidth / 2 - 3, DialHeight + 3);
            Point point2 = new Point(Convert.ToInt32(x) - 3, Convert.ToInt32(y) + 3);

            buffer.DrawLine(shadow, point1, point2);

            // draw the needle
            Pen red = new Pen(on ? Color.FromArgb(255, 255, 0, 00) : Color.FromArgb(255, 64, 0, 00), 3);

            point1 = new Point(DialWidth / 2, DialHeight);
            point2 = new Point(Convert.ToInt32(x), Convert.ToInt32(y));
            buffer.DrawLine(red, point1, point2);

            // draw the scale
            FontFamily fontFamily = new FontFamily("Arial");
            Font       font       = new Font(fontFamily, DialHeight / 10, FontStyle.Regular, GraphicsUnit.Pixel);

            buffer.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 20, 20, 20));

            for (int i = 0; i <= 10; i++)
            {
                double dvalue = ((maxvalue / 1000) * i / 10);
                angle = 3.1416 * (180 - 30 - 120 * (i / 10.0)) / 180;
                string text = dvalue.ToString();
                SizeF  size = buffer.MeasureString(text, font);
                int    tx   = Convert.ToInt32(DialWidth / 2 + Math.Cos(angle) * DialHeight * 1.01 - size.Width / 2);
                int    ty   = Convert.ToInt32(DialHeight * 1.066 - Math.Sin(angle) * DialHeight * 0.98 - size.Height / 2);
                buffer.DrawString(dvalue.ToString(), font, solidBrush, new PointF(tx, ty));
            }

            Bitmap frame = new Bitmap(Properties.Resources.frame);

            buffer.DrawImage(frame, 0, 0);

            Graphics Viewable = pictureBox1.CreateGraphics();

            // fast rendering
            //Viewable.DrawImageUnscaled(BackBuffer, 0, 0);

            // slower, but pictureBox can be resized, rendering will still be ok,
            // try to respect a 2:1 ratio anyway
            Viewable.DrawImage(BackBuffer, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));

            Viewable.Dispose();
        }