Skip to content

Commit d36d920

Browse files
committed
Fixed Linux support to share the I2C handle with other libraries
1 parent 60f24d2 commit d36d920

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

src/OneBitDisplay.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ void ONE_BIT_DISPLAY::setI2CPins(int iSDA, int iSCL, int iReset)
106106
_obd.iSDAPin = iSDA;
107107
_obd.iSCLPin = iSCL;
108108
_obd.iRSTPin = iReset;
109+
#ifdef __LINUX__
110+
_obd.bbi2c.file_i2c = -1;
111+
#endif
109112
}
110113
void ONE_BIT_DISPLAY::setBitBang(bool bBitBang)
111114
{

src/OneBitDisplay.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
// For Linux and esp-idf we add a file/device handle member
2424
// to the BBI2C structure
25-
#ifndef ARDUINO
25+
#if !defined( ARDUINO ) && !defined(__BB_I2C__)
26+
#define __BB_I2C__
2627
typedef struct _tagbbi2c
2728
{
2829
int file_i2c;
@@ -537,7 +538,7 @@ uint8_t * obdPlayAnimFrame(OBDISP *pOBD, uint8_t *pAnimation, uint8_t *pCurrent,
537538
void obdWriteCommand(OBDISP *pOBD, unsigned char c);
538539
void obdSetPosition(OBDISP *pOBD, int x, int y, int bRender);
539540
void obdWriteDataBlock(OBDISP *pOBD, unsigned char *ucBuf, int iLen, int bRender);
540-
void RawWriteData(OBDISP *pOBD, unsigned char *ucBuf, int iLen);
541+
//void RawWriteData(OBDISP *pOBD, unsigned char *ucBuf, int iLen);
541542
//
542543
// Scroll the internal buffer by 1 scanline (up/down)
543544
// width is in pixels, lines is group of 8 rows

src/linux_io.inl

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,22 @@
1313
static uint8_t u8Temp[40]; // for stretched character drawing
1414
static volatile uint8_t u8End = 0;
1515
static uint8_t u8Cache[MAX_CACHE];
16-
struct gpiod_chip *chip = NULL;
17-
struct gpiod_line *lines[128];
16+
static struct gpiod_chip *chip = NULL;
17+
static struct gpiod_line *lines[128];
1818
//
1919
// I/O wrapper functions for Linux
2020
//
21-
int digitalRead(int iPin)
22-
{
23-
return gpiod_line_get_value(lines[iPin]);
24-
} /* digitalRead() */
25-
26-
void digitalWrite(int iPin, int iState)
21+
static void digitalWrite(int iPin, int iState)
2722
{
2823
gpiod_line_set_value(lines[iPin], iState);
2924
} /* digitalWrite() */
3025

31-
void _delay(int l)
26+
static void _delay(int l)
3227
{
3328
usleep(l * 1000);
3429
}
3530

36-
void pinMode(int iPin, int iMode)
31+
static void pinMode(int iPin, int iMode)
3732
{
3833
if (chip == NULL) {
3934
chip = gpiod_chip_open_by_name("gpiochip0");
@@ -48,12 +43,7 @@ void pinMode(int iPin, int iMode)
4843
}
4944
} /* pinMode() */
5045

51-
void delay(uint32_t u32)
52-
{
53-
usleep(u32*1000);
54-
} /* delay() */
55-
56-
void initSPI(OBDISP *pOBD, int iSpeed, int iMOSI, int iCLK, int iCS)
46+
static void initSPI(OBDISP *pOBD, int iSpeed, int iMOSI, int iCLK, int iCS)
5747
{
5848
char szName[32];
5949

@@ -74,23 +64,26 @@ char szName[32];
7464
} /* initSPI() */
7565

7666
// Initialize the I2C bus on Linux
77-
void I2CInit(BBI2C *pI2C, uint32_t iClock)
67+
static void I2CInit(BBI2C *pI2C, uint32_t iClock)
7868
{
7969
char filename[32];
8070
int iChannel = pI2C->iSDA;
8171

72+
// Only try to initialize it if it hasn't already been initialized
73+
if (pI2C->file_i2c == -1) {
8274
sprintf(filename, "/dev/i2c-%d", iChannel);
8375
if ((pI2C->file_i2c = open(filename, O_RDWR)) < 0)
8476
{
8577
fprintf(stderr, "Failed to open the i2c bus\n");
8678
return;
8779
}
80+
}
8881
} /* I2CInit() */
8982
//
9083
// Test if an I2C address responds
9184
// returns 1 for success, 0 for failure
9285
//
93-
uint8_t I2CTest(BBI2C *pI2C, uint8_t addr)
86+
static uint8_t I2CTest(BBI2C *pI2C, uint8_t addr)
9487
{
9588
uint8_t response = 0;
9689
if (ioctl(pI2C->file_i2c, I2C_SLAVE, addr) >= 0) {
@@ -104,7 +97,7 @@ uint8_t response = 0;
10497
//
10598
// Read n bytes from the given I2C address
10699
//
107-
int I2CRead(BBI2C *pI2C, uint8_t iAddr, uint8_t *pData, int iLen)
100+
static int I2CRead(BBI2C *pI2C, uint8_t iAddr, uint8_t *pData, int iLen)
108101
{
109102
int rc;
110103
ioctl(pI2C->file_i2c, I2C_SLAVE, iAddr);
@@ -114,7 +107,7 @@ int rc;
114107
//
115108
// Read n bytes from the given address, after setting the register number
116109
//
117-
int I2CReadRegister(BBI2C *pI2C, uint8_t iAddr, uint8_t u8Register, uint8_t *pData, int iLen)
110+
static int I2CReadRegister(BBI2C *pI2C, uint8_t iAddr, uint8_t u8Register, uint8_t *pData, int iLen)
118111
{
119112
int rc;
120113
// Reading from an I2C device involves first writing the 8-bit register
@@ -131,15 +124,15 @@ int rc;
131124
//
132125
// Write n bytes to the given address
133126
//
134-
int I2CWrite(BBI2C *pI2C, uint8_t iAddr, uint8_t *pData, int iLen)
127+
static int I2CWrite(BBI2C *pI2C, uint8_t iAddr, uint8_t *pData, int iLen)
135128
{
136129
int rc;
137130
ioctl(pI2C->file_i2c, I2C_SLAVE, iAddr);
138131
rc = write(pI2C->file_i2c, pData, iLen);
139132
return rc;
140133
} /* I2CWrite() */
141134

142-
void SPIWrite(OBDISP *pOBD, uint8_t *pData, int iLen)
135+
static void SPIWrite(OBDISP *pOBD, uint8_t *pData, int iLen)
143136
{
144137
struct spi_ioc_transfer spi;
145138

@@ -170,7 +163,7 @@ static void RawWrite(OBDISP *pOBD, unsigned char *pData, int iLen)
170163
}
171164
} /* RawWrite() */
172165

173-
void RawWriteData(OBDISP *pOBD, unsigned char *pData, int iLen)
166+
static void RawWriteData(OBDISP *pOBD, unsigned char *pData, int iLen)
174167
{
175168
uint8_t u8Temp[256];
176169
if (pOBD->com_mode == COM_I2C) {// I2C device

0 commit comments

Comments
 (0)