From 36f88e11a45b46679994eadbed3a9c63e6769bbb Mon Sep 17 00:00:00 2001 From: David K Date: Sun, 22 Oct 2023 18:15:57 +0100 Subject: [PATCH] Lights still aren't quite there --- skullOS.Core/LED Elements/SkullLed.cs | 2 + skullOS.Output/Data/Output.txt | 8 ++-- skullOS.Output/Output.cs | 56 ++++++++++++++++++++------- skullOS.Output/SkullLed.cs | 9 ++++- skullOS.Output/SkullNeoPixel.cs | 3 +- skullOS/Program.cs | 8 ++++ 6 files changed, 64 insertions(+), 22 deletions(-) diff --git a/skullOS.Core/LED Elements/SkullLed.cs b/skullOS.Core/LED Elements/SkullLed.cs index 1bfa7f2..17f3571 100644 --- a/skullOS.Core/LED Elements/SkullLed.cs +++ b/skullOS.Core/LED Elements/SkullLed.cs @@ -23,6 +23,8 @@ public SkullLed(string LedName, int ledPin, GpioController controller) name = LedName; pin = ledPin; gpioController = controller; + + gpioController.OpenPin(pin, PinMode.Output); } public void ToggleState() diff --git a/skullOS.Output/Data/Output.txt b/skullOS.Output/Data/Output.txt index e751d8f..27f0030 100644 --- a/skullOS.Output/Data/Output.txt +++ b/skullOS.Output/Data/Output.txt @@ -1,4 +1,4 @@ -BuzzerPin = 17 -NeoPixel = True -NeoPixelCount = 8 -LedPin = 23 \ No newline at end of file +BuzzerPin=17 +NeoPixel=True +NeoPixelCount=8 +LedPin=23 \ No newline at end of file diff --git a/skullOS.Output/Output.cs b/skullOS.Output/Output.cs index 0a5b144..5e7d757 100644 --- a/skullOS.Output/Output.cs +++ b/skullOS.Output/Output.cs @@ -10,35 +10,63 @@ public class Output : Controller public override void Run(GpioController controller) { - var pixelDisplay = (SkullNeoPixel)outputDevices.Select(x => x).Where(x => x.Name == "NeoPixel").FirstOrDefault(); - pixelDisplay.device.Image.SetPixel(0, 0, Color.AliceBlue); + SkullNeoPixel? pixelDisplay = (SkullNeoPixel)outputDevices.Select(x => x).FirstOrDefault(x => x.Name == "NeoPixel"); + pixelDisplay?.device.Image.SetPixel(0, 0, Color.AliceBlue); + var lifeLed = (SkullLed)outputDevices.Select(x => x).FirstOrDefault(x => x.Name == "Life Light"); + lifeLed.ToggleState(); } public override bool Setup(GpioController controller) { - var settings = SettingsLoader.LoadConfig(@"Data/Settings.txt"); + var settings = SettingsLoader.LoadConfig(@"Data/Output.txt"); var defaultValue = new KeyValuePair("", ""); - - if (settings.ContainsKey("Buzzer")) +#if DEBUG + Console.WriteLine("Output device settings contain:"); + foreach (var item in settings) + { + Console.WriteLine(item.Key + " : " + item.Value); + } +#endif + if (settings.ContainsKey("BuzzerPin")) { - settings.TryGetValue("Buzzer", out string BuzzerPin); - var deviceBuzzer = new SkullBuzzer("Buzzer", int.Parse(BuzzerPin)); - outputDevices.Add(deviceBuzzer); + if (settings.TryGetValue("BuzzerPin", out string BuzzerPin)) + { + int buzzerPinNumber = Convert.ToInt32(BuzzerPin); + SkullBuzzer deviceBuzzer = new("Buzzer", buzzerPinNumber); + outputDevices.Add(deviceBuzzer); + } } + if (settings.ContainsKey("NeoPixel")) { - settings.TryGetValue("NeoPixelCount", out string count); - var deviceNeoPixel = new SkullNeoPixel("NeoPixel", int.Parse(count)); - outputDevices.Add(deviceNeoPixel); + if (settings.TryGetValue("NeoPixelCount", out string count)) + { + int neoPixelPinNumber = Convert.ToInt32(count); + SkullNeoPixel deviceNeoPixel = new("NeoPixel", neoPixelPinNumber); + outputDevices.Add(deviceNeoPixel); + } } + if (settings.ContainsKey("LedPin")) { - settings.TryGetValue("LedPin", out string pin); - var deviceLed = new skullOS.Output.SkullLed("Life Light", int.Parse(pin), controller); - outputDevices.Add(deviceLed); + if (settings.TryGetValue("LedPin", out string pin)) + { + int lifeLedPinNumber = Convert.ToInt32(pin); + SkullLed deviceLed = new("Life Light", lifeLedPinNumber, controller); + + outputDevices.Add(deviceLed); + } } + +#if DEBUG + Console.WriteLine("The following output devices have been registered:"); + foreach (var item in outputDevices) + { + Console.WriteLine(item.Name); + } +#endif return true; } diff --git a/skullOS.Output/SkullLed.cs b/skullOS.Output/SkullLed.cs index 1594e18..913849b 100644 --- a/skullOS.Output/SkullLed.cs +++ b/skullOS.Output/SkullLed.cs @@ -5,7 +5,9 @@ namespace skullOS.Output { public class SkullLed : SkullOutputDevice { - public string name = ""; + /// + /// SOMETHING IN HERE ISN'T WORKING! + /// public int pin = 0; public LedBehaviour state = LedBehaviour.Off; @@ -14,14 +16,17 @@ public class SkullLed : SkullOutputDevice public SkullLed(string LedName, int ledPin, GpioController controller) { - name = LedName; + Name = LedName; pin = ledPin; gpioController = controller; + + gpioController.OpenPin(pin, PinMode.Output); } public void ToggleState() { gpioController.Write(pin, ((ledOn) ? PinValue.High : PinValue.Low)); + ledOn = !ledOn; } } } diff --git a/skullOS.Output/SkullNeoPixel.cs b/skullOS.Output/SkullNeoPixel.cs index 58b7adb..22183a8 100644 --- a/skullOS.Output/SkullNeoPixel.cs +++ b/skullOS.Output/SkullNeoPixel.cs @@ -23,8 +23,7 @@ public SkullNeoPixel(string name, int count) }; using SpiDevice spi = SpiDevice.Create(spiSettings); - Ws2812b neopixel = new(spi, count); - + neoPixel = new Ws2812b(spi, count); } } } diff --git a/skullOS/Program.cs b/skullOS/Program.cs index d76d719..7edab32 100644 --- a/skullOS/Program.cs +++ b/skullOS/Program.cs @@ -29,6 +29,14 @@ public static async Task Main(string[] args) break; } + await Console.Out.WriteLineAsync("Testign output!"); + int pin = 23; + GpioController controller = new(); + if (!controller.IsPinOpen(23)) + { + controller.OpenPin(23, PinMode.Output); + } + controller.Write(pin, PinValue.High); await Task.Delay(Timeout.Infinite); }