コード例 #1
0
ファイル: TWIDisplay.cs プロジェクト: per1234/twidisplay
 public void writeChar(char val)
 {
     I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] {
         I2CDevice.CreateWriteTransaction(new byte[] { (byte)val })
     };
     m_display.Execute(write, 1000);
 }
コード例 #2
0
ファイル: TWIDisplay.cs プロジェクト: per1234/twidisplay
 public void showAddress()
 {
     I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] {
         I2CDevice.CreateWriteTransaction(new byte[] { 0x90 })
     };
     m_display.Execute(write, 1000);
 }
コード例 #3
0
ファイル: TWIDisplay.cs プロジェクト: per1234/twidisplay
        public void setDots(bool dot0, bool dot1, bool dot2, bool dot3)
        {
            m_dots = 0;
            if (dot0)
            {
                m_dots |= 1 << 1;
            }
            if (dot1)
            {
                m_dots |= 1 << 2;
            }
            if (dot2)
            {
                m_dots |= 1 << 3;
            }
            if (dot3)
            {
                m_dots |= 1 << 4;
            }

            I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateWriteTransaction(new byte[] { 0x85, m_dots })
            };
            m_display.Execute(write, 1000);
        }
コード例 #4
0
ファイル: TWIDisplay.cs プロジェクト: per1234/twidisplay
 public void setScrollMode()
 {
     I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] {
         I2CDevice.CreateWriteTransaction(new byte[] { 0x83, 0x1 })
     };
     m_display.Execute(write, 1000);
 }
コード例 #5
0
        public int GetNumSensors(I2CDevice device)
        {
            device.Write(0x01);
            int numSensors = device.Read();

            return(numSensors);
        }
コード例 #6
0
ファイル: TWIDisplay.cs プロジェクト: per1234/twidisplay
 public void clear()
 {
     I2CDevice.I2CTransaction[] write = new I2CDevice.I2CTransaction[] {
         I2CDevice.CreateWriteTransaction(new byte[] { 0x82 })
     };
     m_display.Execute(write, 1000);
 }
コード例 #7
0
 public EEPROM_I2C()
 {
     I2C_Address   = EEPROM_I2C_1;
     I2C_ClockRate = 100;
     I2CDevice.Configuration config = new I2CDevice.Configuration(I2C_Address, I2C_ClockRate);
     I2C_device = new I2CDevice(config);
 }
コード例 #8
0
        public byte[] readI2CRegister(int addr)
        {
            Debug.Print("Attempting to read I2C register " + addr.ToString("X2"));
            i2cDevice.Config = i2cReadConfig;

            byte Addr = (byte)(addr & 0x00FF);

            byte[] buffer = new byte[1];
            I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[2];
            reading[0] = I2CDevice.CreateWriteTransaction(new byte[] { Addr });
            reading[1] = I2CDevice.CreateReadTransaction(buffer);

            Debug.Print("Executing read transaction...");
            int bytesRead = i2cDevice.Execute(reading, 100);

            if (bytesRead == 0)
            {
                throw new NullReferenceException("0 bytes read for i2c register " + addr.ToString("X2"));
            }

            string message = "Read I2c " + addr.ToString("X2") + ":";

            for (int index = 0; index < buffer.Length; index++)
            {
                message += " " + buffer[index].ToString("X2");
            }

            Debug.Print("Transaction complete, reading " + buffer.Length + " bytes");
            Debug.Print(message);

            return(buffer);
        }
コード例 #9
0
        static ArrayList FindDevices()
        {
            var deviceAddresses = new ArrayList();

            for (ushort address = 0x48; address < 0x48 + 0x07; address++)
            {
                var device = new I2CDevice(new I2CDevice.Configuration(address, ClockRateKHz));

                byte[] writeBuffer = { 0x00 };
                byte[] readBuffer  = new byte[2];

                I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[]
                {
                    I2CDevice.CreateWriteTransaction(writeBuffer)
                    , I2CDevice.CreateReadTransaction(readBuffer)
                };

                var transactionResult = device.Execute(action, transactionTimeout);
                if (transactionResult == 3)
                {
                    deviceAddresses.Add(device.Config.Address);
                    Debug.Print("Found device @" + device.Config.Address);
                }
                else
                {
                    Debug.Print("No device found @" + device.Config.Address);
                }
                device.Dispose();
            }
            return(deviceAddresses);
        }
コード例 #10
0
        void Write(byte[] writeBuffer)
        {
            Connect();

            // create a write transaction containing the bytes to be written to the device
            var writeTransaction = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateWriteTransaction(writeBuffer)
            };

            // write the data to the device
            int written = this.i2cDevice.Execute(writeTransaction, TransactionTimeout);

            while (written < writeBuffer.Length)
            {
                byte[] newBuffer = new byte[writeBuffer.Length - written];
                Array.Copy(writeBuffer, written, newBuffer, 0, newBuffer.Length);

                writeTransaction = new I2CDevice.I2CTransaction[]
                {
                    I2CDevice.CreateWriteTransaction(newBuffer)
                };

                written += this.i2cDevice.Execute(writeTransaction, TransactionTimeout);
            }

            // make sure the data was sent
            if (written != writeBuffer.Length)
            {
                throw new Exception("Could not write to device.");
            }
        }
コード例 #11
0
        public byte[] WriteRead(byte[] write, ushort length)
        {
            _device.Config = _configuration;
            var read = new byte[length];

            I2CDevice.I2CTransaction[] transaction =
            {
                I2CDevice.CreateWriteTransaction(write),
                I2CDevice.CreateReadTransaction(read)
            };
            var bytesTransferred = 0;
            var retryCount       = 0;

            while (_device.Execute(transaction, _transactionTimeout) != (write.Length + read.Length))
            {
                if (retryCount > 3)
                {
                    throw new Exception("WriteRead: Retry count exceeded.");
                }
                retryCount++;
            }

            //while (bytesTransferred != (write.Length + read.Length))
            //{
            //    if (retryCount > 3)
            //    {
            //        throw new Exception("WriteRead: Retry count exceeded.");
            //    }
            //    retryCount++;
            //    bytesTransferred = _device.Execute(transaction, _transactionTimeout);
            //}
            return(read);
        }
コード例 #12
0
        /// <summary>
        /// Reading temperature in desired format
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        public override double ReadTemperature(TemperatureFormat format)
        {
            I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[2];
            byte[] register = new byte[1];
            byte[] data     = new byte[2];

            register[0] = TEMPERATURE_REG_ADDR;
            i2cTx[0]    = I2CDevice.CreateWriteTransaction(register);
            i2cTx[1]    = I2CDevice.CreateReadTransaction(data);

            int result = i2cDevice.Execute(i2cTx, TIMEOUT);

            //converting 12-bit temperature to double
            int rawTemperature = (data[0] << 4) | (data[1] >> 4);

            double temperature = 0;

            if (register.Length + data.Length == result)
            {
                if ((rawTemperature & 0x0800) > 0)
                {
                    rawTemperature = (rawTemperature ^ 0xffff) + 1;
                    temperature    = -1;
                }
                temperature *= rawTemperature * 0.0625f;
            }
            return(temperature + ((format == TemperatureFormat.KELVIN) ? 273.15 : 0.0));
        }
コード例 #13
0
        static void Main(string[] args)
        {
            Console.WriteLine("Test of SHT31-D and armbianI2Clib");

            try
            {
                using (var device = new I2CDevice("/dev/i2c-0", 0x44))
                {
                    Console.WriteLine("init complete");

                    device.Write(new byte[] { 0x24, 0x00 }, 2);
                    Thread.Sleep(100);
                    Console.WriteLine("write complete");

                    var readBuff = device.Read(3);

                    Console.WriteLine($"Read count: {readBuff.Length}");
                    Console.WriteLine($"Read 1:{readBuff[0]}, 2:{readBuff[1]}, 3:{readBuff[2]}");
                    Console.WriteLine($"Temp: {CalcTemp(readBuff[0], readBuff[1])}C");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }
            Console.WriteLine("END");
        }
コード例 #14
0
ファイル: SSD1306.cs プロジェクト: valoni/BrainPad2_NetMF44
 private void send(byte data)
 {
     this._device.Execute(
         new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { data }) },
         _timeout
         );
 }
コード例 #15
0
ファイル: Program.cs プロジェクト: sgoebel/SSD1306-Sharp
        public static void Main(string[] args)
        {
            using (var i2cBus = new I2CBus("/dev/i2c-1"))
            {
                var i2cDevice = new I2CDevice(i2cBus, Display.DefaultI2CAddress);

                display = new SSD1306.Display(i2cDevice, 128, 32);
                display.Init();

                var Tahmona8       = new Tahmona8();
                var tahmona10      = new Tahmona10();
                var tahmona12      = new Tahmona12();
                var tahmona14      = new Tahmona14();
                var dinerRegular24 = new DinerRegular24();

                display.WriteLineBuffProportional(dinerRegular24, "192.168.0.5");
                display.DisplayUpdate();

                while (true)
                {
                    foreach (var dfont in new IFont[] { dinerRegular24, Tahmona8, tahmona10, tahmona12, tahmona14 })
                    {
                        display.WriteLineBuff(dfont, "Hello World 123456", dfont.GetType().Name);
                        display.DisplayUpdate();
                        Console.ReadLine();
                    }
                }
            }
        }
コード例 #16
0
 public EEPROM_I2C(byte subordinate_address, byte subordinate_clockrate)
 {
     I2C_Address   = subordinate_address;
     I2C_ClockRate = subordinate_clockrate;
     I2CDevice.Configuration config = new I2CDevice.Configuration(I2C_Address, I2C_ClockRate);
     I2C_device = new I2CDevice(config);
 }
コード例 #17
0
        private const byte RegisterDeviceId = 0xE5;         //

        public static void Main()
        {
            using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(I2CAddressDefault, ClockRateKHz)))
            {
                byte[] writeBuffer = { RegisterDevice };
                byte[] readBuffer  = new byte[1];

                // check that the ADXL345 sensor connected
                I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[]
                {
                    I2CDevice.CreateWriteTransaction(writeBuffer),
                    I2CDevice.CreateReadTransaction(readBuffer)
                };

                if (device.Execute(action, TransactionTimeoutMilliseconds) != 0)
                {
                    Debug.Print("DeviceId read success");
                }
                else
                {
                    Debug.Print("DeviceId read failure");
                }

                if (readBuffer[0] == RegisterDeviceId)
                {
                    Debug.Print("DeviceId correct");
                }
                else
                {
                    Debug.Print("DeviceId incorrect");
                }
            }
        }
コード例 #18
0
ファイル: ColorClick.cs プロジェクト: valoni/NETMF44
        private void Write(Byte register, Byte data)
        {
            var actions = new I2CDevice.I2CTransaction[1];

            actions[0] = I2CDevice.CreateWriteTransaction(new [] { register, data });
            Hardware.I2CBus.Execute(_config, actions, 1000);
        }
コード例 #19
0
 /// <summary>
 /// Scan range of addresses and print devices to debug output.
 /// </summary>
 /// <param name="startAddress">Start of scanning (included)</param>
 /// <param name="endAddress">End of scanning (included)</param>
 /// <param name="clockRateKhz">frequency in Khz</param>
 public static void ScanAddresses(ushort startAddress, ushort endAddress, ushort clockRateKhz = 100)
 {
     Debug.Print("Scanning...");
     for (ushort adr = startAddress; adr <= endAddress; adr++)
     {
         I2CDevice device = new I2CDevice(new I2CDevice.Configuration(adr, clockRateKhz));
         byte[]    buff   = new byte[1];
         try
         {
             I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(buff);
             var ret = device.Execute(new I2CDevice.I2CTransaction[] { read }, 1000);
             if (ret > 0)
             {
                 Debug.Print("Device on address: " + adr + " (0x" + adr.ToString("X") + ")");
             }
         }
         catch (Exception)
         {
             continue;
         }
         finally
         {
             //otestovat yda se dela pokazde
             device.Dispose();
             device = null;
         }
     }
     Debug.Print("Scanning finished.");
 }
コード例 #20
0
ファイル: Accelerometer.cs プロジェクト: kobo8924/IoT-KUT
 /**
  * RegWrite()
  * Caller : SetDataFormat()
  */
 public void RegWrite(byte reg, byte val)
 {
     wdata[0]   = reg;
     wdata[1]   = val;
     trRegWrite = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(wdata) };
     int result = i2c.Execute(trRegWrite, timeout);
 }
コード例 #21
0
        /// <summary>
        /// Receive data back from the nunchuck,
        /// returns 1 on successful read. returns 0 on failure
        /// </summary>
        public bool GetData()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException();
            }

            if (!IsConnected)
            {
                Init(InitTimeout);
                return(false);
            }

            // send request for data
            WriteToDevice(0x00);

            // get the data
            byte[] inputBuffer     = new byte[6];
            var    readTransaction = I2CDevice.CreateReadTransaction(inputBuffer);

            // execute both transactions
            int tranferred = _device.Execute(new I2CDevice.I2CTransaction[] { readTransaction }, TransactionTimeout);

            // less then 6 bytes read?
            if (tranferred != inputBuffer.Length)
            {
                // communication error, no need to reinitialize
                return(false);
            }

            if (!_disableEncryption)
            {
                // decrypt data in buffer
                for (int i = 0; i < 6; i++)
                {
                    inputBuffer[i] = DecodeByte(inputBuffer[i]);
                }
            }

            // check if all 0xff read?
            byte cnt = 0;

            for (int i = 0; i < inputBuffer.Length; i++)
            {
                if (inputBuffer[i] == 0xff)
                {
                    cnt++;
                }
            }

            if (cnt == inputBuffer.Length)
            {
                // this is connection error.
                _isConnected = false;
                return(false);
            }

            ExtractData(inputBuffer);
            return(true);
        }
コード例 #22
0
        private static void UpdateBlinkM(I2CDevice.Configuration blinkM, I2CDevice i2cDevice, NetworkCredential credential)
        {
            string deviceAddressText = blinkM.Address.ToString();
            string defaultHsbText    = "38,255,42";
            string hsbText           = ConfigUtil.GetOrInitConfigValue(NapkinServerUri, DeviceId, "blinkM_" + deviceAddressText + "_hsb", defaultHsbText, credential);

            string[] hsbArray = hsbText.Split(',');
            if ((hsbArray == null) || (hsbArray.Length != 3))
            {
                Debug.Print("Badly formed HSB data: " + hsbText);
                hsbText = defaultHsbText;
            }

            try
            {
                byte hue        = (byte)int.Parse(hsbArray[0]);
                byte saturation = (byte)int.Parse(hsbArray[1]);
                byte brightness = (byte)int.Parse(hsbArray[2]);
                BlinkMCommand.FadeToHSB.Execute(i2cDevice, blinkM, hue, saturation, brightness);
            }
            catch (Exception)
            {
                Debug.Print("Error parsing HSB data: " + hsbText);
            }
        }
コード例 #23
0
        public CapTouchDriver(I2CDevice sharedBus)
        {
            display = new DisplayNhd5(sharedBus);

            //LCDbacklight = new OutputPort(GHI.Pins.G120.P1_19, true);
            //LCDbacklight.Write(true);
            msBacklightTime  = 0;           // Default is off
            msDisplayTimeout = 0;

            display.TouchUp   += display_TouchUp;
            display.TouchDown += display_TouchDown;
            display.ZoomIn    += display_ZoomIn;
            display.ZoomOut   += display_ZoomOut;

            touches    = new Microsoft.SPOT.Touch.TouchInput[1];
            touches[0] = new TouchInput();
            //
            // Using interrupt (for G120)
            //
            touchPin = new InterruptPort(GHI.Pins.G120.P0_25, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);

            //touchPin = new InterruptPort(GHI.Pins.G120.P2_21, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            touchPin.OnInterrupt += touchPin_OnInterrupt;
            //
            // Create thread the handle the backlight timeout
            //
            Thread threadBacklight = new Thread(backlightHandler);

            threadBacklight.Start();
        }
コード例 #24
0
        public void SetupDevice()
        {
            relayPort = new OutputPort((Cpu.Pin) 21, false);
            relayPort.Write(true);
            var i2cConfig = new I2CDevice.Configuration(0x51, 400);

            i2cSensor = new I2CDevice(i2cConfig);
            var xwsaction = I2CDevice.CreateWriteTransaction(new byte[] { 0x01 });
            var xrsaction = I2CDevice.CreateReadTransaction(new byte[8]);

            i2cSensor.Execute(new I2CDevice.I2CTransaction[] { xwsaction, xrsaction }, 100);

            var read = xrsaction.Buffer;

            var temperature = BitConverter.ToDouble(read, 0);

            var accelOrder = I2CDevice.CreateWriteTransaction(new byte[] { 0x02 });
            var accelRead  = I2CDevice.CreateReadTransaction(new byte[24]);

            i2cSensor.Execute(new I2CDevice.I2CTransaction[] { accelOrder, accelRead }, 100);

            var accelX = BitConverter.ToDouble(accelRead.Buffer, 0);
            var accelY = BitConverter.ToDouble(accelRead.Buffer, 8);
            var accelZ = BitConverter.ToDouble(accelRead.Buffer, 16);
        }
コード例 #25
0
ファイル: I2CIO.cs プロジェクト: jakkaj/libMPSSE-.Net-Wrapper
        public int Begin(int address)
        {
            if (_i2cDevice == null)
            {
                var adci2cConfig = new FtChannelConfig
                {
                    ClockRate = ConnectionSpeed,
                    LatencyTimer = LatencyTimer
                };

                if (_i2cDevice == null)
                {
                    _i2cDevice = new I2CDevice(adci2cConfig, address);

                    var b = _i2cDevice.Read(1);

                    if (b == null)
                    {
                        Debugger.Break();
                    }

                    _shadow = Convert.ToInt32(b[0]);
                    _initialised = true;
                }
            }

            return 1;
        }
コード例 #26
0
ファイル: mcp342x.cs プロジェクト: WebGE/MCP342x
                /// <summary>
                /// Reads the raw counts from the ADC
                /// </summary>
                /// <param name="number">Number of bytes to read (four if 18-bit conversion else three) </param>
                /// <returns>Result of 12, 14, 16 or 18-bit conversion</returns>
                double dataReadRaw(byte number)
                {
                    Int32 data = 0;

                    byte[] inbuffer = new byte[number];
                    I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inbuffer);
                    I2CDevice.I2CTransaction[]   xAction         = new I2CDevice.I2CTransaction[] { readTransaction };

                    _i2cBus = new I2CDevice(_config);
                    int transferred = _i2cBus.Execute(xAction, _transactionTimeOut);

                    _i2cBus.Dispose();

                    if (transferred < inbuffer.Length)
                    {
                        throw new System.IO.IOException("Error i2cBus " + _sla);
                    }
                    else if ((inbuffer[number - 1] & 0x80) == 0) // End of conversion
                    {
                        if (_resolution == SampleRate.EighteenBits)
                        {
                            data = (((Int32)inbuffer[0]) << 16) + (((Int32)inbuffer[1]) << 8) + (Int32)inbuffer[2];
                        }
                        else
                        {
                            data = (((Int32)inbuffer[0]) << 8) + (Int32)inbuffer[1];
                        }
                        _endOfConversion = true; return(data &= _dataMask);
                    }
                    else
                    {
                        _endOfConversion = false;
                        return(0); // return something
                    }
                }
コード例 #27
0
        private void SetRegister(Byte register, Byte value)
        {
            var actions = new I2CDevice.I2CTransaction[1];

            actions[0] = I2CDevice.CreateWriteTransaction(new [] { register, value });
            Hardware.I2CBus.Execute(_config, actions, 1000);
            Thread.Sleep(20);
        }
コード例 #28
0
 /// <summary>
 /// Writes an array of 8-bit bytes to the interface, and reads an array of 8-bit bytes from the interface.
 /// </summary>
 /// <param name="WriteBuffer">An array with 8-bit bytes to write</param>
 /// <param name="ReadBuffer">An array with 8-bit bytes to read</param>
 /// <returns>The amount of transferred bytes</returns>
 public int WriteRead(byte[] WriteBuffer, byte[] ReadBuffer)
 {
     _I2CDevice.Config = this._Configuration;
     return(_I2CDevice.Execute(new I2CDevice.I2CTransaction[] {
         I2CDevice.CreateWriteTransaction(WriteBuffer),
         I2CDevice.CreateReadTransaction(ReadBuffer)
     }, this.Timeout));
 }
コード例 #29
0
        private void SetRegisterBit(Byte register, Byte bitIndex, Boolean state)
        {
            var actions = new I2CDevice.I2CTransaction[1];

            actions[0] = I2CDevice.CreateWriteTransaction(new [] { register, Bits.Set(ReadByte(register), bitIndex, state) });
            Hardware.I2CBus.Execute(_config, actions, 1000);
            Thread.Sleep(20);
        }
コード例 #30
0
 public PCA9685Device(I2CDevice device)
 {
     Device = device;
     WriteAddressByte(PCA9685_MODE1_ADDRESS, 0x30);
     WriteAddressByte(PCA9685_MODE2_ADDRESS, 0x04);
     SetFrequency(Frequency);
     WriteAddressByte(PCA9685_MODE1_ADDRESS, 0x00);
 }
コード例 #31
0
ファイル: WiiChuck.cs プロジェクト: coolkev/Netduino
        public WiiChuck(bool disableEncryption)
        {
            _disableEncryption = disableEncryption;

            var config = new I2CDevice.Configuration(DeviceAddress, ClockRateKHz);

            _device = new I2CDevice(config);
        }
コード例 #32
0
ファイル: Hmc5883.cs プロジェクト: brookpatten/MrGibbs
		public Hmc5883(I2CDevice device)
        {
			_i2c = device;
        }
コード例 #33
0
ファイル: Mpu6050.cs プロジェクト: brookpatten/MrGibbs
 /** Default constructor, uses default I2C address.
 * @see MPU6050_DEFAULT_ADDRESS
 */
 public Mpu6050(I2CDevice device,ILogger logger)
 {
     _i2c = device;
     _logger = logger;
 }