@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
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;
}