Zeran Gu / Mbed 2 deprecated STM32F103C8T6_Wii_IRCam

Dependencies:   mbed-STM32F103C8T6 mbed

Fork of Wii_IRCam_Test by Michael Shimniok

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stm32f103c8t6.h"
00002 #include "mbed.h"
00003 
00004 // Adapted from kako's source code: http://www.kako.com/neta/2008-009/2008-009.html
00005 // i2c protocol details from - http://blog.makezine.com/archive/2008/11/hacking_the_wiimote_ir_ca.html
00006 // 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
00007 // obviously mbed is 3.3v so no level translation is needed
00008 // using built in i2c on pins 9/10
00009 //
00010 // PC GUI client here: http://code.google.com/p/wii-cam-blobtrack/
00011 //
00012 // Interfacing details here: http://www.bot-thoughts.com/2010/12/connecting-mbed-to-wiimote-ir-camera.html
00013 //
00014 
00015 DigitalOut myled(LED1);
00016 //PwmOut servo(PA_0);
00017 
00018 //I2C i2c(PB_7, PB_6);        // sda, scl
00019 I2C i2c(PB_9, PB_8);        // sda, scl
00020 const int addr = 0xB0;   // define the I2C Address of camera
00021 
00022 void i2c_write2(int addr, char a, char b)
00023 {
00024     char cmd[2];
00025     
00026     cmd[0] = a;
00027     cmd[1] = b;
00028     i2c.write(addr, cmd, 2);
00029     wait(0.07); // delay 70ms    
00030 }
00031 
00032 
00033 void cam_init()
00034 {
00035     // Init IR Camera sensor
00036     i2c_write2(addr, 0x30, 0x01);
00037     i2c_write2(addr, 0x30, 0x08);    
00038     i2c_write2(addr, 0x06, 0x90);
00039     i2c_write2(addr, 0x08, 0xC0);
00040     i2c_write2(addr, 0x1A, 0x40);
00041     i2c_write2(addr, 0x33, 0x33);
00042     wait(0.1);
00043 }
00044 
00045 int main() {
00046     Serial      pc(PA_2, PA_3);
00047     confSysClock();
00048     char cmd[8];
00049     char buf[16];
00050     int Ix1,Iy1,Ix2,Iy2;
00051     int Ix3,Iy3,Ix4,Iy4;
00052     int s;
00053 
00054 
00055     
00056     // PC serial output    
00057     pc.baud(115200);
00058     //pc.printf("Initializing camera...");
00059 
00060     cam_init();
00061   
00062     //pc.printf("complete\n");
00063 
00064     // read I2C stuff
00065     while(1) {
00066         myled = 1;
00067         // IR Sensor read
00068         cmd[0] = 0x36;
00069         i2c.write(addr, cmd, 1);
00070         i2c.read(addr, buf, 16); // read the 16-byte result
00071 
00072         myled = 0;
00073         
00074         Ix1 = buf[1];
00075         Iy1 = buf[2];
00076         s   = buf[3];
00077         Ix1 += (s & 0x30) <<4;
00078         Iy1 += (s & 0xC0) <<2;
00079         
00080         Ix2 = buf[4];
00081         Iy2 = buf[5];
00082         s   = buf[6];
00083         Ix2 += (s & 0x30) <<4;
00084         Iy2 += (s & 0xC0) <<2;
00085         
00086         Ix3 = buf[7];
00087         Iy3 = buf[8];
00088         s   = buf[9];
00089         Ix3 += (s & 0x30) <<4;
00090         Iy3 += (s & 0xC0) <<2;
00091         
00092         Ix4 = buf[10];
00093         Iy4 = buf[11];
00094         s   = buf[12];
00095         Ix4 += (s & 0x30) <<4;
00096         Iy4 += (s & 0xC0) <<2;
00097         
00098         // print the coordinate data
00099         //pc.printf("Ix1: %4d, Iy1: %4d\n", Ix1, Iy1 );
00100         //pc.printf("Ix2: %4d, Iy2: %4d\n", Ix2, Iy2 );
00101         //pc.printf("%d,%d,%d,%d,%d,%d,%d,%d\r\n", Ix1, Iy1, Ix2, Iy2, Ix3, Iy3, Ix4, Iy4);
00102         if (Ix1 <1023 && Iy1<1023) {
00103             //pc.printf("%d\r\n",Ix1);
00104             if (Ix1 > 512) {
00105                 pc.printf("a");
00106             } else {
00107                 pc.printf("d");
00108             }
00109             if (Iy1 > 512) {
00110                 pc.printf("w");
00111             } else {
00112                 pc.printf("s");
00113             }
00114         }
00115         
00116         
00117         wait(0.050);
00118     }
00119 
00120 }