Пример #1
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);
            }
        }