Linux load bitmap

GTT TFT Support

Moderator: Mods

Post Reply
michaelmalak
LCD?
Posts: 7
Joined: Thu Nov 30, 2023 4:30 pm

Linux load bitmap

Post by michaelmalak »

Newbie here. Attempting to just display a bitmap on GTT35 under Linux in C. I'm using the driver code found in Firmware\Client\gttclient . Below is my code -- it always hangs in the midst of gtt_load_bitmap(). As you can see from the commented-out code, I also tried the gtt25_ API.

#include <termios.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>

#include <gtt_protocol.h>
#include <gtt_device.h>
#include <gtt_text.h>

#define BAUDRATE B115200
#define port "/dev/ttyUSB0"

// Buffer for incoming data
uint8_t rx_buffer[512];

// Buffer for outgoing data
uint8_t tx_buffer[512];

int generic_write(gtt_device *device, uint8_t *data, size_t length)
{
// This function is a platform-dependent write function that
// transfers 'length' bytes of 'data' to the unit.
// This function returns the number of bytes written
return write((intptr_t)device->Context, data, length);
}

int generic_read(gtt_device *device)
{
static uint8_t buffer[512];

// This function is a platform-dependent read function that
// reads a single byte from the unit.
// this function returns the byte read when data is available
// and returns -1 if no data is currently available.
return read((intptr_t)device->Context, buffer, sizeof(buffer) / sizeof(buffer[0]));
}


// The gtt_device structre keeps the state of the gtt protocol and allows
// the library to talk to several devices connected to the same system
// it needs a platform depenend read/write function and some basic
// information about the size and location of the instance specific rx/tx buffers.
gtt_device gtt_device_instance = {
.Write = generic_write,
.Read = generic_read,
.rx_buffer = rx_buffer,
.rx_buffer_size = sizeof(rx_buffer),
.tx_buffer = tx_buffer,
.tx_buffer_size = sizeof(tx_buffer),
};

int main(int argc, char** argv) {
gtt_device *gtt = &gtt_device_instance;
struct termios term;

//Open the serial port
int lcd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);

//handle errors
if (lcd < 0) {
printf("Error opening serial port %s\n", port);
return -1;
}

//Setup the serial port 8 data bits, no parity ,1 stopbit, no flow control
term.c_cflag = BAUDRATE | CS8 | CSTOPB | CLOCAL | CREAD;
term.c_iflag = 0;
term.c_oflag = 0;
term.c_lflag = 0;
term.c_cc[VMIN] = 0; // Minimum number of characters for non-blocking read
term.c_cc[VTIME] = 0; // Timeout in deciseconds for non-blocking read
tcflush(lcd, TCIFLUSH);
tcsetattr(lcd, TCSANOW, &term);

gtt->Context = (void *)(intptr_t)lcd;

//Clear the screen
printf("*********** Clearing screen....\n");
gtt_clear_screen(gtt);

//Draw a line in the default drawing color
// gtt_draw_line(gtt, 0, 0, 480, 272);
// gtt_load_bitmap(gtt, 20, "logo4835monochromeontransparent200.bmp");
printf("******** Loading bitmap...\n");
gtt_load_bitmap(gtt, 20, "Logo70A.bmp");
// gtt25_bitmap_load(gtt, 20, gtt_make_text_ascii("logo4835monochromeontransparent200.bmp"));
printf("********* Displaying bitmap...\n");
gtt_display_bitmap(gtt, 20, 0, 0);
// gtt25_bitmap_capture(gtt, 20, 0, 0, 200, 200);
printf("********* Done\n");

//Process any data coming in
/*
while (1) {
gtt_parser_process(gtt);
}
*/

return 0;
}


Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Re: Linux load bitmap

Post by Raquel »

Hello Michael,

Thank you for posting on the forum.

I see that you have flow control off on your port settings.
Please note that the GTT has default flow control on.
You can choose to have it off via the GTT Designer project.

Can you also please try a simple query command like gtt_get_protocol_version()?

Thanks,
Raquel Malinis
Design and Development
Matrix Orbital

michaelmalak
LCD?
Posts: 7
Joined: Thu Nov 30, 2023 4:30 pm

Re: Linux load bitmap

Post by michaelmalak »

So this works:

gtt_clear_screen(gtt);
gtt_draw_line(gtt, 0, 0, 480, 272);

...the screen is cleared and the line is drawn. However, this hangs on the gtt_get_protocol_version():

gtt_clear_screen(gtt);
gtt_get_protocol_version_return v = gtt_get_protocol_version(gtt);

I tried with and without flow control -- same results

michaelmalak
LCD?
Posts: 7
Joined: Thu Nov 30, 2023 4:30 pm

Re: Linux load bitmap

Post by michaelmalak »

After further investigation, it appears it's never able to read any input from the device, and that's why it's in an infinite loop waiting. The read() in my generic_read() is always returning -1.

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Re: Linux load bitmap

Post by Raquel »

Glad to hear that you resolved the issue.
Thank you,
Raquel Malinis
Design and Development
Matrix Orbital

michaelmalak
LCD?
Posts: 7
Joined: Thu Nov 30, 2023 4:30 pm

Re: Linux load bitmap

Post by michaelmalak »

It's not resolved -- I'm at a loss as to why no bytes are being read. It appears that bytes can be written -- that's why clearing the screen and drawing a line works -- but that bytes cannot be read, such as to get the protocol version. Do you know why that might be? Do I need to set up the serial device differently?

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Re: Linux load bitmap

Post by Raquel »

Hi Michael,

Did you try to turn flow control off on the GTT?
Please check that the autoexec file includes the command to turn the flow control off.

Or, if you have left the GTT default (flow control on), and turned the flow control on on your Linux host, did you connect the CTS/RTS lines to the GTT?

Thank you,
Raquel Malinis
Design and Development
Matrix Orbital

michaelmalak
LCD?
Posts: 7
Joined: Thu Nov 30, 2023 4:30 pm

Re: Linux load bitmap

Post by michaelmalak »

I'm on USB, so is flow control still applicable?

michaelmalak
LCD?
Posts: 7
Joined: Thu Nov 30, 2023 4:30 pm

Re: Linux load bitmap

Post by michaelmalak »

To give an update -- I'm getting closer. I was able to verify sending/receiving from the command line by execute in one terminal window:

stty -F /dev/ttyUSB0 115200 raw
cat /dev/ttyUSB0 | xxd

...and executing in a second terminal window:

echo -ne '\xFE\x00' > /dev/ttyUSB0

...which gives the output:

cat /dev/ttyUSB0 | xxd
00000000: fc00 0002 020e fc00 0002 020e fc00 0002 ................

(I executed the echo three times in order to fill up the xxd line so we could see it.)

That of course is the expected response.

So now that I've verified communication from the Linux command line, I now need to figure out how to do it from a Linux C program. I will work on that in the coming days. In the meantime, don't worry about this until I've had time to look into it more.

michaelmalak
LCD?
Posts: 7
Joined: Thu Nov 30, 2023 4:30 pm

Re: Linux load bitmap

Post by michaelmalak »

I got it working. Mostly the problem was that my generic_read() was reading multiple bytes when it should have been reading only byte at a time. If anyone Googles this thread, the corrected code is below.

#include <termios.h>
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>

#include <gtt_protocol.h>
#include <gtt_device.h>
#include <gtt_text.h>

#define BAUDRATE B115200
#define port "/dev/ttyUSB0"

// Buffer for incoming data
uint8_t rx_buffer[512];

// Buffer for outgoing data
uint8_t tx_buffer[512];

int generic_write(gtt_device *device, uint8_t *data, size_t length)
{
// This function is a platform-dependent write function that
// transfers 'length' bytes of 'data' to the unit.
// This function returns the number of bytes written
return write((intptr_t)device->Context, data, length);
}

int generic_read(gtt_device *device)
{
uint8_t buffer = 0;

// This function is a platform-dependent read function that
// reads a single byte from the unit.
// this function returns the byte read when data is available
// and returns -1 if no data is currently available.
read((intptr_t)device->Context, &buffer, sizeof(buffer));
return buffer;
}


// The gtt_device structre keeps the state of the gtt protocol and allows
// the library to talk to several devices connected to the same system
// it needs a platform depenend read/write function and some basic
// information about the size and location of the instance specific rx/tx buffers.
gtt_device gtt_device_instance = {
.Write = generic_write,
.Read = generic_read,
.rx_buffer = rx_buffer,
.rx_buffer_size = sizeof(rx_buffer),
.tx_buffer = tx_buffer,
.tx_buffer_size = sizeof(tx_buffer),
};

int main(int argc, char** argv) {
gtt_device *gtt = &gtt_device_instance;
struct termios term;

//Open the serial port
int lcd = open(port, O_RDWR | O_NOCTTY);

//handle errors
if (lcd < 0) {
printf("Error opening serial port %s\n", port);
return -1;
}

//Setup the serial port 8 data bits, no parity ,1 stopbit, no flow control
// term.c_cflag = BAUDRATE | CS8 | CSTOPB | CLOCAL | CREAD;
term.c_cflag = BAUDRATE | CS8 | CSTOPB | CREAD;
term.c_iflag = 0;
term.c_oflag = 0;
term.c_lflag = 0;
term.c_cc[VMIN] = 0; // Minimum number of characters for non-blocking read
term.c_cc[VTIME] = 5; // Timeout in deciseconds for non-blocking read
tcflush(lcd, TCIFLUSH);
tcsetattr(lcd, TCSANOW, &term);

gtt->Context = (void *)(intptr_t)lcd;

gtt_clear_screen(gtt);
gtt_load_bitmap(gtt, 20, "logo4835whiteonblack200.bmp");
gtt_display_bitmap(gtt, 20, 0, 0);

//Process any data coming in
/*
while (1) {
gtt_parser_process(gtt);
}
*/

return 0;
}


Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Re: Linux load bitmap

Post by Raquel »

Good to hear you got it working, Michael.
Thank you for sharing your code here.

Thanks,
Raquel Malinis
Design and Development
Matrix Orbital

Post Reply