Samsung Intercept
Find Your Forum
  
Welcome, Guest. Please login or register.
Did you miss your activation email?

News: Do you like Samsung Intercept?
Advanced search
Scratch-Proof your Samsung Intercept              Best Screen Protector for Samsung Intercept Latest Ringtone for your mobile
Pages: 1 [2] 3 4 ... 6
Print
Topic: Help getting LED flash to work?  (Read 37615 times)
jvrobert
Newbie
*

Cookies: 8
Posts: 21


View Profile Email
« Reply #15 on: December 29, 2009, 01:01:05 pm »

I have the problem about 75% cracked.  I can turn on the flash with some reliability and also off from an APK using a native library that does an ioctl.

The problem is the ioctl is very sensitive to the state of the camera and I'm not quite sure how manage the camera state correctly.  It ends up crashing either the camera (meaning if you close app and open camera it fails to load), the app (in which case the flash locks on), or the phone (have to remove battery and reboot it).

Anyway, here's what seems to (barely and badly) work for setting the flash to on/off.  I'm managing the state from the Java side with the Camera class, but maybe I need to do some init ioctls from C.

Code:

#include <string.h>
#include <jni.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <linux/videodev2.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */
#define VIDIOC_S_FLASH_CAMERA      _IOW  ('V', 98, void *)
#define VIDIOC_S_FLASH_MOVIE      _IOW  ('V', 99, void *)
#define VIDIOC_S_CAMERA_START    _IO  ('V', BASE_VIDIOC_PRIVATE + 0)

extern "C" {
JNIEXPORT jstring JNICALL
Java_jvr_test_FlashTest_setFlash( JNIEnv* env,
                                            jobject thiz, int i)
{
   struct v4l2_control cont;
   cont.id = 0;
   cont.value = i;
   int fd= open("/dev/video13", O_RDWR);
   //int r = ioctl(fd, VIDIOC_S_CAMERA_START, &cont);
   int r = ioctl(fd, VIDIOC_S_FLASH_MOVIE, &cont);
   close(fd);
   if (i)
          return env->NewStringUTF("Flash On");
   else   
          return env->NewStringUTF("Flash Off");
}
}
Logged
jvrobert
Newbie
*

Cookies: 8
Posts: 21


View Profile Email
« Reply #16 on: December 29, 2009, 01:36:56 pm »

Got it..  I guess anyone who's done kernel stuff would know this but I haven't done a bunch - you need to open the ioctl, do your ops, then close it.

This code, in a simple Java app that opens the camera, has a button which calls setFlash() alternately with 1/0, and then closes the camera on exit seems to work perfectly - flash go on, flash go off, flash go on, flash go off  ;D

If anyone's interested I could post the Java code (it's straightforward) or a sample APK.

#include <string.h>
#include <jni.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <linux/videodev2.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */
#define VIDIOC_S_FLASH_CAMERA      _IOW  ('V', 98, void *)
#define VIDIOC_S_FLASH_MOVIE      _IOW  ('V', 99, void *)
#define VIDIOC_S_CAMERA_START    _IO  ('V', BASE_VIDIOC_PRIVATE + 0)
#define VIDIOC_S_CAMERA_STOP    _IO  ('V', BASE_VIDIOC_PRIVATE + 1)

extern "C" {

static int fd;
JNIEXPORT void JNICALL
Java_jvr_test_FlashTest_openCamera( JNIEnv* env,
                                            jobject thiz)
{
   fd = open("/dev/video13", O_RDWR);
   int r = ioctl(fd, VIDIOC_S_CAMERA_START, (unsigned long)1);
}

JNIEXPORT void JNICALL
Java_jvr_test_FlashTest_closeCamera( JNIEnv* env,
                                            jobject thiz)
{
   int r = ioctl(fd, VIDIOC_S_CAMERA_STOP, (unsigned long)1);
   close(fd);
}

JNIEXPORT jstring JNICALL
Java_jvr_test_FlashTest_setFlash( JNIEnv* env,
                                            jobject thiz, int i)
{
   struct v4l2_control cont;
   cont.id = 0;
   cont.value = i;
   //int r = ioctl(fd, VIDIOC_S_CAMERA_START, &cont);
   int r = ioctl(fd, VIDIOC_S_FLASH_MOVIE, &cont);
   if (i)
          return env->NewStringUTF("Flash On");
   else   
          return env->NewStringUTF("Flash Off");
}

}
Logged
serac
Newbie
*

Cookies: 0
Posts: 3


View Profile
« Reply #17 on: December 29, 2009, 02:15:54 pm »

Thats really cool. If you posted the .apk i would love to test it out for you.
Logged
ducktard13
Newbie
*

Cookies: 0
Posts: 5


View Profile
« Reply #18 on: December 29, 2009, 03:08:09 pm »

I have no clue abotu the programming what-so-ever but please post up so that people can test and i can leech :)
Logged
jvrobert
Newbie
*

Cookies: 8
Posts: 21


View Profile Email
« Reply #19 on: December 29, 2009, 03:09:17 pm »

I'll post it shortly.  I'm trying to make a Widget out of it so it's a desktop button you can hit to toggle.  I'm brand new to Android development so bit of a learning curve, I'm 80% done.
Logged
ducktard13
Newbie
*

Cookies: 0
Posts: 5


View Profile
« Reply #20 on: December 29, 2009, 03:11:18 pm »

I am really going to have to start learning to code and all this sillyness. just a matter of time i suppose.  Time which i don't have in the first place....well, back to the lab to tweak the delorean!
Logged
jvrobert
Newbie
*

Cookies: 8
Posts: 21


View Profile Email
« Reply #21 on: December 29, 2009, 05:22:32 pm »

OK, I've attached a test apk.  It _should_ allow you to (from home) click Menu, Add, Widget, then "FlashTest" at which point a button _should_ appear on your home screen if you have space (if not, use alternate).

Then it _should_, if you click it, toggle the LED on and off.

Caveats:
My first android app of any kind
Could damage your LED as far as I know if you leave it on too long (and needless to say I'm not responsible if this happens).
Could crash your phone
Could interact with other camera apps in a bad way

In general, caveat emptor - this is just for testing.  That said - it appears to work perfectly on my Intercept and the flash toggles on and off as expected.

I'm (I think?) setting the flash to "movie mode" which means it should be the lesser brightness and shouldn't blow up your LED.  If anyone can confirm that it looks like the lesser brightness let me know.

[Temporarily removed apk, needs a little more debugging]
« Last Edit: December 29, 2009, 06:53:08 pm by jvrobert » Logged
latinmaxima
Newbie
*

Cookies: 0
Posts: 7


View Profile
« Reply #22 on: December 29, 2009, 06:32:19 pm »

How do I install this on the phone again? Sorry I came here on the mobile browser and downloaded it. It is asking me though is I want to use one of the emulators on my phone to open?

Nevermind. did some searching and am downloading app manager to install from sd card. I will let you know.

Ok have it installed and added as a widget to the home screen. It is causes a force close though? Could I have installed it wrong?
« Last Edit: December 29, 2009, 06:47:30 pm by latinmaxima » Logged
jvrobert
Newbie
*

Cookies: 8
Posts: 21


View Profile Email
« Reply #23 on: December 29, 2009, 06:52:36 pm »

Lol, I rebooted my phone and now I get the same problem which is actually good as I can debug it further...  I'll post another version when I figure out what happened.

EDIT: OK - it's an "easy" problem, for some reason when I install it the shared library isn't going on unless I do it through the Eclipse UI deployer.  So it's nothing fundamental.. should be easy fix here shortly.

EDIT: Here's round 2...  This one's signed (with dummy key) and properly loads the shared library for me even when installed through app manager.  Let me know if it works for you or not.

EDIT: One note, something's still slightly off.  You'll need to take a picture with your camera (with flash on) then exit camera app and then it should work.  Still some shenanigans going on with the camera initialization, I'll muck with it more but let me know if this works on your phone after taking flash picture.  If you don't open and use the camera app first it'll probably lock your phone necessitating a battery removal/reset.

* Flash.apk (13 KB - downloaded 179 times.)
« Last Edit: December 29, 2009, 07:25:37 pm by jvrobert » Logged
chibucks
Jr. Member
**

Cookies: 6
Posts: 66


View Profile Email
« Reply #24 on: December 29, 2009, 07:29:23 pm »

Worked for me!  Awesome job.

Edit...  didn't have to do the camera trick either.

Edit2:  yeah, may have used the camera after reboot...
« Last Edit: December 29, 2009, 10:28:50 pm by chibucks » Logged
jvrobert
Newbie
*

Cookies: 8
Posts: 21


View Profile Email
« Reply #25 on: December 29, 2009, 08:54:55 pm »

OK, so sounds like it works for at least one other person besides me.  I'll work on a version that doesn't require the "use the camera first" trick and post it up here sometime in the next day or two.  My guess is if it worked "out of the box" for you you had used your camera already at some point since last reboot.  Otherwise it will probably hang your phone requiring a battery restart.

Stay tuned.
Logged
maxdamage2122
Newbie
*

Cookies: 0
Posts: 1


View Profile
« Reply #26 on: December 29, 2009, 09:53:57 pm »

Ya I just tried it without reading all the posts and my phone froze. Worked after a reboot and using the camera flash before using the app. Also the widget takes up two spaces which I don't think you meant to do.
Logged
scriz
Newbie
*

Cookies: 0
Posts: 11


View Profile Email
« Reply #27 on: December 29, 2009, 10:47:15 pm »

worked for me!...need to work out the camera issue tho :P
Logged
jvrobert
Newbie
*

Cookies: 8
Posts: 21


View Profile Email
« Reply #28 on: December 30, 2009, 12:07:12 am »

Ya I just tried it without reading all the posts and my phone froze. Worked after a reboot and using the camera flash before using the app. Also the widget takes up two spaces which I don't think you meant to do.

Yeah, I don't know much android yet so just hacked together bits and pieces of stuff out there.  Layout's just a quick "make it work" job. 

I'll do something a little nicer (maybe just a little button with a red light in the middle indicating on, no red light indicating off) and fix the camera bug soon.  Maybe also allow selection of "torch mode" and "camera mode" in a configuration or by double tapping the button or something.
Logged
halmo20
Newbie
*

Cookies: 0
Posts: 1


View Profile
« Reply #29 on: December 30, 2009, 12:35:35 am »

I've been looking for something like this for a long time.  Just want to say thank you for your great work!

Ya I just tried it without reading all the posts and my phone froze. Worked after a reboot and using the camera flash before using the app. Also the widget takes up two spaces which I don't think you meant to do.

Yeah, I don't know much android yet so just hacked together bits and pieces of stuff out there.  Layout's just a quick "make it work" job. 

I'll do something a little nicer (maybe just a little button with a red light in the middle indicating on, no red light indicating off) and fix the camera bug soon.  Maybe also allow selection of "torch mode" and "camera mode" in a configuration or by double tapping the button or something.
Logged
Pages: 1 [2] 3 4 ... 6
Print

Jump to:  

Got a new phone? Find the forum here






Thanks for visiting Samsung Intercept Forum Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Sitemap