Skip to content

Commit a8ea278

Browse files
author
Adafruit Adabot
committed
replace 240 with DISPLAY_SIZE
1 parent 35cf46b commit a8ea278

3 files changed

Lines changed: 42 additions & 41 deletions

File tree

M4_Eyes/M4_Eyes.ino

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void dma_callback(Adafruit_ZeroDMA *dma) {
9696

9797
SPISettings settings(DISPLAY_FREQ, MSBFIRST, SPI_MODE0);
9898

99-
// The time required to issue one scanline (240 pixels x 16 bits) over
99+
// The time required to issue one scanline (DISPLAY_SIZE pixels x 16 bits) over
100100
// SPI is a known(ish) quantity. The DMA scheduler isn't always perfectly
101101
// deterministic though...especially on startup, as things make their way
102102
// into caches. Very occasionally, something (not known yet) is causing
@@ -107,7 +107,7 @@ SPISettings settings(DISPLAY_FREQ, MSBFIRST, SPI_MODE0);
107107
// below, to allow for caching/scheduling fudge). If so, that's our signal
108108
// that something is likely amiss and we take evasive maneuvers, resetting
109109
// the affected DMA channel (DMAbuddy::fix()).
110-
#define DMA_TIMEOUT ((240 * 16 * 4000) / (DISPLAY_FREQ / 1000))
110+
#define DMA_TIMEOUT ((DISPLAY_SIZE * 16 * 4000) / (DISPLAY_FREQ / 1000))
111111

112112
static inline uint16_t readBoop(void) {
113113
uint16_t counter = 0;
@@ -228,7 +228,7 @@ void setup() {
228228
eye[e].column[i].descriptor[j].DSTADDR.reg = spi_data_reg;
229229
}
230230
}
231-
eye[e].colNum = 240; // Force initial wraparound to first column
231+
eye[e].colNum = DISPLAY_SIZE; // Force initial wraparound to first column
232232
eye[e].colIdx = 0;
233233
eye[e].dma_busy = false;
234234
eye[e].column_ready = false;
@@ -357,7 +357,7 @@ void setup() {
357357

358358
status = loadEyelid(upperEyelidFilename ?
359359
upperEyelidFilename : (char *)"upper.bmp",
360-
upperClosed, upperOpen, 239, maxRam);
360+
upperClosed, upperOpen, DISPLAY_SIZE-1, maxRam);
361361

362362
status = loadEyelid(lowerEyelidFilename ?
363363
lowerEyelidFilename : (char *)"lower.bmp",
@@ -407,7 +407,7 @@ void setup() {
407407
yield();
408408
if(boopPin >= 0) {
409409
boopThreshold = 0;
410-
for(int i=0; i<240; i++) {
410+
for(int i=0; i<DISPLAY_SIZE; i++) {
411411
boopThreshold += readBoop();
412412
}
413413
boopThreshold = boopThreshold * 110 / 100; // 10% overhead
@@ -486,7 +486,7 @@ void loop() {
486486
eyeX = eyeOldX;
487487
eyeY = eyeOldY;
488488
if(dt > eyeMoveDuration) { // Time up? Begin new move.
489-
float r = (float)mapDiameter - 240.0 * M_PI_2; // radius of motion
489+
float r = (float)mapDiameter - (float)DISPLAY_SIZE * M_PI_2; // radius of motion
490490
r *= 0.6;
491491
eyeNewX = random(-r, r);
492492
float h = sqrt(r * r - x * x);
@@ -533,10 +533,10 @@ void loop() {
533533
float uq, lq; // So many sloppy temp vars in here for now, sorry
534534
if(tracking) {
535535
// Eyelids naturally "track" the pupils (move up or down automatically)
536-
int ix = (int)map2screen(mapRadius - eye[eyeNum].eyeX) + 120, // Pupil position
537-
iy = (int)map2screen(mapRadius - eye[eyeNum].eyeY) + 120; // on screen
536+
int ix = (int)map2screen(mapRadius - eye[eyeNum].eyeX) + (DISPLAY_SIZE/2), // Pupil position
537+
iy = (int)map2screen(mapRadius - eye[eyeNum].eyeY) + (DISPLAY_SIZE/2); // on screen
538538
iy += irisRadius * trackFactor;
539-
if(eyeNum & 1) ix = 239 - ix; // Flip for right eye
539+
if(eyeNum & 1) ix = DISPLAY_SIZE - 1 - ix; // Flip for right eye
540540
if(iy > upperOpen[ix]) {
541541
uq = 1.0;
542542
} else if(iy < upperClosed[ix]) {
@@ -625,40 +625,40 @@ void loop() {
625625

626626
// Should be possible for these to be local vars,
627627
// but the animation becomes super chunky then, what gives?
628-
xPositionOverMap = (int)(eye[eyeNum].eyeX - 120.0);
629-
yPositionOverMap = (int)(eye[eyeNum].eyeY - 120.0);
628+
xPositionOverMap = (int)(eye[eyeNum].eyeX - (DISPLAY_SIZE/2.0));
629+
yPositionOverMap = (int)(eye[eyeNum].eyeY - (DISPLAY_SIZE/2.0));
630630

631631
// These are constant across frame and could be stored in eye struct
632632
float upperLidFactor = (1.0 - eye[eyeNum].blinkFactor) * eye[eyeNum].upperLidFactor,
633633
lowerLidFactor = (1.0 - eye[eyeNum].blinkFactor) * eye[eyeNum].lowerLidFactor;
634634
iPupilFactor = (int)((float)eye[eyeNum].iris.height * 256 * (1.0 / eye[eyeNum].pupilFactor));
635635

636636
int y1, y2;
637-
int lidColumn = (eyeNum & 1) ? (239 - x) : x; // Reverse eyelid columns for left eye
637+
int lidColumn = (eyeNum & 1) ? (DISPLAY_SIZE - 1 - x) : x; // Reverse eyelid columns for left eye
638638

639639
DmacDescriptor *d = &eye[eyeNum].column[eye[eyeNum].colIdx].descriptor[0];
640640

641641
if(upperOpen[lidColumn] == 255) {
642642
// No eyelid data for this line; eyelid image is smaller than screen.
643643
// Great! Make a full scanline of nothing, no rendering needed:
644644
d->BTCTRL.bit.SRCINC = 0;
645-
d->BTCNT.reg = 240 * 2;
645+
d->BTCNT.reg = DISPLAY_SIZE * 2;
646646
d->SRCADDR.reg = (uint32_t)&eyelidIndex;
647647
d->DESCADDR.reg = 0; // No linked descriptor
648648
} else {
649649
y1 = lowerClosed[lidColumn] + (int)(0.5 + lowerLidFactor *
650650
(float)((int)lowerOpen[lidColumn] - (int)lowerClosed[lidColumn]));
651651
y2 = upperClosed[lidColumn] + (int)(0.5 + upperLidFactor *
652652
(float)((int)upperOpen[lidColumn] - (int)upperClosed[lidColumn]));
653-
if(y1 > 239) y1 = 239; // Clip results in case lidfactor
653+
if(y1 > DISPLAY_SIZE-1) y1 = DISPLAY_SIZE-1; // Clip results in case lidfactor
654654
else if(y1 < 0) y1 = 0; // is beyond the usual 0.0 to 1.0 range
655-
if(y2 > 239) y2 = 239;
655+
if(y2 > DISPLAY_SIZE-1) y2 = DISPLAY_SIZE-1;
656656
else if(y2 < 0) y2 = 0;
657657
if(y1 >= y2) {
658658
// Eyelid is fully or partially closed, enough that there are no
659659
// pixels to be rendered for this line. Make "nothing," as above.
660660
d->BTCTRL.bit.SRCINC = 0;
661-
d->BTCNT.reg = 240 * 2;
661+
d->BTCNT.reg = DISPLAY_SIZE * 2;
662662
d->SRCADDR.reg = (uint32_t)&eyelidIndex;
663663
d->DESCADDR.reg = 0; // No linked descriptors
664664
} else {
@@ -681,11 +681,11 @@ void loop() {
681681
d->BTCNT.reg = renderlen * 2;
682682
d->SRCADDR.reg = (uint32_t)eye[eyeNum].column[eye[eyeNum].colIdx].renderBuf + renderlen * 2; // Point to END of data!
683683
#else
684-
// Full column will be rendered; 240 pixels, point source to end of
684+
// Full column will be rendered; DISPLAY_SIZE pixels, point source to end of
685685
// renderBuf and enable source increment.
686686
d->BTCTRL.bit.SRCINC = 1;
687-
d->BTCNT.reg = 240 * 2;
688-
d->SRCADDR.reg = (uint32_t)eye[eyeNum].column[eye[eyeNum].colIdx].renderBuf + 240 * 2;
687+
d->BTCNT.reg = DISPLAY_SIZE * 2;
688+
d->SRCADDR.reg = (uint32_t)eye[eyeNum].column[eye[eyeNum].colIdx].renderBuf + DISPLAY_SIZE * 2;
689689
d->DESCADDR.reg = 0; // No linked descriptors
690690
#endif
691691
// Render column 'x' into eye's next available renderBuf
@@ -704,28 +704,28 @@ void loop() {
704704
uint8_t *displaceX, *displaceY;
705705
int8_t xmul; // Sign of X displacement: +1 or -1
706706
int doff; // Offset into displacement arrays
707-
if(x < 120) { // Left half of screen (quadrants 2, 3)
708-
displaceX = &displace[ 119 - x ];
709-
displaceY = &displace[(119 - x) * 120];
707+
if(x < (DISPLAY_SIZE/2)) { // Left half of screen (quadrants 2, 3)
708+
displaceX = &displace[ (DISPLAY_SIZE/2 - 1) - x ];
709+
displaceY = &displace[((DISPLAY_SIZE/2 - 1) - x) * (DISPLAY_SIZE/2)];
710710
xmul = -1; // X displacement is always negative
711711
} else { // Right half of screen( quadrants 1, 4)
712-
displaceX = &displace[ x - 120 ];
713-
displaceY = &displace[(x - 120) * 120];
712+
displaceX = &displace[ x - (DISPLAY_SIZE/2) ];
713+
displaceY = &displace[(x - (DISPLAY_SIZE/2)) * (DISPLAY_SIZE/2)];
714714
xmul = 1; // X displacement is always positive
715715
}
716716

717717
for(; y<=y2; y++) { // For each pixel of open eye in this column...
718718
int yy = yPositionOverMap + y;
719719
int dx, dy;
720720

721-
if(y < 120) { // Lower half of screen (quadrants 3, 4)
722-
doff = 119 - y;
721+
if(y < (DISPLAY_SIZE/2)) { // Lower half of screen (quadrants 3, 4)
722+
doff = (DISPLAY_SIZE/2 - 1) - y;
723723
dy = -displaceY[doff];
724724
} else { // Upper half of screen (quadrants 1, 2)
725-
doff = y - 120;
725+
doff = y - (DISPLAY_SIZE/2);
726726
dy = displaceY[doff];
727727
}
728-
dx = displaceX[doff * 120];
728+
dx = displaceX[doff * (DISPLAY_SIZE/2)];
729729
if(dx < 255) { // Inside eyeball area
730730
dx *= xmul; // Flip sign of x offset if in quadrants 2 or 3
731731
int mx = xx + dx; // Polar angle/dist map coords
@@ -795,17 +795,17 @@ void loop() {
795795

796796
#if NUM_DESCRIPTORS == 1
797797
// Render upper eyelid if needed
798-
for(; y<240; y++) *ptr++ = eyelidColor;
798+
for(; y<DISPLAY_SIZE; y++) *ptr++ = eyelidColor;
799799
#else
800-
if(y2 >= 239) {
800+
if(y2 >= (DISPLAY_SIZE-1)) {
801801
// No third descriptor; close it off
802802
d->DESCADDR.reg = 0;
803803
} else {
804804
next = &eye[eyeNum].column[eye[eyeNum].colIdx].descriptor[(y1 > 0) ? 2 : 1];
805805
d->DESCADDR.reg = (uint32_t)next; // link to next descriptor
806806
d = next; // Increment descriptor
807807
d->BTCTRL.bit.SRCINC = 0;
808-
d->BTCNT.reg = (239 - y2) * 2;
808+
d->BTCNT.reg = ((DISPLAY_SIZE-1) - y2) * 2;
809809
d->SRCADDR.reg = (uint32_t)&eyelidIndex;
810810
d->DESCADDR.reg = 0; // end of descriptor list
811811
}
@@ -840,7 +840,7 @@ void loop() {
840840
// Initialize new SPI transaction & address window...
841841
eye[eyeNum].spi->beginTransaction(settings);
842842
digitalWrite(eye[eyeNum].cs, LOW); // Chip select
843-
eye[eyeNum].display->setAddrWindow(0, 0, 240, 240);
843+
eye[eyeNum].display->setAddrWindow(0, 0, DISPLAY_SIZE, DISPLAY_SIZE);
844844
delayMicroseconds(1);
845845
digitalWrite(eye[eyeNum].dc, HIGH); // Data mode
846846
if(eyeNum == (NUM_EYES-1)) {
@@ -923,7 +923,7 @@ void loop() {
923923
eye[eyeNum].dma_busy = true;
924924
eye[eyeNum].dma.startJob();
925925
eye[eyeNum].dmaStartTime = micros();
926-
if(++eye[eyeNum].colNum >= 240) { // If last line sent...
926+
if(++eye[eyeNum].colNum >= DISPLAY_SIZE) { // If last line sent...
927927
eye[eyeNum].colNum = 0; // Wrap to beginning
928928
}
929929
eye[eyeNum].colIdx ^= 1; // Alternate 0/1 line structs

M4_Eyes/globals.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#endif
2020

2121
// GLOBAL VARIABLES --------------------------------------------------------
22-
22+
#define MAX_DISPLAY_SIZE 240
23+
GLOBAL_VAR int DISPLAY_SIZE GLOBAL_INIT(240); // Start with assuming a 240x240 display
2324
GLOBAL_VAR uint32_t stackReserve GLOBAL_INIT(5192); // See image-loading code
2425
GLOBAL_VAR int eyeRadius GLOBAL_INIT(0); // 0 = Use default in loadConfig()
2526
GLOBAL_VAR int eyeDiameter; // Calculated from eyeRadius later
@@ -48,10 +49,10 @@ GLOBAL_VAR int mapDiameter; // calculated in loadConfig()
4849
GLOBAL_VAR uint8_t *displace GLOBAL_INIT(NULL);
4950
GLOBAL_VAR uint8_t *polarAngle GLOBAL_INIT(NULL);
5051
GLOBAL_VAR int8_t *polarDist GLOBAL_INIT(NULL);
51-
GLOBAL_VAR uint8_t upperOpen[240];
52-
GLOBAL_VAR uint8_t upperClosed[240];
53-
GLOBAL_VAR uint8_t lowerOpen[240];
54-
GLOBAL_VAR uint8_t lowerClosed[240];
52+
GLOBAL_VAR uint8_t upperOpen[MAX_DISPLAY_SIZE];
53+
GLOBAL_VAR uint8_t upperClosed[MAX_DISPLAY_SIZE];
54+
GLOBAL_VAR uint8_t lowerOpen[MAX_DISPLAY_SIZE];
55+
GLOBAL_VAR uint8_t lowerClosed[MAX_DISPLAY_SIZE];
5556
GLOBAL_VAR char *upperEyelidFilename GLOBAL_INIT(NULL);
5657
GLOBAL_VAR char *lowerEyelidFilename GLOBAL_INIT(NULL);
5758
GLOBAL_VAR uint16_t lightSensorMin GLOBAL_INIT(0);
@@ -106,7 +107,7 @@ GLOBAL_VAR uint32_t modulate GLOBAL_INIT(30); // Dalek pitch
106107
// with a single descriptor. This is NOT a problem with a single eye
107108
// (since only one channel) and we can still use the hack for HalloWing M4.
108109
typedef struct {
109-
uint16_t renderBuf[240]; // Pixel buffer
110+
uint16_t renderBuf[MAX_DISPLAY_SIZE]; // Pixel buffer
110111
DmacDescriptor descriptor[NUM_DESCRIPTORS]; // DMA descriptor list
111112
} columnStruct;
112113

M4_Eyes/tablegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void calcDisplacement() {
1616
// when rendering. Additionally, only a single axis displacement need
1717
// be calculated, since eye shape is X/Y symmetrical one can just swap
1818
// axes to look up displacement on the opposing axis.
19-
if(displace = (uint8_t *)malloc(120 * 120)) {
19+
if(displace = (uint8_t *)malloc((DISPLAY_SIZE/2) * (DISPLAY_SIZE/2))) {
2020
float eyeRadius2 = (float)(eyeRadius * eyeRadius);
2121
uint8_t x, y;
2222
float dx, dy, d2, d, h, a, pa;
@@ -28,7 +28,7 @@ void calcDisplacement() {
2828
yield(); // Periodic yield() makes sure mass storage filesystem stays alive
2929
dy = (float)y + 0.5;
3030
dy *= dy; // Now dy^2
31-
for(x=0; x<120; x++) {
31+
for(x=0; x<(DISPLAY_SIZE/2); x++) {
3232
// Get distance to origin point. Pixel centers are at +0.5, this is
3333
// normal, desirable and by design -- screen center at (120.0,120.0)
3434
// falls between pixels and allows numerically-correct mirroring.

0 commit comments

Comments
 (0)