/// <summary> /// Gets the current position of the encoder's shaft. For 12-bit devices, this ranges from /// 0 to 4095. For 14-bit devices, this ranges from 0 to 16383. /// </summary> /// <returns>The position of the shaft the encoder is coupled to.</returns> public int GetPosition() { // Read from the device and validate that it came back OK byte[] buffer = { 0x00, 0x00 }; Spi.TransferData(buffer); try { ValidateChecksum(buffer); } catch (Exception ex) { Logger.Error(ex.Message); return(-1); } // Make a short from the data ushort data = 0; data |= (ushort)(buffer[0] << 8); data |= buffer[1]; // Get rid of the 2 checksum bits at the top data &= 0x3FFF; return(data); }
/// <summary> /// Gets the status of the device. /// </summary> /// <returns>The device status</returns> public L6470Status GetStatus() { // Send the GetStatus command byte command = 0b11010000; byte[] buffer = { command }; Spi.TransferData(buffer); // Get the 2 status bytes ushort status = 0; buffer[0] = 0; Spi.TransferData(buffer); status |= (ushort)(buffer[0] << 8); buffer[0] = 0; Spi.TransferData(buffer); status |= buffer[0]; return(new L6470Status(status)); }