How are you ?
I have a 20×4 I2C LCD display from Joy-IT on my raspberry pi4.
I saved my binary image in a txt file as follows:
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111001000100110011111111111111111111111111111111111111111111111111111111111111111111111
1111111111110001001000000100011011111111111111111111111111111111111111111111111111111111111111111111
1111111111111001100100000100100011111111111111111111111111111111111111111111111111111111111111111111
1111111111001100000000000000100110011111111111111111111111111111111111111111111111111111111111111111
1111111101100111111111111011001000111111111111111111111111111111111111111111111111111111111111111111
1111111000110011111111111111111001100111111111111111111111111111111111111111111111111111111111111111
1111111110001111111111111111111110001111110001111111111111111111111111111111111111111111111111111111
1111110001101111111111111111111110010001110001111111111111111111111111111111111111111111111111111111
1111111100011111111111111111111111000001111111111111111111111111111111111111111111111111111111111111
1111000011101111111111111111111110011001110001110000011110000111100001110001100011000001111000011111
1111100000011111111111111111111111000000110001100000001100000011000000110001100110000000110000001111
1111111111001111111111111111111110000001110011100110001000110011001100011001100110011100110011001111
1111000000001111111111111111111110000000110011100011011100011111000000011001000110000000110000111111
1111111110001111111111111111111100000000110011100011111110000111000011111000000110000111111000011111
1111000000001111111111111111111000000001110011100011001111100011001111111100001110001111111110001111
1111001111001111111111111111100000000001110011100110001000110011001100011100001110011100110011000111
1111111000011111111111100000000000000011110001100000001100000011000000011100001110000000110000001111
1111100011101111111111000000000000000011110001110000111110000111100001111110011111100001111000011111
1111111100011111111111000000000000010011111111111111111111111111111111111000011111111111111111111111
1111111001111111111111000000000000000111111111111111111111111111111111111000111111111111111111111111
1111111111000111111110000000000000000111111111111111111111111111111111111111111111111111111111111111
1111111110011000110000000000000000111111111111111111111111111111111111111111111111111111111111111111
1111111111100000000000000000000000111111111111111111111111111111111111111111111111111111111111111111
1111111111110000000000000000000111111111111111111111111111111111111111111111111111111111111111111111
1111111111111110000000000000011111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111001111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
I manage to reread my file and cut it into 8×5 special character matrix.
However I can not make the display correctly on my entire LCD display. I’m using C not python
#include <stdlib.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <unistd.h>
#define LCD_ADDRESS 0x27
#define LCD_CHR 1
#define LCD_CMD 0
#define LCD_BACKLIGHT 0x08
#define ENABLE 0b00000100
#define E_PULSE 0.0005
#define E_DELAY 0.0005
int fd;
void lcd_toggle_enable(int bits);
void lcd_byte(int bits, int mode);
void lcd_init();
void lcd_loc(int line);
void lcd_loc_pos(int line, int position);
void lcd_custom_char(int location, unsigned char charmap[]);
void lcd_string(const char *message, int line);
void lcd_init() {
fd = wiringPiI2CSetup(LCD_ADDRESS);
lcd_byte(0x33, LCD_CMD);
lcd_byte(0x32, LCD_CMD);
lcd_byte(0x06, LCD_CMD);
lcd_byte(0x0C, LCD_CMD);
lcd_byte(0x28, LCD_CMD);
lcd_byte(0x01, LCD_CMD);
usleep(5000);
}
void lcd_byte(int bits, int mode) {
int bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT;
int bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT;
wiringPiI2CReadReg8(fd, bits_high);
lcd_toggle_enable(bits_high);
wiringPiI2CReadReg8(fd, bits_low);
lcd_toggle_enable(bits_low);
}
void lcd_toggle_enable(int bits) {
usleep(E_DELAY);
wiringPiI2CReadReg8(fd, (bits | ENABLE));
usleep(E_PULSE);
wiringPiI2CReadReg8(fd, (bits & ~ENABLE));
usleep(E_DELAY);
}
void lcd_loc(int line) {
switch (line) {
case 1:
lcd_byte(0x80, LCD_CMD);
break;
case 2:
lcd_byte(0xC0, LCD_CMD);
break;
case 3:
lcd_byte(0x94, LCD_CMD);
break;
case 4:
lcd_byte(0xD4, LCD_CMD);
break;
}
}
void lcd_loc_pos(int line, int position) {
int addr;
switch (line) {
case 1:
addr = 0x80 + position;
break;
case 2:
addr = 0xC0 + position;
break;
case 3:
addr = 0x94 + position;
break;
case 4:
addr = 0xD4 + position;
break;
default:
addr = 0x80;
break;
}
lcd_byte(addr, LCD_CMD);
}
void lcd_custom_char(int location, unsigned char charmap[]) {
location &= 0x7; // On peut stocker jusqu'à 8 caractères (0-7)
lcd_byte(0x40 | (location << 3), LCD_CMD);
for (int i = 0; i < 8; i++) {
lcd_byte(charmap[i], LCD_CHR);
}
}
void lcd_string(const char *message, int line) {
lcd_loc(line);
while (*message) {
lcd_byte(*(message++), LCD_CHR);
}
}
void read_image(const char *filename, unsigned char mat[32][100]) {
FILE *file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "Failed to open file.n");
exit(1);
}
char line[103]; // For 100 characters + null terminator
int row = 0;
while (fgets(line, sizeof(line), file) && row < 32) {
for (int col = 0; col < 100; col++) {
if (line[col] == '1') {
mat[row][col] = 1;
} else {
mat[row][col] = 0;
}
}
row++;
}
fclose(file);
}
void extract_custom_chars(unsigned char mat[32][100], unsigned char custom_chars[80][8]) {
int char_index = 0;
for (int i = 0; i < 32; i += 8) {
for (int j = 0; j < 100; j += 5) {
for (int row = 0; row < 8; row++) {
unsigned char binary = 0;
for (int col = 0; col < 5; col++) {
if (mat[i + row][j + col] == 1) {
binary |= (1 << (4 - col));
}
}
custom_chars[char_index][row] = binary;
}
char_index++;
}
}
}
void print_matrix(unsigned char mat[32][100]) {
printf("Matrix:n");
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 100; j++) {
printf("%d", mat[i][j]);
}
printf("n");
}
}
void print_custom_chars(unsigned char custom_chars[80][8]) {
for (int i = 0; i < 80; i++) {
printf("Custom char %d:n", i);
printf("unsigned char char_%d[8] = {n", i);
for (int j = 0; j < 8; j++) {
printf(" 0b");
for (int k = 4; k >= 0; k--) {
printf("%c", (custom_chars[i][j] & (1 << k)) ? '1' : '0');
}
printf(",n");
}
printf("};nn");
}
}
void display_custom_chars(unsigned char custom_chars[80][8]) {
int index = 0;
for (int line = 1; line <= 4; line++) {
for (int column = 0; column < 20; column++) {
// Charger le caractère courant dans l'emplacement 'column % 8'
int char_location = column % 8;
lcd_custom_char(char_location, custom_chars[index]);
// Positionner le curseur à la position (column) de la ligne (line)
lcd_loc_pos(line, column);
// Afficher le caractère personnalisé à l'emplacement 'char_location'
lcd_byte(char_location, LCD_CHR);
index++;
}
}
}
int main() {
if (wiringPiSetup() == -1) {
fprintf(stderr, "Failed to initialize wiringPi.n");
exit(1);
}
lcd_init(); // Initialiser l'écran LCD
unsigned char mat[32][100] = {0};
read_image("lcd_data.txt", mat);
print_matrix(mat);
unsigned char custom_chars[80][8] = {0};
extract_custom_chars(mat, custom_chars);
//print_custom_chars(custom_chars);
display_custom_chars(custom_chars);
return 0;
}```