Example #1
0
        private static (int, int, bool) JoystickState(SenseHat sh)
        {
            sh.ReadJoystickState();

            int dx = 0;
            int dy = 0;

            if (sh.HoldingUp)
            {
                dy--; // y goes down
            }
            if (sh.HoldingDown)
            {
                dy++;
            }

            if (sh.HoldingLeft)
            {
                dx--;
            }

            if (sh.HoldingRight)
            {
                dx++;
            }

            return(dx, dy, sh.HoldingButton);
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            var display = SenseHat.OpenDisplay();

            display.Draw(GetDrawable(Resource.Drawable.xamarin2));
        }
Example #3
0
        private static void ReadAndShowTemperature(SenseHat senseHat, RgbColor foreground, RgbColor background)
        {
            var task = Task.Run(async() =>
            {
                var temparature = await senseHat.TemparatureSensor.ReadTemparature();
                await senseHat.LedMatrix.ShowMessage(temparature.AsDescription(), 0.2f, foreground, background);
            });

            Task.WaitAll(task);
        }
Example #4
0
        public SenseHat Create()
        {
            _devicePythonFileFactory.AssureInitialized();

            var ledMatrixScriptPath = _devicePythonFileFactory.CreateScriptFile(typeof(LedMatrix));
            var led = new LedMatrix(_pythonExecutor, ledMatrixScriptPath);

            var joystickScriptPath = _devicePythonFileFactory.CreateScriptFile(typeof(Joystick));
            var joystick           = new Joystick(_pythonExecutor, joystickScriptPath);

            var temparatureSensorScriptPath = _devicePythonFileFactory.CreateScriptFile(typeof(TemparatureSensor));
            var tempSensor = new TemparatureSensor(_pythonExecutor, temparatureSensorScriptPath);

            var senseHat = new SenseHat(led, joystick, tempSensor);

            return(senseHat);
        }
Example #5
0
        public static void Main(string[] args)
        {
            // Sample for each device separately
            // LedMatrix.Run();
            // Joystick.Run();
            // AccelerometerAndGyroscope.Run();
            // Magnetometer.Run();
            // TemperatureAndHumidity.Run();
            // PressureAndTemperature.Run();

            using (var sh = new SenseHat())
            {
                int n = 0;
                int x = 3, y = 3;

                while (true)
                {
                    Console.Clear();

                    (int dx, int dy, bool holding) = JoystickState(sh);

                    if (holding)
                    {
                        n++;
                    }

                    x = (x + 8 + dx) % 8;
                    y = (y + 8 + dy) % 8;

                    sh.Fill(n % 2 == 0 ? Color.DarkBlue : Color.DarkRed);
                    sh.SetPixel(x, y, Color.Yellow);

                    Console.WriteLine($"Temperature: Sensor1: {sh.Temperature} C   Sensor2: {sh.Temperature2} C");
                    Console.WriteLine($"Humidity: {sh.Humidity} %rH");
                    Console.WriteLine($"Pressure: {sh.Pressure} hPa");
                    Console.WriteLine($"Acceleration: {sh.Acceleration} g");
                    Console.WriteLine($"Angular rate: {sh.AngularRate} DPS");
                    Console.WriteLine($"Magnetic induction: {sh.MagneticInduction} gauss");

                    Thread.Sleep(1000);
                }
            }
        }
Example #6
0
        public MainPage()
        {
            this.InitializeComponent();

            _senseHat         = new SenseHat();
            _iotHubConnection = new IoTHubConnection();

            this.ActivateSenseHat();

            this.Loaded += (sender, e) =>
            {
                DispatcherTimer timer = new DispatcherTimer();

                timer.Tick += async(x, y) =>
                {
                    // Just to show temperature on device screen
                    var temperatureTelemetry = _senseHat.GetTemperature();
                    this.temperatureTextBlock.Text = "Temperature: " + temperatureTelemetry.Sense_hat_temperature.ToString();

                    // send temperature, time, pressure and humidity to cloud
                    await _iotHubConnection.SendEventAsync(JsonConvert.SerializeObject(temperatureTelemetry));


                    // Just to show humidity value on device screen
                    var humidityTelemetry = _senseHat.GetHumidity();
                    this.humidityTextBlock.Text = "Humidity: " + humidityTelemetry.Humidity.ToString();

                    // Just to show pressure value on device screen
                    var pressureTelemetry = _senseHat.GetPressure();
                    this.pressureTextBlock.Text = "Pressure: " + pressureTelemetry.Pressure.ToString();

                    _senseHat.ScreenControl();
                };

                timer.Interval = TimeSpan.FromSeconds(30);
                timer.Start();
            };
        }
Example #7
0
        public MainPage()
        {
            this.InitializeComponent();

            _senseHat         = new SenseHat();
            _iotHubConnection = new IoTHubConnection();

            this.ActivateSenseHat();

            this.Loaded += (sender, e) =>
            {
                DispatcherTimer timer = new DispatcherTimer();

                timer.Tick += async(x, y) =>
                {
                    var temperatureTelemetry = _senseHat.GetTemperature();
                    this.temperatureTextBlock.Text = "Temperature: " + temperatureTelemetry.Temperature.ToString();
                    await _iotHubConnection.SendEventAsync(JsonConvert.SerializeObject(temperatureTelemetry));
                };

                timer.Interval = TimeSpan.FromSeconds(3);
                timer.Start();
            };
        }
Example #8
0
        public static void Main(string[] args)
        {
            // Sample for each device separately
            // LedMatrix.Run();
            // Joystick.Run();
            // AccelerometerAndGyroscope.Run();
            // Magnetometer.Run();
            // TemperatureAndHumidity.Run();
            // PressureAndTemperature.Run();

            // set this to the current sea level pressure in the area for correct altitude readings
            var defaultSeaLevelPressure = Pressure.MeanSeaLevel;

            using (var sh = new SenseHat())
            {
                int n = 0;
                int x = 3, y = 3;

                while (true)
                {
                    Console.Clear();

                    (int dx, int dy, bool holding) = JoystickState(sh);

                    if (holding)
                    {
                        n++;
                    }

                    x = (x + 8 + dx) % 8;
                    y = (y + 8 + dy) % 8;

                    sh.Fill(n % 2 == 0 ? Color.DarkBlue : Color.DarkRed);
                    sh.SetPixel(x, y, Color.Yellow);

                    var tempValue  = sh.Temperature;
                    var temp2Value = sh.Temperature2;
                    var preValue   = sh.Pressure;
                    var humValue   = sh.Humidity;
                    var accValue   = sh.Acceleration;
                    var angValue   = sh.AngularRate;
                    var magValue   = sh.MagneticInduction;
                    var altValue   = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue);

                    Console.WriteLine($"Temperature Sensor 1: {tempValue.Celsius:0.#}\u00B0C");
                    Console.WriteLine($"Temperature Sensor 2: {temp2Value.Celsius:0.#}\u00B0C");
                    Console.WriteLine($"Pressure: {preValue:0.##}hPa");
                    Console.WriteLine($"Altitude: {altValue:0.##}m");
                    Console.WriteLine($"Acceleration: {sh.Acceleration}g");
                    Console.WriteLine($"Angular rate: {sh.AngularRate}DPS");
                    Console.WriteLine($"Magnetic induction: {sh.MagneticInduction}gauss");
                    Console.WriteLine($"Relative humidity: {humValue:0.#}%");

                    // WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
                    Console.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).Celsius:0.#}\u00B0C");
                    Console.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).Celsius:0.#}\u00B0C");

                    Thread.Sleep(1000);
                }
            }
        }