Skip to content

Commit 7259b72

Browse files
authored
Merge pull request #2544 from ladyada/edidtester
Edidtester
2 parents ecd84e2 + d1d87e6 commit 7259b72

File tree

3 files changed

+29695
-0
lines changed

3 files changed

+29695
-0
lines changed

TFP401_800x480_RP2040/.feather_rp2040_dvi.test.only

Whitespace-only changes.
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
// SPDX-FileCopyrightText: 2023 PaintYourDragon for Adafruit
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// "Aquarium" example for PicoDVI library. If just starting out,
6+
// see the 8bit_double_buffer which explains the PicoDVI groundwork.
7+
// Comments in THIS file are mostly distinct & new concepts.
8+
// The flying toasters example also goes into more detail.
9+
10+
// IF NO OUTPUT OR RED FLICKER SCANLINES: try Tools->Optimize->(-O3)
11+
12+
#include <PicoDVI.h>
13+
#include "Adafruit_EEPROM_I2C.h"
14+
#include "sprites.h" // Graphics data
15+
16+
//DVIGFX8 display(DVI_RES_320x240p60, true, adafruit_dvibell_cfg);
17+
18+
// See notes in 8bit_double_buffer regarding 400x240 mode.
19+
DVIGFX8 display(DVI_RES_400x240p60, true, adafruit_feather_dvi_cfg);
20+
// Also requires -O3 setting.
21+
22+
// This structure holds pointers to sprite graphics and masks in sprites.h.
23+
const struct {
24+
const uint8_t *sprite[2][2]; // L/R directions and A/B frames
25+
const uint8_t *mask[2][2]; // Same
26+
} spritedata[] = {
27+
// There are FOUR sprites/masks for each fish (and kelp):
28+
// two left-facing, two right-facing, and A/B frames for each.
29+
{ sprite0LA , sprite0LB , sprite0RA , sprite0RB , mask0LA , mask0LB , mask0RA , mask0RB },
30+
{ sprite1LA , sprite1LB , sprite1RA , sprite1RB , mask1LA , mask1LB , mask1RA , mask1RB },
31+
{ sprite2LA , sprite2LB , sprite2RA , sprite2RB , mask2LA , mask2LB , mask2RA , mask2RB },
32+
{ sprite3LA , sprite3LB , sprite3RA , sprite3RB, mask3LA , mask3LB , mask3RA , mask3RB },
33+
{ sprite4LA , sprite4LB , sprite4RA , sprite4RB , mask4LA , mask4LB , mask4RA , mask4RB },
34+
{ sprite5LA , sprite5LB , sprite5RA , sprite5RB , mask5LA , mask5LB , mask5RA , mask5RB },
35+
{ sprite6LA , sprite6LB , sprite6RA , sprite6RB , mask6LA , mask6LB , mask6RA , mask6RB },
36+
{ sprite7LA , sprite7LB , sprite7RA , sprite7RB , mask7LA , mask7LB , mask7RA , mask7RB },
37+
{ sprite8LA , sprite8LB , sprite8RA , sprite8RB , mask8LA , mask8LB , mask8RA , mask8RB },
38+
{ sprite9LA , sprite9LB , sprite9RA , sprite9RB , mask9LA , mask9LB , mask9RA , mask9RB },
39+
{ sprite10LA, sprite10LB, sprite10RA, sprite10RB, mask10LA, mask10LB, mask10RA, mask10RB },
40+
{ sprite11LA, sprite11LB, sprite11RA, sprite11RB, mask11LA, mask11LB, mask11RA, mask11RB },
41+
{ sprite12LA, sprite12LB, sprite12RA, sprite12RB, mask12LA, mask12LB, mask12RA, mask12RB },
42+
{ sprite13LA, sprite13LB, sprite13RA, sprite13RB, mask13LA, mask13LB, mask13RA, mask13RB },
43+
{ sprite14LA, sprite14LB, sprite14RA, sprite14RB, mask14LA, mask14LB, mask14RA, mask14RB },
44+
{ sprite15LA, sprite15LB, sprite15RA, sprite15RB, mask15LA, mask15LB, mask15RA, mask15RB },
45+
{ sprite16LA, sprite16LB, sprite16RA, sprite16RB, mask16LA, mask16LB, mask16RA, mask16RB },
46+
{ sprite17LA, sprite17LB, sprite17RA, sprite17RB, mask17LA, mask17LB, mask17RA, mask17RB },
47+
// Bubbles are a special case. No right/left versions, but A/B frames.
48+
// To use same struct (not a special case), just duplicate each 2X.
49+
{ sprite18A , sprite18B , sprite18A , sprite18B , mask18A , mask18B , mask18A , mask18B },
50+
};
51+
52+
#define N_SPRITES 12 // MUST be >= 6
53+
54+
// This structure contains positions and other data for the sprites
55+
// in motion (notice it's not "const", because contents change).
56+
struct {
57+
int16_t pos[2]; // sprite position (X,Y) * 16
58+
int8_t speed; // sprite speed (-16 to -8 or +8 to +16)
59+
uint8_t index; // which index (in spritedata) to use
60+
uint8_t offset; // Timer offset to de-sync each sprite's animation
61+
} sprite[N_SPRITES];
62+
63+
// Initialize one sprite (index passed to function) to a random offscreen
64+
// position, also randomizing speed and sprite (fish) type.
65+
void randomsprite(uint8_t i) {
66+
// To move the sprites at slightly different speeds, coordinates are
67+
// stored in 1/16 pixel units. When drawing, the stored values get
68+
// divided by 16 to yield final pixel coordinates.
69+
sprite[i].speed = random(8, 17); // 1/2 to 1 pixel per frame
70+
if (random(2)) { // 50/50 random chance...
71+
sprite[i].speed *= -1; // Fish moves right-to-left
72+
sprite[i].pos[0] = (display.width() + random(64)) * 16; // Start off right edge
73+
} else { // Fish moves left-to-right
74+
sprite[i].pos[0] = random(64, 128) * -16; // Start off left edge
75+
}
76+
// WHEEL. OF. FISH. -2 here is to ignore last 2 sprites (kelp, bubbles)
77+
sprite[i].index = random(sizeof spritedata / sizeof spritedata[0] - 2);
78+
if (sprite[i].index == 8) { // Sprite #8 is crab, keep close to ground
79+
sprite[i].pos[1] = random(display.height() - 96, display.height() - 64) * 16;
80+
} else { // Is a fish, upper part of screen
81+
sprite[i].pos[1] = random(display.height() - 96) * 16;
82+
}
83+
sprite[i].offset = random(256); // De-synchronize sprite animation
84+
}
85+
86+
87+
88+
/* Adafruit breakout @ 800x480 ! */
89+
uint8_t const eepromdat[128] = {
90+
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x04, 0x81, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00,
91+
0x01, 0x11, 0x01, 0x03, 0x80, 0x0F, 0x0A, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
93+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x0C, 0x20, 0x80, 0x30, 0xE0, 0x2D, 0x10, 0x28, 0x30,
94+
0xD3, 0x00, 0x6C, 0x44, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
95+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
96+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
97+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17,
98+
};
99+
#define EEPROMSIZE 256UL // 0.5 Kb
100+
#define ADDRESS_SIZE 8
101+
#define EEPROM_ADDR 0x50 // the default address!
102+
103+
byte i2c_eeprom_read_byte(uint8_t deviceaddress, uint16_t eeaddress ) {
104+
byte rdata = 0xFF;
105+
Wire.beginTransmission(deviceaddress); // MSB
106+
Wire.write(eeaddress); // LSB
107+
Wire.endTransmission();
108+
Wire.requestFrom(deviceaddress, (uint8_t)1);
109+
while (!Wire.available()) yield();
110+
rdata = Wire.read();
111+
return rdata;
112+
}
113+
void i2c_eeprom_write_byte(uint8_t deviceaddress, uint16_t eeaddress, byte data ) {
114+
Wire.beginTransmission(deviceaddress); // MSB
115+
Wire.write((byte)eeaddress); // LSB
116+
Wire.write((byte)data);
117+
Wire.endTransmission();
118+
}
119+
120+
bool verify_eeprom() {
121+
for (uint16_t addr = 0; addr < EEPROMSIZE; addr++) {
122+
uint8_t b = 0xFF;
123+
if (addr < sizeof(eepromdat)) {
124+
b = eepromdat[addr];
125+
}
126+
uint8_t d = i2c_eeprom_read_byte(0x50, addr);
127+
delay(5);
128+
Serial.printf("0x%02X, ", d);
129+
if ((addr % 32) == 31)
130+
Serial.println();
131+
132+
if (b != d) {
133+
Serial.printf("verification failed at 0x%02x\n\r", addr);
134+
return false;
135+
}
136+
}
137+
return true;
138+
}
139+
140+
Adafruit_EEPROM_I2C i2ceeprom;
141+
142+
void setup() { // Runs once on startup
143+
Serial.begin(115200);
144+
145+
//while (!Serial) yield();
146+
delay(1000);
147+
148+
if (!display.begin()) { // Blink LED if insufficient RAM
149+
pinMode(LED_BUILTIN, OUTPUT);
150+
for (;;) digitalWrite(LED_BUILTIN, (millis() / 500) & 1);
151+
}
152+
delay(1000);
153+
154+
// Initialize color palette from table in sprites.h. Rather than
155+
// calling setColor() for each one, we can just dump it directly...
156+
memcpy(display.getPalette(), palette, sizeof palette);
157+
display.swap(false, true); // Duplicate same palette to front & back buffers
158+
159+
160+
display.fillScreen(0);
161+
display.setCursor(0, 0);
162+
display.setTextColor(1);
163+
display.setTextSize(3);
164+
display.println("TFP401 800x480 EDID");
165+
166+
if (i2ceeprom.begin(0x50)) { // you can stick the new i2c addr in here, e.g. begin(0x51);
167+
Serial.println("Found I2C EEPROM");
168+
display.println("I2C EEPROM found!");
169+
display.swap();
170+
} else {
171+
Serial.println("I2C EEPROM not identified ... check your connections?\r\n");
172+
display.println("I2C EEPROM NOT found?");
173+
display.swap();
174+
while (1) delay(10);
175+
}
176+
177+
178+
if (! verify_eeprom() ) {
179+
display.swap();
180+
display.print("Writing EEPROM...");
181+
display.swap();
182+
183+
Serial.println("Starting");
184+
for (uint16_t addr = 0; addr < EEPROMSIZE; addr++) {
185+
uint8_t b = 0xFF;
186+
if (addr < sizeof(eepromdat)) {
187+
b = eepromdat[addr];
188+
}
189+
i2c_eeprom_write_byte(0x50, addr, b);
190+
delay(5);
191+
Serial.printf("0x%02X, ", b);
192+
if ((addr % 32) == 31)
193+
Serial.println();
194+
}
195+
display.swap();
196+
display.println("OK!");
197+
display.swap();
198+
}
199+
200+
display.swap();
201+
display.print("Verifying EEPROM...");
202+
display.swap();
203+
if (verify_eeprom()) {
204+
display.swap();
205+
display.println("OK!");
206+
display.swap();
207+
} else {
208+
display.swap();
209+
display.println(" :(");
210+
display.swap();
211+
while (1);
212+
}
213+
214+
display.swap();
215+
display.println("Time for Fish ><(((o>");
216+
display.swap();
217+
delay(1000);
218+
219+
// Randomize initial sprite states
220+
randomSeed(analogRead(A0)); // Seed randomness from unused analog in
221+
int range = display.width() + 64;
222+
for (int i=0; i<3; i++) { // FIRST THREE sprites...
223+
sprite[i].index = 17; // Are always kelp
224+
sprite[i].speed = random(2) ? 1 : -1; // 50/50 left/right flip
225+
sprite[i].pos[0] = (random(range * i / 3, range * (i + 1) / 3 - 64) - 32) * 16;
226+
sprite[i].pos[1] = random(display.height() - 120, display.height() - 100) * 16;
227+
sprite[i].offset = random(256);
228+
}
229+
for (int i=3; i<6; i++) { // NEXT THREE sprites...
230+
sprite[i].index = 18; // Are always bubbles
231+
sprite[i].speed = 0;
232+
sprite[i].pos[0] = display.width() * 16; // Start them all offscreen
233+
sprite[i].pos[1] = random(display.height()) * 8;
234+
sprite[i].offset = random(256);
235+
}
236+
for (int i=6; i<N_SPRITES; i++) randomsprite(i); // Rest are fish
237+
}
238+
239+
uint8_t frame = 0; // Counter for animation
240+
241+
242+
243+
void loop() { // Runs once every frame
244+
display.fillScreen(0); // Clear back framebuffer,
245+
for (int x=0; x<display.width(); x += 192) { // Tile background sprite
246+
// Although DVIGFX8 is a COLOR display type, we leverage GFX's
247+
// drawGrayscaleBitmap() function to draw the sprites...it saves us
248+
// writing a ton of code this way.
249+
display.drawGrayscaleBitmap(x, display.height() - 64, gravel, 192, 64);
250+
}
251+
252+
for (int i=0; i<N_SPRITES; i++) { // and then the rest of the sprites...
253+
uint8_t dir = sprite[i].speed > 0; // Left/right
254+
uint8_t fr = ((frame + sprite[i].offset) >> 4) & 1; // A/B frame
255+
if (sprite[i].speed) { // FISH or KELP; 64x64 sprite
256+
display.drawGrayscaleBitmap(sprite[i].pos[0] / 16, sprite[i].pos[1] / 16,
257+
spritedata[sprite[i].index].sprite[dir][fr],
258+
spritedata[sprite[i].index].mask[dir][fr], 64, 64);
259+
if (abs(sprite[i].speed) > 1) { // Not kelp...
260+
sprite[i].pos[0] += sprite[i].speed; // Update position, check if offscreen...
261+
if (((sprite[i].speed > 0) && (sprite[i].pos[0] > (display.width() * 16))) ||
262+
((sprite[i].speed < 0) && (sprite[i].pos[0] < -64 * 16)))
263+
randomsprite(i); // Replace with a new fish
264+
}
265+
} else { // Is BUBBLES
266+
display.drawGrayscaleBitmap(sprite[i].pos[0] / 16, sprite[i].pos[1] / 16,
267+
spritedata[sprite[i].index].sprite[0][fr],
268+
spritedata[sprite[i].index].mask[0][fr], 64, 16);
269+
sprite[i].pos[1] -= 16; // Move up by 1 pixel
270+
if (sprite[i].pos[1] < -256) { // Off top of screen?
271+
int j = random(6, N_SPRITES); // Pick a random fish,
272+
sprite[i].pos[0] = sprite[j].pos[0]; // and move bubbles there
273+
sprite[i].pos[1] = sprite[j].pos[1] + 384;
274+
}
275+
}
276+
}
277+
278+
// Swap front/back buffers, do not duplicate current screen state
279+
// to next frame, we'll draw it new from scratch each time.
280+
display.swap();
281+
frame++; // Increment animation counter; "rolls over" 0-255 automatically.
282+
}

0 commit comments

Comments
 (0)