Esempio n. 1
0
        public void t6(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            PigpiodIf.Callback callback = null;
            try
            {
                int tp, t, p;

                Console.WriteLine("\r\nTrigger tests.");

                pigpiodIf.gpio_write(GPIO, PigpiodIf.PI_LOW);

                tp = 0;

                int    t6_count   = 0;
                int    t6_on      = 0;
                UInt32 t6_on_tick = 0;
                callback = pigpiodIf.callback(GPIO, PigpiodIf.EITHER_EDGE, (gpio, level, tick, user) =>
                {
                    if (level == 1)
                    {
                        t6_on_tick = tick;
                        t6_count++;
                    }
                    else
                    {
                        if (t6_on_tick != 0)
                        {
                            t6_on += (int)(tick - t6_on_tick);
                        }
                    }
                });

                pigpiodIf.time_sleep(0.2);

                for (t = 0; t < 5; t++)
                {
                    pigpiodIf.time_sleep(0.1);
                    p   = 10 + (t * 10);
                    tp += p;
                    pigpiodIf.gpio_trigger(GPIO, (UInt32)p, 1);
                }

                pigpiodIf.time_sleep(0.5);

                CHECK(6, 1, t6_count, 5, 0, "gpio trigger count", ct);

                CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length", ct);
            }
            finally
            {
                pigpiodIf.callback_cancel(callback);
            }
        }
Esempio n. 2
0
        private async void buttonOn_Click(object sender, EventArgs e)
        {
            buttonOn.Enabled   = false;
            buttonTest.Enabled = false;
            buttonOff.Enabled  = true;
            PigpiodIf.Callback callback = null;
            try
            {
                callback = pigpiodIf.callback(GPIO, PigpiodIf.EITHER_EDGE, (gpio, level, tick, user) =>
                {
                    Console.WriteLine("callback: {0}, {1}, {2}, {3}", gpio, level, tick, user);
                    Invoke(new Action(() =>
                    {
                        bool isLow               = (level == PigpiodIf.PI_LOW);
                        textBoxAddress.Enabled   = isLow;
                        textBoxAddress.BackColor = isLow ? Color.Lime : Color.Aqua;
                    }));
                });

                cts = new CancellationTokenSource();
                var ct = cts.Token;
                await Task.Run(async() =>
                {
                    while (!ct.IsCancellationRequested)
                    {
                        pigpiodIf.gpio_write(GPIO, PigpiodIf.PI_HIGH);
                        await Task.Delay(500, ct);
                        pigpiodIf.gpio_write(GPIO, PigpiodIf.PI_LOW);
                        await Task.Delay(500, ct);
                    }
                }, ct);
            }
            catch (OperationCanceledException)
            {
                // nothing to do
            }
            finally
            {
                pigpiodIf.callback_cancel(callback);
                textBoxAddress.Enabled   = true;
                textBoxAddress.BackColor = SystemColors.Window;

                buttonOn.Enabled   = true;
                buttonTest.Enabled = true;
                buttonOff.Enabled  = false;
            }
        }
Esempio n. 3
0
        public void t7(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            PigpiodIf.Callback callback = null;
            try
            {
                int c, oc;

                Console.WriteLine("\r\nWatchdog tests.");

                int t7_count = 0;
                /* type of edge shouldn't matter for watchdogs */
                callback = pigpiodIf.callback(GPIO, PigpiodIf.FALLING_EDGE, (gpio, level, tick, user) =>
                {
                    if (level == PigpiodIf.PI_TIMEOUT)
                    {
                        t7_count++;
                    }
                });

                pigpiodIf.set_watchdog(GPIO, 50);                 /* 50 ms, 20 per second */
                pigpiodIf.time_sleep(0.5);
                oc = t7_count;
                pigpiodIf.time_sleep(2);
                c = t7_count - oc;
                CHECK(7, 1, c, 39, 5, "set watchdog on count", ct);

                pigpiodIf.set_watchdog(GPIO, 0);                 /* 0 switches watchdog off */
                pigpiodIf.time_sleep(0.5);
                oc = t7_count;
                pigpiodIf.time_sleep(2);
                c = t7_count - oc;
                CHECK(7, 2, c, 0, 1, "set watchdog off count", ct);
            }
            finally
            {
                pigpiodIf.callback_cancel(callback);
            }
        }
Esempio n. 4
0
        public void t2(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            PigpiodIf.Callback callback = null;
            try
            {
                int dc, f, r, rr, oc;

                Console.WriteLine("\r\nPWM dutycycle/range/frequency tests.");

                pigpiodIf.set_PWM_range(GPIO, 255);
                pigpiodIf.set_PWM_frequency(GPIO, 0);
                f = pigpiodIf.get_PWM_frequency(GPIO);
                CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency", ct);

                int t2_count = 0;
                callback = pigpiodIf.callback(GPIO, PigpiodIf.EITHER_EDGE, (gpio, level, tick, user) =>
                {
                    t2_count++;
                });

                pigpiodIf.set_PWM_dutycycle(GPIO, 0);
                dc = pigpiodIf.get_PWM_dutycycle(GPIO);
                CHECK(2, 2, dc, 0, 0, "get PWM dutycycle", ct);

                pigpiodIf.time_sleep(0.5);                 /* allow old notifications to flush */
                oc = t2_count;
                pigpiodIf.time_sleep(2);
                f = t2_count - oc;
                CHECK(2, 3, f, 0, 0, "set PWM dutycycle, callback", ct);

                pigpiodIf.set_PWM_dutycycle(GPIO, 128);
                dc = pigpiodIf.get_PWM_dutycycle(GPIO);
                CHECK(2, 4, dc, 128, 0, "get PWM dutycycle", ct);

                pigpiodIf.time_sleep(0.2);
                oc = t2_count;
                pigpiodIf.time_sleep(2);
                f = t2_count - oc;
                CHECK(2, 5, f, 40, 5, "set PWM dutycycle, callback", ct);

                pigpiodIf.set_PWM_frequency(GPIO, 100);
                f = pigpiodIf.get_PWM_frequency(GPIO);
                CHECK(2, 6, f, 100, 0, "set/get PWM frequency", ct);

                pigpiodIf.time_sleep(0.2);
                oc = t2_count;
                pigpiodIf.time_sleep(2);
                f = t2_count - oc;
                CHECK(2, 7, f, 400, 1, "callback", ct);

                pigpiodIf.set_PWM_frequency(GPIO, 1000);
                f = pigpiodIf.get_PWM_frequency(GPIO);
                CHECK(2, 8, f, 1000, 0, "set/get PWM frequency", ct);

                pigpiodIf.time_sleep(0.2);
                oc = t2_count;
                pigpiodIf.time_sleep(2);
                f = t2_count - oc;
                CHECK(2, 9, f, 4000, 1, "callback", ct);

                r = pigpiodIf.get_PWM_range(GPIO);
                CHECK(2, 10, r, 255, 0, "get PWM range", ct);

                rr = pigpiodIf.get_PWM_real_range(GPIO);
                CHECK(2, 11, rr, 200, 0, "get PWM real range", ct);

                pigpiodIf.set_PWM_range(GPIO, 2000);
                r = pigpiodIf.get_PWM_range(GPIO);
                CHECK(2, 12, r, 2000, 0, "set/get PWM range", ct);

                rr = pigpiodIf.get_PWM_real_range(GPIO);
                CHECK(2, 13, rr, 200, 0, "get PWM real range", ct);
            }
            finally
            {
                pigpiodIf.set_PWM_dutycycle(GPIO, 0);

                pigpiodIf.callback_cancel(callback);
            }
        }
Esempio n. 5
0
        public void t9(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            PigpiodIf.Callback callback = null;
            try
            {
                int      s, oc, c, e;
                UInt32[] p = new UInt32[10];

                Console.WriteLine("\r\nScript store/run/status/stop/delete tests.");

                pigpiodIf.gpio_write(GPIO, 0);                 /* need known state */

                /*
                 * 100 loops per second
                 * p0 number of loops
                 * p1 GPIO
                 */
                string script = @"

   ld p9 p0
   tag 0
   w p1 1
   mils 5
   w p1 0
   mils 5
   dcr p9
   jp 0";

                int t9_count = 0;
                callback = pigpiodIf.callback(GPIO, PigpiodIf.RISING_EDGE, (gpio, level, tick, user) =>
                {
                    t9_count++;
                });

                var bytes = System.Text.Encoding.UTF8.GetBytes(script);
                s = pigpiodIf.store_script(bytes);

                /* Wait for script to finish initing. */
                while (true)
                {
                    pigpiodIf.time_sleep(0.1);
                    e = pigpiodIf.script_status((UInt32)s, p);
                    if (e != PigpiodIf.PI_SCRIPT_INITING)
                    {
                        break;
                    }
                }

                oc = t9_count;
                pigpiodIf.run_script((UInt32)s, new UInt32[] { 99, GPIO });
                pigpiodIf.time_sleep(2);
                c = t9_count - oc;
                CHECK(9, 1, c, 100, 0, "store/run script", ct);

                oc = t9_count;
                pigpiodIf.run_script((UInt32)s, new UInt32[] { 200, GPIO });
                while (true)
                {
                    pigpiodIf.time_sleep(0.1);
                    e = pigpiodIf.script_status((UInt32)s, p);
                    if (e != PigpiodIf.PI_SCRIPT_RUNNING)
                    {
                        break;
                    }
                }
                c = t9_count - oc;
                pigpiodIf.time_sleep(0.1);
                CHECK(9, 2, c, 201, 0, "run script/script status", ct);

                oc = t9_count;
                pigpiodIf.run_script((UInt32)s, new UInt32[] { 2000, GPIO });
                while (true)
                {
                    pigpiodIf.time_sleep(0.1);
                    e = pigpiodIf.script_status((UInt32)s, p);
                    if (e != PigpiodIf.PI_SCRIPT_RUNNING)
                    {
                        break;
                    }
                    if (p[9] < 1600)
                    {
                        pigpiodIf.stop_script((UInt32)s);
                    }
                }
                c = t9_count - oc;
                pigpiodIf.time_sleep(0.1);
                CHECK(9, 3, c, 410, 10, "run/stop script/script status", ct);

                e = pigpiodIf.delete_script((UInt32)s);
                CHECK(9, 4, e, 0, 0, "delete script", ct);
            }
            finally
            {
                pigpiodIf.callback_cancel(callback);
            }
        }
Esempio n. 6
0
        public void t5(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            PigpiodIf.Callback callback = null;
            try
            {
                int BAUD = 4800;

                string TEXT = @"
Now is the winter of our discontent
Made glorious summer by this sun of York;
And all the clouds that lour'd upon our house
In the deep bosom of the ocean buried.
Now are our brows bound with victorious wreaths;
Our bruised arms hung up for monuments;
Our stern alarums changed to merry meetings,
Our dreadful marches to delightful measures.
Grim - visaged war hath smooth'd his wrinkled front;
And now, instead of mounting barded steeds
To fright the souls of fearful adversaries,
He capers nimbly in a lady's chamber
To the lascivious pleasing of a lute.
";

                TEXT = TEXT.Replace("\r\n", "\n");

                PigpiodIf.GpioPulse[] wf = new PigpiodIf.GpioPulse[]
                {
                    new PigpiodIf.GpioPulse()
                    {
                        gpioOn = 1 << GPIO, gpioOff = 0, usDelay = 10000
                    },
                    new PigpiodIf.GpioPulse()
                    {
                        gpioOn = 0, gpioOff = 1 << GPIO, usDelay = 30000
                    },
                    new PigpiodIf.GpioPulse()
                    {
                        gpioOn = 1 << GPIO, gpioOff = 0, usDelay = 60000
                    },
                    new PigpiodIf.GpioPulse()
                    {
                        gpioOn = 0, gpioOff = 1 << GPIO, usDelay = 100000
                    },
                };

                int e, oc, c, wid;

                byte[] recv = new byte[2048];

                Console.WriteLine("\r\nWaveforms & serial read/write tests.");

                int t5_count = 0;
                callback = pigpiodIf.callback(GPIO, PigpiodIf.FALLING_EDGE, (gpio, level, tick, user) =>
                {
                    t5_count++;
                });

                pigpiodIf.set_mode(GPIO, PigpiodIf.PI_OUTPUT);

                e = pigpiodIf.wave_clear();
                CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear", ct);

                e = pigpiodIf.wave_add_generic(wf);
                CHECK(5, 2, e, 4, 0, "pulse, wave add generic", ct);

                wid = pigpiodIf.wave_create();
                e   = pigpiodIf.wave_send_repeat((UInt32)wid);
                if (e < 14)
                {
                    CHECK(5, 3, e, 9, 0, "wave tx repeat", ct);
                }
                else
                {
                    CHECK(5, 3, e, 19, 0, "wave tx repeat", ct);
                }

                oc = t5_count;
                pigpiodIf.time_sleep(5.05);
                c = t5_count - oc;
                CHECK(5, 4, c, 50, 2, "callback", ct);

                e = pigpiodIf.wave_tx_stop();
                CHECK(5, 5, e, 0, 0, "wave tx stop", ct);

                e = pigpiodIf.bb_serial_read_open(GPIO, (UInt32)BAUD, 8);
                CHECK(5, 6, e, 0, 0, "serial read open", ct);

                pigpiodIf.wave_clear();
                var bytes = System.Text.Encoding.UTF8.GetBytes(TEXT);
                e = pigpiodIf.wave_add_serial(GPIO, (UInt32)BAUD, 8, 2, 5000000, bytes);
                CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial", ct);

                wid = pigpiodIf.wave_create();
                e   = pigpiodIf.wave_send_once((UInt32)wid);
                if (e < 6964)
                {
                    CHECK(5, 8, e, 6811, 0, "wave tx start", ct);
                }
                else
                {
                    CHECK(5, 8, e, 7116, 0, "wave tx start", ct);
                }

                oc = t5_count;
                pigpiodIf.time_sleep(3);
                c = t5_count - oc;
                CHECK(5, 9, c, 0, 0, "callback", ct);

                oc = t5_count;
                while (pigpiodIf.wave_tx_busy() != 0)
                {
                    pigpiodIf.time_sleep(0.1);
                }
                pigpiodIf.time_sleep(0.1);
                c = t5_count - oc;
                CHECK(5, 10, c, 1702, 0, "wave tx busy, callback", ct);

                c    = pigpiodIf.bb_serial_read(GPIO, recv);
                recv = recv.Take(c).ToArray();
                string text = System.Text.Encoding.UTF8.GetString(recv);
                CHECK(5, 11, string.Compare(TEXT, text), 0, 0, "wave tx busy, serial read", ct);

                e = pigpiodIf.bb_serial_read_close(GPIO);
                CHECK(5, 12, e, 0, 0, "serial read close", ct);

                c = pigpiodIf.wave_get_micros();
                CHECK(5, 13, c, 6158148, 0, "wave get micros", ct);

                c = pigpiodIf.wave_get_high_micros();
                if (c > 6158148)
                {
                    c = 6158148;
                }
                CHECK(5, 14, c, 6158148, 0, "wave get high micros", ct);

                c = pigpiodIf.wave_get_max_micros();
                CHECK(5, 15, c, 1800000000, 0, "wave get max micros", ct);

                c = pigpiodIf.wave_get_pulses();
                CHECK(5, 16, c, 3405, 0, "wave get pulses", ct);

                c = pigpiodIf.wave_get_high_pulses();
                CHECK(5, 17, c, 3405, 0, "wave get high pulses", ct);

                c = pigpiodIf.wave_get_max_pulses();
                CHECK(5, 18, c, 12000, 0, "wave get max pulses", ct);

                c = pigpiodIf.wave_get_cbs();
                if (c < 6963)
                {
                    CHECK(5, 19, c, 6810, 0, "wave get cbs", ct);
                }
                else
                {
                    CHECK(5, 19, c, 7115, 0, "wave get cbs", ct);
                }

                c = pigpiodIf.wave_get_high_cbs();
                if (c < 6963)
                {
                    CHECK(5, 20, c, 6810, 0, "wave get high cbs", ct);
                }
                else
                {
                    CHECK(5, 20, c, 7115, 0, "wave get high cbs", ct);
                }

                c = pigpiodIf.wave_get_max_cbs();
                CHECK(5, 21, c, 25016, 0, "wave get max cbs", ct);
            }
            finally
            {
                pigpiodIf.callback_cancel(callback);
            }
        }
Esempio n. 7
0
        public void t3(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            PigpiodIf.Callback callback = null;
            try
            {
                int[] pw = new int[] { 500, 1500, 2500 };
                int[] dc = new int[] { 20, 40, 60, 80 };

                int   f, rr, v;
                float on, off;

                int t;

                Console.WriteLine("\r\nPWM/Servo pulse accuracy tests.");

                int    t3_reset = 1;
                int    t3_count = 0;
                UInt32 t3_tick  = 0;
                float  t3_on    = 0.0f;
                float  t3_off   = 0.0f;
                callback = pigpiodIf.callback(GPIO, PigpiodIf.EITHER_EDGE, (gpio, level, tick, user) =>
                {
                    UInt32 td;

                    //Console.WriteLine("pi={0} g{1} l={2} t={3}", pi, gpio, level, tick);
                    if (t3_reset != 0)
                    {
                        t3_count = 0;
                        t3_on    = 0.0f;
                        t3_off   = 0.0f;
                        t3_reset = 0;
                    }
                    else
                    {
                        td = tick - t3_tick;

                        if (level == 0)
                        {
                            t3_on += td;
                        }
                        else
                        {
                            t3_off += td;
                        }
                    }

                    t3_count++;
                    t3_tick = tick;
                });

                for (t = 0; t < 3; t++)
                {
                    pigpiodIf.set_servo_pulsewidth(GPIO, (UInt32)pw[t]);
                    v = pigpiodIf.get_servo_pulsewidth(GPIO);
                    CHECK(3, t + t + 1, v, pw[t], 0, "get servo pulsewidth", ct);

                    pigpiodIf.time_sleep(1);
                    t3_reset = 1;
                    pigpiodIf.time_sleep(4);
                    on  = t3_on;
                    off = t3_off;
                    CHECK(3, t + t + 2, (int)((1000.0 * (on + off)) / on), (int)(20000000.0 / pw[t]), 1,
                          "set servo pulsewidth", ct);
                }

                pigpiodIf.set_servo_pulsewidth(GPIO, 0);
                pigpiodIf.set_PWM_frequency(GPIO, 1000);
                f = pigpiodIf.get_PWM_frequency(GPIO);
                CHECK(3, 7, f, 1000, 0, "set/get PWM frequency", ct);

                rr = pigpiodIf.set_PWM_range(GPIO, 100);
                CHECK(3, 8, rr, 200, 0, "set PWM range", ct);

                for (t = 0; t < 4; t++)
                {
                    pigpiodIf.set_PWM_dutycycle(GPIO, (UInt32)dc[t]);
                    v = pigpiodIf.get_PWM_dutycycle(GPIO);
                    CHECK(3, t + t + 9, v, dc[t], 0, "get PWM dutycycle", ct);

                    pigpiodIf.time_sleep(1);
                    t3_reset = 1;
                    pigpiodIf.time_sleep(2);
                    on  = t3_on;
                    off = t3_off;
                    CHECK(3, t + t + 10, (int)((1000.0 * on) / (on + off)), (int)(10.0 * dc[t]), 1,
                          "set PWM dutycycle", ct);
                }
            }
            finally
            {
                pigpiodIf.set_PWM_dutycycle(GPIO, 0);

                pigpiodIf.callback_cancel(callback);
            }
        }
Esempio n. 8
0
        private async void buttonStart_Click(object sender, EventArgs e)
        {
            do
            {
                repeatRequested = false;

                buttonStart.Enabled = false;
                buttonStop.Enabled  = true;

                bool complete = false;
                PigpiodIf.Callback callback = pigpiodIf.callback(COMPLETE, PigpiodIf.RISING_EDGE, (gpio, level, tick, user) =>
                {
                    complete = true;
                });
                try
                {
                    cts = new CancellationTokenSource();
                    var ct = cts.Token;
                    await Task.Run(async() =>
                    {
                        uint addr = 0x10000;

                        int[] divs   = new int[] { 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 };
                        int div      = divs[horizontal];
                        int position = ((NUM_SAMPLES - 1) * positionPct) / 100;
                        int level    = (4095 * levelPct) / 100;
                        avalonMM.WriteUInt32Packet(addr + 0x201 * 4, (uint)div);
                        avalonMM.WriteUInt32Packet(addr + 0x202 * 4, (uint)position);
                        avalonMM.WriteUInt32Packet(addr + 0x203 * 4, (uint)source);
                        avalonMM.WriteUInt32Packet(addr + 0x204 * 4, (uint)polarity);
                        avalonMM.WriteUInt32Packet(addr + 0x205 * 4, (uint)level);

                        complete = false;
                        avalonMM.WriteUInt32Packet(addr + 0x200 * 4, 1);
                        DateTime lastAccessTime = DateTime.Now;

                        List <DataPoint>[] dataPoints = new List <DataPoint> [NUM_CHANNELS];
                        for (int ch = 0; ch < NUM_CHANNELS; ch++)
                        {
                            dataPoints[ch] = new List <DataPoint>();
                        }

                        while (!ct.IsCancellationRequested)
                        {
                            double deltaT = (1.0 / 0.5e6) * div;

                            while (!ct.IsCancellationRequested && !complete)
                            {
                                int leftInMS = (int)(deltaT * 1000 * NUM_SAMPLES + AUTO_TRIGGER_MS) - (int)(DateTime.Now - lastAccessTime).TotalMilliseconds;
                                if (leftInMS <= 0)
                                {
                                    break;
                                }
                                await Task.Delay(Math.Min(leftInMS, 10), ct);
                            }

                            uint[] res;
                            lock (avalonMM.LockObject)
                            {
                                avalonMM.WriteUInt32Packet(addr + 0x200 * 4, 0);
                                res      = avalonMM.ReadUInt32Packet(NUM_SAMPLES, addr, true);
                                complete = false;
                                avalonMM.WriteUInt32Packet(addr + 0x200 * 4, 1);
                                lastAccessTime = DateTime.Now;
                            }
                            if (res.Length != NUM_SAMPLES)
                            {
                                Console.WriteLine("the response size {0} not equal to the requested size {1}", res.Length, NUM_SAMPLES);
                                continue;
                            }

                            for (int ch = 0; ch < NUM_CHANNELS; ch++)
                            {
                                dataPoints[ch].Clear();
                            }
                            for (int i = 0; i < res.Length; i++)
                            {
                                int d1    = (int)(res[i] & 0xffff);
                                int d2    = (int)((res[i] >> 16) & 0xffff);
                                double v1 = 3.3 * (d1 / 4096.0);
                                double v2 = 3.3 * (d2 / 4096.0);
                                dataPoints[0].Add(new DataPoint(deltaT * (i - position), v1));
                                dataPoints[1].Add(new DataPoint(deltaT * (i - position) + 1e-6, v2));
                            }

                            Invoke(new Action(() =>
                            {
                                for (int ch = 0; ch < NUM_CHANNELS; ch++)
                                {
                                    series[ch].Points.Clear();
                                    series[ch].Points.AddRange(dataPoints[ch]);
                                }
                                plotModel.InvalidatePlot(true);
                                if (plotView1.Model != plotModel)
                                {
                                    plotView1.Model = plotModel;
                                }
                                else
                                {
                                    plotView1.Invalidate();
                                }
                            }));
                        }
                    }, ct);
                }
                catch (OperationCanceledException)
                {
                    // nothing to do
                }
                finally
                {
                    if (callback != null)
                    {
                        pigpiodIf.callback_cancel(callback);
                    }

                    cts = null;

                    buttonStart.Enabled = true;
                    buttonStop.Enabled  = false;
                }
            } while (repeatRequested);
        }