Post by graaaeme10 on Jan 18, 2021 21:35:00 GMT -8
Hi there, I've been scouring the internet and forum for a solution to this, but I haven't found anything yet.
I'm trying to take a screenshot and retrieve with python, as shown in some examples.
Here's the arduino code:
And the python script:
After a bit of fiddling I managed to get a .ppm output, which unfortunately appears corrupted when opened in irfanview:
I am on windows 10, using python 2.7 to run the script.
Arduino Uno, gameduino 3x
Any ideas to solve this problem?
Thanks
I'm trying to take a screenshot and retrieve with python, as shown in some examples.
Here's the arduino code:
#include <EEPROM.h>
#include <SPI.h>
#include <GD2.h>
////////////////////////////////////////////////////////////////////////
#define REG_SCREENSHOT_EN (ft8xx_model ? 0x302010UL : 0x102410UL) // Set to enable screenshot mode
#define REG_SCREENSHOT_Y (ft8xx_model ? 0x302014UL : 0x102414UL) // Y line register
#define REG_SCREENSHOT_START (ft8xx_model ? 0x302018UL : 0x102418UL) // Screenshot start trigger
#define REG_SCREENSHOT_BUSY (ft8xx_model ? 0x3020e8UL : 0x1024d8UL) // Screenshot ready flags
#define REG_SCREENSHOT_READ (ft8xx_model ? 0x302174UL : 0x102554UL) // Set to enable readout
#define RAM_SCREENSHOT (ft8xx_model ? 0x3c2000UL : 0x1C2000UL) // Screenshot readout buffer
////////////////////////////////////////////////////////////////////////
void screenshot_480x272() {
int x, y;
GD.wr(REG_SCREENSHOT_EN, 1);
Serial.write("P6\n480\n272\n255\n"); // PPM header
for (y = 0; y < 272; y++) {
GD.wr16(REG_SCREENSHOT_Y, y);
GD.wr(REG_SCREENSHOT_START, 1);
while (GD.rd32(REG_SCREENSHOT_BUSY) | GD.rd32(REG_SCREENSHOT_BUSY + 4));
GD.wr(REG_SCREENSHOT_READ, 1);
for (x = 0; x < 480; x++) {
char rgb[3];
unsigned char b = GD.rd(RAM_SCREENSHOT + (4 * x) + 0);
unsigned char g = GD.rd(RAM_SCREENSHOT + (4 * x) + 1);
unsigned char r = GD.rd(RAM_SCREENSHOT + (4 * x) + 2);
unsigned char a = GD.rd(RAM_SCREENSHOT + (4 * x) + 3);
Serial.write(r);
Serial.write(g);
Serial.write(b);
}
GD.wr(REG_SCREENSHOT_READ, 0);
}
GD.wr16(REG_SCREENSHOT_EN, 0);
}
void setup()
{
Serial.begin(115200);
GD.begin();
}
void loop()
{
GD.cmd_gradient(0, 0, 0x000000, 480, 272, 0x303080);
GD.Begin(POINTS);
for (int i = 0; i < 100; i++) {
GD.ColorRGB(GD.random(256), GD.random(256), GD.random(256));
GD.PointSize(GD.random(200));
GD.Vertex2ii(GD.random(480), GD.random(272));
}
GD.ColorRGB(0xffffff);
GD.cmd_text(240, 136, 31, OPT_CENTER, "This is a screenshot");
GD.swap();
delay(5000); // 6 second delay, time to start serial capture
screenshot_480x272();
}
And the python script:
import sys
import time
import serial
if __name__ == '__main__':
import sys, getopt
try:
optlist, args = getopt.getopt(sys.argv[1:], "vh:s:")
except getopt.GetoptError, reason:
print reason
print
print 'usage: getppm.py -h <usb port> -s <usb speed>'
print
print
sys.exit(1)
optdict = dict(optlist)
if '-s' in optdict:
speed = int(optdict['-s'])
else:
speed = 115200
port = optdict.get('-h', "COM3")
print 'Opening %s at %d' % (port, speed)
ser = serial.Serial(port, speed)
ser.setDTR(0)
time.sleep(0.1)
ser.setDTR(1)
# Expected size of the PPM. 15 bytes for the header, then 480x272 image
expected_size = 15 + (3 * 480 * 272)
for frame in xrange(1):
# z = ser.read_until('0x400806b8')
# print z
# print 'From here -----'
s = ser.read(expected_size)
print s
open("shot%04d.ppm" % frame, "w").write(s)
print "frame %d written" % frame
After a bit of fiddling I managed to get a .ppm output, which unfortunately appears corrupted when opened in irfanview:
I am on windows 10, using python 2.7 to run the script.
Arduino Uno, gameduino 3x
Any ideas to solve this problem?
Thanks