Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the camera light flash in a specific sequence?

I am developing a simple Morse code app that converts English to Morse and vice versa. When the user enters in a specific letter the camera light on the device is supposed to flash that Morse sequence for the letter, for example:

A = dot dash with the dot being 200ms and the dash being 500ms.

The problem is that i am not sure exactly how to achieve this. I have being attempting to resolve this using a for loop but i cannot seem to solve it. Below is the code i am currently trying adjust - i am not sure if using a for loop is the best methods but i cannot think of another way. Does anyone have any suggestions/ideas, if so they would be greatly appreciated.

  public void flashTranslation() {

    char[] cArray = message.toCharArray();

    for (int i = 0; i < cArray.length; i++) {


        if (cArray[i] == '.') {
            turnOn();

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    turnOff();

                    camera.release();
                }
            }, dot);

        } else {

            turnOn();

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    turnOff();

                    camera.release();
                }
            }, dash);
        }

    }
}

public void turnOn() {

    if (camera != null) {
        Camera.Parameters p = camera.getParameters();
        p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        camera.startPreview();
    }


}

public void turnOff() {

    if (camera != null) {
        Camera.Parameters p = camera.getParameters();
        p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
    }
}
like image 260
Mr_Tree Avatar asked Jan 01 '16 13:01

Mr_Tree


People also ask

How do you sync flash with shutter speed?

To set your camera and flash for high speed sync, go to your camera's Custom Setting menu, then scroll to Bracketing/Flash, where you'll see flash sync speed choices. Set the highest speed you see—it'll be either 1/200, 1/250 or 1/320 second depending on your camera.


1 Answers

The reason this code may be giving you issues is due to the fact that there is no time in between the light turning off and turning back on again.

In an instant, the light turns on. Then, the runnable waits 200ms before turning it off. After turning it off, it will instantly turn it back on again, which means that although the point between turning it on to off was 200ms, the time between turning it off back to on was (approaching) 0ms. This might be causing the light to simply appear to be consistently on without any of the intended pauses.

If I'm not mistaken, creating a new postDelayed handler simply executes a runnable on a new thread, which isn't really what you'd want due to the fact you want the morse to be read in a queue.

Try something like this:

public void flashTranslation() {
    int offIntervalTime = 50;

    char[] cArray = message.toCharArray();

    for (int i = 0; i < cArray.length; i++) {
        if (cArray[i] == '.') {
            turnOn();
            Thread.sleep(dot);

            turnOff();
            Thread.sleep(offIntervalTime);
        } else {
            turnOn();
            Thread.sleep(dash);

            turnOff();
            Thread.sleep(offIntervalTime);
        }
    }
}

You'll probably also need to implement exception handling for thread interruptions, but this is the basic structure of what you can use. Notice there is an offIntervalTime variable. This is used to define the constant gap in which the light should be off, thus allowing it to actually flash.

When you call the function, make sure it is not on the primary thread, so do something like this where you call it:

Thread morseMessageThread = new Thread(new Runnable() { flashTranslation() });
morseMessageThread.start();
like image 120
Shane Duffy Avatar answered Sep 28 '22 08:09

Shane Duffy