bool ExpanderWrite(uint8_t _data) { //Wire.beginTransmission(_Addr); //Wire.Write((int)(_data) | _backlightval); //Wire.endTransmission(); // here // Must Send the the i2c ud + value //trace("ew", (int)(_data) | _backlightval); var currentBaud = this._nusbio.GetBaudRate(); var resetBaudRate = false; if (currentBaud != MAX_BAUD_RATE) { resetBaudRate = true; this._nusbio.SetBaudRate(MAX_BAUD_RATE); } ///var r = this._i2c.Send1ByteCommand((uint8_t)(_data | _backlightval)); var r = Ii2cOutImpl.i2c_Send1ByteCommand((uint8_t)(_data | _backlightval)); if (resetBaudRate) { this._nusbio.SetBaudRate(currentBaud); } return(r); }
/* Not faster * bool ExpanderWriteAndPusle(uint8_t _data1, uint8_t _data2) * { * var currentBaud = this._nusbio.GetBaudRate(); * var resetBaudRate = false; * if (currentBaud != MAX_BAUD_RATE) * { * resetBaudRate = true; * this._nusbio.SetBaudRate(MAX_BAUD_RATE); * } * * var buffer = new List<uint8_t>(); * buffer.Add((uint8_t) (_data1 | _backlightval)); * buffer.Add((uint8_t) ((_data1 | En) | _backlightval)); // pusle on * buffer.Add((uint8_t) ((_data1 & ~En) | _backlightval)); // pusle off * * buffer.Add((uint8_t) (_data2 | _backlightval)); * buffer.Add((uint8_t) ((_data2 | En) | _backlightval)); // pusle on * buffer.Add((uint8_t) ((_data2 & ~En) | _backlightval)); // pusle off * * var r1 = this._i2c.WriteBuffer(buffer.ToArray(), false); * * if (resetBaudRate) * { * this._nusbio.SetBaudRate(currentBaud); * } * * return true; * }*/ bool ExpanderWriteAndPusle(uint8_t _data) { var currentBaud = this._nusbio.GetBaudRate(); var resetBaudRate = false; if (currentBaud != MAX_BAUD_RATE) { resetBaudRate = true; this._nusbio.SetBaudRate(MAX_BAUD_RATE); } //var r1 = this._i2c.Send3BytesCommand( // (uint8_t)(_data | _backlightval), // (uint8_t)((_data | En) | _backlightval), // pusle on // (uint8_t)((_data & ~En) | _backlightval) // pusle off // ); var buffer = new List <byte>() { (uint8_t)(_data | _backlightval), (uint8_t)((_data | En) | _backlightval), // pusle on (uint8_t)((_data & ~En) | _backlightval) // pusle off }; var r1 = Ii2cOutImpl.i2c_WriteBuffer(buffer.ToArray()); if (resetBaudRate) { this._nusbio.SetBaudRate(currentBaud); } return(true); }
private UInt16 read16(uint8_t reg) { UInt16 value = 0; #if OPTIMIZE_I2C_CALL var result = this._i2c.Send1ByteRead2Bytes(reg); if (!result.Succeeded) { throw new ArgumentException(); } #else //if(Ii2cOutImpl.i2c_WriteBuffer(new byte[1] { reg })) //{ // var buffer = new byte[2]; // Ii2cOutImpl.i2c_ReadBuffer(buffer, buffer.Length); // value = (System.UInt16)((buffer[0] << 8) + buffer[1]); //} //else throw new ArgumentException(); //if(Ii2cOutImpl.i2c_WriteBuffer(new byte[1] { reg })) //{ // var buffer = new byte[2]; // Ii2cOutImpl.i2c_ReadBuffer(buffer, buffer.Length); // value = (System.UInt16)((buffer[0] << 8) + buffer[1]); //} //else throw new ArgumentException(); var buffer = new byte[2]; // Allocate the response expected if (Ii2cOutImpl.i2c_WriteReadBuffer(new byte[1] { reg }, buffer)) { value = (System.UInt16)((buffer[0] << 8) + buffer[1]); } #endif // Calling the MCP9808 too fast disrupt the chip. // Only Sleep(40) seems to work //System.Threading.Thread.Sleep(40); return(value); }