Library for googly eyes
Eyes.cpp@2:9dd81fc2623f, 2016-04-12 (annotated)
- Committer:
- electromotivated
- Date:
- Tue Apr 12 02:30:40 2016 +0000
- Revision:
- 2:9dd81fc2623f
- Parent:
- 0:06df44729143
Added new method look_there(theta, dist) allowing finer control of direction and distance to look;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
electromotivated | 0:06df44729143 | 1 | #include "Eyes.h" |
electromotivated | 0:06df44729143 | 2 | |
electromotivated | 0:06df44729143 | 3 | Eyes::Eyes(PinName tx, PinName rx, PinName reset): lcd(tx, rx, reset){ |
electromotivated | 0:06df44729143 | 4 | right_eye_center.x = 37; right_eye_center.y = 42; |
electromotivated | 0:06df44729143 | 5 | left_eye_center.x = 89; left_eye_center.y = 42; |
electromotivated | 0:06df44729143 | 6 | active_expression = NORMAL; |
electromotivated | 0:06df44729143 | 7 | active_direction = C; |
electromotivated | 0:06df44729143 | 8 | eye_radius = 20; |
electromotivated | 0:06df44729143 | 9 | iris_radius = 10; |
electromotivated | 0:06df44729143 | 10 | pupil_radius = 5; |
electromotivated | 0:06df44729143 | 11 | lcd.baudrate(3000000); // Jack up baud rate for smooth operator eyes |
electromotivated | 0:06df44729143 | 12 | lcd.cls(); // Master clear screen |
electromotivated | 0:06df44729143 | 13 | draw(); // Initialize the eyes on screen |
electromotivated | 0:06df44729143 | 14 | } |
electromotivated | 0:06df44729143 | 15 | |
electromotivated | 0:06df44729143 | 16 | void Eyes::look(DIRECTION direction){ |
electromotivated | 0:06df44729143 | 17 | if(direction == C){ |
electromotivated | 0:06df44729143 | 18 | eye_direction_offset.x = 0; |
electromotivated | 0:06df44729143 | 19 | eye_direction_offset.y = 0; |
electromotivated | 0:06df44729143 | 20 | } |
electromotivated | 0:06df44729143 | 21 | else{ |
electromotivated | 0:06df44729143 | 22 | int radius(eye_radius - iris_radius); |
electromotivated | 0:06df44729143 | 23 | eye_direction_offset.x = (int)(radius*cos(direction*3.14/180.0)); |
electromotivated | 0:06df44729143 | 24 | eye_direction_offset.y = (int)(radius*sin(direction*3.14/180.0)); |
electromotivated | 0:06df44729143 | 25 | } |
electromotivated | 0:06df44729143 | 26 | } |
electromotivated | 0:06df44729143 | 27 | |
electromotivated | 2:9dd81fc2623f | 28 | void Eyes::look_there(int theta, float dist){ |
electromotivated | 2:9dd81fc2623f | 29 | theta = theta % 361; // Clamp theta to 0 to 360 degrees |
electromotivated | 2:9dd81fc2623f | 30 | if(dist > 1.0) dist = 1.0; // Clamp dist to 1.0 max |
electromotivated | 2:9dd81fc2623f | 31 | if(dist < 0.0) dist = 0.0; // Clamp dist to 0.0 min |
electromotivated | 2:9dd81fc2623f | 32 | |
electromotivated | 2:9dd81fc2623f | 33 | int radius(eye_radius - iris_radius); |
electromotivated | 2:9dd81fc2623f | 34 | eye_direction_offset.x = (int)(dist*radius*cos(theta*3.14/180.0)); |
electromotivated | 2:9dd81fc2623f | 35 | eye_direction_offset.y = (int)(dist*radius*sin(theta*3.14/180.0)); |
electromotivated | 2:9dd81fc2623f | 36 | } |
electromotivated | 2:9dd81fc2623f | 37 | |
electromotivated | 0:06df44729143 | 38 | void Eyes::express(EXPRESSION expression){} |
electromotivated | 0:06df44729143 | 39 | |
electromotivated | 0:06df44729143 | 40 | void Eyes::gesture(GESTURE gesture){ |
electromotivated | 0:06df44729143 | 41 | switch(gesture){ |
electromotivated | 0:06df44729143 | 42 | // "Clear" Screen/ Black out Eyes |
electromotivated | 0:06df44729143 | 43 | case BLINK: |
electromotivated | 0:06df44729143 | 44 | lcd.filled_circle(right_eye_center.x, right_eye_center.y, |
electromotivated | 0:06df44729143 | 45 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 46 | lcd.filled_circle(left_eye_center.x, left_eye_center.y, |
electromotivated | 0:06df44729143 | 47 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 48 | // Manually Draw lines that look like closed eye lids |
electromotivated | 0:06df44729143 | 49 | lcd.line(right_eye_center.x - eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 50 | right_eye_center.x + eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 51 | LGREY); |
electromotivated | 0:06df44729143 | 52 | lcd.line(left_eye_center.x - eye_radius, left_eye_center.y, |
electromotivated | 0:06df44729143 | 53 | left_eye_center.x + eye_radius, left_eye_center.y, |
electromotivated | 0:06df44729143 | 54 | LGREY); |
electromotivated | 0:06df44729143 | 55 | callback_timer.attach(this, &Eyes::draw, (rand()%301)*0.001 + 0.075); // Between 100ish and 400ish (serial overhead makes it ish) |
electromotivated | 0:06df44729143 | 56 | break; |
electromotivated | 0:06df44729143 | 57 | |
electromotivated | 0:06df44729143 | 58 | case CLOSE: |
electromotivated | 0:06df44729143 | 59 | lcd.filled_circle(right_eye_center.x, right_eye_center.y, |
electromotivated | 0:06df44729143 | 60 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 61 | lcd.filled_circle(left_eye_center.x, left_eye_center.y, |
electromotivated | 0:06df44729143 | 62 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 63 | // Manually Draw lines that look like closed eye lids |
electromotivated | 0:06df44729143 | 64 | lcd.line(right_eye_center.x - eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 65 | right_eye_center.x + eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 66 | LGREY); |
electromotivated | 0:06df44729143 | 67 | lcd.line(left_eye_center.x - eye_radius, left_eye_center.y, |
electromotivated | 0:06df44729143 | 68 | left_eye_center.x + eye_radius, left_eye_center.y, |
electromotivated | 2:9dd81fc2623f | 69 | LGREY); |
electromotivated | 2:9dd81fc2623f | 70 | break; |
electromotivated | 2:9dd81fc2623f | 71 | |
electromotivated | 2:9dd81fc2623f | 72 | case SHAKE: |
electromotivated | 2:9dd81fc2623f | 73 | break; |
electromotivated | 0:06df44729143 | 74 | } |
electromotivated | 0:06df44729143 | 75 | } |
electromotivated | 0:06df44729143 | 76 | |
electromotivated | 0:06df44729143 | 77 | void Eyes::draw(){ |
electromotivated | 2:9dd81fc2623f | 78 | //Draw over last eye in black to "erase" before drawing new eye |
electromotivated | 0:06df44729143 | 79 | lcd.filled_circle(right_eye_center.x, |
electromotivated | 0:06df44729143 | 80 | right_eye_center.y, |
electromotivated | 0:06df44729143 | 81 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 82 | |
electromotivated | 0:06df44729143 | 83 | lcd.filled_circle(left_eye_center.x, |
electromotivated | 0:06df44729143 | 84 | left_eye_center.y, |
electromotivated | 0:06df44729143 | 85 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 86 | |
electromotivated | 0:06df44729143 | 87 | // Draw right eye |
electromotivated | 0:06df44729143 | 88 | lcd.filled_circle(right_eye_center.x, |
electromotivated | 0:06df44729143 | 89 | right_eye_center.y, |
electromotivated | 0:06df44729143 | 90 | eye_radius, WHITE); |
electromotivated | 0:06df44729143 | 91 | lcd.filled_circle(right_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 92 | right_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 93 | iris_radius, BLUE); |
electromotivated | 0:06df44729143 | 94 | lcd.filled_circle(right_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 95 | right_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 96 | pupil_radius, BLACK); |
electromotivated | 0:06df44729143 | 97 | |
electromotivated | 0:06df44729143 | 98 | // Draw left eye |
electromotivated | 0:06df44729143 | 99 | lcd.filled_circle(left_eye_center.x, |
electromotivated | 0:06df44729143 | 100 | left_eye_center.y, |
electromotivated | 0:06df44729143 | 101 | eye_radius, WHITE); |
electromotivated | 0:06df44729143 | 102 | lcd.filled_circle(left_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 103 | left_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 104 | iris_radius, BLUE); |
electromotivated | 0:06df44729143 | 105 | lcd.filled_circle(left_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 106 | left_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 107 | pupil_radius, BLACK); |
electromotivated | 0:06df44729143 | 108 | } |