Skip to content

Commit

Permalink
Remove space (#1275)
Browse files Browse the repository at this point in the history
* Remove space

* Fix Mcp3xxx switch condition causing the build to break

* Fix other switch statements that were changed incorrectly

Co-authored-by: Jose Perez Rodriguez <[email protected]>
  • Loading branch information
richlander and joperezr authored Nov 10, 2020
1 parent 0b7733e commit 084a7d8
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 42 deletions.
25 changes: 19 additions & 6 deletions src/devices/Card/Mifare/MifareCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,26 @@ public void SetCapacity(ushort ATAQ, byte SAK)
/// </summary>
/// <param name="blockNumber">Input block number</param>
/// <returns>True if it is a sector block</returns>
public bool IsSectorBlock(byte blockNumber) => Capacity switch
public bool IsSectorBlock(byte blockNumber)
{
MifareCardCapacity.Mifare300 | MifareCardCapacity.Mifare1K | MifareCardCapacity.Mifare2K => blockNumber % 4 == 3,
MifareCardCapacity.Mifare4K when blockNumber < 128 => blockNumber % 4 == 3,
MifareCardCapacity.Mifare4K => blockNumber % 16 == 15,
_ => blockNumber % 4 == 3,
};
switch (Capacity)
{
default:
case MifareCardCapacity.Mifare300:
case MifareCardCapacity.Mifare1K:
case MifareCardCapacity.Mifare2K:
return blockNumber % 4 == 3;
case MifareCardCapacity.Mifare4K:
if (blockNumber < 128)
{
return blockNumber % 4 == 3;
}
else
{
return blockNumber % 16 == 15;
}
}
}

/// <summary>
/// Depending on the command, serialize the needed data
Expand Down
30 changes: 19 additions & 11 deletions src/devices/GrovePi/GrovePi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,26 @@ public void WriteCommand(GrovePiCommand command, GrovePort pin, byte param1, byt
/// <returns></returns>
public byte[]? ReadCommand(GrovePiCommand command, GrovePort pin)
{
int numberBytesToRead = command switch
int numberBytesToRead = 0;
switch (command)
{
GrovePiCommand.DigitalRead => 1,
GrovePiCommand.AnalogRead | GrovePiCommand.UltrasonicRead | GrovePiCommand.LetBarGet => 3,
GrovePiCommand.Version => 4,
GrovePiCommand.DhtTemp => 9,
_ => 0,
};

if (numberBytesToRead == 0)
{
return null;
case GrovePiCommand.DigitalRead:
numberBytesToRead = 1;
break;
case GrovePiCommand.AnalogRead:
case GrovePiCommand.UltrasonicRead:
case GrovePiCommand.LetBarGet:
numberBytesToRead = 3;
break;
case GrovePiCommand.Version:
numberBytesToRead = 4;
break;
case GrovePiCommand.DhtTemp:
numberBytesToRead = 9;
break;
// No other commands are for read
default:
return null;
}

byte[] outArray = new byte[numberBytesToRead];
Expand Down
53 changes: 39 additions & 14 deletions src/devices/Mcp3428/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,47 @@ public static byte I2CAddressFromPins(PinState adr0, PinState adr1)
{
byte addr = 0b1101000; // Base value from doc

int idx = (byte)adr0 << 4 + (byte)adr1;
var idx = (byte)adr0 << 4 + (byte)adr1;

byte addr2 = idx switch
switch (idx)
{
0 | 0x22 => 0,
0x02 => 1,
0x01 => 2,
0x10 => 4,
0x12 => 5,
0x11 => 6,
0x20 => 3,
0x21 => 7,
_ => throw new ArgumentException("Invalid combination"),
};

return addr += addr2;
case 0:
case 0x22:
break;

case 0x02:
addr += 1;
break;

case 0x01:
addr += 2;
break;

case 0x10:
addr += 4;
break;

case 0x12:
addr += 5;
break;

case 0x11:
addr += 6;
break;

case 0x20:
addr += 3;
break;

case 0x21:
addr += 7;
break;

default:
throw new ArgumentException("Invalid combination");
}

return addr;
}

public static byte SetChannelBits(byte configByte, int channel)
Expand Down
43 changes: 33 additions & 10 deletions src/devices/Mcp3xxx/Mcp3xxx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,47 @@ public virtual int ReadDifferential(int valueChannel, int referenceChannel)
/// <returns>A value corresponding to relative voltage level on specified device channel</returns>
protected int ReadInternal(int channel, InputType inputType, int adcResolutionBits)
{
int channelVal;
int requestVal;

CheckChannelRange(channel, inputType == InputType.SingleEnded ? ChannelCount : ChannelCount / 2);

// create a value that represents the channel value. For differental inputs
// then incorporate the lower bit which indicates if the channel is inverted or not.
int channelVal = inputType switch
switch (inputType)
{
InputType.Differential | InputType.InvertedDifferential => channel * 2,
_ =>channel,
};
case InputType.Differential:
channelVal = channel * 2;
break;

case InputType.InvertedDifferential:
channelVal = channel * 2;
break;

default:
channelVal = channel;
break;
}

// create a value to represent the request to the ADC
int requestVal = ChannelCount switch
switch (ChannelCount)
{
4 | 8 => (inputType == InputType.SingleEnded ? 0b1_1000 : 0b1_0000) | channelVal,
2 => (inputType == InputType.SingleEnded ? 0b1101 : 0b1001) | channelVal << 1,
1 => 0,
_ => throw new ArgumentOutOfRangeException("Unsupported Channel Count"),
};
case 4:
case 8:
requestVal = (inputType == InputType.SingleEnded ? 0b1_1000 : 0b1_0000) | channelVal;
break;

case 2:
requestVal = (inputType == InputType.SingleEnded ? 0b1101 : 0b1001) | channelVal << 1;
break;

case 1:
requestVal = 0;
break;

default:
throw new ArgumentOutOfRangeException("Unsupported Channel Count");
}

// read the data from the device...
// the delayBits is set to account for the extra sampling delay on the 3004, 3008, 3204, 3208, 3302 and 3304
Expand Down
2 changes: 1 addition & 1 deletion src/devices/Pca9685/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ You can also use it to control servos.

https://www.nxp.com/docs/en/data-sheet/PCA9685.pdf

## References
## References

https://www.nxp.com/products/analog/interfaces/ic-bus/ic-led-controllers/16-channel-12-bit-pwm-fm-plus-ic-bus-led-controller:PCA9685

0 comments on commit 084a7d8

Please sign in to comment.