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: Fun With The Arduino Esplora: A Digital Picture Frame - miguelgrinberg.com
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:
/* Demo of draw circle's APP
drawCircle(int poX, int poY, int r,INT16U color);
fillCircle(int poX, int poY, int r,INT16U color);
*/
#include <stdint.h>
#include <TFTv2.h>
#include <SPI.h>
int ApertureDiameter = 12;
int delayMS = 400;
int x_size = 240;
int y_size = 320;
int x_centre = x_size/2;
int y_centre = y_size/2;
int NA_radius = 50;
unsigned short *line = 0;
void setup()
{
Tft.TFTinit(); //init TFT library
Serial.begin(115200);
delay(1000);
Tft.fillCircle(x_centre, y_centre, ApertureDiameter/2, WHITE);
delay(1000);
Tft.fillCircle(x_centre, y_centre, ApertureDiameter/2, BLACK);
line = new unsigned short[x_size];
Serial.println(-1.0);
}
void loop()
{
int bytes;
if (Serial.available() > 0) {
char cmd = Serial.read();
switch (cmd) {
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).