Skip to content

Commit a35f733

Browse files
committed
move to hardware i2c
1 parent 62498f8 commit a35f733

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

src/GameControllers.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <Arduino.h>
55
#include "debouncer.h"
66
#include "SegaController.h"
7-
#define NUNCHUCK_SOFT_I2C // currently, HardWire doesn't work well for hotplugging
7+
// #define NUNCHUCK_SOFT_I2C // currently, HardWire doesn't work well for hotplugging
88
// Also, it probably won't well with SPI remap
99

1010
#ifdef NUNCHUCK_SOFT_I2C
@@ -109,19 +109,23 @@ class NunchuckController : public GameController {
109109
uint8_t sendBytes(uint8_t location, uint8_t value);
110110
const uint8_t i2cAddress = 0x52;
111111
uint8_t buffer[6];
112-
unsigned scl;
113-
unsigned sda;
114112
#ifdef NUNCHUCK_SOFT_I2C
115-
SoftWire* wire;
113+
SoftWire wire;
116114
#else
117-
TwoWire* wire;
115+
TwoWire wire;
118116
#endif
119117

120118

121119
public:
122120
bool begin(void);
123121
bool read(GameControllerData_t* data);
124-
NunchuckController(unsigned scl=PB6, unsigned sda=PB7);
122+
NunchuckController(unsigned scl=PB6, unsigned sda=PB7) :
123+
#ifdef NUNCHUCK_SOFT_I2C
124+
wire(scl, sda, SOFT_STANDARD)
125+
#else
126+
wire(1, 0)
127+
#endif
128+
{};
125129
};
126130

127131
class GamePortController : public GameController {

src/nunchuck.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
#include "GameControllers.h"
22
#include <string.h>
33

4-
NunchuckController::NunchuckController(unsigned _scl, unsigned _sda) {
5-
scl = _scl;
6-
sda = _sda;
7-
}
8-
94
bool NunchuckController::begin() {
10-
if (wire == NULL) {
11-
#ifdef NUNCHUCK_SOFT_I2C
12-
wire = new SoftWire(scl, sda, SOFT_STANDARD);
13-
#else
14-
#error Hardware I2C has trouble with hotplugging.
15-
wire = new TwoWire(1, 0); // I2C_FAST_MODE);
16-
#endif
17-
wire->begin();
18-
}
5+
wire.begin();
196

207
#ifdef MANUAL_DECRYPT
218
if (!sendBytes(0x40,0x00)) {
@@ -35,10 +22,10 @@ bool NunchuckController::begin() {
3522
}
3623

3724
uint8_t NunchuckController::sendBytes(uint8_t location, uint8_t value) {
38-
wire->beginTransmission(i2cAddress);
39-
wire->write(location);
40-
wire->write(value);
41-
return 0 == wire->endTransmission();
25+
wire.beginTransmission(i2cAddress);
26+
wire.write(location);
27+
wire.write(value);
28+
return 0 == wire.endTransmission();
4229
}
4330

4431
uint16_t NunchuckController::rescale(uint8_t x) {
@@ -52,23 +39,23 @@ uint16_t NunchuckController::rescale(uint8_t x) {
5239
}
5340

5441
bool NunchuckController::read(GameControllerData_t* data) {
55-
wire->beginTransmission(i2cAddress);
56-
wire->write(0x00);
57-
if (0!=wire->endTransmission())
42+
wire.beginTransmission(i2cAddress);
43+
wire.write(0x00);
44+
if (0!=wire.endTransmission())
5845
return 0;
5946

6047
delayMicroseconds(500);
6148
#ifdef SERIAL_DEBUG
6249
// Serial.println("Requested");
6350
#endif
6451

65-
wire->requestFrom(i2cAddress, 6);
52+
wire.requestFrom(i2cAddress, 6);
6653
int count = 0;
67-
while (wire->available() && count<6) {
54+
while (wire.available() && count<6) {
6855
#ifdef MANUAL_DECRYPT
69-
buffer[count++] = ((uint8_t)0x17^(uint8_t)wire->read()) + (uint8_t)0x17;
56+
buffer[count++] = ((uint8_t)0x17^(uint8_t)wire.read()) + (uint8_t)0x17;
7057
#else
71-
buffer[count++] = wire->read();
58+
buffer[count++] = wire.read();
7259
#endif
7360
}
7461
if (count < 6)
@@ -90,5 +77,3 @@ bool NunchuckController::read(GameControllerData_t* data) {
9077

9178
return true;
9279
}
93-
94-

0 commit comments

Comments
 (0)