Esempio n. 1
0
        /// <summary>
        /// Turns off device
        /// </summary>
        /// <returns>Returns the status of device after operation</returns>
        public async Task <StatusEnum> TurnOff(int SlaveAddress)
        {
            var Response = await I2CHelper.WriteRead(SlaveAddress, I2CHelper.Mode.Mode2, (byte)Pin, 0);

            Status = StatusEnum.Off;
            return(Status);
        }
        public APDS9301_LightSensor(I2cDevice ledDevice, TimeSpan updateInterval)
        {
            if (updateInterval.TotalMilliseconds > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval), $"Update Interval must be less than {ushort.MaxValue} milliseconds");
            }
            else if (updateInterval.TotalMilliseconds <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval), $"Update Interval must be greater than 0 but it's {updateInterval.TotalMilliseconds} milliseconds");
            }

            _updateInterval = (ushort)updateInterval.TotalMilliseconds;

            _ledDevice = ledDevice;
            _i2cHelper = new I2CHelper(_ledDevice);

            TurnOn();
            SensorGain = Gain.Low;
            Timing     = IntegrationTiming.Ms13;
            DisableInterrupt();

            // Wait for the sensor to prepare the first reading (402ms after power on).
            Thread.Sleep(410);
            _updateThread = new Thread(() =>
            {
                while (true)
                {
                    UpdateLumosity();
                    Thread.Sleep(_updateInterval);
                }
            });
            _updateThread.Start();
        }
Esempio n. 3
0
        /// <summary>
        /// Turns on device
        /// </summary>
        /// <returns>Returns the status of device after operation</returns>
        public async Task <StatusEnum> TurnOn()
        {
            var Response = await I2CHelper.WriteRead(I2C_Slave_Address, I2CHelper.Mode.Mode2, (byte)Pin, 1);

            Status = StatusEnum.On;
            return(Status);
        }
Esempio n. 4
0
        public async Task <byte> GetState()
        {
            // Return 0 if device does not have valid I2C_Slave_Address
            if (I2C_Slave_Address == 0)
            {
                return(0);
            }

            // Create mode 1 request and return appropriate byte from response
            var Response = I2CHelper.WriteRead(I2C_Slave_Address, I2CHelper.Mode.Mode1).Result;

            switch (Pin)
            {
            case PinsEnum.D0:
                return(Response[0]);

            case PinsEnum.D1:
                return(Response[1]);

            case PinsEnum.D3:
                return(Response[2]);

            case PinsEnum.D4:
                return(Response[3]);

            case PinsEnum.D5:
                return(Response[4]);

            case PinsEnum.D6:
                return(Response[5]);

            case PinsEnum.D7:
                return(Response[6]);

            case PinsEnum.D8:
                return(Response[7]);

            case PinsEnum.D9:
                return(Response[8]);

            case PinsEnum.D10:
                return(Response[9]);

            case PinsEnum.D11:
                return(Response[10]);

            case PinsEnum.D12:
                return(Response[11]);

            case PinsEnum.A2:
                return(Response[12]);

            case PinsEnum.A3:
                return(Response[13]);

            default:
                return(0);
            }
        }
        private async Task <ArduinoI2CResponse> GetTemperatureFromArduino(int adress, I2CMessageFrame message)
        {
            try
            {
                byte[] mahByteArray = I2CHelper.PrepareMessageToSend(message);
                byte[] responseData = await I2CHelper.WriteRead(adress, mahByteArray);

                // ArduinoI2CResponse ph = (ArduinoI2CResponse) GetObjectFromBytes(aaa, typeof(ArduinoI2CResponse));
                return(I2CHelper.VerifyIncomingData(responseData));
            }
            catch (Exception e)
            {
                return(new ArduinoI2CResponse {
                    Status = (short)I2CResponseStatus.Error
                });
            }
        }
Esempio n. 6
0
            /// <summary>
            /// Collects sensor data at interval of 1 second. To get data of specific room, do not call this function instead update value to property I2C_Slave_Address and this function will collect data from that slave.
            /// </summary>
            public static void CollectData()
            {
                if (AlreadyRunning == false)
                {
                    AlreadyRunning = true;

                    Sensors = new SensorSturct();
                    Sensors.AmbientLight = new AmbientLight();
                    Sensors.PassiveIR    = new PassiveIR();
                    Sensors.Temperature  = new Temperature();

                    Task Task_CollectSensorData = new Task(async() =>
                    {
                        while (true)
                        {
                            var Response = await I2CHelper.WriteRead(I2C_Slave_Address, I2CHelper.Mode.Mode0);

                            // Update Ambient Light
                            Sensors.AmbientLight.RawData = (short)Response[0];

                            //Update PIR State
                            Sensors.PassiveIR.HumanDetected = (((byte)Response[1]) == 1) ? true : false;

                            // Update Temperature
                            Sensors.Temperature.Celsius  = (byte)Response[3];
                            Sensors.Temperature.Celsius *= (((byte)Response[2]) == 0) ? -1 : 1; // Update Temperature Sign. Refer mode 0 for details.

                            Debug.WriteLine(Sensors.AmbientLight.RawData);
                            Debug.WriteLine(Sensors.PassiveIR.HumanDetected.ToString());
                            Debug.WriteLine(Sensors.Temperature.Celsius);
                            Debug.WriteLine("=======================");

                            await Task.Delay(1000);
                        }
                    });

                    Task_CollectSensorData.Start();
                    Task_CollectSensorData.Wait();
                }
            }
        public async Task <SensorTemperatureValues> ReadTemperatureFromArduino(Sensors sensor)
        {
            try
            {
                //   LoggerFactory.LogInfo("Wysyłanie zapytanie o temperatur");
                SensorTemperatureValues val     = new SensorTemperatureValues();
                I2CMessageFrame         message = new I2CMessageFrame()
                {
                    Operation  = I2COperation.Temp,
                    TempSensor = (SensorTypeEnum)sensor.SensorType,
                    Pin        = sensor.Pin != null?short.Parse(sensor.Pin) : (short)0,
                                     SensorAddress = string.IsNullOrEmpty(sensor.Address) ? new byte[8] : I2CHelper.StringToByteArray(sensor.Address)
                };

                LoggerFactory.LogInfo("Wysyłanie zapytanie o temperature", "Sensor:", new { sensor });
                ArduinoI2CResponse response = await GetTemperatureFromArduino(Convert.ToInt32(sensor.DataBusEx.Address, 16), message);

                if (response.Status == (short)I2CResponseStatus.Ok)
                {
                    val.Value    = (double)response.Temperature;
                    val.Date     = DateTime.Now;
                    val.SensorId = sensor.Id;
                }
                else
                {
                    LoggerFactory.LogError("Błąd, status: " + response.Status.ToString());
                    Debug.WriteLine("Błąd, status: " + response.Status.ToString());
                }
                return(val);
            }
            catch (Exception ex)
            {
                LoggerFactory.LogExceptionStack(ex);
                throw;
            }
        }