Wiimote IR Camera test program, adapted from kako\'s source: http://www.kako.com/neta/2008-009/2008-009.html Generates ~20MHz clock signal for camera using PWM on p21. Communicates with camera via I2C on p9, p10. Schematic: {{http://lh4.ggpht.com/_59HHJuZw_Rk/TRxBhT0q6iI/AAAAAAAABq8/_dlbN1rIQb4/s912/mbed_schematic.jpg|600|400}} [[http://code.google.com/p/wii-cam-blobtrack/|Java GUI]] [[http://www.bot-thoughts.com/2010/12/connecting-mbed-to-wiimote-ir-camera.html|Interfacing Details]]

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 // Adapted from kako's source code: http://www.kako.com/neta/2008-009/2008-009.html
00004 // i2c protocol details from - http://blog.makezine.com/archive/2008/11/hacking_the_wiimote_ir_ca.html
00005 // wiring from - http://translate.google.com/translate?u=http://www.kako.com/neta/2007-001/2007-001.html&hl=en&ie=UTF-8&sl=ja&tl=en
00006 // obviously mbed is 3.3v so no level translation is needed
00007 // using built in i2c on pins 9/10
00008 //
00009 // PC GUI client here: http://code.google.com/p/wii-cam-blobtrack/
00010 //
00011 // Interfacing details here: http://www.bot-thoughts.com/2010/12/connecting-mbed-to-wiimote-ir-camera.html
00012 //
00013 
00014 DigitalOut myled(LED1);
00015 PwmOut servo(p21);
00016 Serial pc(USBTX, USBRX); // tx, rx
00017 I2C i2c(p9, p10);        // sda, scl
00018 const int addr = 0xB0;   // define the I2C Address of camera
00019 
00020 void i2c_write2(int addr, char a, char b)
00021 {
00022     char cmd[2];
00023     
00024     cmd[0] = a;
00025     cmd[1] = b;
00026     i2c.write(addr, cmd, 2);
00027     wait(0.07); // delay 70ms    
00028 }
00029 
00030 void clock_init()
00031 {
00032     // set up ~20-25MHz clock on p21
00033     LPC_PWM1->TCR = (1 << 1);               // Reset counter, disable PWM
00034     LPC_SC->PCLKSEL0 &= ~(0x3 << 12);  
00035     LPC_SC->PCLKSEL0 |= (1 << 12);          // Set peripheral clock divider to /1, i.e. system clock
00036     LPC_PWM1->MR0 = 4;                     // Match Register 0 is shared period counter for all PWM1
00037     LPC_PWM1->MR6 = 2;                      // Pin 21 is PWM output 6, so Match Register 6
00038     LPC_PWM1->LER |= 1;                     // Start updating at next period start
00039     LPC_PWM1->TCR = (1 << 0) || (1 << 3);   // Enable counter and PWM
00040 }
00041 
00042 void cam_init()
00043 {
00044     // Init IR Camera sensor
00045     i2c_write2(addr, 0x30, 0x01);
00046     i2c_write2(addr, 0x30, 0x08);    
00047     i2c_write2(addr, 0x06, 0x90);
00048     i2c_write2(addr, 0x08, 0xC0);
00049     i2c_write2(addr, 0x1A, 0x40);
00050     i2c_write2(addr, 0x33, 0x33);
00051     wait(0.1);
00052 }
00053 
00054 int main() {
00055     char cmd[8];
00056     char buf[16];
00057     int Ix1,Iy1,Ix2,Iy2;
00058     int Ix3,Iy3,Ix4,Iy4;
00059     int s;
00060 
00061     clock_init();
00062     
00063     // PC serial output    
00064     pc.baud(115200);
00065     //pc.printf("Initializing camera...");
00066 
00067     cam_init();
00068   
00069     //pc.printf("complete\n");
00070 
00071     // read I2C stuff
00072     while(1) {
00073         myled = 1;
00074         // IR Sensor read
00075         cmd[0] = 0x36;
00076         i2c.write(addr, cmd, 1);
00077         i2c.read(addr, buf, 16); // read the 16-byte result
00078 
00079         myled = 0;
00080         
00081         Ix1 = buf[1];
00082         Iy1 = buf[2];
00083         s   = buf[3];
00084         Ix1 += (s & 0x30) <<4;
00085         Iy1 += (s & 0xC0) <<2;
00086         
00087         Ix2 = buf[4];
00088         Iy2 = buf[5];
00089         s   = buf[6];
00090         Ix2 += (s & 0x30) <<4;
00091         Iy2 += (s & 0xC0) <<2;
00092         
00093         Ix3 = buf[7];
00094         Iy3 = buf[8];
00095         s   = buf[9];
00096         Ix3 += (s & 0x30) <<4;
00097         Iy3 += (s & 0xC0) <<2;
00098         
00099         Ix4 = buf[10];
00100         Iy4 = buf[11];
00101         s   = buf[12];
00102         Ix4 += (s & 0x30) <<4;
00103         Iy4 += (s & 0xC0) <<2;
00104         
00105         // print the coordinate data
00106         //pc.printf("Ix1: %4d, Iy1: %4d\n", Ix1, Iy1 );
00107         //pc.printf("Ix2: %4d, Iy2: %4d\n", Ix2, Iy2 );
00108         pc.printf("%d,%d,%d,%d,%d,%d,%d,%d\r\n", Ix1, Iy1, Ix2, Iy2, Ix3, Iy3, Ix4, Iy4);
00109         
00110         wait(0.050);
00111     }
00112 
00113 }