Beispiel #1
0
        // Explorations into I2C usage
        public static void Sketch()
        {
            // Create the config object with device address and clock speed
            I2CDevice.Configuration c = new I2CDevice.Configuration(0x68, 100);
            I2CDevice d = new I2CDevice(c);

            byte[] read_byte = new byte[1];

            byte test_value = 0x00;

            while (test_value < 0xff)
            {
                // Read the register
                I2CDevice.I2CWriteTransaction w = I2CDevice.CreateWriteTransaction(new byte[] { 0x14 });
                I2CDevice.I2CReadTransaction r = I2CDevice.CreateReadTransaction(read_byte);
                int bytes_exchanged = d.Execute(new I2CDevice.I2CTransaction[] { w, r }, 100);

                // Write to the register
                w = I2CDevice.CreateWriteTransaction(new byte[] { 0x14, test_value });
                bytes_exchanged = d.Execute(new I2CDevice.I2CTransaction[] { w }, 100);

                foreach (byte b in read_byte)
                {
                    Debug.Print(b.ToString());
                }
                test_value++;

                Thread.Sleep(3000);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Example sensor constructor
        /// </summary>
        /// <param name="address">I2C device address of the example sensor</param>
        public DS1621(byte address)
        {
            Address = address;
            _slaveConfig = new I2CDevice.Configuration(address, ClockRateKHz);

            I2CBus.GetInstance().Write(_slaveConfig, new byte[] { CONFIG_CMD, 0x01 }, TransactionTimeout);
        }
Beispiel #3
0
        public TSL2561(byte I2CAddress, int ClockinKHz)
        {
            I2CConfig = new I2CDevice.Configuration(I2CAddress, ClockinKHz);
              I2C = new I2CDevice(I2CConfig);

              // read the ID register.
              var Actions = new I2CDevice.I2CTransaction[2];
              byte[] rx = new byte[1];
              Actions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x0a });
              Actions[1] = I2CDevice.CreateReadTransaction(rx);
              if (I2C.Execute(Actions, 1000) == 0)
              {
            Debug.Print("Read ID Register failed");
            // exit or something
              }
              else
              {
            Debug.Print("ID value: " + rx[0].ToString());
              }
              // 4 msb must be 0001 for a TSL2561
              if ((rx[0] & 0xf0) != 0xa0)
              {
            // exit or something
              }

              setGain(0x10);
              Thread.Sleep(5);   // Mandatory after each Write transaction !!!
        }
Beispiel #4
0
        void run()
        {
            values[0] = values[1] = values[2] = -1;
            oldValues[0] = oldValues[1] = oldValues[2] = -1;

            I2CDevice.Configuration configuration = new I2CDevice.Configuration(deviceAddress, 400);
            I2CDevice adxl345 = new I2CDevice(configuration);
            writeCommand(adxl345, 0x2d, 0);
            writeCommand(adxl345, 0x2d, 16);
            writeCommand(adxl345, 0x2d, 8);
            // Set the g range to 2g - typical output: 17, -32, 257 (scale = 3.9 => 66 mg, -124 mg, 1002 mg)
            writeCommand(adxl345, 0x31, 0);
            // Set the g range to 16g - typical output: 2, -4, 31 (scale = 31.2 => 62.4 mg, -124.8 mg, 967 mg)
            // writeCommand(adxl345, 0x31, 3);
            while (true)
            {
                readValues(adxl345, values);
                if ((changed(values[0], oldValues[0])) || (changed(values[1], oldValues[1])) || (changed(values[2], oldValues[2])))
                {
                    //Debug.Print("Values: " + values[0] + ", " + values[1] + ", " + values[2]+"\n");
                    reporter.report(values[0] + "," + values[1] + "," + values[2]+"\n\r");
                }
                oldValues[0] = values[0];
                oldValues[1] = values[1];
                oldValues[2] = values[2];
            }
        }
Beispiel #5
0
 /// <summary>
 /// Create a new abstract I2C device.
 /// </summary>
 /// <param name="Address">I2C address.</param>
 /// <param name="ClockRateKhz">I2C clockrate.</param>
 public I2CPlug(Byte    Address,
                UInt32  ClockRateKhz = DefaultClockRate)
 {
     this._Address    = Address;
     this.I2C_Config  = new I2CDevice.Configuration(this.Address, (Int32) ClockRateKhz);
     this.I2C_Device  = new I2CDevice(this.I2C_Config);
 }
Beispiel #6
0
        public Accelerometer(I2CDevice bus, ushort deviceAddress = 0x53, int clockRate = 400)
        {
            this.bus = bus;
            configuration = new I2CDevice.Configuration(deviceAddress, clockRate);

            ConfigureDevice();
        }
Beispiel #7
0
        public static void Main()
        {
            //our 10 bit address
            const ushort address10Bit = 0x1001; //binary 1000000001 = 129
            const byte addressMask = 0x78; //reserved address mask 011110XX for 10 bit addressing

            //first MSB part of address
            ushort address1 = addressMask | (address10Bit >> 8); //is 7A and contains the two MSB of the 10 bit address
            I2CDevice.Configuration config = new I2CDevice.Configuration(address1, 100);
            I2CDevice device = new I2CDevice(config);

            //second LSB part of address
            byte address2 = (byte)(address10Bit & 0xFF); //the other 8 bits (LSB)
            byte[] address2OutBuffer = new byte[] { address2 };
            I2CDevice.I2CWriteTransaction addressWriteTransaction = device.CreateWriteTransaction(address2OutBuffer);

            //prepare buffer to write data
            byte[] outBuffer = new byte[] { 0xAA };
            I2CDevice.I2CWriteTransaction writeTransaction = device.CreateWriteTransaction(outBuffer);

            //prepare buffer to read data
            byte[] inBuffer = new byte[4];
            I2CDevice.I2CReadTransaction readTransaction = device.CreateReadTransaction(inBuffer);

            //execute transactions
            I2CDevice.I2CTransaction[] transactions =
                new I2CDevice.I2CTransaction[] { addressWriteTransaction, writeTransaction, readTransaction };
            device.Execute(transactions, 100);
        }
Beispiel #8
0
        private static void test1602()
        {
            //I2CScanner.ScanAddresses(0, 120);
            //address 39
            I2CDevice.Configuration conf = new I2CDevice.Configuration(39, 100);

            LcdCharacter_I2C lcd = new LcdCharacter_I2C(new I2CDevice(conf),4,20);
            lcd.Init();

            lcd.WriteText("TEST ZAPISU delsi".ToCharArray());
            lcd.SetCursorDirection(false);
            lcd.SetCursorPosition(1, 3);
            Thread.Sleep(5000);
            //lcd.MoveText(true);
            Thread.Sleep(5000);
            lcd.SetupDisplay(true, true, false);

            lcd.SetCursorPosition(0, 19);
            lcd.WriteChar('E');
            lcd.SetCursorPosition(1, 19);
            lcd.WriteChar('E');
            lcd.SetCursorPosition(2, 19);
            lcd.WriteChar('E');
            lcd.SetCursorPosition(3, 19);
            lcd.WriteChar('E');

            //lcd.ResetDisplay();
            //lcd.WriteChar('A');
        }
Beispiel #9
0
        public static void Sketch01()
        {
            //create I2C object
            //note that the netmf i2cdevice configuration requires a 7-bit address! It set the 8th R/W bit automatically.
            I2CDevice.Configuration con =
                new I2CDevice.Configuration(0x68, 100);
            I2CDevice MyI2C = new I2CDevice(con);

            // Create transactions
            // We need 2 in this example, we are reading from the device
            // First transaction is writing the "read command"
            // Second transaction is reading the data
            I2CDevice.I2CTransaction[] xActions =
                new I2CDevice.I2CTransaction[2];

            // create write buffer (we need one byte)
            byte[] RegisterNum = new byte[1] { 0x14 };
            xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
            // create read buffer to read the register
            byte[] RegisterValue = new byte[1];
            xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);

            // Now we access the I2C bus using a timeout of one second
            // if the execute command returns zero, the transaction failed (this
            // is a good check to make sure that you are communicating with the device correctly
            // and don’t have a wiring issue or other problem with the I2C device)
            if (MyI2C.Execute(xActions, 1000) == 0)
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
                Debug.Print("Register value: " + RegisterValue[0].ToString());
            }
        }
        /// <summary>
        /// Turns on the Oscillator. Turns on the Display. Turns off Blinking. Sets Brightness to full.
        /// </summary>
        public void Init()
        {
            Config = new I2CDevice.Configuration(HT16K33_ADRESS, HT16K33_CLKRATE);
            Matrix = new I2CDevice(Config);

            byte[] write = new byte[1];
            write[0] = HT16K33_OSC_ON; // IC Oscillator ON

            byte[] write2 = new byte[1];
            write2[0] = HT16K33_DISPLAY_ON; // Display ON

            I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[1];
            i2cTx[0] = I2CDevice.CreateWriteTransaction(write);

            I2CDevice.I2CTransaction[] i2cTx2 = new I2CDevice.I2CTransaction[1];
            i2cTx2[0] = I2CDevice.CreateWriteTransaction(write2);

            Matrix.Execute(i2cTx, Timeout);
            Matrix.Execute(i2cTx2, Timeout);

            // initialize DisplayBuffer
            for (int i = 0; i < 8; i++)
            {
                DisplayBuffer[i] = 0x00;
            }
        }
Beispiel #11
0
        void Connect ()
        {
            if (i2cDevice != null)
                return;

            this.i2cConfig = new I2CDevice.Configuration (this.Address, clockRateKhz);
            this.i2cDevice = new I2CDevice (this.i2cConfig);
        }
Beispiel #12
0
        public BaseI2CDevice(byte address = 0, int transactionTimeout = 1000, int clockRate = 100)
        {
            this.Address = address;
            this.TransactionTimeout = transactionTimeout;
            this.ClockRate = clockRate;

            config = new I2CDevice.Configuration(Address, ClockRate);
        }
Beispiel #13
0
        public BMP085_BaroSensor(byte address)
        {
            // initialize i2c device.
            Address = address;
            _slaveConfig = new I2CDevice.Configuration(address, ClockRateKHz);

            // Pull calbration data from sensor and store in variables.
            GetInitialCalibration();
        }
Beispiel #14
0
 /// <summary>
 /// Constructor - Sets up configuration and starts thread
 /// </summary>
 /// <param name="address">I2C Address</param>
 /// <param name="clockRateKhz">Clockrate</param>
 public LM75Driver(ushort address, int clockRateKhz)
 {
     //Setup configuration
     config = new I2CDevice.Configuration(address, clockRateKhz);
     // Initial, not being used, but then thread wont be null used throughout to check status.
     thread = new Thread(Pull);
     //Start driver
     Start();
 }
Beispiel #15
0
 public I2CDevice2(I2CDevice.Configuration config)
 {
     // In the constructor a
     if (config == null)
         throw new ArgumentNullException();
     this.Config = config;
     if (singletonDevice == null)
         singletonDevice = new I2CDevice(config);
     instanceCount++;
 }
Beispiel #16
0
        public TWIDisplay(ushort address)
        {
            I2CDevice.Configuration config = new I2CDevice.Configuration(address, 100);
            m_display = new I2CDevice(config);

            m_data = new byte[4];
            m_dots = 0;

            setRotateMode();
        }
Beispiel #17
0
        public BlinkMArray()
        {
            _i2cDevice = new I2CDevice(null);
            _blinkM_D = new I2CDevice.Configuration(0x0D, BlinkMCommand.DefaultClockRateKhz);
            _blinkM_E = new I2CDevice.Configuration(0x0E, BlinkMCommand.DefaultClockRateKhz);
            _blinkM_F = new I2CDevice.Configuration(0x0F, BlinkMCommand.DefaultClockRateKhz);

            InitBlinkM(_i2cDevice, _blinkM_D);
            InitBlinkM(_i2cDevice, _blinkM_E);
            InitBlinkM(_i2cDevice, _blinkM_F);
        }
Beispiel #18
0
        public NativeI2CBus(Socket socket, ushort address, int clockRateKhz, Module module)
        {
            if (_device == null)
            {
                socket.ReservePin(Socket.Pin.Eight, module);
                socket.ReservePin(Socket.Pin.Nine, module);

                _device = new I2CDevice(new I2CDevice.Configuration(0, 50));
            }

            _configuration = new I2CDevice.Configuration(address, clockRateKhz);
        }
        public BMP085(byte address, DeviceMode deviceMode)
        {
            Address = address;
            _slaveConfig = new I2CDevice.Configuration(address, ClockRateKHz);
            _oversamplingSetting = (byte)deviceMode;

            // Get calibration data that will be used for future measurement taking.
            GetCalibrationData();

            // Take initial measurements.
            TakeMeasurements();

        }
Beispiel #20
0
 public I2CDevice2(I2CDevice.Configuration config)
 {
     // In the constructor a
     if (config == null)
     {
         throw new ArgumentNullException();
     }
     this.Config = config;
     if (singletonDevice == null)
     {
         singletonDevice = new I2CDevice(config);
     }
     instanceCount++;
 }
        public ECO2Result ReadCO2(ref int co2Data)
        {
            co2Data = 0;
              ECO2Result result = ECO2Result.UnknownResult;
              I2CDevice.Configuration slaveConfig = new I2CDevice.Configuration(0x7F, 10);

              byte[] dataWrite = new byte[4] { 0x22, 0x00, 0x08, 0x2A };
              int transferred = Write(slaveConfig, dataWrite, 5000);

              if (transferred > 0)
              {
              Thread.Sleep(50);

              byte[] dataRead = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
              transferred = Read(slaveConfig, dataRead, 5000);

              if (transferred > 0)
              {
                  if ((dataRead[0] & 0x01) == 1)
                  {
                      co2Data |= dataRead[1] & 0xFF;
                      co2Data = co2Data << 8;
                      co2Data |= dataRead[2] & 0xFF;

                      if (dataRead[3] == CheckSum(dataRead, 3))
                      {
                          result = ECO2Result.ValidResult;
                      }
                      else
                      {
                          result = ECO2Result.ChecksumError;
                      }
                  }
                  else
                  {
                      result = ECO2Result.ReadIncomplete;
                  }
              }
              else
              {
                  result = ECO2Result.NoReadDataTransfered;
              }
              }
              else
              {
              result = ECO2Result.NoWriteDataTransfered;
              }

              return result;
        }
Beispiel #22
0
        protected override ArrayList GetOnlineModules()
        {
            ArrayList result = new ArrayList();

            for (byte address = 1; address <= 127; address++)
            {
                byte type = (byte)BusModuleType.Unknown;

                var config = new I2CDevice.Configuration(address, BusConfigurationI2C.ClockRate); // config for I2C-module with "address"
                if (busConfig.Bus.TryGetRegister(config, BusConfigurationI2C.Timeout, BusModuleAPI.CmdGetType, out type)) // query module
                    result.Add(new BusModule(this, new byte[] { address }, (BusModuleType)type));
            }

            return result;
        }
Beispiel #23
0
        /// <summary>
        /// Initializes a new I²C device
        /// </summary>
        /// <param name="Address">The address of the I²C device</param>
        /// <param name="ClockRateKhz">The speed in Khz of the I²C device</param>
        public MultiI2C(ushort Address, int ClockRateKhz = 100)
        {
            // Sets the configuration in a local value
            this._Configuration = new I2CDevice.Configuration(Address, ClockRateKhz);

            // Sets the default timeout
            this.Timeout = 100;

            // If no I2C Device exists yet, we create it's first instance
            if (_I2CDevice == null)
            {
                // Creates the SPI Device
                _I2CDevice = new I2CDevice(this._Configuration);
            }
        }
Beispiel #24
0
        protected override void ScanModules(out ArrayList modulesAdded, out ArrayList modulesRemoved)
        {
            modulesAdded = new ArrayList();
            modulesRemoved = new ArrayList();

            for (byte address = 1; address <= 127; address++)
            {
                byte type = (byte)BusModuleType.Unknown;

                var config = new I2CDevice.Configuration(address, BusConfigurationI2C.ClockRate); // config for I2C-module with "address"
                if (busConfig.Bus.TryGetRegister(config, BusConfigurationI2C.Timeout, BusModuleAPI.CmdGetType, out type)) // query module
                {
                    // module with this address is online;
                    // check if it's already registered in BusModules:

                    BusModule busModule = this[new byte[] { address }];

                    if (busModule == null) // module with this address isn't registered
                    {
                        busModule = new BusModule(this, new byte[] { address }, (BusModuleType)type);

                        // query module control lines count with updating lines states:
                        //busModule.QueryControlLines(true);

                        // register this module in BusModules:
                        modulesAdded.Add(busModule);
                        BusModules.Add(busModule);
                    }
                    else // module with this address is already registered
                    {
                        // updated when added;
                    }
                }
                else
                {
                    // module with this address is offline;
                    // check if it's already registered in BusModules:

                    BusModule busModule = this[new byte[] { address }];

                    if (busModule != null) // offline module
                    {
                        modulesRemoved.Add(busModule);
                        BusModules.Remove(busModule);
                    }
                }
            }
        }
        public BMP085(byte address, DeviceMode deviceMode)
        {
            Address = address;
            _slaveConfig = new I2CDevice.Configuration(address, ClockRateKHz);
            _oversamplingSetting = (byte)deviceMode;

            // Get calibration data that will be used for future measurement taking.
            GetCalibrationData();

            // Take initial measurements.
            TakeMeasurements();

            // Take new measurements every 30 seconds.
            //modificacion mia, en vez de tomar las mediciones aca, hago al metodo publico y lo llamo yo
            //_sensorTimer = new Timer(TakeMeasurements, null, 200, 30000);
        }
        /// <summary>
        /// Ensure to instantiate the MicroMaestro with the Robot given to the student and the device num (probably 13 or 14)
        /// This will take care of connecting the MicroMaestro to the robot's system
        /// </summary>
        /// <param name="robo"> The robot this Simple is connected to. </param>
        /// <param name="deviceNum"> The device number of this Simple. </param>
        public I2CMotorController(Robot robo, ushort deviceAdd)
        {
            //attach to robot, allowing itself to be updated by the Robot thread when called
            robot = robo;
            robot.actuators.Add(this);
            deviceAddress = deviceAdd;

            //initialize the motor as not turning
            canMove = true;
            motorSpeed = 0;
            motorBrake = 0;
            //create I2C Device object representing both devices on our bus
            conDeviceA = new I2CDevice.Configuration(deviceAddress, 100);

            //create I2C Bus object using one of the devices on the bus
            //I2CA = new I2CDevice(conDeviceA);
        }
Beispiel #27
0
        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <remarks>This automatically checks that the socket supports Type I, and reserves the SDA and SCL pins.
        /// An exception will be thrown if there is a problem with these checks.</remarks>
        /// <param name="address">The address for the I2C device.</param>
        /// <param name="clockRateKhz">The clock rate, in kHz, used when communicating with the I2C device.</param>
        /// <param name="socket">The socket for this I2C device interface.</param>
        /// <param name="module">The module using this I2C interface, which can be null if unspecified.</param>
        public I2CBus(Socket socket, ushort address, int clockRateKhz, Module module)
        {
            socket.EnsureTypeIsSupported('I', module);
            
            lock (I2CLock)
            {
                if (device == null)
                {
                    socket.ReservePin(Socket.Pin.Eight, module);
                    socket.ReservePin(Socket.Pin.Nine, module);
                    
                    device = new I2CDevice(new I2CDevice.Configuration(0, 50));
                }

                this.configuration = new I2CDevice.Configuration(address, clockRateKhz);
            }
        }
Beispiel #28
0
        public static void Main()
        {
            OutputPort Led = new OutputPort(Pins.ONBOARD_LED, false);

            byte[] Addr=new byte[2];
            Addr[0]=0x00;
            Addr[1]=0x01;

            byte[] TxBuff = new byte[4];
            TxBuff[0] = (byte)'1';
            TxBuff[1] = (byte)'2';
            TxBuff[2] = (byte)'3';
            TxBuff[3] = (byte)'4';

            byte[] RxBuff= new byte[4];

            I2CDevice.Configuration I2C_Configuration = new I2CDevice.Configuration(0x50, 400);
            I2CDevice I2C1 = new I2CDevice(I2C_Configuration);

            I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(Addr),
                I2CDevice.CreateWriteTransaction(TxBuff)
            };

            I2CDevice.I2CTransaction[] ReadTran = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(Addr),
                I2CDevice.CreateReadTransaction(RxBuff)
            };

            while(true)
            {
                Led.Write(true);
                I2C1.Execute(WriteTran, 1000);
                Debug.Print("Write Success!");
                Thread.Sleep(200);
                I2C1.Execute(ReadTran, 1000);
                Debug.Print("Read Success!");
                string ReadOut = new string(System.Text.Encoding.UTF8.GetChars(RxBuff));
                Debug.Print("EEPROM CONTENT:"+ReadOut);
                Led.Write(false);
                Thread.Sleep(200);

            }
        }
Beispiel #29
0
        public PolarBear(Robot robo, ushort deviceAdd)
            : base(robo)
        {
            //attach to robot, allowing itself to be updated by the Robot thread when called

            robot.actuators.Add(this);
            deviceAddress = deviceAdd;

            //initialize the motor as not turning
            canMove = true;
            velocity = 0;
            brakeAmount = 0;
            upperStopZone = 0;
            lowerStopZone = 0;
            minVelocity = -255;
            maxVelocity = 255;

            //create I2C Device object representing both devices on our bus
            conDeviceA = new I2CDevice.Configuration(deviceAddress, 100);
        }
Beispiel #30
0
        public static void Main() {
            
            led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
            config = new I2CDevice.Configuration(address, clockRateKHz);
            device = new I2CDevice(config);
            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true);
            Dhcp.EnableDhcp(new byte[] { 0x00, 0x26, 0x1C, 0x7B, 0x29, 0xE8 }, "tt");
            

            while (true) {
                      
                
                
                uploadData(gatherData());
                getHeaterStatus();
                setHeaterStatus();

                Thread.Sleep(updateTime);
            }

        }
Beispiel #31
0
        public void Init()
        {
            LCD_I2C_Config = new I2CDevice.Configuration(LCD_I2C_Address, LCD_I2C_Speed);
            LCD_I2C = new I2CDevice(LCD_I2C_Config);
            Thread.Sleep(10);

            WriteCommand(0x33);//Init Back to 8 bits mode  befor set to 4 bits mode
            Thread.Sleep(1);

            WriteCommand(0x32);//Init Back to 8 bits mode  befor set to 4 bits mode
            Thread.Sleep(1);

            WriteCommand(0x28);//Config to 4bits mode,2 Row display
            Thread.Sleep(1);

            WriteCommand(0x0C);//display on,cursor off
            Thread.Sleep(1);

            WriteCommand(0x01);//clear display
            Thread.Sleep(1);
        }