Exemple #1
0
        /// <summary>
        /// Checks if lightning, disturbance or noise has been detected. This method can be used when polling the sensor, that is : not in IRQ mode.
        /// <remarks>A separate event is thrown for each case.</remarks>
        /// </summary>
        /// <example> This sample shows how to use the Check() method
        /// <code language="C#">
        ///             // ThunderClick board is inserted in Socket 1
        ///             _thunder = new ThunderClick(Hardware.SocketOne);
        ///
        ///             // Subscribe to events
        ///             _thunder.LightningDetected += TH_LightningDetected;
        ///             _thunder.DisturbanceDetected += TH_DisturbanceDetected;
        ///             _thunder.NoiseDetected += TH_NoiseDetected;
        ///
        ///             // Poll the board for events every 5 sec
        ///             while (true)
        ///             {
        ///                 _thunder.Check();
        ///                 Thread.Sleep(5000);
        ///             }
        ///
        ///             Thread.Sleep(Timeout.Infinite);
        ///         }
        ///
        ///         static void TH_NoiseDetected(object sender, EventArgs e)
        ///         {
        ///             Debug.Print("Noise detected");
        ///         }
        ///
        ///         static void TH_DisturbanceDetected(object sender, EventArgs e)
        ///         {
        ///             Debug.Print("Disturbance detected");
        ///         }
        ///
        ///         static void TH_LightningDetected(object sender, MikroBusNet.Events.LightningEventArgs e)
        ///         {
        ///             Debug.Print(DateTime.Now+" : lightning detected at " + e.Distance + " km, energy : " + e.Energy);
        ///         }
        ///     }
        /// }
        /// </code>
        /// </example>
        public void Check()
        {
            var source = GetRegister(0x03) & 0x0F;

            switch (source)
            {
            case 8:         // Lightning
                var energyTmp = Energy();
                if (energyTmp > 0)
                {
                    LightningEventHandler lightEvent = LightningDetected;
                    lightEvent(this, new LightningEventArgs(Distance(), energyTmp));
                }
                break;

            case 4:         // Disturbance
                EventHandler disturbEvent = DisturbanceDetected;
                disturbEvent(this, null);
                break;

            case 1:             // Noise
                EventHandler noiseEvent = NoiseDetected;
                noiseEvent(this, null);
                break;
            }
        }
        private void OnLightningEvent(LightningDetectorSPI sender, LightningEventArgs e)
        {
            if (onLightningEvent == null)
                onLightningEvent = OnLightningEvent;

            if (Program.CheckAndInvoke(LightningDetected, onLightningEvent, sender, e))
            {
                LightningDetected(sender, e);

                if (onLightningEvent == null)
                    onLightningEvent = OnLightningEvent;
            }
        }