Example #1
0
 internal AverageConfig(DataAttributes attributes, byte samples, bool hpf, bool supportsHpf) : base(ID)
 {
     output           = attributes.length();
     input            = attributes.length();
     this.samples     = samples;
     nInputs          = (byte)attributes.sizes.Length;
     this.hpf         = hpf;
     this.supportsHpf = supportsHpf;
 }
Example #2
0
 internal DifferentialConfig(DataAttributes attributes, Differential mode, int delta) : base(ID)
 {
     input      = attributes.length();
     isSigned   = attributes.signed;
     this.mode  = mode;
     this.delta = delta;
 }
Example #3
0
 internal ThresholdConfig(DataAttributes attributes, Threshold mode, int boundary, short hysteresis) : base(ID)
 {
     input           = attributes.length();
     isSigned        = attributes.signed;
     this.mode       = mode;
     this.boundary   = boundary;
     this.hysteresis = hysteresis;
 }
Example #4
0
        internal virtual Tuple <DataTypeBase, DataTypeBase> transform(DataProcessorConfig config)
        {
            switch (config.id)
            {
            case DataProcessorConfig.BufferConfig.ID:
                return(Tuple.Create(
                           new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, new DataAttributes(new byte[] { }, 0, 0, false)) as DataTypeBase,
                           dataProcessorStateCopy(this, this.attributes)
                           ));

            case DataProcessorConfig.AccumulatorConfig.ID: {
                DataProcessorConfig.AccumulatorConfig casted = (DataProcessorConfig.AccumulatorConfig)config;
                DataAttributes attributes = new DataAttributes(new byte[] { casted.output }, 1, 0, !casted.counter && this.attributes.signed);

                return(Tuple.Create(
                           casted.counter ? new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, attributes) : dataProcessorCopy(this, attributes),
                           casted.counter ? new IntegralDataType(null, DATA_PROCESSOR, Util.setRead(DataProcessor.STATE), NO_ID, attributes) :
                           dataProcessorStateCopy(this, attributes)
                           ));
            }

            case DataProcessorConfig.AverageConfig.ID:
            case DataProcessorConfig.DelayConfig.ID:
            case DataProcessorConfig.TimeConfig.ID:
                return(Tuple.Create <DataTypeBase, DataTypeBase>(dataProcessorCopy(this, this.attributes.dataProcessorCopy()), null));

            case DataProcessorConfig.PassthroughConfig.ID:
                return(Tuple.Create(
                           dataProcessorCopy(this, this.attributes.dataProcessorCopy()),
                           new IntegralDataType(DATA_PROCESSOR, Util.setRead(DataProcessor.STATE), NO_ID, new DataAttributes(new byte[] { 2 }, 1, 0, false)) as DataTypeBase
                           ));

            case DataProcessorConfig.MathConfig.ID: {
                DataProcessorConfig.MathConfig casted = (DataProcessorConfig.MathConfig)config;
                DataTypeBase processor = null;
                switch (casted.op)
                {
                case Add:
                    processor = dataProcessorCopy(this, this.attributes.dataProcessorCopySize(4));
                    break;

                case Multiply:
                    processor = dataProcessorCopy(this, this.attributes.dataProcessorCopySize(Math.Abs(casted.rhs) < 1 ? this.attributes.sizes[0] : (byte)4));
                    break;

                case Divide:
                    processor = dataProcessorCopy(this, this.attributes.dataProcessorCopySize(Math.Abs(casted.rhs) < 1 ? (byte)4 : this.attributes.sizes[0]));
                    break;

                case Subtract:
                    processor = dataProcessorCopy(this, this.attributes.dataProcessorCopySigned(true));
                    break;

                case AbsValue:
                    processor = dataProcessorCopy(this, this.attributes.dataProcessorCopySigned(false));
                    break;

                case Modulus:
                    processor = dataProcessorCopy(this, this.attributes.dataProcessorCopy());
                    break;

                case Exponent:
                    processor = new ByteArrayDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, this.attributes.dataProcessorCopySize(4));
                    break;

                case LeftShift:
                    processor = new ByteArrayDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY,
                                                      this.attributes.dataProcessorCopySize((byte)Math.Min(this.attributes.sizes[0] + (casted.rhs / 8), 4)));
                    break;

                case RightShift:
                    processor = new ByteArrayDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY,
                                                      this.attributes.dataProcessorCopySize((byte)Math.Max(this.attributes.sizes[0] - (casted.rhs / 8), 1)));
                    break;

                case Sqrt:
                    processor = new ByteArrayDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, this.attributes.dataProcessorCopySigned(false));
                    break;

                case Constant:
                    DataAttributes attributes = new DataAttributes(new byte[] { 4 }, (byte)1, (byte)0, casted.rhs >= 0);
                    processor = new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, attributes);
                    break;
                }
                if (processor != null)
                {
                    return(Tuple.Create <DataTypeBase, DataTypeBase>(processor, null));
                }
                break;
            }

            case DataProcessorConfig.PulseConfig.ID: {
                DataProcessorConfig.PulseConfig casted = (DataProcessorConfig.PulseConfig)config;
                DataTypeBase processor = null;
                switch (casted.mode)
                {
                case Width:
                    processor = new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, new DataAttributes(new byte[] { 2 }, 1, 0, false));
                    break;

                case Area:
                    processor = dataProcessorCopy(this, attributes.dataProcessorCopySize(4));
                    break;

                case Peak:
                    processor = dataProcessorCopy(this, attributes.dataProcessorCopy());
                    break;

                case OnDetect:
                    processor = new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, new DataAttributes(new byte[] { 1 }, 1, 0, false));
                    break;
                }
                if (processor != null)
                {
                    return(Tuple.Create <DataTypeBase, DataTypeBase>(processor, null));
                }
                break;
            }

            case DataProcessorConfig.ComparisonConfig.ID: {
                DataTypeBase processor = null;
                if (config is DataProcessorConfig.SingleValueComparisonConfig)
                {
                    processor = dataProcessorCopy(this, attributes.dataProcessorCopy());
                }
                else if (config is DataProcessorConfig.MultiValueComparisonConfig)
                {
                    DataProcessorConfig.MultiValueComparisonConfig casted = (DataProcessorConfig.MultiValueComparisonConfig)config;
                    if (casted.mode == ComparisonOutput.PassFail || casted.mode == ComparisonOutput.Zone)
                    {
                        processor = new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, new DataAttributes(new byte[] { 1 }, 1, 0, false));
                    }
                    else
                    {
                        processor = dataProcessorCopy(this, attributes.dataProcessorCopy());
                    }
                }
                if (processor != null)
                {
                    return(Tuple.Create <DataTypeBase, DataTypeBase>(processor, null));
                }
                break;
            }

            case DataProcessorConfig.ThresholdConfig.ID: {
                DataProcessorConfig.ThresholdConfig casted = (DataProcessorConfig.ThresholdConfig)config;
                switch (casted.mode)
                {
                case Threshold.Absolute:
                    return(Tuple.Create <DataTypeBase, DataTypeBase>(dataProcessorCopy(this, attributes.dataProcessorCopy()), null));

                case Threshold.Binary:
                    return(Tuple.Create <DataTypeBase, DataTypeBase>(new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY,
                                                                                          new DataAttributes(new byte[] { 1 }, 1, 0, true)), null));
                }
                break;
            }

            case DataProcessorConfig.DifferentialConfig.ID: {
                DataProcessorConfig.DifferentialConfig casted = (DataProcessorConfig.DifferentialConfig)config;
                switch (casted.mode)
                {
                case Differential.Absolute:
                    return(Tuple.Create <DataTypeBase, DataTypeBase>(dataProcessorCopy(this, attributes.dataProcessorCopy()), null));

                case Differential.Differential:
                    throw new InvalidOperationException("Differential processor in 'difference' mode must be handled by subclasses");

                case Differential.Binary:
                    return(Tuple.Create <DataTypeBase, DataTypeBase>(new IntegralDataType(this, DATA_PROCESSOR, DataProcessor.NOTIFY, new DataAttributes(new byte[] { 1 }, 1, 0, true)), null));
                }
                break;
            }

            case DataProcessorConfig.PackerConfig.ID: {
                DataProcessorConfig.PackerConfig casted = (DataProcessorConfig.PackerConfig)config;
                return(Tuple.Create <DataTypeBase, DataTypeBase>(dataProcessorCopy(this, attributes.dataProcessorCopyCopies(casted.count)), null));
            }

            case DataProcessorConfig.AccounterConfig.ID: {
                DataProcessorConfig.AccounterConfig casted = (DataProcessorConfig.AccounterConfig)config;
                return(Tuple.Create <DataTypeBase, DataTypeBase>(dataProcessorCopy(this, new DataAttributes(new byte[] { casted.length, attributes.length() }, 1, 0, attributes.signed)), null));
            }
            }
            throw new InvalidOperationException("Unable to determine the DataTypeBase object for config: " + Util.arrayToHexString(config.Build()));
        }
Example #5
0
        internal string CreateIdentifier(IModuleBoardBridge bridge)
        {
            string myIdentifier = null;

            switch ((Module)eventConfig[0])
            {
            case SWITCH:
                myIdentifier = "switch";
                break;

            case ACCELEROMETER: {
                var module = bridge.GetModule <IAccelerometer>();
                if (module is AccelerometerMma8452q)
                {
                    myIdentifier = AccelerometerMma8452q.createIdentifier(this);
                }
                else if (module is AccelerometerBmi160)
                {
                    myIdentifier = AccelerometerBmi160.createIdentifier(this);
                }
                else if (module is AccelerometerBma255)
                {
                    myIdentifier = AccelerometerBosch.createIdentifier(this);
                }
                else
                {
                    myIdentifier = null;
                }
                break;
            }

            case TEMPERATURE:
                myIdentifier = string.Format("temperature[{0}]", eventConfig[2]);
                break;

            case GPIO:
                myIdentifier = Gpio.createIdentifier(this);
                break;

            case DATA_PROCESSOR:
                myIdentifier = DataProcessor.createIdentifier(this, bridge.GetModule <IDataProcessor>() as DataProcessor, bridge.getFirmware());
                break;

            case SERIAL_PASSTHROUGH:
                myIdentifier = SerialPassthrough.createIdentifier(this);
                break;

            case SETTINGS:
                myIdentifier = Settings.createIdentifier(this);
                break;

            case BAROMETER:
                myIdentifier = BarometerBosch.createIdentifier(this);
                break;

            case GYRO:
                myIdentifier = attributes.length() > 2 ? "angular-velocity" : string.Format("angular-velocity[{0}]", (attributes.offset >> 1));
                break;

            case AMBIENT_LIGHT:
                myIdentifier = "illuminance";
                break;

            case MAGNETOMETER:
                myIdentifier = attributes.length() > 2 ? "magnetic-field" : string.Format("magnetic-field[{0}]", (attributes.offset >> 1));
                break;

            case HUMIDITY:
                myIdentifier = "relative-humidity";
                break;

            case COLOR_DETECTOR:
                myIdentifier = attributes.length() > 2 ? "color" : string.Format("color[{0}]", (attributes.offset >> 1));
                break;

            case PROXIMITY:
                myIdentifier = "proximity";
                break;

            case SENSOR_FUSION:
                myIdentifier = SensorFusionBosch.createIdentifier(this);
                break;
            }

            if (myIdentifier == null)
            {
                throw new InvalidOperationException("Cannot create identifier string for data type: " + Util.arrayToHexString(eventConfig));
            }

            return((input != null && !input.eventConfig.SequenceEqual(eventConfig) ? input.CreateIdentifier(bridge) + ":" : "") + myIdentifier);
        }