Good day.
I'm working with the Tinkerforge Java API to do some programming.
I ran into a Problem, where i've to create a short Mask looking something like:
"0b11011011"
I'm using the following code to parse my config into the Mask Required: I'm using a Char Array as an easy way to modify the mask, and Parsing it to a String , and using short.parseshort to create the short mask.
//Code Before...
log.debug("Calculating I Mask for Port A");
Mask = CalculateIMask(A);
log.debug("Recieved Mask: " + Mask);
//Applying Mask to the IOPort
IO.setPortConfiguration('a', Mask, 'i', true);
//...
public short CalculateIMask(List<IOPort> IOPL)
{
char[] Mask = {'0','0','0','0','0','0','0','0'};
for(IOPort IOP : IOPL)
{
if(IOP.isInput())
{
log.debug("Setting Pin: " + IOP.getPin() + " to 1");
Mask[Integer.valueOf(IOP.getPin())] = '1';
}
}
String SMask = String.valueOf(Mask);
log.debug("Returning Mask: " + SMask);
return Short.parseShort(SMask, 2);
}
The Log looks something like this.
[2016-09-16 13:06:48,252] DEBUG [IO16Register] Calculating I Mask for Port A
[2016-09-16 13:06:48,252] DEBUG [IO16Register] Setting Pin: 0 to 1
[2016-09-16 13:06:48,252] DEBUG [IO16Register] Setting Pin: 2 to 1
[2016-09-16 13:06:48,252] DEBUG [IO16Register] Returning Mask: 10100000
[2016-09-16 13:06:48,252] DEBUG [IO16Register] Recieved Mask: 160
The IO.setPortConfiguration does not throw an error, but also does not apply my mask, i suppose it has something to do with the Short.parseShort.
Also the Second Logfile after Parsing does not show the correct mask.
I'm struggling with this issue for a while now, i also tried using a
short[] Mask = {'0','0','0','0','0','0','0','0'};
instead of the char Array. and using a simple for
(short Piece : Mask)
to apply all the pieces to one short, but the log resulted in showing: "48484948494848".
I hope someone can give me some advice, i looked in some of the threads, but none of those seem to help me with my problem.
Thank you
Sincerly
Fabian95qw
//edit: The Problem was caused by missing authentication before trying to modify the settings.
I was doing that beforehand, but forgot i swallowed the exception without applying it to the logfile...
com.tinkerforge.TimeoutException: Did not receive response in time for function ID -1 at com.tinkerforge.DeviceBase.sendRequest(DeviceBase.java:169) at com.tinkerforge.BrickletIO16.getIdentity(BrickletIO16.java:502) at nucom.module.tinkerforge.bricklets.IO16.ReloadConfig(IO16.java:85)
One Problem remains: How the Binary appends to the Brick:
I'm generating the Following:
Binary: ==0 0 0 0 0 0 0 0
Port No:==0 1 2 3 4 5 6 7
But the Program Appends it in this Direction:
Binary: ==0 0 0 0 0 0 0 0
Port No:==7 6 5 4 3 2 1 0
Now i just need to modify the code to correctly invert it...
//edit: And i've done it.
I'm using this Code Snipped to correctly generate my mask.
public short CalculateIMask(List<IOPort> IOPL)
{
char[] Mask = {'0','0','0','0','0','0','0','0'};
for(IOPort IOP : IOPL)
{
if(IOP.isInput())
{
log.debug("Setting Pin: " + IOP.getPin() + " to 1");
//Old Code Piece:
//Mask[Integer.valueOf(IOP.getPin())] = '1';
Mask[7-Integer.valueOf(IOP.getPin())] = '1';
}
}
String SMask = String.valueOf(Mask);
log.debug("Returning Mask: " + SMask);
return Short.parseShort(SMask, 2);
}