Added i2c_disconnect to c sdk

* Added closeSerialPort() for Linux build
* i2c_disconnect() closes the serial port, and resets the `connected` flag to zero.
This commit is contained in:
michaeluman 2024-05-22 13:10:08 -07:00
parent 6e0696e0bf
commit 801eeb00e4
2 changed files with 17 additions and 1 deletions

View File

@ -184,8 +184,14 @@ void writeToSerialPort(int fd, const uint8_t *b, size_t s)
for (i = 0; i < s; i++) for (i = 0; i < s; i++)
printf("%02x ", 0xff & b[i]); printf("%02x ", 0xff & b[i]);
printf("\n"); printf("\n");
#endif #endif
} }
void closeSerialPort(HANDLE hSerial)
{
close((int)hSerial);
}
#endif // } #endif // }
// ****************************** CCITT CRC ********************************* // ****************************** CCITT CRC *********************************
@ -268,6 +274,15 @@ void i2c_connect(I2CDriver *sd, const char* portname)
sd->e_ccitt_crc = sd->ccitt_crc; 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) static void charCommand(I2CDriver *sd, char c)
{ {
writeToSerialPort(sd->port, (uint8_t*)&c, 1); writeToSerialPort(sd->port, (uint8_t*)&c, 1);

View File

@ -29,6 +29,7 @@ typedef struct {
} I2CDriver; } I2CDriver;
void i2c_connect(I2CDriver *sd, const char* portname); void i2c_connect(I2CDriver *sd, const char* portname);
void i2c_disconnect(I2CDriver *sd);
void i2c_getstatus(I2CDriver *sd); void i2c_getstatus(I2CDriver *sd);
int i2c_write(I2CDriver *sd, const uint8_t bytes[], size_t nn); int i2c_write(I2CDriver *sd, const uint8_t bytes[], size_t nn);
void i2c_read(I2CDriver *sd, uint8_t bytes[], size_t nn); void i2c_read(I2CDriver *sd, uint8_t bytes[], size_t nn);