Skip to content

Commit 15c8bea

Browse files
committed
Modified for platform agnosticism
- original code used pointers to registers in the AVR arch to speed read/write pin access. Changed that to normal digitalWrite digitalRead functions to allow the code to compile across multiple platforms w/o any need to adjust library
1 parent a512b46 commit 15c8bea

File tree

2 files changed

+6
-44
lines changed

2 files changed

+6
-44
lines changed

src/SFE_MicroOLED.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ void MicroOLED::begin()
178178
setDrawMode(NORM);
179179
setCursor(0,0);
180180

181-
dcport = portOutputRegister(digitalPinToPort(dcPin));
182-
dcpinmask = digitalPinToBitMask(dcPin);
183-
dcreg = portModeRegister(digitalPinToPort(dcPin));
184-
185181
pinMode(dcPin, OUTPUT);
186182
pinMode(rstPin, OUTPUT);
187183

@@ -251,10 +247,8 @@ void MicroOLED::command(uint8_t c) {
251247

252248
if (interface == MODE_SPI)
253249
{
254-
*dcport &= ~dcpinmask; // DC pin LOW for a command
255-
*ssport &= ~sspinmask; // SS LOW to initialize transfer
250+
digitalWrite(dcPin, LOW);; // DC pin LOW for a command
256251
spiTransfer(c); // Transfer the command byte
257-
*ssport |= sspinmask; // SS HIGH to end transfer
258252
}
259253
else if (interface == MODE_I2C)
260254
{
@@ -280,11 +274,9 @@ void MicroOLED::data(uint8_t c) {
280274

281275
if (interface == MODE_SPI)
282276
{
283-
*dcport |= dcpinmask; // DC HIGH for a data byte
277+
digitalWrite(dcPin, HIGH); // DC HIGH for a data byte
284278

285-
*ssport &= ~sspinmask; // SS LOW to initialize SPI transfer
286279
spiTransfer(c); // Transfer the data byte
287-
*ssport |= sspinmask; // SS HIGH to end SPI transfer
288280
}
289281
else if (interface == MODE_I2C)
290282
{

src/hardware.cpp

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4141
**/
4242
void MicroOLED::spiSetup()
4343
{
44-
// Gather the CS pin's PORT, PIN, and DDR registers. Writing
45-
// these directly will make things much faster.
46-
ssport = portOutputRegister(digitalPinToPort(csPin));
47-
sspinmask = digitalPinToBitMask(csPin);
48-
ssreg = portModeRegister(digitalPinToPort(csPin));
49-
5044
// Initialize the pins:
5145
pinMode(MOSI, OUTPUT); // MOSI is an OUTPUT
5246
pinMode(SCK, OUTPUT); // SCK is an OUTPUT
@@ -67,7 +61,9 @@ void MicroOLED::spiSetup()
6761
**/
6862
void MicroOLED::spiTransfer(byte data)
6963
{
64+
digitalWrite(csPin, LOW);
7065
SPI.transfer(data);
66+
digitalWrite(csPin, HIGH);
7167
}
7268

7369
/** \brief Initialize the I2C Interface
@@ -79,9 +75,6 @@ void MicroOLED::i2cSetup()
7975
{
8076
// Initialize Wire library (I2C)
8177
Wire.begin();
82-
83-
// SCL frequency = (F_CPU) / (16 + 2(TWBR) * (prescalar))
84-
TWBR = ((F_CPU / I2C_FREQ) - 16) / 2;
8578
}
8679

8780
/** \brief Write a byte over I2C
@@ -105,21 +98,6 @@ void MicroOLED::i2cWrite(byte address, byte dc, byte data)
10598
**/
10699
void MicroOLED::parallelSetup()
107100
{
108-
// Gather the CS pin's PORT, PIN and DDR registers.
109-
ssport = portOutputRegister(digitalPinToPort(csPin));
110-
sspinmask = digitalPinToBitMask(csPin);
111-
ssreg = portModeRegister(digitalPinToPort(csPin));
112-
113-
// Gather the WR pin's PORT, PIN and DDR registers.
114-
wrport = portOutputRegister(digitalPinToPort(wrPin));
115-
wrpinmask = digitalPinToBitMask(wrPin);
116-
wrreg = portModeRegister(digitalPinToPort(wrPin));
117-
118-
// Gather the RD pin's PORT, PIN and DDR registers.
119-
rdport = portOutputRegister(digitalPinToPort(rdPin));
120-
rdpinmask = digitalPinToBitMask(rdPin);
121-
rdreg = portModeRegister(digitalPinToPort(rdPin));
122-
123101
// Initialize WR, RD, CS and data pins as outputs.
124102
pinMode(wrPin, OUTPUT);
125103
digitalWrite(wrPin, HIGH);
@@ -146,18 +124,12 @@ void MicroOLED::parallelWrite(byte data, byte dc)
146124

147125
// chip select high->low
148126
digitalWrite(csPin, LOW);
149-
//*ssport &= ~sspinmask; // SS LOW
150127

151128
// dc high or low
152129
digitalWrite(dcPin, dc);
153-
/*if (dc)
154-
*dcport |= dcpinmask; // DC HIGH
155-
else
156-
*dcport &= ~dcpinmask; // DC pin LOW*/
157130

158131
// wr high->low
159132
digitalWrite(wrPin, LOW);
160-
//*wrport &= ~wrpinmask; // SS LOW
161133

162134
// set data pins
163135
for (int i=0; i<8; i++)
@@ -167,13 +139,11 @@ void MicroOLED::parallelWrite(byte data, byte dc)
167139
else
168140
digitalWrite(dPins[i], LOW);
169141
}
170-
//PORTD = data;
171142

172143
// wr low->high
173144
digitalWrite(wrPin, HIGH);
174-
//*wrport |= wrpinmask; // SS HIGH
175145

176146
// cs high
177147
digitalWrite(csPin, HIGH);
178-
//*ssport |= sspinmask; // SS HIGH
179-
}
148+
}
149+

0 commit comments

Comments
 (0)