Library for googly eyes
Eyes.cpp@0:06df44729143, 2015-12-15 (annotated)
- Committer:
- electromotivated
- Date:
- Tue Dec 15 00:36:42 2015 +0000
- Revision:
- 0:06df44729143
- Child:
- 2:9dd81fc2623f
Upload;
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 | 0:06df44729143 | 28 | void Eyes::express(EXPRESSION expression){} |
electromotivated | 0:06df44729143 | 29 | |
electromotivated | 0:06df44729143 | 30 | void Eyes::gesture(GESTURE gesture){ |
electromotivated | 0:06df44729143 | 31 | switch(gesture){ |
electromotivated | 0:06df44729143 | 32 | // "Clear" Screen/ Black out Eyes |
electromotivated | 0:06df44729143 | 33 | case BLINK: |
electromotivated | 0:06df44729143 | 34 | lcd.filled_circle(right_eye_center.x, right_eye_center.y, |
electromotivated | 0:06df44729143 | 35 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 36 | lcd.filled_circle(left_eye_center.x, left_eye_center.y, |
electromotivated | 0:06df44729143 | 37 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 38 | // Manually Draw lines that look like closed eye lids |
electromotivated | 0:06df44729143 | 39 | lcd.line(right_eye_center.x - eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 40 | right_eye_center.x + eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 41 | LGREY); |
electromotivated | 0:06df44729143 | 42 | lcd.line(left_eye_center.x - eye_radius, left_eye_center.y, |
electromotivated | 0:06df44729143 | 43 | left_eye_center.x + eye_radius, left_eye_center.y, |
electromotivated | 0:06df44729143 | 44 | LGREY); |
electromotivated | 0:06df44729143 | 45 | callback_timer.attach(this, &Eyes::draw, (rand()%301)*0.001 + 0.075); // Between 100ish and 400ish (serial overhead makes it ish) |
electromotivated | 0:06df44729143 | 46 | break; |
electromotivated | 0:06df44729143 | 47 | |
electromotivated | 0:06df44729143 | 48 | case CLOSE: |
electromotivated | 0:06df44729143 | 49 | lcd.filled_circle(right_eye_center.x, right_eye_center.y, |
electromotivated | 0:06df44729143 | 50 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 51 | lcd.filled_circle(left_eye_center.x, left_eye_center.y, |
electromotivated | 0:06df44729143 | 52 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 53 | // Manually Draw lines that look like closed eye lids |
electromotivated | 0:06df44729143 | 54 | lcd.line(right_eye_center.x - eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 55 | right_eye_center.x + eye_radius, right_eye_center.y, |
electromotivated | 0:06df44729143 | 56 | LGREY); |
electromotivated | 0:06df44729143 | 57 | lcd.line(left_eye_center.x - eye_radius, left_eye_center.y, |
electromotivated | 0:06df44729143 | 58 | left_eye_center.x + eye_radius, left_eye_center.y, |
electromotivated | 0:06df44729143 | 59 | LGREY); |
electromotivated | 0:06df44729143 | 60 | } |
electromotivated | 0:06df44729143 | 61 | } |
electromotivated | 0:06df44729143 | 62 | |
electromotivated | 0:06df44729143 | 63 | void Eyes::draw(){ |
electromotivated | 0:06df44729143 | 64 | /* TODO: Draw over last eye in black to "erase" before drawing |
electromotivated | 0:06df44729143 | 65 | new eye */ |
electromotivated | 0:06df44729143 | 66 | lcd.filled_circle(right_eye_center.x, |
electromotivated | 0:06df44729143 | 67 | right_eye_center.y, |
electromotivated | 0:06df44729143 | 68 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 69 | |
electromotivated | 0:06df44729143 | 70 | lcd.filled_circle(left_eye_center.x, |
electromotivated | 0:06df44729143 | 71 | left_eye_center.y, |
electromotivated | 0:06df44729143 | 72 | eye_radius, BLACK); |
electromotivated | 0:06df44729143 | 73 | |
electromotivated | 0:06df44729143 | 74 | // Draw right eye |
electromotivated | 0:06df44729143 | 75 | lcd.filled_circle(right_eye_center.x, |
electromotivated | 0:06df44729143 | 76 | right_eye_center.y, |
electromotivated | 0:06df44729143 | 77 | eye_radius, WHITE); |
electromotivated | 0:06df44729143 | 78 | lcd.filled_circle(right_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 79 | right_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 80 | iris_radius, BLUE); |
electromotivated | 0:06df44729143 | 81 | lcd.filled_circle(right_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 82 | right_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 83 | pupil_radius, BLACK); |
electromotivated | 0:06df44729143 | 84 | |
electromotivated | 0:06df44729143 | 85 | // Draw left eye |
electromotivated | 0:06df44729143 | 86 | lcd.filled_circle(left_eye_center.x, |
electromotivated | 0:06df44729143 | 87 | left_eye_center.y, |
electromotivated | 0:06df44729143 | 88 | eye_radius, WHITE); |
electromotivated | 0:06df44729143 | 89 | lcd.filled_circle(left_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 90 | left_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 91 | iris_radius, BLUE); |
electromotivated | 0:06df44729143 | 92 | lcd.filled_circle(left_eye_center.x + eye_direction_offset.x, |
electromotivated | 0:06df44729143 | 93 | left_eye_center.y + eye_direction_offset.y, |
electromotivated | 0:06df44729143 | 94 | pupil_radius, BLACK); |
electromotivated | 0:06df44729143 | 95 | } |