Library for googly eyes

Dependents:   Eyes_Demo

Files at this revision

API Documentation at this revision

Comitter:
electromotivated
Date:
Tue Dec 15 00:36:42 2015 +0000
Child:
1:0791bb5735b4
Commit message:
Upload;

Changed in this revision

Eyes.cpp Show annotated file Show diff for this revision Revisions of this file
Eyes.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Eyes.cpp	Tue Dec 15 00:36:42 2015 +0000
@@ -0,0 +1,95 @@
+#include "Eyes.h"
+
+Eyes::Eyes(PinName tx, PinName rx, PinName reset): lcd(tx, rx, reset){
+    right_eye_center.x = 37; right_eye_center.y = 42;
+    left_eye_center.x = 89; left_eye_center.y = 42;
+    active_expression = NORMAL;
+    active_direction   = C;
+    eye_radius   = 20;
+    iris_radius  = 10;
+    pupil_radius = 5;
+    lcd.baudrate(3000000);              // Jack up baud rate for smooth operator eyes
+    lcd.cls();                          // Master clear screen
+    draw();                             // Initialize the eyes on screen
+}
+
+void Eyes::look(DIRECTION direction){
+    if(direction == C){
+        eye_direction_offset.x = 0; 
+        eye_direction_offset.y = 0;
+    }
+    else{
+        int radius(eye_radius - iris_radius);
+        eye_direction_offset.x = (int)(radius*cos(direction*3.14/180.0)); 
+        eye_direction_offset.y = (int)(radius*sin(direction*3.14/180.0));
+    }
+}
+
+void Eyes::express(EXPRESSION expression){}
+
+void Eyes::gesture(GESTURE gesture){
+    switch(gesture){
+        // "Clear" Screen/ Black out Eyes
+        case BLINK:
+            lcd.filled_circle(right_eye_center.x, right_eye_center.y,
+                              eye_radius, BLACK);
+            lcd.filled_circle(left_eye_center.x, left_eye_center.y,
+                              eye_radius, BLACK);    
+            // Manually Draw lines that look like closed eye lids
+            lcd.line(right_eye_center.x - eye_radius, right_eye_center.y,
+                     right_eye_center.x + eye_radius, right_eye_center.y,
+                     LGREY);
+            lcd.line(left_eye_center.x - eye_radius, left_eye_center.y,
+                     left_eye_center.x + eye_radius, left_eye_center.y,
+                     LGREY);
+            callback_timer.attach(this, &Eyes::draw, (rand()%301)*0.001 + 0.075); // Between 100ish and 400ish (serial overhead makes it ish)
+            break;
+        
+        case CLOSE:
+            lcd.filled_circle(right_eye_center.x, right_eye_center.y,
+                              eye_radius, BLACK);
+            lcd.filled_circle(left_eye_center.x, left_eye_center.y,
+                              eye_radius, BLACK);
+            // Manually Draw lines that look like closed eye lids
+            lcd.line(right_eye_center.x - eye_radius, right_eye_center.y,
+                     right_eye_center.x + eye_radius, right_eye_center.y,
+                     LGREY);
+            lcd.line(left_eye_center.x - eye_radius, left_eye_center.y,
+                     left_eye_center.x + eye_radius, left_eye_center.y,
+                     LGREY); 
+    }
+}
+
+void Eyes::draw(){
+    /* TODO: Draw over last eye in black to "erase" before drawing 
+        new eye */
+    lcd.filled_circle(right_eye_center.x,
+                      right_eye_center.y,
+                      eye_radius, BLACK);
+    
+    lcd.filled_circle(left_eye_center.x,
+                      left_eye_center.y,
+                      eye_radius, BLACK);
+    
+    // Draw right eye
+    lcd.filled_circle(right_eye_center.x,
+                      right_eye_center.y,
+                      eye_radius, WHITE);
+    lcd.filled_circle(right_eye_center.x + eye_direction_offset.x,
+                      right_eye_center.y + eye_direction_offset.y,
+                      iris_radius, BLUE);
+    lcd.filled_circle(right_eye_center.x + eye_direction_offset.x,
+                      right_eye_center.y + eye_direction_offset.y,
+                      pupil_radius, BLACK);
+                      
+    // Draw left eye
+    lcd.filled_circle(left_eye_center.x,
+                      left_eye_center.y,
+                      eye_radius, WHITE);
+    lcd.filled_circle(left_eye_center.x + eye_direction_offset.x,
+                      left_eye_center.y + eye_direction_offset.y,
+                      iris_radius, BLUE);
+    lcd.filled_circle(left_eye_center.x + eye_direction_offset.x,
+                      left_eye_center.y + eye_direction_offset.y,
+                      pupil_radius, BLACK);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Eyes.h	Tue Dec 15 00:36:42 2015 +0000
@@ -0,0 +1,85 @@
+/*
+    Library for the 4DGL uLCD-144-G2 LCD Display that Makes and 
+    Contols Eyes. 
+    
+    TODO: Allow user to define eye color, size, etc.
+          Allow user to adjust serial baudrate, etc.
+*/
+
+#ifndef EYES_H
+#define EYES_H
+
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "Timeout.h"
+
+class Eyes{
+    public: 
+    enum EXPRESSION{NORMAL, SUPRISED, SCARED, ANGRY}; 
+    enum DIRECTION{U = 270, D = 90, L = 0, R = 180, C, 
+                   UL= 315, UR= 225,DL= 45, DR = 135};
+    enum GESTURE{BLINK, CLOSE};
+    typedef struct{int x; int y;}Coord_t;
+    
+    /*
+        Constructor for Eyes Objects
+    */
+    Eyes(PinName tx, PinName rx, PinName reset);
+    
+    /* 
+        Change the active direction eyes look. 
+        @param direction     The direction to look
+    */
+    void look(DIRECTION direction);
+    
+    /*
+        Display a gesture. 
+        
+        BLINK is random ~100 to ~400ms. For user defined blink duration
+        call CLOSE, then draw() some user defined time later.
+        
+        NOTE: Some gestures use a timer interrupt to make a 
+        callback to complete a gesture, such as BLINK. Although this should
+        be ok, as the draw method is called on the interrupt and simply
+        redraws eyes with current setting, the user should be aware if they
+        are getting some weird side effects.
+        
+        @param gesture  The gesture to display
+    */
+    void gesture(GESTURE gesture);
+    
+    /* 
+        Draw eyes conveying given expression (keeps current look direction)
+        @param expression    The expression of the eyes
+    */  
+    void express(EXPRESSION expression);
+    
+    /*
+        Draw Eyes. Uses the currently active direction and expression.
+        Call this after calling express() and or look() methods, this
+        allows greater flexibility to user and reduces calls to draw
+        as user can set both expression and direction and make a single
+        call to draw.
+    */
+    void draw();  
+    
+    private:
+    uLCD_4DGL lcd; // serial tx, serial rx, reset pin;
+    Timeout callback_timer;
+    
+    EXPRESSION active_expression;
+    DIRECTION  active_direction;
+    /*TODO: Make these user defined*/
+    int eye_radius;
+    int iris_radius;
+    int pupil_radius;
+    /******************************/
+    Coord_t right_eye_center;  
+    Coord_t left_eye_center;
+    Coord_t eye_direction_offset; // Updated by look direction
+                                  // Used to apply an offset to 
+                                  // iris and pupils to make
+                                  // eyes look in a direction
+};
+
+#endif
\ No newline at end of file