Skip to content

Commit e1f643a

Browse files
committed
make I2C working
1 parent 6ad95a4 commit e1f643a

File tree

6 files changed

+82
-42
lines changed

6 files changed

+82
-42
lines changed

DisplayBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void DisplayBase::display(lcd_mode_t mode) {
151151
}
152152
}
153153

154-
void DisplayBase::create(uint8_t location, uint8_t charmap[]) {
154+
void DisplayBase::create(uint8_t location, const uint8_t charmap[]) {
155155
if (location > 7) { // only 8 locations available
156156
return;
157157
}

DisplayBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class DisplayBase : public Stream {
105105
* @param location index 0-7
106106
* @param charmap data with custom character
107107
*/
108-
void create(uint8_t location, uint8_t charmap[]);
108+
void create(uint8_t location, const uint8_t charmap[]);
109109

110110
/**
111111
* @brief Writes a single char to a given position, usefull for UDC

TextLCD_I2C.cpp

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@ SOFTWARE.
2020

2121
#include "TextLCD_I2C.h"
2222

23-
TextLCD_I2C::TextLCD_I2C(lcd_size_t size, int8_t address):
24-
DisplayBase{size, true} {
25-
26-
dataWrite(0);
27-
en(0);
28-
rw(1);
23+
TextLCD_I2C::TextLCD_I2C(bool alt_pinmap, lcd_size_t size, int8_t address):
24+
DisplayBase{size, false},
25+
_i2c_addr(address),
26+
_alt_pinmap(alt_pinmap) {
2927
}
3028

31-
TextLCD_I2C::TextLCD_I2C(PinName sda, PinName scl, lcd_size_t size, int8_t address,
32-
uint32_t frequency):
33-
DisplayBase{size, true} {
34-
35-
dataWrite(0);
36-
en(0);
37-
rw(1);
38-
29+
TextLCD_I2C::TextLCD_I2C(PinName sda, PinName scl, bool alt_pinmap, lcd_size_t size,
30+
int8_t address, uint32_t frequency):
31+
DisplayBase{size, false},
32+
_i2c_addr(address),
33+
_alt_pinmap(alt_pinmap) {
3934
_i2c = new (_i2c_obj) I2C(sda, scl);
4035
_i2c->frequency(frequency);
4136
}
@@ -52,6 +47,11 @@ void TextLCD_I2C::initI2C(I2C *i2c_obj) {
5247
}
5348

5449
MBED_ASSERT(_i2c);
50+
51+
dataWrite(0);
52+
en(0);
53+
rw(1);
54+
setBacklight(0);
5555
}
5656

5757
bool TextLCD_I2C::init(I2C *i2c_obj, lcd_char_t chars) {
@@ -86,36 +86,66 @@ uint8_t TextLCD_I2C::dataRead() {
8686
}
8787

8888
void TextLCD_I2C::dataWrite(uint8_t pins) {
89-
_pins &= ~0b11110000;
90-
_pins |= (pins & 0b1111) << 4;
89+
if (_alt_pinmap) {
90+
_pins &= ~0b00001111;
91+
_pins |= (pins & 0b1111);
92+
93+
} else {
94+
_pins &= ~0b11110000;
95+
_pins |= (pins & 0b1111) << 4;
96+
}
9197

9298
i2cWrite();
9399
}
94100

95101
void TextLCD_I2C::en(bool state) {
96-
_pins &= ~0b100;
97-
_pins |= state << 2;
102+
if (_alt_pinmap) {
103+
_pins &= ~0b00010000;
104+
_pins |= state << 4;
105+
106+
} else {
107+
_pins &= ~0b100;
108+
_pins |= state << 2;
109+
}
98110

99111
i2cWrite();
100112
}
101113

102-
void TextLCD_I2C::rs(bool state) {
103-
_pins &= ~0b1;
104-
_pins |= state;
114+
void TextLCD_I2C::rw(bool state) {
115+
if (_alt_pinmap) {
116+
_pins &= ~0b00100000;
117+
_pins |= state << 5;
118+
119+
} else {
120+
_pins &= ~0b10;
121+
_pins |= state << 1;
122+
}
105123

106124
i2cWrite();
107125
}
108126

109-
void TextLCD_I2C::rw(bool state) {
110-
_pins &= ~0b10;
111-
_pins |= state << 1;
127+
void TextLCD_I2C::rs(bool state) {
128+
if (_alt_pinmap) {
129+
_pins &= ~0b01000000;
130+
_pins |= state << 6;
131+
132+
} else {
133+
_pins &= ~0b1;
134+
_pins |= state;
135+
}
112136

113137
i2cWrite();
114138
}
115139

116140
void TextLCD_I2C::setBacklight(bool on) {
117-
_pins &= ~0b1000;
118-
_pins |= on << 3;
141+
if (_alt_pinmap) {
142+
_pins &= ~0b10000000;
143+
_pins |= !on << 7;
144+
145+
} else {
146+
_pins &= ~0b1000;
147+
_pins |= on << 3;
148+
}
119149

120150
i2cWrite();
121151
}

TextLCD_I2C.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,26 @@ class TextLCD_I2C: public DisplayBase {
3030
/**
3131
* @brief Create an I2C LCD interface
3232
*
33+
* @param alt_pinmap PCF8574 altternative pin maping
3334
* @param size Panel size
3435
* @param address 7-bit I2C address of the expander
3536
* @param frequency I2C bus speed
3637
*/
37-
TextLCD_I2C(lcd_size_t size = SIZE_16x2, int8_t address = TEXT_DISPLAY_I2C_ADDRESS);
38+
TextLCD_I2C(bool alt_pinmap = false, lcd_size_t size = SIZE_16x2,
39+
int8_t address = TEXT_DISPLAY_I2C_ADDRESS);
3840

3941
/**
4042
* @brief Create an I2C LCD interface
4143
*
4244
* @param sda SDA pin
4345
* @param scl SCL pin
46+
* @param alt_pinmap PCF8574 altternative pin maping
4447
* @param size Panel size
4548
* @param address 7-bit I2C address of the expander
4649
* @param frequency I2C bus speed
4750
*/
48-
TextLCD_I2C(PinName sda, PinName scl, lcd_size_t size = SIZE_16x2,
49-
int8_t address = TEXT_DISPLAY_I2C_ADDRESS, uint32_t frequency = 400000);
51+
TextLCD_I2C(PinName sda, PinName scl, bool alt_pinmap = false, lcd_size_t size = SIZE_16x2,
52+
int8_t address = TEXT_DISPLAY_I2C_ADDRESS, uint32_t frequency = 100000);
5053

5154
/**
5255
* @brief Destructor
@@ -78,15 +81,19 @@ class TextLCD_I2C: public DisplayBase {
7881
void rs(bool state) override;
7982
void rw(bool state) override;
8083

81-
void initI2C(I2C * i2c_obj = nullptr);
84+
void initI2C(I2C *i2c_obj = nullptr);
8285

8386
private:
84-
I2C *_i2c;
85-
int8_t _i2c_addr;
87+
I2C *_i2c;
88+
const int8_t _i2c_addr;
89+
const bool _alt_pinmap = false;
8690
char _pins = 0;
8791
uint32_t _i2c_obj[sizeof(I2C) / sizeof(uint32_t)] = {0};
8892

8993
bool i2cWrite();
94+
95+
void dataInput() {};
96+
void dataOutput() {};
9097
};
9198

9299
#endif

TextOLED_I2C.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ SOFTWARE.
2020

2121
#include "TextOLED_I2C.h"
2222

23-
TextOLED_I2C::TextOLED_I2C(lcd_size_t size, int8_t address):
24-
TextLCD_I2C{size, address} {
23+
TextOLED_I2C::TextOLED_I2C(bool alt_pinmap, lcd_size_t size, int8_t address):
24+
TextLCD_I2C{alt_pinmap, size, address} {
2525
MBED_ASSERT(size != SIZE_20x4);
2626
}
2727

28-
TextOLED_I2C::TextOLED_I2C(PinName sda, PinName scl, lcd_size_t size, int8_t address,
29-
uint32_t frequency):
30-
TextLCD_I2C{sda, scl, size, address, frequency} {
28+
TextOLED_I2C::TextOLED_I2C(PinName sda, PinName scl, bool alt_pinmap, lcd_size_t size,
29+
int8_t address, uint32_t frequency):
30+
TextLCD_I2C{sda, scl, alt_pinmap, size, address, frequency} {
3131
MBED_ASSERT(size != SIZE_20x4);
3232
}
3333

TextOLED_I2C.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@ class TextOLED_I2C: public TextLCD_I2C {
2828
/**
2929
* @brief Create an I2C OLED interface
3030
*
31+
* @param alt_pinmap PCF8574 altternative pin maping
3132
* @param size Panel size
3233
* @param address 7-bit I2C address of the expander
3334
* @param frequency I2C bus speed
3435
*/
35-
TextOLED_I2C(lcd_size_t size = SIZE_16x2, int8_t address = TEXT_DISPLAY_I2C_ADDRESS);
36+
TextOLED_I2C(bool alt_pinmap = false, lcd_size_t size = SIZE_16x2,
37+
int8_t address = TEXT_DISPLAY_I2C_ADDRESS);
3638

3739
/**
3840
* @brief Create an I2C OLED interface
3941
*
4042
* @param sda SDA pin
4143
* @param scl SCL pin
44+
* @param alt_pinmap PCF8574 altternative pin maping
4245
* @param size Panel size
4346
* @param address 7-bit I2C address of the expander
4447
* @param frequency I2C bus speed
4548
*/
46-
TextOLED_I2C(PinName sda, PinName scl, lcd_size_t size = SIZE_16x2,
47-
int8_t address = TEXT_DISPLAY_I2C_ADDRESS, uint32_t frequency = 400000);
49+
TextOLED_I2C(PinName sda, PinName scl, bool alt_pinmap = false, lcd_size_t size = SIZE_16x2,
50+
int8_t address = TEXT_DISPLAY_I2C_ADDRESS, uint32_t frequency = 100000);
4851

4952
/**
5053
* @brief Initialize display

0 commit comments

Comments
 (0)