// https://github.com/pimoroni/unicorn-hat/blob/master/examples/rainbow.py
        static void Rainbow()
        {
            const double intensity = 0.4;
            const int    width     = 8;
            const int    height    = 8;

            Color[] data   = new Color[width * height];
            double  i      = 0.0;
            double  offset = 30;

            SenseHatLedMatrixI2c ledMatrix = new SenseHatLedMatrixI2c();

            while (true)
            {
                i = i + 0.3;
                for (int y = 0; y < width; y++)
                {
                    for (int x = 0; x < height; x++)
                    {
                        var r = (Math.Cos((x + i) / 2.0) + Math.Cos((y + i) / 2.0)) * 96.0 + 32.0;
                        var g = (Math.Sin((x + i) / 1.5) + Math.Sin((y + i) / 2.0)) * 96.0 + 32.0;
                        var b = (Math.Sin((x + i) / 2.0) + Math.Cos((y + i) / 1.5)) * 96.0 + 32.0;

                        r = Math.Max(0, Math.Min(255, r + offset));
                        g = Math.Max(0, Math.Min(255, g + offset));
                        b = Math.Max(0, Math.Min(255, b + offset));

                        data[x + (y * 8)] = Color.FromArgb((byte)r, (byte)g, (byte)b);
                    }
                }
                ledMatrix.Write(data);
            }
        }
Exemplo n.º 2
0
        public static void Run()
        {
            using (var magnetometer = new SenseHatMagnetometer())
                using (var ledMatrix = new SenseHatLedMatrixI2c())
                {
                    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);
                    }
                }
        }
Exemplo n.º 3
0
        public static void Run()
        {
            // another implementation which can be used is: SenseHatLedMatrixSysFs
            // I2C implementation does not require installing anything
            // SysFs implementation is faster and has arguably better colors
            using (var ledMatrix = new SenseHatLedMatrixI2c())
            {
                WriteDemo(ledMatrix);

                // Uncomment to see demo of SetPixel/Fill
                // SetPixelDemo(m);
            }
        }
 public static void Rainbars()
 {
     using (var ledMatrix = new SenseHatLedMatrixI2c())
     {
         while (true)
         {
             for (int i = 0; i < pixels.Length; i++)
             {
                 nextColor(ref pixels[i]);
             }
             ledMatrix.Write(pixels);
         }
     }
 }
        public static void sparkle()
        {
            SenseHatLedMatrixI2c ledMatrix = new SenseHatLedMatrixI2c();

            ledMatrix.Fill(Color.Purple);
            var rand = new Random();

            while (true)
            {
                var c = Color.FromArgb((byte)rand.Next(0, 255), (byte)rand.Next(0, 255), (byte)rand.Next(0, 255));
                var x = rand.Next(0, 8);
                var y = rand.Next(0, 8);
                ledMatrix.SetPixel(x, y, c);
                Thread.Sleep(50);
            }
        }
        public static void DoWork()
        {
            int cycle = 0;
            SenseHatLedMatrixI2c ledMatrix = new SenseHatLedMatrixI2c();

            Color[] data = new Color[64];

            while (true)
            {
                cycle++;
                for (int y = 0; y < 8; y++)
                {
                    currentColour = colours[(y + cycle) % 8];
                    for (int x = 0; x < 8; x++)
                    {
                        data[x + (y * 8)] = currentColour;
                    }
                }
                ledMatrix.Write(data);
                Thread.Sleep(100);
            }
        }