private static void ApplicationLoop(SSD1306Manager manager, GpioController controller, ButtonsManager buttonsManager, Dictionary <int, bool> enabledRelays) { Console.WriteLine("Start application loop"); SetRelayStatus(manager, enabledRelays); while (true) { List <MatrixButton> pressedButtons = buttonsManager.ArePressed(); foreach (var button in pressedButtons) { int buttonIndex = _matrixButtons.IndexOf(button); if (buttonIndex < 0) { continue; } int relayToEnable = _pinouts[buttonIndex]; if (!enabledRelays[relayToEnable]) { controller.Write(relayToEnable, PinValue.Low); enabledRelays[relayToEnable] = true; } else { controller.Write(relayToEnable, PinValue.High); enabledRelays[relayToEnable] = false; } } if (pressedButtons.Count > 0) { SetRelayStatus(manager, enabledRelays); } Thread.Sleep(500); if (!enabledRelays.ContainsValue(false)) { break; } } Console.WriteLine("End application loop"); }