Skip to content

Commit

Permalink
initial setup working
Browse files Browse the repository at this point in the history
  • Loading branch information
captFuture committed Apr 8, 2022
0 parents commit 13914ed
Show file tree
Hide file tree
Showing 1,100 changed files with 349,269 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.ino_": "cpp",
"functional": "cpp"
}
}
39 changes: 39 additions & 0 deletions include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
21 changes: 21 additions & 0 deletions lib/esp32-wii-nunchuk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Ricardo Massaro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
133 changes: 133 additions & 0 deletions lib/esp32-wii-nunchuk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# esp32-wii-nunchuk

This is a library to use the Wii Nunchuk with the ESP32 via I2C. It
can be used both with the Arduino IDE or with code using the ESP-IDF
directly. To use the library in your Arduino IDE sketch, just copy
the files `wii_i2c.c` and `wii_i2c.h` to your sketch directory.

This library uses the [ESP-IDF I2C
API](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/i2c.html)
because the [Arduino Wire
library](https://www.arduino.cc/en/reference/wire) doesn't work
reliably in the ESP32 with Wii controllers.

![ESP32 connected to a Wii Nunchuk](images/photo.jpg)

This library supports the **Wii Nunchuk** and the **Wii Classic
Controller**. It shouldn't be hard to adapt it to work with other I2C
devices that plug in the Wiimote (like the Classic Controller Pro, Wii
Motion Plus, etc.) with the information available in the [Wiibrew
project](http://wiibrew.org/wiki/Wiimote/Extension_Controllers), but I
have none of these devices so I don't know for sure.

### Coexisting with Arduino Wire Library

The Arduino Wire Library for the ESP32 (from `Wire.h`) uses I2C port 0
for the `Wire` object, and port 1 for the `Wire1` object. So don't
use `Wire` if using I2C port 0 with this library, and don't use
`Wire1` if using I2C port 1.

### Example Code

Here's a simple example using the Wii Nunchuk. For a more complete
example that detects and handles multiple controller types, see
`esp32-wii-nunchuk.ino`.

```C++
#include "wii_i2c.h"

// pins connected to the Nunchuk:
#define PIN_SDA 32
#define PIN_SCL 33

// ESP32 I2C port (0 or 1):
#define WII_I2C_PORT 0

void setup()
{
Serial.begin(115200);

if (wii_i2c_init(WII_I2C_PORT, PIN_SDA, PIN_SCL) != 0) {
Serial.printf("Error initializing nunchuk :(");
return;
}
wii_i2c_request_state();
}

void loop()
{
const unsigned char *data = wii_i2c_read_state();
wii_i2c_request_state();
if (! data) {
Serial.printf("no data available :(")
} else {
wii_i2c_nunchuk_state state;
wii_i2c_decode_nunchuk(data, &state);
Serial.printf("Stick position: (%d,%d)\n", state.x, state.y);
Serial.printf("C button is %s\n", (state.c) ? "pressed" : "not pressed");
Serial.printf("Z button is %s\n", (state.z) ? "pressed" : "not pressed");
}
delay(1000);
}
```

### Multi-Core Support

If you have time-sensitive code that can't wait for the controller to
respond, use the API function that spawns a task that reads the
controller state in a different core. For example:

```C++
#include "wii_i2c.h"

// pins connected to the controller:
#define PIN_SDA 32
#define PIN_SCL 33

// ESP32 I2C port (0 or 1):
#define WII_I2C_PORT 0

// CPU id where the task will run (1=the core
// where your code usually runs, 0=the other core):
#define READ_TASK_CPU 0

// delay in milliseconds between controller reads:
#define READ_DELAY 30

static unsigned int controller_type;

void setup()
{
Serial.begin(115200);

if (wii_i2c_init(WII_I2C_PORT, PIN_SDA, PIN_SCL) != 0) {
Serial.printf("Error initializing nunchuk :(");
return;
}

// if you want to read the controller identity,
// do it BEFORE starting the read task:
const unsigned char *ident = wii_i2c_read_ident();
controller_type = wii_i2c_decode_ident(ident);

// start the a task that reads the controller state in a different CPU:
if (wii_i2c_start_read_task(READ_TASK_CPU, READ_DELAY) != 0) {
Serial.printf("Error creating task to read controller state");
return;
}
}

void loop()
{
// this function always returns quickly, either
// with new data or NULL if data isn't ready:
const unsigned char *data = wii_i2c_read_data_from_task();
if (data) {
// decode data according to controller_type:
// wii_i2c_decode_nunchuk(data, &nunchuk_state);
// wii_i2c_decode_classic(data, &classic_state);
}

// do other timing-sensitive stuff
}
```
76 changes: 76 additions & 0 deletions lib/esp32-wii-nunchuk/esp32-wii-nunchuk.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "wii_i2c.h"

#define PIN_SDA 32
#define PIN_SCL 33
#define WII_I2C_PORT 0

unsigned int controller_type = 0;

void show_nunchuk(const unsigned char *data)
{
wii_i2c_nunchuk_state state;
wii_i2c_decode_nunchuk(data, &state);

Serial.printf("a = (%5d,%5d,%5d)\n", state.acc_x, state.acc_y, state.acc_z);
Serial.printf("d = (%5d,%5d)\n", state.x, state.y);
Serial.printf("c=%d, z=%d\n", state.c, state.z);
}

void show_classic(const unsigned char *data)
{
wii_i2c_classic_state state;
wii_i2c_decode_classic(data, &state);

Serial.printf("lx,ly = (%3d,%3d)\n", state.lx, state.ly);
Serial.printf("rx,ry = (%3d,%3d)\n", state.rx, state.ry);
Serial.printf("a lt,rt = (%3d,%3d)\n", state.a_lt, state.a_rt);
Serial.printf("d lt,rt = (%d,%d)\n", state.d_lt, state.d_rt);
Serial.printf("a,b,x,y = (%d,%d,%d,%d)\n", state.a, state.b, state.x, state.y);
Serial.printf("up, down, left, right = (%d,%d,%d,%d)\n", state.up, state.down, state.left, state.right);
Serial.printf("home, plus, minus = (%d,%d,%d)\n", state.home, state.plus, state.minus);
Serial.printf("zl, zr = (%d,%d)\n", state.zl, state.zr);
}

void setup()
{
Serial.begin(115200);
Serial.printf("Starting...\n");

if (wii_i2c_init(WII_I2C_PORT, PIN_SDA, PIN_SCL) != 0) {
Serial.printf("ERROR initializing wii i2c controller\n");
return;
}
const unsigned char *ident = wii_i2c_read_ident();
if (! ident) {
Serial.printf("no ident :(\n");
return;
}

controller_type = wii_i2c_decode_ident(ident);
switch (controller_type) {
case WII_I2C_IDENT_NUNCHUK: Serial.printf("-> nunchuk detected\n"); break;
case WII_I2C_IDENT_CLASSIC: Serial.printf("-> classic controller detected\n"); break;
default: Serial.printf("-> unknown controller detected: 0x%06x\n", controller_type); break;
}
wii_i2c_request_state();
}

void loop()
{
const unsigned char *data = wii_i2c_read_state();
wii_i2c_request_state();
if (data) {
switch (controller_type) {
case WII_I2C_IDENT_NUNCHUK: show_nunchuk(data); break;
case WII_I2C_IDENT_CLASSIC: show_classic(data); break;
default:
Serial.printf("data: %02x %02x %02x %02x %02x %02x\n",
data[0], data[1], data[2], data[3], data[4], data[5]);
break;
}
} else {
Serial.printf("no data :(\n");
}

delay(1000);
}
Binary file added lib/esp32-wii-nunchuk/images/photo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 13914ed

Please sign in to comment.