0

I am writing a program to control a flashbulb. The flash fires in response to a key press by the user. I am trying to limit the occurence regularity of the flash to prevent the bulb burning out. I have already received some help from this forum, but am unable to implement the code with my own. A user suggested using a class, as follows:

class bulb
{
    __int64 clocks;
    __int64 frequency;
    public:
    bulb()
    {
        LARGE_INTEGER li;
        QueryPerformanceFrequency(&li);
        frequency = li.QuadPart;
        clocks = 0;
    }
    void WINAPI flash (HINSTANCE hThisInstance,
           HINSTANCE hPrevInstance,
           LPSTR lpszArgument,
           int nFunsterStil)
    {
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);

        // If this is the first occurence, set the 'clocks' to system time (+10000 to allow flash to occur)
        if (clocks == 0) clocks = li.QuadPart + 10000;

        __int64 timepassed = clocks - li.QuadPart;
        if (timepassed >= (((double)frequency) / 10000))
        {
            //Set the clock
            clocks = li.QuadPart;
            //Define the serial port procedure
            HANDLE hSerial;
            //Open the serial port (fire the flash)
            hSerial = CreateFile("COM1", GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
            //Close the serial port
            CloseHandle(hSerial);
        }
    }
};

I receive a few syntax errors that I can't seem to shift, all of which are at either the first or last bracket of the class - "syntax error : identifier 'bulb'", "syntax error : ';'", "syntax error : '}'" and "syntax error : '}'". I have never worked with classes before though, so expect this is something to do with that. Where am I going wrong?

Please note '10000' is the minimum delay between flashes.

CaptainProg
  • 5,610
  • 23
  • 71
  • 116

2 Answers2

1

There are a few major issues with your code:

  • Missing a ';' at the end of the class definition.
  • Missing a definition HANDLE hSerial before it is used.
  • You are comparing the time and frequency incorrectly on the line if (timepassed >= (((double)frequency) / 10000)). If you wish to convert the counter from QueryPerformanceCounter into a real time use something like:

    double RealTime = (double) clocks / (double) frequency;

If you are getting other error messages they are related to the code before or after the snippet you posted. A few more minor issues and comments:

  • Both QueryPerformanceFrequency and QueryPerformanceCounter can fail. Unless getting invalid values doesn't matter you should be checking the return values from these.
  • You open a COM port but don't write anything to it or confirm that the open succeeded or not.
  • To avoid future issues the if statement if (clocks == 0) should all be on one line or include brackets, i.e., one of:

:

if (clocks == 0) clocks = li.QuadPart + 10000;

if (clocks == 0) {
    clocks = li.QuadPart + 10000;
}

Edit: Example for converting QueryPerformanceCounter into real times (error checking not included):

LARGE_INTEGER Frequency;
LARGE_INTEGER Counter;

QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&Counter);

   //Time in seconds
double RealTime = (double) Counter.QuadPart / (double)Frequency.QuadPart;

LARGE_INTEGER Counter1;
QueryPerformanceCounter(&Counter1);

   //Elapsed time in seconds
double DeltaTime = (double) (Counter1.QuadPart - Counter.QuadPart) / (double)Frequency.QuadPart;

See Also: How to use QueryPerformanceCounter?

Community
  • 1
  • 1
uesp
  • 6,194
  • 20
  • 15
  • Thanks - I have implemented everything you have mentioned but don't understand the conversion of QueryPerformanceCounter into a 'real time'. I was given most of the above code by another member of this forum and don't understand how 'frequency' is used - as far as I can see, it will always remain zero? If this is the case then RealTime will always be the QueryPerformanceCounter value divided by zero, which is impossible. Your help is appreciated! – CaptainProg May 14 '11 at 13:53
  • I strongly disagree with the last point (about the brackets) -- the argument about adding new lines later doesn't sell me because I've never seen anyone do it. Ever. And formatting issues aren't really issues with the OP's code in any case. – Billy ONeal May 14 '11 at 14:56
  • It is more of a style issue which perhaps is beyond the scope of the question, but in most discussions people seem to prefer either keeping braces or keeping it all on one line: for example see http://stackoverflow.com/questions/815601/to-do-or-not-to-do-one-line-if-statements-and-curly-braces for a good discussion among others on SO. – uesp May 15 '11 at 01:07
0

add ; after last } in your code

class bulb
{
...
};
pure cuteness
  • 1,635
  • 11
  • 26