private void Dispose(bool fDisposing) { if (!this.isDisposed) { try { if (instanceCount > 0) { instanceCount--; if (instanceCount == 0) { if (singletonDevice != null) { singletonDevice.Dispose(); singletonDevice = null; } } } } finally { this.isDisposed = true; } } }
/// <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."); }
/* There is no generic public Write EEPROM command. * Specific commands are available for setting EEPROM values * where appropriate to the device. */ void WriteEeprom(EEPROMAddress addr, byte data) { myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)Command.WriteEEPROM, (byte)addr, data }); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C }
public void WakeUp() { myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)Command.WakeUp }); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C }
// Set the operational mode. // Mode = Standby, Query, Continuous // Frequency = 1, 45, 10, 20 Hz // Periodic reset = true/false public void SetOperationalMode(OperationalMode mode, Frequency freq, Boolean periodicReset) { byte r = periodicReset ? (byte)(0x01 << 3) : (byte) 0; byte f = (byte)((byte)freq << 5); byte op = (byte)((byte)mode | r | f); myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)Command.WriteEEPROM, (byte)EEPROMAddress.OperationalModeByte, op }); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C }
// Public methode /// <summary> /// Lecture des 17 registres de la carte MD2x /// et calcul de la valeur des encodeurs /// </summary> /// <returns></returns> public int GetAllRegisters() { // Buffer d'écriture byte[] outBuffer = new byte[] { 0 }; I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(outBuffer); // Buffer de lecture byte[] inBuffer = new byte[17]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inBuffer); // Tableau des transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeTransaction, readTransaction }; // Exécution des transactions busI2C = new I2CDevice(configMD2x); // Connexion virtuelle de la carte MD2x au bus I2C if (busI2C.Execute(transactions, TRANSACTIONEXECUTETIMEOUT) != 0) { // Success //Debug.Print("Received the first data from at device " + busI2C.Config.Address + ": " + ((int)inBuffer[0]).ToString()); // Sauvegarde de la valeur contenue dans les registres speed1 = inBuffer[0]; speed2Turn = inBuffer[1]; battery = inBuffer[10]; current1 = inBuffer[11]; current2 = inBuffer[12]; softrev = inBuffer[13]; acceleration = inBuffer[14]; mode = inBuffer[15]; command = inBuffer[16]; // Calcul de la valeur contenue dans les codeurs 32 bits signée encoder1 = (Int32)(inBuffer[2] << 24) | (Int32)(inBuffer[3] << 16) | (Int32)(inBuffer[4] << 8) | (Int32)(inBuffer[5]); encoder2 = (Int32)(inBuffer[6] << 24) | (Int32)(inBuffer[7] << 16) | (Int32)(inBuffer[8] << 8) | (Int32)(inBuffer[9]); } else { // Failed //Debug.Print("Failed to execute transaction at device: " + busI2C.Config.Address + "."); } busI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C return 1; }
public void Read() { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] readActions = new I2CDevice.I2CTransaction[2]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[1] { 2 }; readActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); // create read buffer to read the register byte[] RegisterValue = new byte[1]; readActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); // Now we access the I2C bus and timeout in one second if no responce MyI2C.Execute(readActions, 1000); if (currentValue != RegisterValue[0]) { currentValue = RegisterValue[0]; if (OnChange != null) { OnChange(dialId, currentValue); } } MyI2C.Dispose(); //Debug.Print("Register value: " + RegisterValue[0].ToString()); }
/// <summary> /// Returns the value contained in a register /// et + /// </summary> /// <param name="RegisterNumber">The register number</param> /// <returns></returns> private byte GetRegister(Registers RegisterNumber) { // Buffer d'écriture byte[] outBuffer = new byte[] { (byte)RegisterNumber }; I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(outBuffer); // Buffer de lecture byte[] inBuffer = new byte[1]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inBuffer); // Tableau des transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeTransaction, readTransaction }; // Exécution des transactions busI2C = new I2CDevice(ConfigSRF08); // Connexion virtuelle du SRF08 au bus I2C if (busI2C.Execute(transactions, TRANSACTIONEXECUTETIMEOUT) != 0) { // Success //Debug.Print("Received the first data from at device " + busI2C.Config.Address + ": " + ((int)inBuffer[0]).ToString()); } else { // Failed //Debug.Print("Failed to execute transaction at device: " + busI2C.Config.Address + "."); } busI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C return inBuffer[0]; }
/// <summary> /// Three cursor modes /// </summary> /// <param name="posCursor"></param> public void SelectCursor(CursorType posCursor) { // Création d'un buffer et d'une transaction pour l'accès au circuit en écriture byte[] outbuffer = new byte[] { 0, (byte)(4 + posCursor) }; I2CDevice.I2CTransaction WriteTransaction = I2CDevice.CreateWriteTransaction(outbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteBytes = new I2CDevice.I2CTransaction[] { WriteTransaction }; // Exécution de la transaction BusI2C = new I2CDevice(ConfigI2CLcd); // Connexion virtuelle de l'objet Lcd au bus I2C BusI2C.Execute(T_WriteBytes, 1000); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C }
/// <summary> /// Write a line of text at x,y /// </summary> /// <param name="x_pos"></param> /// <param name="y_pos"></param> /// <param name="Text"></param> public void PutString(byte x_pos, byte y_pos, string Text) { byte addr = 0x80; if (x_pos < 17) addr += x_pos; // This is for 16 x 2, adjust as nessesary if (y_pos == 1) addr += 0x40; byte[] txt = System.Text.Encoding.UTF8.GetBytes((byte)'0' + (byte)'0' + (byte)'0' + Text); for (byte z = 3; z < (Text.Length + 3); z++) // All characters have to be moved up!! { txt[z] += 128; } txt[2] = 0x40; // R/S to Data // Création d'un buffer et de deux transactions pour l'accès au circuit en écriture byte[] outbuffer = new byte[] { 0, addr }; I2CDevice.I2CTransaction WriteBytes = I2CDevice.CreateWriteTransaction(outbuffer); // set address I2CDevice.I2CTransaction WriteText = I2CDevice.CreateWriteTransaction(txt); // Print line // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteBytes = new I2CDevice.I2CTransaction[] { WriteBytes, WriteText }; // Exécution de la transaction BusI2C = new I2CDevice(ConfigI2CLcd); // Connexion virtuelle de l'objet Lcd au bus I2C BusI2C.Execute(T_WriteBytes, 1000); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C }
/// <summary> /// Single character write at x,y /// </summary> /// <param name="x_pos"></param> /// <param name="y_pos"></param> /// <param name="z_char"></param> public void PutChar(byte x_pos, byte y_pos, byte z_char) { byte addr = 0x80; // This is for 16 x 2, adjust as nessesary if (x_pos < 17) addr += x_pos; // x dir if (y_pos == 1) addr += 0x40; // y dir // Création d'un buffer et de deux transactions pour l'accès au circuit en écriture byte[] outbuffer = new byte[] { 0, addr }; I2CDevice.I2CTransaction WriteBytes = I2CDevice.CreateWriteTransaction(outbuffer); // set address (R/S to Cmd ) I2CDevice.I2CTransaction WriteText = I2CDevice.CreateWriteTransaction(new byte[] { 0, 0, 0x40, z_char }); // single char write // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteBytes = new I2CDevice.I2CTransaction[] { WriteBytes, WriteText }; // Exécution de la transaction BusI2C = new I2CDevice(ConfigI2CLcd); // Connexion virtuelle de l'objet Lcd au bus I2C BusI2C.Execute(T_WriteBytes, 1000); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C }
/// <summary> /// Goto start of line /// </summary> /// <param name="x"></param> public void LineBegin(byte x) { byte addr = 0x80; if (x == 1) addr += 0x40; // Création d'un buffer et d'une transaction pour l'accès au circuit en écriture byte[] outbuffer = new byte[] { 0, addr }; I2CDevice.I2CTransaction WriteTransaction = I2CDevice.CreateWriteTransaction(outbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteBytes = new I2CDevice.I2CTransaction[] { WriteTransaction }; // Exécution de la transaction BusI2C = new I2CDevice(ConfigI2CLcd); // Connexion virtuelle de l'objet Lcd au bus I2C BusI2C.Execute(T_WriteBytes, 1000); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C }
// Méthodes publiques /// <summary> /// Initialize the LCD before use /// </summary> public void Init() { Thread.Sleep(200); // Création d'un buffer et d'une transaction pour l'accès au circuit en écriture // Nop, Function_set, Display_ctl, Entry_mode_set, Function_set, Disp_conf, Temp_ctl, Hv_gen, VLCD_Set, Function_Set, Set DDRAM, Return Home // Configuration pour un afficheur MIDAS par défaut byte[] outbuffer = new byte[] { 0x00, 0x34, 0x0d, 0x06, 0x35, 0x05, 0x10, 0x40, 0x99, 0x34, 0x83, 0x02 }; if (i2c_Add_7bits == (ushort)LcdManufacturer.BATRON) { outbuffer[5] = 0x04; //Disp_conf outbuffer[7] = 0x42; // Hv_gen outbuffer[8] = 0x9F; //VLCD_Set } I2CDevice.I2CTransaction WriteTransaction = I2CDevice.CreateWriteTransaction(outbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteBytes = new I2CDevice.I2CTransaction[] { WriteTransaction }; // Exécution de la transaction BusI2C = new I2CDevice(ConfigI2CLcd); // Connexion virtuelle de l'objet Lcd au bus I2C BusI2C.Execute(T_WriteBytes, 1000); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C }
// If you use built in Clr // Clear screen with 0xA0 not 0x20 /// <summary> /// Clear screen /// </summary> public void ClearScreen() { // the screen doen't clear byte[] clr = new byte[19]; // you just get arrows clr[0] = 0; clr[1] = 0; clr[2] = 0x40; // R/S to Data for (byte x = 3; x < 19; x++) // Space is not 0x20 on this unit! { clr[x] = 0xA0; } // Création d'un buffer et de quatre transactions pour l'accès au circuit en écriture I2CDevice.I2CTransaction SetTop = I2CDevice.CreateWriteTransaction(new byte[] { 0, 0x80 }); // set top line (R/S to Cmd ) I2CDevice.I2CTransaction Setclr = I2CDevice.CreateWriteTransaction(clr); I2CDevice.I2CTransaction SetBottom = I2CDevice.CreateWriteTransaction(new byte[] { 0, 0, 0, 0xC0 }); // set bottom line (R/S to Cmd ) I2CDevice.I2CTransaction SetHome = I2CDevice.CreateWriteTransaction(new byte[] { 0, 0, 0, 0x80 }); // Cursor to home (R/S to Cmd ) // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteBytes = new I2CDevice.I2CTransaction[] { SetTop, Setclr, SetBottom, Setclr, SetHome }; // Exécution de la transaction BusI2C = new I2CDevice(ConfigI2CLcd); // Connexion virtuelle de l'objet Lcd au bus I2C BusI2C.Execute(T_WriteBytes, 1000); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C }
// Private methode private int SetRegister(WRegister Register, byte value) { // Création d'un buffer et d'une transaction pour l'accès au circuit en écriture byte[] outbuffer = new byte[] { (byte)Register, value }; I2CDevice.I2CTransaction writeTransaction = I2CDevice.CreateWriteTransaction(outbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteByte = new I2CDevice.I2CTransaction[] { writeTransaction }; busI2C = new I2CDevice(configMD2x); // Connexion virtuelle de l'objet MD2x au bus I2C busI2C.Execute(T_WriteByte, TRANSACTIONEXECUTETIMEOUT); // Exécution de la transaction busI2C.Dispose(); // Déconnexion virtuelle de l'objet MD2x du bus I2C return 1; }
/// <summary> /// Triggers a shot ulrasons, wait for 75ms and return result in the unit of measure /// </summary> /// <param name="units">unit of measure expected</param> /// <returns>range in cm or inches or millisec</returns> public UInt16 ReadRange(MeasuringUnits units) { this.unit = units; // Calcul du mot de commande à partir de l'unité de mesure byte command = (byte)(80 + (byte)units); // Création d'un buffer et d'une transaction pour l'accès au module en écriture byte[] outbuffer = new byte[] { (byte)Registers.Command, command }; I2CDevice.I2CTransaction WriteUnit = I2CDevice.CreateWriteTransaction(outbuffer); // Création d'un buffer et d'une transaction pour l'accès au module en lecture byte[] inbuffer = new byte[4]; I2CDevice.I2CTransaction ReadDist = I2CDevice.CreateReadTransaction(inbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteUnit = new I2CDevice.I2CTransaction[] { WriteUnit }; I2CDevice.I2CTransaction[] T_ReadDist = new I2CDevice.I2CTransaction[] { ReadDist }; // Exécution des transactions busI2C = new I2CDevice(ConfigSRF08); // Connexion virtuelle de l'objet SRF08 au bus I2C busI2C.Execute(T_WriteUnit, TRANSACTIONEXECUTETIMEOUT); // Transaction : Activation US Thread.Sleep(75); // attente echo US busI2C.Execute(T_ReadDist, TRANSACTIONEXECUTETIMEOUT); // Transaction : Lecture distance UInt16 range = (UInt16)((UInt16)(inbuffer[3] << 8) + inbuffer[2]); // Calcul de la distance busI2C.Dispose(); // Déconnexion virtuelle de l'objet SRF08 du bus I2C return range; }
/// <summary> /// Only triggers a shot ulrasons /// </summary> /// <param name="units">unit of measure expected</param> public void TrigShotUS(MeasuringUnits units) { this.unit = units; // Calcul du mot de commande à partir de l'unité de mesure byte commandByte = (byte)(80 + (byte)units); // Création d'un buffer et d'une transaction pour l'accès au module en écriture byte[] outbuffer = new byte[] { (byte)Registers.Command, commandByte }; I2CDevice.I2CTransaction WriteUnit = I2CDevice.CreateWriteTransaction(outbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteUnit = new I2CDevice.I2CTransaction[] { WriteUnit }; // Exécution de la transactions busI2C = new I2CDevice(ConfigSRF08); // Connexion virtuelle de l'objet SRF08 au bus I2C busI2C.Execute(T_WriteUnit, TRANSACTIONEXECUTETIMEOUT); // Transaction : Activation US busI2C.Dispose(); // Déconnexion virtuelle de l'objet SRF08 du bus I2C }
/// <summary> /// Set brightness 20 dark -> 0 light /// </summary> /// <param name="bright"></param> public void SetBacklight(byte bright) { byte BLight=0x99; if (i2c_Add_7bits == (ushort)LcdManufacturer.MIDAS) { if (bright > 20) { bright = 20; } BLight = (byte)(0x90 + (byte)(bright & 0x1F)); } else if (i2c_Add_7bits == (ushort)LcdManufacturer.BATRON) { if (bright > 20) { bright = 20; } BLight = (byte)(0x95 + (byte)(bright & 0x1F)); } // Création d'un buffer et d'une transaction pour l'accès au circuit en écriture byte[] outbuffer = new byte[] { 0, 0x35, BLight, 0x34 }; I2CDevice.I2CTransaction WriteTransaction = I2CDevice.CreateWriteTransaction(outbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteBytes = new I2CDevice.I2CTransaction[] { WriteTransaction }; // Exécution de la transaction BusI2C = new I2CDevice(ConfigI2CLcd); // Connexion virtuelle de l'objet Lcd au bus I2C BusI2C.Execute(T_WriteBytes, 1000); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C }
public void Init() { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] initActions = new I2CDevice.I2CTransaction[1]; // create write buffer (we need one byte) //initActions[0] = MyI2C.CreateWriteTransaction(new byte[] { 0, (byte)(DialMode.AUT | DialMode.CYC | DialMode.OneFree) }); I2CDevice.CreateWriteTransaction(new byte[] { 0, (byte)(DialMode.AUT | DialMode.CYC | DialMode.OneFree) }); int result = MyI2C.Execute(initActions, 1000); //Debug.Print("Device Init Result " + result); MyI2C.Dispose(); }
public float GetHeading() { myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte) Command.GetData }); myI2Command[1] = I2CDevice.CreateReadTransaction(myReadBuffer); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C float heading = ((myReadBuffer[0] << 8) + myReadBuffer[1]) / 10f; return (heading); }
public void SetValue(byte newValue) { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] setActions = new I2CDevice.I2CTransaction[1]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[2] { 2, newValue }; setActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); MyI2C.Execute(setActions, 1000); MyI2C.Dispose(); }
public byte ReadEeprom(EEPROMAddress addr) { myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)Command.ReadEEPROM, (byte) addr }); myI2Command[1] = I2CDevice.CreateReadTransaction(myReadEEPROM); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C return myReadEEPROM[0]; }