예제 #1
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);
            }
        }