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); } }
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); } }