wiringPi/examples: improve i2c-lcd.c

Signed-off-by: Steve Jeong <how2soft@gmail.com>
Change-Id: I8602dca171b3d681d5481b541e087bed47d2a761
This commit is contained in:
Steve Jeong
2023-06-02 11:43:29 +09:00
parent 0bdc5a78e4
commit 18668e8b6c

View File

@@ -28,7 +28,6 @@
// Define some device parameters
#define I2C_ADDR 0x3f // I2C device address
#define LCD_WIDTH 20
// Define some device constants
#define LCD_CHR 1 // Mode - Sending data
@@ -40,7 +39,7 @@
#define LINE4 0xD4 // 4th line
#define LCD_BACKLIGHT 0x08 // On
// LCD_BACKLIGHT = 0x00 # Off
// #define LCD_BACKLIGHT 0x00 // Off
#define ENABLE 0b00000100 // Enable bit
@@ -50,15 +49,12 @@ void lcd_toggle_enable(int bits);
void display_string(const char *string, int line);
int fd;
char *bus; /* /dev/i2c-0 ~ /dev/i2c-9 */
int address;
char *bus = "0"; /* /dev/i2c-0 ~ /dev/i2c-9 */
int address = 0x3f;
int main(int argc, char *argv[]) {
char device[16] = "/dev/i2c-";
bus = "0";
address = 0x3f;
char device[12];
if (wiringPiSetup () == -1)
exit (1);
@@ -71,7 +67,7 @@ int main(int argc, char *argv[]) {
address = strtoul(argv[2], NULL, 16);
}
strncat(device, bus, 1);
snprintf(device, 11, "%s%s", "/dev/i2c-", bus);
fd = wiringPiI2CSetupInterface(device, address);
lcd_init(); // setup LCD
@@ -82,19 +78,16 @@ int main(int argc, char *argv[]) {
}
return 0;
}
void display_string(const char *string, int line) {
// go to location on LCD
lcd_byte(line, LCD_CMD);
if (strlen(string) > LCD_WIDTH) {
printf("message is too long!\n");
return;
while (*string) {
lcd_byte(*(string++), LCD_CHR);
}
while (*string) lcd_byte(*(string++), LCD_CHR);
}
void lcd_byte(int bits, int mode) {