Skip to content

Commit a1a0112

Browse files
committed
by request
1 parent ee64d15 commit a1a0112

File tree

3 files changed

+29687
-0
lines changed

3 files changed

+29687
-0
lines changed

TFP401_800x480_RP2040/.feather_rp2040_dvi.test.only

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

0 commit comments

Comments
 (0)