Skip to content

Commit 6321e13

Browse files
committed
2 parents 5246746 + 13b231c commit 6321e13

2 files changed

Lines changed: 43 additions & 40 deletions

File tree

M4_Eyes/M4_Eyes.ino

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,16 @@ void setup() {
390390
lastLightReadTime = micros() + 2000000; // Delay initial light reading
391391
}
392392

393+
static inline uint16_t readLightSensor(void) {
394+
#if NUM_EYES > 1
395+
if(lightSensorPin >= 100) {
396+
return seesaw.analogRead(lightSensorPin - 100);
397+
}
398+
#else
399+
return analogRead(lightSensorPin);
400+
#endif
401+
}
402+
393403
// LOOP FUNCTION - CALLED REPEATEDLY UNTIL POWER-OFF -----------------------
394404

395405
/*
@@ -817,8 +827,7 @@ void loop() {
817827
// pupils will react even if the opposite eye is stimulated.
818828
// Meaning we can get away with using a single light sensor for
819829
// both eyes. This comment has nothing to do with the code.
820-
uint16_t rawReading = (lightSensorPin >= 100) ?
821-
seesaw.analogRead(lightSensorPin - 100) : analogRead(lightSensorPin);
830+
uint16_t rawReading = readLightSensor();
822831
if(rawReading <= 1023) {
823832
if(rawReading < lightSensorMin) rawReading = lightSensorMin; // Clamp light sensor range
824833
else if(rawReading > lightSensorMax) rawReading = lightSensorMax; // to within usable range

M4_Eyes/tablegen.cpp

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,44 +99,38 @@ void calcMap(void) {
9999

100100
// If slit pupil is enabled, override iris area of polarDist map.
101101
if(slitPupilRadius > 0) {
102-
// This is a bit ugly in that it makes repeated passes over the polarDist
103-
// array, drawing (and redrawing) the intersection of two overlapping
104-
// circles...I wasn't able to come up with an approach that gives a
105-
// single correct answer at each pixel (surely it's possible, I'm just
106-
// lousy at this kind of math). Also, lots of floating-point math.
107-
// This is only needed once at startup, so it's not a complete disaster.
108-
int i;
109-
for(i=0; i<127; i++) {
110-
float ratio = i / 128.0; // 0.0 (open) to just-under-1.0 (slit) (>= 1.0 will cause trouble)
111-
// Interpolate a point between top of iris and top of slit pupil, based on ratio
112-
float y1 = iRad - (iRad - slitPupilRadius) * ratio;
113-
// (x1 is 0 and thus dropped from equation below)
114-
// And another point between right of iris and center of eye, inverse ratio
115-
float x2 = iRad * (1.0 - ratio);
116-
// (y2 is also zero, same deal)
117-
// Find X coordinate of center of circle that crosses above two points
118-
// and has Y at 0.0
119-
float xc = (x2 * x2 - y1 * y1) / (2 * x2);
120-
// Distance from center of circle to right edge
121-
dx = x2 - xc;
122-
float r2 = dx * dx; // center-to-right distance squared
123-
// Iterate over each pixel in the iris section of the polar map...
124-
for(y=0; y < mapRadius; y++) {
125-
dy = y + 0.5; // Distance to center, Y component
126-
dy2 = dy * dy;
127-
for(x=0; x < mapRadius; x++) {
128-
dx = x + 0.5; // Distance to center point, X component
129-
d2 = dx * dx + dy2; // Distance to center, squared
130-
if(d2 <= irisRadius2) { // If inside iris...
131-
xp = x + 0.5;
132-
dx = xp - xc; // X component of...
133-
d2 = dx * dx + dy2; // Distance from pixel to left 'xc' point
134-
if(d2 <= r2) { // If point is within the first circle...
135-
dx = xp + xc; // X component of...
136-
d2 = dx * dx + dy2; // Distance from pixel to right 'xc' point
137-
if(d2 <= r2) { // If point is also within the second circle...
138-
polarDist[y * mapRadius + x] = (int8_t)(-1 - i); // Set to distance 'i'
139-
}
102+
// Iterate over each pixel in the iris section of the polar map...
103+
for(y=0; y < mapRadius; y++) {
104+
dy = y + 0.5; // Distance to center, Y component
105+
dy2 = dy * dy;
106+
for(x=0; x < mapRadius; x++) {
107+
dx = x + 0.5; // Distance to center point, X component
108+
d2 = dx * dx + dy2; // Distance to center, squared
109+
if(d2 <= irisRadius2) { // If inside iris...
110+
xp = x + 0.5;
111+
// This is a bit ugly in that it iteratively calculates the
112+
// polarDist value...trial and error. It should be possible to
113+
// algebraically simplify this and find the single polarDist
114+
// point for a given pixel, but I've not worked that out yet.
115+
// This is only needed once at startup, not a complete disaster.
116+
for(int i=126; i>=0; i--) {
117+
float ratio = i / 128.0; // 0.0 (open) to just-under-1.0 (slit) (>= 1.0 will cause trouble)
118+
// Interpolate a point between top of iris and top of slit pupil, based on ratio
119+
float y1 = iRad - (iRad - slitPupilRadius) * ratio;
120+
// (x1 is 0 and thus dropped from equation below)
121+
// And another point between right of iris and center of eye, inverse ratio
122+
float x2 = iRad * (1.0 - ratio);
123+
// (y2 is also zero, same deal)
124+
// Find X coordinate of center of circle that crosses above two points
125+
// and has Y at 0.0
126+
float xc = (x2 * x2 - y1 * y1) / (2 * x2);
127+
dx = x2 - xc; // Distance from center of circle to right edge
128+
float r2 = dx * dx; // center-to-right distance squared
129+
dx = xp - xc; // X component of...
130+
d2 = dx * dx + dy2; // Distance from pixel to left 'xc' point
131+
if(d2 <= r2) { // If point is within circle...
132+
polarDist[y * mapRadius + x] = (int8_t)(-1 - i); // Set to distance 'i'
133+
break;
140134
}
141135
}
142136
}

0 commit comments

Comments
 (0)