Page 6 of 8

Re: Brightness auto adjusting to ambient light level

Posted: Mon Jul 04, 2011 5:42 pm
by erdem_ua
!- I learn that If you start with @ sign, you cannot post that message -!
@sbav1 I think this as command line program... Easier to run/debug :)

@tom_van I moved to general section.
Yes you warned. Dongle working this days without crystal. But sometime it's not. Stability might come with xtall but I couldn't make it run with Xtall yet.
Also got something like flickering (using 10 time refresh per sec). Specially on night, does it because incandescent lamp. I think it's due read value is border line of the backlight. Will digg this. And I also find similar device at sparkfun. With small hack, we could also use that $10 device...
http://www.sparkfun.com/products/9147
Image

Here is the sample code:

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <usb.h>

#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define BACK_LIGHT_REG 0x30220684
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

int main()

{
	int fd;
	unsigned long read_result, write_val;
	void *map_base, *virt_addr;

	//Enable access to registers
	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
		printf("/dev/mem can't open.\n");
		return 0;
		};
   printf("/dev/mem opened.\n");
   fflush(stdout);
	map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BACK_LIGHT_REG & ~MAP_MASK);
	if(map_base == (void *) -1){
		printf("Mem can't mapped\n");
		return 0;
		}
   printf("Memory mapped at address %p.\n", map_base);
   fflush(stdout);

	virt_addr = map_base + (BACK_LIGHT_REG & MAP_MASK);
   read_result = *((unsigned char *) virt_addr);

	//Initializing USB device
	struct usb_bus *bus;
	struct usb_device *dev;

	usb_init();
	usb_find_busses();
	usb_find_devices();

	for (bus = usb_busses; bus; bus = bus->next) {
		for (dev = bus->devices; dev; dev = dev->next) {
			//Currently using Vid:0x4242 Pid:0x0002
			if ( dev->descriptor.bDeviceClass == 0 && dev->descriptor.idVendor==0x4242 && dev->descriptor.idProduct==0x0002  ) {
			printf( "Found one!\n" );
			usb_dev_handle* usb_handle = usb_open(dev);
			if (usb_handle == 0) {
				printf( "Not able to claim the USB device\n" );
				return 0;
				}

			int ret;
			char string[255];
			if (dev->descriptor.iManufacturer) {
				ret = usb_get_string_simple(usb_handle, dev->descriptor.iManufacturer, string, sizeof(string));
				if (ret > 0)
					printf("- Manufacturer : %s\n", string);
				}
			if (dev->descriptor.iProduct) {
				ret = usb_get_string_simple(usb_handle, dev->descriptor.iProduct, string, sizeof(string));
				if (ret > 0)
					printf("- Product : %s\n", string );
				}

			unsigned char buffer[10];
			int i;
			for( i=0 ; i < sizeof(buffer) ; i++) buffer[i]=0;

			int x=0;
			x=usb_detach_kernel_driver_np( usb_handle, 0);
			printf("- usb_detach_kernel_driver_np : %d\n", x);
			x=usb_set_configuration(usb_handle, 1);
			printf("- usb_set_configuration : %d\n", x);

			x=usb_claim_interface(usb_handle, 0);
			printf("- usb_claim_interface : %d\n", x);
			int err_cnt=0;
			while(1)
				{
				int read;
				//read = usb_bulk_read(usb_handle, 0x81, (char*)buffer, 3, 10000);
				read = usb_interrupt_read(usb_handle, USB_ENDPOINT_IN | 1,  buffer,  sizeof(buffer), 10000);

				if(read < 0 ){
					printf( "Error : %s", *strerror(read) );
					//if some error, like removed USB device, this will break the while loop.
					if( err_cnt++ > 100);
						break;
					}

				unsigned sensor_data = buffer[0];// convert 10 bit buffer to uint16_t
				sensor_data = sensor_data << 8;			// little endian
				sensor_data += (unsigned char)buffer[1];

				//register accepts 0x33 to x100 = 0xCD = 205 different level. 1024 is max sensor return.
				unsigned long temp = (sensor_data*205)/1023;

				///Here we could multiple temp to adjust/calibrate brightnest
				//temp = temp *2; //this doubles the effect...

				write_val = temp + 0x33;

				if(write_val > 0x100) write_val=0x100;
				if(write_val < 0x33) write_val=0x33;
				if(read == 2 ){
					printf( "Read byte count : %d - as decimal sensol value :  %d Write val : 0x%X\n" , read, sensor_data, write_val);

					read_result = *((unsigned long *) virt_addr);
					//this discards noise, unwanted flickery etc but halves the resolution...
					if(abs(write_val - read_result) > 1){
						*((unsigned long *) virt_addr) = write_val;
						read_result = *((unsigned long *) virt_addr);
						}
					printf("Register 0x%X : 0x%X\n", BACK_LIGHT_REG, read_result);

					}
				usleep( 100000 );
				}
			usb_close( usb_handle );
			}
		}
	}
	return 0;
}
Edit: avr chip code just put 2 byte light sensor value, which is 1023 maximum (10 bit). But couldn't cleaned code yet.

Re: Brightness auto adjusting to ambient light level

Posted: Mon Jul 04, 2011 7:45 pm
by arris69
do you guys plan to use this devices on c-series too?
the kernel has some filter for device classes.

maybe you just need to know :)

arris

Re: Brightness auto adjusting to ambient light level

Posted: Mon Jul 04, 2011 9:36 pm
by erdem_ua
Off course, even on D series.
I am interested with that filtering subject.
Which restrictions they have?
It's good to complain about those at meeting*.

Re: Brightness auto adjusting to ambient light level

Posted: Mon Jul 04, 2011 10:19 pm
by tom_van
erdem: I commited http://samygo.svn.sourceforge.net/viewv ... iew=markup
for your reference. We should unify method of usb access. Does GenericHID have better chance to pass a filter in C-series on not?
erdem_ua wrote: Also got something like flickering (using 10 time refresh per sec). Specially on night, does it because incandescent lamp. I think it's due read value is border line of the backlight.
Quite high refresh freq. I take a sample once a second and use "lazy rounding". Haven't experienced any flickerig since using I2C write to LocalDimmer.
Does not interfere your test program with writes from exeDSP to the same register?
erdem_ua wrote: And I also find similar device at sparkfun. With small hack, we could also use that $10 device...
http://www.sparkfun.com/products/9147
Nice kitty. But again soft-only usb.

Re: Brightness auto adjusting to ambient light level

Posted: Mon Jul 04, 2011 10:47 pm
by erdem_ua
My device is genericHID (gamepad) device, already.
For rounding, it's better to handle it on MCU code.

Also I wanted to have "responsive" light sensor. When I open the light, wanted to see update on live. Not once at every second :)
I used continuous update before. Than lowered to 10 per sec later :)
I believe it's not related with exeDSP since disabled even dynamic contrast...

Re: Brightness auto adjusting to ambient light level

Posted: Tue Jul 05, 2011 5:48 am
by tom_van
erdem_ua wrote: For rounding, it's better to handle it on MCU code.
It would be better only if MCU outputs final value to write into backlight register.
As we both do some math is TV sw, the final value should be protected from digital noise.
erdem_ua wrote: Also I wanted to have "responsive" light sensor. When I open the light, wanted to see update on live. Not once at every second :)
http://en.wikipedia.org/wiki/Adaptation_%28eye%29
Perfectly ergonomic autobright would follow your pupillary light reflex, not real light. So there is no such hury...

Re: Brightness auto adjusting to ambient light level

Posted: Tue Jul 05, 2011 9:50 am
by arris69
erdem_ua wrote:Off course, even on D series.
I am interested with that filtering subject.
Which restrictions they have?
something like this

Code: Select all

+/* on chip USB device for US9000 and HotelTV*/
+#define        USB_DRM_CHIP_VID        0x0525
+#define        USB_DRM_CHIP_PID        0xa4a1
+#define        USB_ENCODER_VID 0x1a8c
+#define        USB_ENCODER_PID 0x0104
+
+static int IsUSBOnChip(struct usb_device *dev)
+{
+       int ret = 0;
+
+       if(dev->descriptor.idVendor || dev->descriptor.idProduct )
+       {
+               if((dev->descriptor.idVendor == USB_DRM_CHIP_VID) && (dev->descriptor.idProduct == USB_DRM_CHIP_PID))
+               {
+                       ret = 1;
+               }
+               else if ((dev->descriptor.idVendor == USB_ENCODER_VID) && (dev->descriptor.idProduct == USB_ENCODER_PID))
+               {
+                       ret =1;
+               }
+       }
+
+       return ret;
+}
+
+
+//VD_COMP_USB_PATCH
+// for USB2.0 cerification
+static int ReadUsbClass(struct usb_device *dev)
+{
+       int i=0;
+       int ret = 0;
+       int usbClass = 0;
+
+       if(dev->actconfig)
+       {
+               for(i=0; i<USB_MAXINTERFACES; i++)
+               {
+                       if(dev->actconfig->interface[i])
+                       {
+                               if(dev->actconfig->interface[i]->cur_altsetting == NULL)
+                               {
+                                       continue;
+                               }
+
+                               usbClass = dev->actconfig->interface[i]->cur_altsetting->desc.bInterfaceClass;
+
+                               switch(usbClass)
+                               {
+                                       /* supported usb device, Not inform */
+                                       case USB_CLASS_STILL_IMAGE:
+                                       case USB_CLASS_MASS_STORAGE:
+                                       case USB_CLASS_HUB:
+                                               ret = 0;
+                                               break;
+                                       /* not supported usb device, conneting inform */
+                                       case USB_CLASS_PER_INTERFACE:
+                                       case USB_CLASS_AUDIO:
+                                       case USB_CLASS_COMM:
+                                       case USB_CLASS_HID:
+                                       case USB_CLASS_PHYSICAL:
+                                       case USB_CLASS_PRINTER:
+                                       case USB_CLASS_CDC_DATA:
+                                       case USB_CLASS_CSCID:
+                                       case USB_CLASS_CONTENT_SEC:
+                                       case USB_CLASS_VIDEO:
+                                       case USB_CLASS_WIRELESS_CONTROLLER:
+                                       case USB_CLASS_MISC:
+                                       case USB_CLASS_APP_SPEC:
+                                       case USB_CLASS_VENDOR_SPEC:
+                                               ret = 1;
+                                               break;
+                                       default:
+                                               ret = 1;
+                                               break;
+                               }
+                               break;
+                       }
+                       else
+                       {
+                               break;
+                       }
+               }
+       }
+
+       return ret;
+}
It's good to complain about those at meeting*.
think won't help, it's for "USB2.0 cerification" lol

arris

Re: Brightness auto adjusting to ambient light level

Posted: Tue Jul 05, 2011 10:21 am
by gooseye
There must be a lot of redundant PS3 jailbreak dongles (AT90USB based) which could be adapted for this purpose.

Re the filtering, what class of device is the Skype camera I wonder?

Re: Brightness auto adjusting to ambient light level

Posted: Tue Jul 05, 2011 11:42 am
by arris69
gooseye wrote:There must be a lot of redundant PS3 jailbreak dongles (AT90USB based) which could be adapted for this purpose.

Re the filtering, what class of device is the Skype camera I wonder?
looks like the skype cam has extra handling based on vid/pid (see: drivers/usb/core/devio.c MOIP_* stuff)

hth
arris

Re: Brightness auto adjusting to ambient light level

Posted: Tue Jul 05, 2011 12:49 pm
by erdem_ua
As I understand, they just filter PID/VID.
It's configurable on V-USB approach. :D
I don't know if it's same on hardware based MCUs.

If we could produce a code for an ordinary webcam to use skype at our TV's (not needed to be HD resolution), it's a big movement. :)