Send Image over Serial to Arduino and Display on TFT (ILI9341 Driver)
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
For a project I want to send an image generated in Matlab to an Arduino which has an external TFT Module (ILI9341-Driver with an 320x240 Display). I'm able to send the bytes over Serial, but the result is not really satisfying seen on the TFT. According to this tutorial: http://blog.miguelgrinberg.com/post/fun-with-the-arduino-esplora-a-digital-picture-frame
I'm pushing each pixel as a 2-Byte information containing the Colorcode over Serial (@115200 Baud). After Reading one row into a buffer on the arduino I display it. I'm having problems with synchronization and colors. I simply tried to adapt the code and ported it from python to matlab. Probably I made some bad mistackes. Any ideas?
Arduino code:
void loop()
{
int bytes;
if (Serial.available() > 0) {
char cmd = Serial.read();
switch (cmd) {
case 'C': // connect
Serial.println(x_size);
Serial.println(y_size);
break;
case 'I': // image
for (int row = 0; row < x_size; row++) {
bytes = x_size * 2; // one line has 240 pixel @ 2 byte per pixel (color)
char* p = (char*)line;
while (bytes > 0) { // serial port has a buffer of only 64 bytes, iterate until the entire line has been read
int n = Serial.readBytes(p, bytes);
if (n == 0) {
// timeout
return;
}
p += n;
bytes -= n;
}
for (int col = 0; col < y_size; col++)
Tft.setPixel(col,row,line[col]);
}
}
}
}
and Matlab-Code:
z = generateSquare(x, x, .4);
z = uint8(z);
z = (cat(3, z, z, z));
%Wait until Arduino is ready for communication
result = 0;
while(result~=-1.0)
result=fscanf(serial_port,'%d')
end
%send image data over serial port
fprintf(serial_port,'I'); %sends string via serial
for j=1:size(z, 2) % cols
for i=1:size(z, 1) % rows
red = z(i, j, 1);
green = z(i, j, 2);
blue = z(i, j, 3);
red = circshift(red,-3,2);
green = circshift(green,-2,2);
blue = circshift(blue,-3,2);
data_1 = dec2bin(bitor(circshift(bitand(green,3), 5, 2), red));
data_2 = dec2bin(bitor(circshift(blue , 3, 2), circshift(green ,-3, 2)));
data = sprintf('%c%c', data_1, data_2);
%dataString = num2str(z(i, j)); %converts to string
fprintf(serial_port, data); %sends string via serial
end
disp(strcat('Row: ', num2str(j) ))
end
The image is a 240x320 px matrix, three channels with a square in the middle of it (Black/White).
0 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!