From 801eeb00e470d5c154d84c9b6f7d744b164db9df Mon Sep 17 00:00:00 2001 From: michaeluman Date: Wed, 22 May 2024 13:10:08 -0700 Subject: [PATCH] Added i2c_disconnect to c sdk * Added closeSerialPort() for Linux build * i2c_disconnect() closes the serial port, and resets the `connected` flag to zero. --- c/common/i2cdriver.c | 17 ++++++++++++++++- c/common/i2cdriver.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/c/common/i2cdriver.c b/c/common/i2cdriver.c index a3a5ac2..6185eb2 100644 --- a/c/common/i2cdriver.c +++ b/c/common/i2cdriver.c @@ -184,8 +184,14 @@ void writeToSerialPort(int fd, const uint8_t *b, size_t s) for (i = 0; i < s; i++) printf("%02x ", 0xff & b[i]); printf("\n"); -#endif +#endif } + +void closeSerialPort(HANDLE hSerial) +{ + close((int)hSerial); +} + #endif // } // ****************************** CCITT CRC ********************************* @@ -268,6 +274,15 @@ void i2c_connect(I2CDriver *sd, const char* portname) sd->e_ccitt_crc = sd->ccitt_crc; } +void i2c_disconnect(I2CDriver *sd) +{ + if (sd->connected) { + closeSerialPort(sd->port); + sd->port = -1; + sd->connected = 0; + } +} + static void charCommand(I2CDriver *sd, char c) { writeToSerialPort(sd->port, (uint8_t*)&c, 1); diff --git a/c/common/i2cdriver.h b/c/common/i2cdriver.h index aa14652..94510db 100644 --- a/c/common/i2cdriver.h +++ b/c/common/i2cdriver.h @@ -29,6 +29,7 @@ typedef struct { } I2CDriver; void i2c_connect(I2CDriver *sd, const char* portname); +void i2c_disconnect(I2CDriver *sd); void i2c_getstatus(I2CDriver *sd); int i2c_write(I2CDriver *sd, const uint8_t bytes[], size_t nn); void i2c_read(I2CDriver *sd, uint8_t bytes[], size_t nn);