private static bool DisplayPotentiometerInfoAndCheckForCancel(MCP41X1_Base ldp, bool tryReadValue = false, int exWaitTime = -1) { Console.WriteLine(ldp.ToString()); if(exWaitTime == -1) TimePeriod.Sleep(_waitTime * 1); else if (exWaitTime >0) TimePeriod.Sleep(exWaitTime); if (tryReadValue) { int val = ldp.Get(); } if (Console.KeyAvailable) { var k = Console.ReadKey(); return true; } else return false; }
private bool Animate(MCP41X1_Base.ADDRESS pot, AnimationDsl animationType, int wait, int count = 1) { var steps = new List<int>(); if (animationType == AnimationDsl.FadeInFadeOut) { Animate(pot, AnimationDsl.FadeIn, wait); Animate(pot, AnimationDsl.Pause, wait); Animate(pot, AnimationDsl.FadeOut, wait); } else if (animationType == AnimationDsl.FadeIn) { steps = _stepsDefinition[pot]; } else if (animationType == AnimationDsl.FadeOut) { steps = new List<int>(_stepsDefinition[pot]); steps.Reverse(); } else if (animationType == AnimationDsl.Pause) { TimePeriod.Sleep(wait*2); return true; } else if (animationType == AnimationDsl.Off) { this.Set(0, pot); return true; } else if (animationType == AnimationDsl.Max) { steps = _stepsDefinition[pot]; this.Set(steps[steps.Count-1], pot); return true; } else if (animationType == AnimationDsl.HalfMax) { steps = _stepsDefinition[pot]; this.Set(steps[steps.Count/2], pot); return true; } else if (animationType == AnimationDsl.Blink) { MCP41X1_Base.ADDRESS Led1 = pot; MCP41X1_Base.ADDRESS Led2 = Led1 == Red ? Green : Red; for (var c = 0; c < count; c++) { Animate(Led1, AnimationDsl.Off, wait); TimePeriod.Sleep(wait); Animate(Led1, AnimationDsl.Max, wait); TimePeriod.Sleep(wait); } return true; } else if (animationType == AnimationDsl.BlinkWithOtherColor) { MCP41X1_Base.ADDRESS Led1 = pot; MCP41X1_Base.ADDRESS Led2 = Led1 == Red ? Green : Red; for (var c = 0; c < count; c++) { Animate(Led1, AnimationDsl.Off, wait); Animate(Led2, AnimationDsl.Max, wait); TimePeriod.Sleep(wait); Animate(Led2, AnimationDsl.Off, wait); Animate(Led1, AnimationDsl.Max, wait); TimePeriod.Sleep(wait); } return true; } else throw new ArgumentException(); foreach (var s in steps) { if (!this.Set(s, pot).Succeeded) Console.WriteLine("Communication error"); TimePeriod.Sleep(wait); } return true; }
static void OtherApiTests(MCP41X1_Base dp) { var quit = false; Console.Clear(); try { var expectedValue = 0; dp.Set(0); for (var i = 0; i <= dp.MaxDigitalValue; i += _demoStep) { expectedValue += _demoStep; if(expectedValue > dp.MaxDigitalValue) expectedValue = dp.MaxDigitalValue; if(DisplayPotentiometerInfoAndCheckForCancel(dp)) { quit = true; break;} dp.Increment(_demoStep); var v = dp.Get(); if(expectedValue != v) Console.WriteLine("Method Get() did not return expected result"); } if(quit) return; dp.Set(dp.MaxDigitalValue); DisplayPotentiometerInfoAndCheckForCancel(dp); expectedValue = dp.MaxDigitalValue; for (var i = dp.MaxDigitalValue; i > 0; i -= _demoStep) { expectedValue -= _demoStep; if(expectedValue < dp.MinDigitalValue) expectedValue = dp.MinDigitalValue; if(DisplayPotentiometerInfoAndCheckForCancel(dp)) { quit = true; break;} dp.Decrement(_demoStep); var v = dp.Get(); if(expectedValue != v) Console.WriteLine("Method Get() did not return expected result"); } } finally { dp.Set(0); } }
static void TestDigitalPotRange(MCP41X1_Base dp, bool tryReadValue = false) { Console.Clear(); for (var i = dp.MinDigitalValue; i <= dp.MaxDigitalValue; i += _demoStep) { if (!dp.Set(i).Succeeded) Console.WriteLine("Communication error"); if(DisplayPotentiometerInfoAndCheckForCancel(dp, tryReadValue)) break; } dp.Set(0); }
static bool AnimateOneLed(MCP4231 dp, MCP41X1_Base.ADDRESS pot0, MCP41X1_Base.ADDRESS? pot1, int wait) { int startStep = 38; int step = 4; for (var i = startStep; i <= dp.MaxDigitalValue; i += step) { if (!dp.Set(i, pot0).Succeeded) Console.WriteLine("Communication error"); if(pot1.HasValue) if (!dp.Set(i, pot1.Value).Succeeded) Console.WriteLine("Communication error"); if(DisplayPotentiometerInfoAndCheckForCancel(dp, exWaitTime:wait)) return true; // Quit } for (var i = dp.MaxDigitalValue; i > startStep; i -= step) { if (!dp.Set(i, pot0).Succeeded) Console.WriteLine("Communication error"); if(pot1.HasValue) if (!dp.Set(i, pot1.Value).Succeeded) Console.WriteLine("Communication error"); if(DisplayPotentiometerInfoAndCheckForCancel(dp, exWaitTime:wait)) return true; // Quit } return false; }
static void OtherApiTests(MCP41X1_Base dp) { var quit = false; Console.Clear(); try { dp.Set(0); for (var i = 0; i <= dp.MaxDigitalValue; i += demoStep) { if(DisplayPotentiometerInfoAndCheckForCancel(dp)) { quit = true; break;} dp.Increment(demoStep); } if(quit) return; dp.Set(dp.MaxDigitalValue); DisplayPotentiometerInfoAndCheckForCancel(dp); for (var i = dp.MaxDigitalValue; i > 0; i -= demoStep) { if(DisplayPotentiometerInfoAndCheckForCancel(dp)) { quit = true; break;} dp.Decrement(demoStep); } } finally { dp.Set(0); } }
static void DigitalPotRangeForLed(MCP41X1_Base dp) { Console.Clear(); var quit = false; var ledThreadHold17Volt = 0; dp.Set(0); int wait = 16; while (!quit) { for (var i = ledThreadHold17Volt; i <= dp.MaxDigitalValue; i += 1) { if (!dp.Set(i).Succeeded) Console.WriteLine("Communication error"); if(DisplayPotentiometerInfoAndCheckForCancel(dp, exWaitTime:wait)) { quit = true; break; } } if(DisplayPotentiometerInfoAndCheckForCancel(dp, exWaitTime:wait)) { quit = true; break; } //_mcp4131_10k.Set(dp.MaxDigitalValue); //TimePeriod.Sleep(waitTime * 10); //for (var i = 0; i < 5; i++) { // dp.Set(0); // TimePeriod.Sleep(waitTime*2); // _mcp4131_10k.Set(dp.MaxDigitalValue); // TimePeriod.Sleep(waitTime*3); //} for (var i = dp.MaxDigitalValue; i > ledThreadHold17Volt; i -= 1) { if (!dp.Set(i).Succeeded) Console.WriteLine("Communication error"); if(DisplayPotentiometerInfoAndCheckForCancel(dp, exWaitTime:wait)) { quit = true; break; } } //TimePeriod.Sleep(waitTime*10); if(DisplayPotentiometerInfoAndCheckForCancel(dp, exWaitTime:wait)) { quit = true; break; } } var k = Console.ReadKey(); dp.Set(0); }