/// <summary> /// Read the value of one analog port using Nusbio spi/hardware acceleration. /// </summary> /// <param name="port"></param> /// <returns></returns> public int Read(int port, int percentageAdjust = -1) { if ((port > 7) || (port < 0)) { throw new ArgumentException(string.Format("Invalid analog port {0}", port)); } const byte junk = (byte)0; var port2 = (byte)((_channelInSingleMode[port] << 4) & 0x03); var bufferReceive = new byte[3] { 0, 0, 0 }; var bufferSend = new List <Byte>() { 0x1, port2, junk }; if (this._spi.Ok(this._spi.QueryReadWrite(bufferSend.ToArray(), bufferReceive))) { var r1 = new SPIResult().Succeed(bufferReceive.ToList()); // System.Console.WriteLine($"bufferOut: {bufferReceive[0]}, {bufferReceive[1]}, {bufferReceive[2]}"); var v = ValidateOperation(r1); if (percentageAdjust != -1) { v = v + (v * percentageAdjust / 100); } return(v); } else { return(-1); } }
private int ValidateOperation(SPIResult result) { if (result.Succeeded && result.Buffer.Count == 3) { //ConsoleEx.WriteLine(0, 15, string.Format("B0:{0:000}, B1:{1:000}, B2:{2:000}", // result.Buffer[0], result.Buffer[1], result.Buffer[2]), ConsoleColor.Yellow); int r = 0; if (WinUtil.BitUtil.IsSet(result.Buffer[1], 1)) { r += 256; } if (WinUtil.BitUtil.IsSet(result.Buffer[1], 2)) { r += 512; } r += result.Buffer[2]; var rr = (result.Buffer[1] & 0x3) << 8 | result.Buffer[2]; return(r); } else { return(-1); } }
//public int Get(ADDRESS pot = ADDRESS.POT_0) //{ // try // { // this._spiEngine.Select(); // int result = -1; // var cmd = build_command(pot, COMMAND.READ); // var r = _spiEngine.Transfer(cmd); // var b0 = r.ReadBuffer[0]; // bool valid = WinUtil.BitUtil.IsSet(b0, CMDERR_MASK); // if (valid) // { // var dataByte = build_data(0); // var r2 = _spiEngine.Transfer(dataByte); // result = r2.ReadBuffer[0]; // } // return result; // } // finally { // this._spiEngine.Unselect(); // } //} public int Get(ADDRESS pot = ADDRESS.POT_0) { try { this._spiEngine.Select(); var r = new SPIResult(); r = Transfer(pot, COMMAND.READ, 0); return(r.Succeeded ? r.Value : -1); } finally { this._spiEngine.Unselect(); } }
public SPIResult WriteRow(int deviceIndex, int row, bool computeBufferOnly = false) { var r = new SPIResult(); if (deviceIndex < 0 || deviceIndex >= _deviceCount) { return(r); } int offset = deviceIndex * MATRIX_ROW_SIZE; r = SpiTransfer(deviceIndex, (byte)(row + 1), _pixels[offset + row], computeBufferOnly: computeBufferOnly); return(r); }
public bool ValidateOperation(SPIResult result, ADDRESS address, COMMAND command, int?value = null) { // We cannot read an answer from the MCP4131. MISO and MOSI pins are combined // and we cannot make it work. So always return true if (this.GetType().Name == "MCP4131") { return(true); } var r = false; // See datasheet http://exploringarduino.com/wp-content/uploads/2013/06/MCP4231-datasheet.pdf // Page 48 // Value Function MOSI (SDI pin) MISO (SDO pin) (2) // 00h Volatile Wiper 0 Write Data nn nnnn nnnn 0000 00nn nnnn nnnn 1111 1111 1111 1111 // Read Data nn nnnn nnnn 0000 11nn nnnn nnnn 1111 111n nnnn nnnn // Increment Wiper — 0000 0100 1111 1111 // Decrement Wiper — 0000 1000 1111 1111 // 01h Volatile Wiper 1 Write Data nn nnnn nnnn 0001 00nn nnnn nnnn 1111 1111 1111 1111 // Read Data nn nnnn nnnn 0001 11nn nnnn nnnn 1111 111n nnnn nnnn // Increment Wiper — 0001 0100 1111 1111 // Decrement Wiper — 0001 1000 1111 1111 // 02h Reserved — — — — // 03h Reserved — — — — // 04h Volatile // TCON Register // Write Data nn nnnn nnnn 0100 00nn nnnn nnnn 1111 1111 1111 1111 // Read Data nn nnnn nnnn 0100 11nn nnnn nnnn 1111 111n nnnn nnnn // 05h Status Register Read Data nn nnnn nnnn 0101 11nn nnnn nnnn 1111 111n nnnn nnnn // 06h-0Fh Reserved — — — — // Note 1: The Data Memory is only 9-bits wide, so the MSb is ignored by the device. // 2: All these Address/Command combinations are valid, so the CMDERR bit is s switch (command) { case COMMAND.WRITE: r = (result.Buffer.Count == 2) && (result.Buffer[0] == 255) && result.Buffer[1] == 255; break; case COMMAND.DECREMENT: case COMMAND.INCREMENT: r = (result.Buffer.Count == 1) && (result.Buffer[0] == 255); break; case COMMAND.READ: r = (result.Buffer.Count == 2) && (result.Buffer[0] >= 254); if (r) { result.Value = result.Buffer[1]; } break; } return(r); }
/* * Set the number of digits (or rows) to be displayed. * See datasheet for sideeffects of the scanlimit on the brightness * of the Display. * Params : * devIndex address of the Display to control * limit number of digits to be displayed (1..8) */ public bool SetScanLimit(int devIndex, int limit) { var r = new SPIResult(); if (devIndex < 0 || devIndex >= _deviceCount) { return(r.Succeeded); } if (limit >= 0 && limit < 8) { r = SpiTransfer(devIndex, OP_SCANLIMIT, (byte)limit); } return(r.Succeeded); }
private SPIResult SpiTransferBuffer(List <byte> buffer, bool software = false) { var r = new SPIResult(); //software = true; if (software) { for (var i = 0; i < buffer.Count; i++) { Shift.ShiftOut(this._nusbio, Nusbio.GetGpioIndex(this._spiEngine.MosiGpio), Nusbio.GetGpioIndex(this._spiEngine.ClockGpio), buffer[i]); } } else { BytesSentOutCounter += buffer.Count; r = this._spiEngine.Transfer(buffer, optimizeDataLine: OptimizeSpiDataLine); } return(r); }
/* * Set the Shutdown (power saving) mode for the device * Params : * devIndex The address of the Display to control * _pixels If true the device goes into power-down mode. Set to false * for normal operation. */ public bool Shutdown(bool status, int devIndex = 0) { var r = new SPIResult(); if (devIndex < 0 || devIndex >= _deviceCount) { return(r.Succeeded); } if (status) { r = SpiTransfer(devIndex, OP_SHUTDOWN, 0); } else { r = SpiTransfer(devIndex, OP_SHUTDOWN, 1); } return(r.Succeeded); }
public SPIResult Decrement(int times = 1, ADDRESS pot = ADDRESS.POT_0) { var r = new SPIResult(); if (times > 1) { for (var i = 0; i < times; i++) { r = this.Decrement(1); } } else { if (this.DigitalValue > this.MinDigitalValue) { this.DigitalValue--; this.Transfer(pot, COMMAND.DECREMENT); } } return(r); }
public SPIResult Increment(int times = 1, ADDRESS pot = ADDRESS.POT_0) { var r = new SPIResult(); if (times > 1) { for (var i = 0; i < times; i++) { r = this.Increment(1); } } else { if (this.DigitalValue < this.MaxDigitalValue + 1) { this.DigitalValue++; this.Transfer(pot, COMMAND.INCREMENT); } } return(r); }
private int ValidateOperation(SPIResult result) { if (result.Succeeded && result.Buffer.Count == 3) { int r = 0; if (WinUtil.BitUtil.IsSet(result.Buffer[1], 1)) { r += 256; } if (WinUtil.BitUtil.IsSet(result.Buffer[1], 2)) { r += 512; } r += result.Buffer[2]; return(r); } else { return(-1); } }
private SPIResult SpiTransfer(int devIndex, byte opCode, byte data, bool software = false, bool computeBufferOnly = false) { SPIResult r = null; var buffer = new List <byte>(10); var deviceToSkipFirst = this.DeviceCount - devIndex - 1; for (var d = 0; d < deviceToSkipFirst; d++) { buffer.Add(0); // OpCode buffer.Add(0); // Data } buffer.Add(opCode); // OpCode buffer.Add(data); // Data var deviceToSkipAfter = this.DeviceCount - (this.DeviceCount - devIndex); for (var d = 0; d < deviceToSkipAfter; d++) { buffer.Add(0); // OpCode buffer.Add(0); // Data } if (computeBufferOnly) { r = new SPIResult() { Succeeded = true }; r.Buffer = buffer; } else { r = SpiTransferBuffer(buffer, software); } return(r); }
public SPIResult Set(int digitalValue, ADDRESS pot = ADDRESS.POT_0) { try { this._spiEngine.Select(); var r = new SPIResult(); if ((digitalValue < this.MinDigitalValue) || (digitalValue > this.MaxDigitalValue)) { return(r); } this.DigitalValue = digitalValue; r = Transfer(pot, COMMAND.WRITE, digitalValue); //// Verify that the command error flag is off //var b0 = r.ReadBuffer[0]; //bool valid = !WinUtil.BitUtil.IsSet(b0, CMDERR_MASK); //r.Succeeded = r.Succeeded && valid; return(r); } finally { this._spiEngine.Unselect(); } }