Help with C++ Code to Flowcode Conversion

unity-control
Posts: 26
http://meble-kuchenne.info.pl
Joined: Wed Sep 08, 2021 10:36 pm
Has thanked: 15 times
Been thanked: 5 times

Help with C++ Code to Flowcode Conversion

Post by unity-control »

Hi all,

I was wondering if someone much more clever than me could help me with the following code I am trying to convert into Flowcode.

Most of the blocks I can understand the logic behind (including the for loop as well), but what I am really struggling to understand is the following line:

if (missTargetCounter++ > 2)


How would one translate this into Flowcode?? I don't see any other part in the code where the missTargetCounter variable increments, so how can this be done inside the if statement? I am really lost with this one and fail to see the logic of this part especially. When does really this counter go over 2? I don't understand this, I am not a programmer per se :-(

This code is part of an automatic ADCOffset adjustment program of a video chip and there is no documentation for this bit, except for this code, that it is supposed to auto adjust the offset of the input signal.

If anyone can help with this it would be very much appreciated! Thanks in advance.

Code: Select all

void calibrateAdcOffset()
{
  uint16_t hitTargetCounter = 0;
  uint16_t readout16 = 0;
  uint8_t missTargetCounter = 0;
  uint8_t readout = 0;
  unsigned long startTimer = 0;

  for (uint8_t i = 0; i < 3; i++) {

    while ((millis() - startTimer) < 800) {
      readout16 = TEST_ADC::read() & 0x7fff;
      if (readout16 < 7) {
        hitTargetCounter++;
        missTargetCounter = 0;
      }
      else if (missTargetCounter++ > 2) {
        if (i == 0) {
          ADC_1::write(ADC_1::read() + 1); // incr. offset
          readout = ADC_1::read();
        }
        else if (i == 1) {
          ADC_2::write(ADC_2::read() + 1);
          readout = ADC_2::read();
        }
        else if (i == 2) {
          ADC_3::write(ADC_3::read() + 1);
          readout = ADC_3::read();
        }
        if (readout >= 0x52) {
          break;
        }
        delay(10);
        hitTargetCounter = 0;
        missTargetCounter = 0;
        startTimer = millis(); // extend timer
      }
      if (hitTargetCounter > 1500) {
        break;
      }
    }
    if (i == 0) {
      adc1 = ADC_1::read();
    }
    if (i == 1) {
      adc2 = ADC_2::read();
    }
    if (i == 2) {
      adc3 = ADC_3::read();
    }
  }
  if (readout >= 0x52) {
    adc1 = adc2 = adc3 = 0x40;
  }
}

mnfisher
Posts: 195
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 25 times
Been thanked: 101 times

Re: Help with C++ Code to Flowcode Conversion

Post by mnfisher »

Hi,

The counter is incremented in the

Code: Select all

else if (misstargetcounter++ > 2)
The ++ is a post increment operator - the comparison is done then the counter is incremented one by 1

Aside: putting ++ before the variable is pre increment and adds one before other operations.

So here - if readout16 is less than 7 the miss counter is reset to 0.
If readout 16 is greater or equal to 7 the 'miss' code is checked.

Martin

mnfisher
Posts: 195
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 25 times
Been thanked: 101 times

Re: Help with C++ Code to Flowcode Conversion

Post by mnfisher »

A quick 'go' in pseudo-flowcode (most of the detail is left as comments in C)
calibrateADC.fcfx
(11.96 KiB) Downloaded 11 times
Martin

unity-control
Posts: 26
Joined: Wed Sep 08, 2021 10:36 pm
Has thanked: 15 times
Been thanked: 5 times

Re: Help with C++ Code to Flowcode Conversion

Post by unity-control »

Hi Martin,

This is fantastic, both the explanation as well as the Flowchart :-)

Many thanks for this, it's been very helpful!

R

mnfisher
Posts: 195
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 25 times
Been thanked: 101 times

Re: Help with C++ Code to Flowcode Conversion

Post by mnfisher »

No problem,

Let us know how it goes...

Martin

unity-control
Posts: 26
Joined: Wed Sep 08, 2021 10:36 pm
Has thanked: 15 times
Been thanked: 5 times

Re: Help with C++ Code to Flowcode Conversion

Post by unity-control »

Hi Martin again,

Another question, this bit

if (readout >= 0x52) {
// some kind of failure
break;
}

I would believe I can use a connection point here, but what loop does this break statement actually break?

Thanks for clarifying again!

mnfisher
Posts: 195
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 25 times
Been thanked: 101 times

Re: Help with C++ Code to Flowcode Conversion

Post by mnfisher »

The 'break' here should exit the inner loop (while (millis - StartTime...))

The layout of the C code (indentation and positioning of brace / else pairs) is not how I'd usually do things - and so, to my eye at least, is a little hard to read... Opening it in a 'C aware' editor (I used Visual Studio) also makes things clearer.

Yes - you can use a connection point - looking at the code - I can't see any reason not to do :

Code: Select all

if (readout >= 0x52) {
   		adc1 = adc2 = adc3 = 0x40;
                break; (goto A:)
}
Then the second

Code: Select all

if (readout >= 0x52){
   ....
}
outside the loop can be removed. Saving a little time/code... (but maybe I miss something?)

Martin

unity-control
Posts: 26
Joined: Wed Sep 08, 2021 10:36 pm
Has thanked: 15 times
Been thanked: 5 times

Re: Help with C++ Code to Flowcode Conversion

Post by unity-control »

Many thanks Martin again!

Another one for you...

Code: Select all

static uint8_t getVideoMode() {
  uint8_t detectedMode = 0;

  if (rto->videoStandardInput >= 14) {
    detectedMode = STATUS_16::read();
    if ((detectedMode & 0x0a) > 0) { // bit 1 or 3 active?
      return rto->videoStandardInput; // still RGBHV bypass, 14 or 15
    }
    else {
      return 0;
    }
  }

  detectedMode = STATUS_00::read();
...
...
...
The "return 0" and the "return rto->videoStandardInput"

Does that totally exit the subroutine with 0 as a value and rto->videoStandardInput respectively? Another place to use "Goto Connection Point" in FC right?

Thanks again!!
R

unity-control
Posts: 26
Joined: Wed Sep 08, 2021 10:36 pm
Has thanked: 15 times
Been thanked: 5 times

Re: Help with C++ Code to Flowcode Conversion

Post by unity-control »

Hi Martin,

And one more please!!

short hSkew = random(-2, 2); // skew the target a little

How can I adapt the above code to Flowcode?

Cheers,
R

mnfisher
Posts: 195
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 25 times
Been thanked: 101 times

Re: Help with C++ Code to Flowcode Conversion

Post by mnfisher »

The return can be set in Flowcode using the 'return value' box at the end of the macro setup...
Screenshot 10-17-2021 16.03.01.png
Screenshot 10-17-2021 16.03.01.png (29.65 KiB) Viewed 358 times
Then to set the return value in the macro - in a calculation box -

Code: Select all

.Return = value
Note that FC only has one return point (officially at least) - the end of the macro, but you can set the value anywhere in the macro - either then 'goto' the endof the macro - or depending on the code - just wait till the end is reached. (It's also possible to 'return' earlier using a 'C' block :-) )

In this case rto will be a structure with a (named) member - videoStandardInput. It's a bit trickier - but you can shoehorn this into FC using a touch of C. Alternatively (depending on rto and how it's setup) - you can just use variables for each of the members..

Martin

Post Reply