Пример #1
0
        public MeadowApp()
        {
            var bus = Device.CreateI2cBus();

            sensor = new Max44009(bus);

            while (true)
            {
                Thread.Sleep(1000);

                Console.WriteLine($"Lux: {sensor.GetIlluminance()}");
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            I2cConnectionSettings settings = new I2cConnectionSettings(1, Max44009.DefaultI2cAddress);
            I2cDevice             device   = I2cDevice.Create(settings);

            // integration time is 100ms
            using (Max44009 sensor = new Max44009(device, IntegrationTime.Time100))
            {
                while (true)
                {
                    // read illuminance
                    Console.WriteLine($"Illuminance: {sensor.Illuminance}Lux");
                    Console.WriteLine();

                    Thread.Sleep(1000);
                }
            }
        }
Пример #3
0
        protected override async Task RunContinuouslyAsync(IMqttClient client, TimeSpan delay, CancellationToken stoppingToken)
        {
            var max44009 = new Max44009("/dev/i2c-1", 0x4A);

            max44009.Reset();

            float oldLux         = 0;
            var   nextReportTime = DateTimeOffset.MinValue;

            while (!stoppingToken.IsCancellationRequested)
            {
                var lux = max44009.Read();
                var now = DateTimeOffset.UtcNow;
                if ((now > nextReportTime) || ShouldReport(oldLux, lux))
                {
                    await SendReading(client, lux, stoppingToken).ConfigureAwait(false);

                    oldLux         = lux;
                    nextReportTime = now.AddSeconds(60);
                }
                await Task.Delay(delay, stoppingToken).ConfigureAwait(false);
            }
        }
Пример #4
0
        private static async Task Main()
        {
            CancellationToken ct = default;

            Console.WriteLine("Hello Max44009!");
            try
            {
                var max44009 = new Max44009("/dev/i2c-1", 0x4A);
                max44009.Reset();

                while (!ct.IsCancellationRequested)
                {
                    var lux = max44009.Read();
                    Console.WriteLine(FormattableString.Invariant($"Lux = {lux}"));
                    await Task.Delay(1000, ct).ConfigureAwait(false);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                Console.WriteLine(FormattableString.Invariant($"Error: {ex}"));
            }
        }