public SenseHatServices() { ledMatrix = new SenseHatLedMatrixI2c(); pressureAndTemperatureSensor = new SenseHatPressureAndTemperature(); temperatureAndHumiditySensor = new SenseHatTemperatureAndHumidity(); temperatureCpuSensor = new CpuTemperature(); }
private static void Frame(Span <Color> colors, float time) { for (int i = 0; i < colors.Length; i++) { (int x, int y) = SenseHatLedMatrix.IndexToPosition(i); colors[i] = Pixel(new Vector2(x / 8.0f, y / 8.0f), time); } }
public EfficientLedMatrix() { _ledMatrix = new SenseHatLedMatrixI2c(); previousColorMatrix = new Color[width * height]; for (var i = 0; i < width * height; i++) { previousColorMatrix[i] = Color.Black; } }
public static void Run() { using SenseHatMagnetometer magnetometer = new(); using SenseHatLedMatrixI2c ledMatrix = new(); Console.WriteLine("Move SenseHat around in every direction until dot on the LED matrix stabilizes when not moving."); ledMatrix.Fill(); Stopwatch sw = Stopwatch.StartNew(); Vector3 min = magnetometer.MagneticInduction; Vector3 max = magnetometer.MagneticInduction; while (min == max) { Vector3 sample = magnetometer.MagneticInduction; min = Vector3.Min(min, sample); max = Vector3.Max(max, sample); Thread.Sleep(50); } const int intervals = 8; Color[] data = new Color[64]; while (true) { Vector3 sample = magnetometer.MagneticInduction; min = Vector3.Min(min, sample); max = Vector3.Max(max, sample); Vector3 size = max - min; Vector3 pos = Vector3.Divide(Vector3.Multiply((sample - min), intervals - 1), size); int x = Math.Clamp((int)pos.X, 0, intervals - 1); // reverse y to match magnetometer coordinate system int y = intervals - 1 - Math.Clamp((int)pos.Y, 0, intervals - 1); int idx = SenseHatLedMatrix.PositionToIndex(x, y); // fading for (int i = 0; i < 64; i++) { data[i] = Color.FromArgb((byte)Math.Clamp(data[i].R - 1, 0, 255), data[i].G, data[i].B); } Color col = data[idx]; col = Color.FromArgb(Math.Clamp(col.R + 20, 0, 255), col.G, col.B); Vector2 pos2 = new Vector2(sample.X, sample.Y); Vector2 center2 = Vector2.Multiply(new Vector2(min.X + max.X, min.Y + max.Y), 0.5f); float max2 = Math.Max(size.X, size.Y); float distFromCenter = (pos2 - center2).Length(); data[idx] = Color.FromArgb(0, 255, (byte)Math.Clamp(255 * distFromCenter / max2, 0, 255)); ledMatrix.Write(data); data[idx] = col; Thread.Sleep(50); } }
// Not used by default but much simpler to understand private static void SetPixelDemo(SenseHatLedMatrix ledMatrix) { ledMatrix.Fill(Color.Purple); ledMatrix.SetPixel(0, 0, Color.Red); ledMatrix.SetPixel(1, 0, Color.Green); ledMatrix.SetPixel(2, 0, Color.Blue); for (int i = 1; i <= 7; i++) { ledMatrix.SetPixel(i, i, Color.White); } }
private static void WriteDemo(SenseHatLedMatrix ledMatrix) { Color[] colors = new Color[SenseHatLedMatrix.NumberOfPixels]; Stopwatch sw = Stopwatch.StartNew(); int frames = 0; while (true) { float time = sw.ElapsedMilliseconds / 1000.0f; Frame(colors, time); ledMatrix.Write(colors); frames++; if (frames % 200 == 0 && time > 1.0) { Console.WriteLine($"Average FPS: {frames / time}"); } Thread.Sleep(30); } }