Library for googly eyes

Dependents:   Eyes_Demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Eyes.cpp Source File

Eyes.cpp

00001 #include "Eyes.h"
00002 
00003 Eyes::Eyes(PinName tx, PinName rx, PinName reset): lcd(tx, rx, reset){
00004     right_eye_center.x = 37; right_eye_center.y = 42;
00005     left_eye_center.x = 89; left_eye_center.y = 42;
00006     active_expression = NORMAL;
00007     active_direction   = C;
00008     eye_radius   = 20;
00009     iris_radius  = 10;
00010     pupil_radius = 5;
00011     lcd.baudrate(3000000);              // Jack up baud rate for smooth operator eyes
00012     lcd.cls();                          // Master clear screen
00013     draw();                             // Initialize the eyes on screen
00014 }
00015 
00016 void Eyes::look(DIRECTION direction){
00017     if(direction == C){
00018         eye_direction_offset.x = 0; 
00019         eye_direction_offset.y = 0;
00020     }
00021     else{
00022         int radius(eye_radius - iris_radius);
00023         eye_direction_offset.x = (int)(radius*cos(direction*3.14/180.0)); 
00024         eye_direction_offset.y = (int)(radius*sin(direction*3.14/180.0));
00025     }
00026 }
00027 
00028 void Eyes::look_there(int theta, float dist){
00029     theta = theta % 361;       // Clamp theta to 0 to 360 degrees
00030     if(dist > 1.0) dist = 1.0;  // Clamp dist to 1.0 max
00031     if(dist < 0.0) dist = 0.0;  // Clamp dist to 0.0 min
00032     
00033     int radius(eye_radius - iris_radius);
00034     eye_direction_offset.x = (int)(dist*radius*cos(theta*3.14/180.0)); 
00035     eye_direction_offset.y = (int)(dist*radius*sin(theta*3.14/180.0));
00036 }
00037 
00038 void Eyes::express(EXPRESSION expression){}
00039 
00040 void Eyes::gesture(GESTURE gesture){
00041     switch(gesture){
00042         // "Clear" Screen/ Black out Eyes
00043         case BLINK:
00044             lcd.filled_circle(right_eye_center.x, right_eye_center.y,
00045                               eye_radius, BLACK);
00046             lcd.filled_circle(left_eye_center.x, left_eye_center.y,
00047                               eye_radius, BLACK);    
00048             // Manually Draw lines that look like closed eye lids
00049             lcd.line(right_eye_center.x - eye_radius, right_eye_center.y,
00050                      right_eye_center.x + eye_radius, right_eye_center.y,
00051                      LGREY);
00052             lcd.line(left_eye_center.x - eye_radius, left_eye_center.y,
00053                      left_eye_center.x + eye_radius, left_eye_center.y,
00054                      LGREY);
00055             callback_timer.attach(this, &Eyes::draw, (rand()%301)*0.001 + 0.075); // Between 100ish and 400ish (serial overhead makes it ish)
00056             break;
00057         
00058         case CLOSE:
00059             lcd.filled_circle(right_eye_center.x, right_eye_center.y,
00060                               eye_radius, BLACK);
00061             lcd.filled_circle(left_eye_center.x, left_eye_center.y,
00062                               eye_radius, BLACK);
00063             // Manually Draw lines that look like closed eye lids
00064             lcd.line(right_eye_center.x - eye_radius, right_eye_center.y,
00065                      right_eye_center.x + eye_radius, right_eye_center.y,
00066                      LGREY);
00067             lcd.line(left_eye_center.x - eye_radius, left_eye_center.y,
00068                      left_eye_center.x + eye_radius, left_eye_center.y,
00069                      LGREY);
00070             break;
00071             
00072         case SHAKE:
00073             break; 
00074     }
00075 }
00076 
00077 void Eyes::draw(){
00078     //Draw over last eye in black to "erase" before drawing new eye
00079     lcd.filled_circle(right_eye_center.x,
00080                       right_eye_center.y,
00081                       eye_radius, BLACK);
00082     
00083     lcd.filled_circle(left_eye_center.x,
00084                       left_eye_center.y,
00085                       eye_radius, BLACK);
00086     
00087     // Draw right eye
00088     lcd.filled_circle(right_eye_center.x,
00089                       right_eye_center.y,
00090                       eye_radius, WHITE);
00091     lcd.filled_circle(right_eye_center.x + eye_direction_offset.x,
00092                       right_eye_center.y + eye_direction_offset.y,
00093                       iris_radius, BLUE);
00094     lcd.filled_circle(right_eye_center.x + eye_direction_offset.x,
00095                       right_eye_center.y + eye_direction_offset.y,
00096                       pupil_radius, BLACK);
00097                       
00098     // Draw left eye
00099     lcd.filled_circle(left_eye_center.x,
00100                       left_eye_center.y,
00101                       eye_radius, WHITE);
00102     lcd.filled_circle(left_eye_center.x + eye_direction_offset.x,
00103                       left_eye_center.y + eye_direction_offset.y,
00104                       iris_radius, BLUE);
00105     lcd.filled_circle(left_eye_center.x + eye_direction_offset.x,
00106                       left_eye_center.y + eye_direction_offset.y,
00107                       pupil_radius, BLACK);
00108 }