private async Task <LightData> ReadLightData()
        {
            LightData ld = null;

            try
            {
                if (colorSensor == null)
                {
                    colorSensor = new TCS34725();
                    await colorSensor.Initialize();
                }

                //Read the approximate color from the sensor
                string colorRead = await colorSensor.getClosestColor();

                RgbData rgb = await colorSensor.getRgbData();

                float lux = TCS34725.getLuxSimple(rgb);
                ld              = new LightData();
                ld.Created      = DateTime.Now;
                ld.rgbData      = rgb;
                ld.Lux          = lux;
                ld.ColorTempinK = TCS34725.calculateColorTemperature(rgb);
                Debug.WriteLine("Current lux: " + lux);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(ld);
        }
        //public static float getLux(RgbData rgb)
        //{
        //    float illuminance;
        //    float ir;

        //    ir = (rgb.Red + rgb.Green + rgb.Blue)

        //    return illuminance;
        //}

        public static float calculateColorTemperature(RgbData rgb)
        {
            float X, Y, Z;      /* RGB to XYZ correlation      */
            float xc, yc;       /* Chromaticity co-ordinates   */
            float n;            /* McCamy's formula            */
            float cct;

            /* 1. Map RGB values to their XYZ counterparts.    */
            /* Based on 6500K fluorescent, 3000K fluorescent   */
            /* and 60W incandescent values for a wide range.   */
            /* Note: Y = Illuminance or lux                    */
            X = (-0.14282F * rgb.Red) + (1.54924F * rgb.Green) + (-0.95641F * rgb.Blue);
            Y = (-0.32466F * rgb.Red) + (1.57837F * rgb.Green) + (-0.73191F * rgb.Blue);
            Z = (-0.68202F * rgb.Red) + (0.77073F * rgb.Green) + (0.56332F * rgb.Blue);

            /* 2. Calculate the chromaticity co-ordinates      */
            xc = (X) / (X + Y + Z);
            yc = (Y) / (X + Y + Z);

            /* 3. Use McCamy's formula to determine the CCT    */
            n = (xc - 0.3320F) / (0.1858F - yc);

            /* Calculate the final CCT */
            cct = (float)((449.0F * Math.Pow(n, 3)) + (3525.0F * Math.Pow(n, 2)) + (6823.3F * n) + 5520.33F);

            /* Return the results in degrees Kelvin */
            return(cct);
        }
        //Method to find the approximate color
        public async Task <string> getClosestColor()
        {
            //Create an object to store the raw color data
            RgbData rgbData = await getRgbData();

            //Create a variable to store the closest color. Black by default.
            KnownColor closestColor = colorList[7];
            //Create a variable to store the minimum Euclidean distance between 2 colors
            double minDiff = double.MaxValue;

            //For every known color, check the Euclidean distance and store the minimum distance
            foreach (var color in colorList)
            {
                Color  colorValue = color.colorValue;
                double diff       = Math.Pow((colorValue.R - rgbData.Red), 2) +
                                    Math.Pow((colorValue.G - rgbData.Green), 2) +
                                    Math.Pow((colorValue.B - rgbData.Blue), 2);
                diff = (int)Math.Sqrt(diff);
                if (diff < minDiff)
                {
                    minDiff      = diff;
                    closestColor = color;
                }
            }
            //Write the approximate color to the debug console
            Debug.WriteLine("Approximate color: " + closestColor.colorName + " - "
                            + closestColor.colorValue.ToString());

            //Return the approximate color
            return(closestColor.colorName);
        }
        public static float  getLuxSimple(RgbData rgb)
        {
            float illuminance;

            /* This only uses RGB ... how can we integrate clear or calculate lux */
            /* based exclusively on clear since this might be more reliable?      */
            illuminance = (-0.32466F * rgb.Red) + (1.57837F * rgb.Green) + (-0.73191F * rgb.Blue);

            return(illuminance);
        }
        //Method to read the RGB data
        public async Task <RgbData> getRgbData()
        {
            //Create an object to store the raw color data
            RgbData rgbData = new RgbData();

            //First get the raw color data
            ColorData colorData = await getRawData();

            //Check if clear data is received
            if (colorData.Clear > 0)
            {
                //Find the RGB values from the raw data using the clear data as reference
                rgbData.Red   = (colorData.Red * 255 / colorData.Clear);
                rgbData.Blue  = (colorData.Blue * 255 / colorData.Clear);
                rgbData.Green = (colorData.Green * 255 / colorData.Clear);
            }
            //Write the RGB values to the debug console
            Debug.WriteLine("RGB Data - red: {0}, green: {1}, blue: {2}", rgbData.Red, rgbData.Green, rgbData.Blue);

            //Return the data
            return(rgbData);
        }
Esempio n. 6
0
        //public static float getLux(RgbData rgb)
        //{
        //    float illuminance;
        //    float ir;

        //    ir = (rgb.Red + rgb.Green + rgb.Blue)

        //    return illuminance;
        //}

        public static float calculateColorTemperature(RgbData rgb)
        {
            float X, Y, Z;      /* RGB to XYZ correlation      */
            float xc, yc;       /* Chromaticity co-ordinates   */
            float n;            /* McCamy's formula            */
            float cct;

            /* 1. Map RGB values to their XYZ counterparts.    */
            /* Based on 6500K fluorescent, 3000K fluorescent   */
            /* and 60W incandescent values for a wide range.   */
            /* Note: Y = Illuminance or lux                    */
            X = (-0.14282F * rgb.Red) + (1.54924F * rgb.Green) + (-0.95641F * rgb.Blue);
            Y = (-0.32466F * rgb.Red) + (1.57837F * rgb.Green) + (-0.73191F * rgb.Blue);
            Z = (-0.68202F * rgb.Red) + (0.77073F * rgb.Green) + (0.56332F * rgb.Blue);

            /* 2. Calculate the chromaticity co-ordinates      */
            xc = (X) / (X + Y + Z);
            yc = (Y) / (X + Y + Z);

            /* 3. Use McCamy's formula to determine the CCT    */
            n = (xc - 0.3320F) / (0.1858F - yc);

            /* Calculate the final CCT */
            cct = (float)((449.0F * Math.Pow(n, 3)) + (3525.0F * Math.Pow(n, 2)) + (6823.3F * n) + 5520.33F);

            /* Return the results in degrees Kelvin */
            return cct;
        }
Esempio n. 7
0
        public static float  getLuxSimple(RgbData rgb)
        {
                float illuminance;

                /* This only uses RGB ... how can we integrate clear or calculate lux */
                /* based exclusively on clear since this might be more reliable?      */
                illuminance = (-0.32466F * rgb.Red) + (1.57837F * rgb.Green) + (-0.73191F * rgb.Blue);

                return illuminance;
        }
Esempio n. 8
0
        //Method to read the RGB data
        public async Task<RgbData> getRgbData()
        {
            //Create an object to store the raw color data
            RgbData rgbData = new RgbData();

            //First get the raw color data
            ColorData colorData = await getRawData();
            //Check if clear data is received
            if (colorData.Clear > 0)
            {
                //Find the RGB values from the raw data using the clear data as reference
                rgbData.Red = (colorData.Red * 255 / colorData.Clear);
                rgbData.Blue = (colorData.Blue * 255 / colorData.Clear);
                rgbData.Green = (colorData.Green * 255 / colorData.Clear);
            }
            //Write the RGB values to the debug console
            Debug.WriteLine("RGB Data - red: {0}, green: {1}, blue: {2}", rgbData.Red, rgbData.Green, rgbData.Blue);

            //Return the data
            return rgbData;
        }