Exemplo n.º 1
0
        public void t1(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            int v;

            Console.WriteLine("\r\nMode/PUD/read/write tests.");

            pigpiodIf.set_mode(GPIO, PigpiodIf.PI_INPUT);
            v = pigpiodIf.get_mode(GPIO);
            CHECK(1, 1, v, 0, 0, "set mode, get mode", ct);

            pigpiodIf.set_pull_up_down(GPIO, PigpiodIf.PI_PUD_UP);
            v = pigpiodIf.gpio_read(GPIO);
            CHECK(1, 2, v, 1, 0, "set pull up down, read", ct);

            pigpiodIf.set_pull_up_down(GPIO, PigpiodIf.PI_PUD_DOWN);
            v = pigpiodIf.gpio_read(GPIO);
            CHECK(1, 3, v, 0, 0, "set pull up down, read", ct);

            pigpiodIf.gpio_write(GPIO, PigpiodIf.PI_LOW);
            v = pigpiodIf.get_mode(GPIO);
            CHECK(1, 4, v, 1, 0, "write, get mode", ct);

            v = pigpiodIf.gpio_read(GPIO);
            CHECK(1, 5, v, 0, 0, "read", ct);

            pigpiodIf.gpio_write(GPIO, PigpiodIf.PI_HIGH);
            v = pigpiodIf.gpio_read(GPIO);
            CHECK(1, 6, v, 1, 0, "write, read", ct);
        }
Exemplo n.º 2
0
        private async void buttonLedStart_Click(object sender, EventArgs e)
        {
            buttonLedStart.Enabled = false;
            buttonLedStop.Enabled  = true;
            try
            {
                ledCts = new CancellationTokenSource();
                var ct = ledCts.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
            {
                ledCts = null;

                buttonLedStart.Enabled = true;
                buttonLedStop.Enabled  = false;
            }
        }
Exemplo n.º 3
0
        public void t8(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            int v;

            Console.WriteLine("\r\nBank read/write tests.");

            pigpiodIf.gpio_write(GPIO, 0);
            v = (int)pigpiodIf.read_bank_1() & (1 << GPIO);
            CHECK(8, 1, v, 0, 0, "read bank 1", ct);

            pigpiodIf.gpio_write(GPIO, 1);
            v = (int)pigpiodIf.read_bank_1() & (1 << GPIO);
            CHECK(8, 2, v, (1 << GPIO), 0, "read bank 1", ct);

            pigpiodIf.clear_bank_1(1 << GPIO);
            v = pigpiodIf.gpio_read(GPIO);
            CHECK(8, 3, v, 0, 0, "clear bank 1", ct);

            pigpiodIf.set_bank_1(1 << GPIO);
            v = pigpiodIf.gpio_read(GPIO);
            CHECK(8, 4, v, 1, 0, "set bank 1", ct);

            v = (int)pigpiodIf.read_bank_2();

            if (v != 0)
            {
                v = 0;
            }
            else
            {
                v = 1;
            }

            CHECK(8, 5, v, 0, 0, "read bank 2", ct);

            v = pigpiodIf.clear_bank_2(0);
            CHECK(8, 6, v, 0, 0, "clear bank 2", ct);

            v = pigpiodIf.clear_bank_2(0xffffff);
            CHECK(8, 7, v, -42, 0, "clear bank 2", ct);

            v = pigpiodIf.set_bank_2(0);
            CHECK(8, 8, v, 0, 0, "set bank 2", ct);

            v = pigpiodIf.set_bank_2(0xffffff);
            CHECK(8, 9, v, -42, 0, "set bank 2", ct);
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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);
            }
        }