Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
Make LED config options work
Browse files Browse the repository at this point in the history
  • Loading branch information
FeralAI committed Sep 15, 2021
1 parent bd1cddb commit 80cc204
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 84 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ jobs:
mv .pio/build/osfrd/APPLICATION.uf2 .pio/build/osfrd/GP2040-OSFRD.uf2
pio run -e raspberry-pi-pico
mv .pio/build/raspberry-pi-pico/APPLICATION.uf2 .pio/build/raspberry-pi-pico/GP2040-PiPico.uf2
pio run -e pimironi-pico-lipo
mv .pio/build/pimironi-pico-lipo/APPLICATION.uf2 .pio/build/pimironi-pico-lipo/GP2040-PimoroniPicoLipo.uf2
pio run -e pimoroni-pico-lipo
mv .pio/build/pimoroni-pico-lipo/APPLICATION.uf2 .pio/build/pimoroni-pico-lipo/GP2040-PimoroniPicoLipo.uf2
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
.pio/build/osfrd/GP2040-OSFRD.uf2
.pio/build/raspberry-pi-pico/GP2040-PiPico.uf2
.pio/build/pimironi-pico-lipo/GP2040-PimoroniPicoLipo.uf2
.pio/build/pimoroni-pico-lipo/GP2040-PimoroniPicoLipo.uf2
84 changes: 52 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ Input latency is tested using the methodology outlined at [WydD's inputlag.scien
| - | - | - | - | - | - | - | - | - |
| All | 1 ms | 0.56 ms | 1.58 ms | 0.89 ms | 0.25 ms | 95.71% | 4.29% | 0% |

## Button Reference

GP2040 uses a generic button labeling for gamepad state, which is then converted to the appropriate input type before sending. Here are the mappings of generic buttons to each supported platform/layout:

| GP2040 | XInput | Switch | PS3 | DirectInput | Arcade |
| ------- | ------ | ------- | ------------ | ------------ | ------ |
| B1 | A | B | Cross | 2 | K1 |
| B2 | B | A | Circle | 3 | K2 |
| B3 | X | Y | Square | 1 | P1 |
| B4 | Y | X | Triangle | 4 | P2 |
| L1 | LB | L | L1 | 5 | P4 |
| R1 | RB | R | R1 | 6 | P3 |
| L2 | LT | ZL | L2 | 7 | K4 |
| R2 | RT | ZR | R2 | 8 | K3 |
| S1 | Back | Minus | Select | 9 | Coin |
| S2 | Start | Plus | Start | 10 | Start |
| L3 | LS | LS | L3 | 11 | LS |
| R3 | RS | RS | R3 | 12 | RS |
| A1 | Guide | Home | - | 13 | - |
| A2 | - | Capture | - | 14 | - |

## Development

The project is built using the PlatformIO VS Code plugin along with the [Wiz-IO Raspberry Pi Pico](https://github.com/Wiz-IO/wizio-pico) platform package, using the baremetal (Pico SDK) configuration. There is an external dependency on the [MPG](https://github.com/FeralAI/MPG) C++ gamepad library for handling input state, providing extra features like Left/Right stick emulation and SOCD cleaning, and converting the generic gamepad state to the appropriate USB report.
Expand All @@ -31,60 +52,59 @@ There are two simple options for building GP2040 for your board. You can either

#### Existing Board

Once you have the project loaded into PlatformIO, edit the `include/definitions/RP2040Board.h` file to map your GPIO pins. Then from the VS Code status bar, use the PlatformIO environment selector to choose `env:raspberry-pi-pico`.
Once you have the project loaded into PlatformIO, edit the `config/Pico/BoardConfig.h` file to map your GPIO pins. Then from the VS Code status bar, use the PlatformIO environment selector to choose `env:raspberry-pi-pico`. The stock pin definitions for a pin-compatible Pico board are:

| Pin Define | GPIO Pin |
| ---------- | -------- |
| PIN_DPAD_UP | 2 |
| PIN_DPAD_DOWN | 3 |
| PIN_DPAD_LEFT | 4 |
| PIN_DPAD_RIGHT | 5 |
| PIN_BUTTON_B1 | 6 |
| PIN_BUTTON_B2 | 7 |
| PIN_BUTTON_B3 | 8 |
| PIN_BUTTON_B4 | 9 |
| PIN_BUTTON_L1 | 10 |
| PIN_BUTTON_R1 | 11 |
| PIN_BUTTON_L2 | 26 |
| PIN_BUTTON_R2 | 27 |
| PIN_BUTTON_S1 | 16 |
| PIN_BUTTON_S2 | 17 |
| PIN_BUTTON_L3 | 18 |
| PIN_BUTTON_R3 | 19 |
| PIN_BUTTON_A1 | 20 |
| PIN_BUTTON_A2 | 21 |

#### Create New Board

You can also add a new board definition to `include/definitions`. If you do, perform the following:
You can also add a new board definition to the `config`. If you do, perform the following:

* Create new board definition file in `include/definitions` with your pin configuration and options.
* Add `#define` for your new board in `include/BoardConfig.h`.
* Add option to `src/RP2040Gamepad.cpp` in the `BOARD_DEFINITION` selection logic.
* Create new board definition file in `config/<BoardNameHere>/BoardConfig.h` with your pin configuration and options.
* Add a new environment to the `platformio.ini`
* Copy from existing environment and rename
* Replace `BOARD_DEFINITION=#` with the number in the `BoardConfig.h` file.
* Update the `build_flags` parameter for the config folder, should look like `-I configs/Pico/` or similar.
* If you're not using a Pico or bare RP2040, check the `include/pico/config_autogen.h` file to see if there is a define for your board. If so, add or update the `-D BOARD_...` option in `build_flags`. The Pimoroni board config is an example of usage.

You will now have a new build environment target for PlatformIO. Use the VS Code status bar to select your new environment target.

### LED Configuration

If your board has WS2812 (or similar) LEDs, these can be configured in your board definition by setting the following:
If your board has WS2812 (or similar) LEDs, these can be configured in your board definition by setting the following in your `BoardConfig.h` file:

| Name | Description | Optional? |
| Name | Description | Required? |
| ---------------- | ---------------------------- | --------- |
| BOARD_LEDS_PIN | Data PIN for your LED strand | No |
| BOARD_LEDS_COUNT | Total LEDs in your strand | No |
| LEDS_BASE_ANIMATION | This can be either "RAINBOW", "CHASE" or "STATIC" to set your base animation | Yes |
| BOARD_LEDS_PIN | Data PIN for your LED strand | Yes |
| LEDS_RAINBOW_CYCLE_TIME | For "RAINBOW," this sets how long (in ms) it takes to cycle from one color step to the next | Yes |
| LEDS_CHASE_CYCLE_TIME | For "CHASE," this sets how long (in ms) it takes to move from one pixel to the next | Yes |
| LEDS_STATIC_COLOR_COLOR | For "STATIC", this sets the static color. This is a uint32_t value. This should be friendlier, I know. | Yes |
| LEDS_STATIC_COLOR_COLOR | For "STATIC", this sets the static color. This is an `RGB` struct which can be found in `AnimationStation/src/Animation.hpp`. Can be custom or one of these predefined values: `ColorBlack`, `ColorWhite`, `ColorRed`, `ColorOrange`, `ColorYellow`, `ColorLimeGreen`, `ColorGreen`, `ColorSeafoam`, `ColorAqua`, `ColorSkyBlue`, `ColorBlue`, `ColorPurple`, `ColorPink`, `ColorMagenta` | Yes |

### Building the Project

You should now be able to build or upload the project to you RP2040 board from the Build and Upload status bar icons. You can also open the PlatformIO tab and select the actions to execute for a particular environment. Output folders are defined in the `platformio.ini` file, but they should all default to a path under `.pio/build/${env:NAME}`.

## Usage

GP2040 uses a generic button labeling for gamepad state, which is then converted to the appropriate input type before sending. Here are the mappings of generic buttons to each supported platform/layout:

| GP2040 | XInput | Switch | PS3 | DirectInput | Arcade |
| ------- | ------ | ------- | ------------ | ------------ | ------ |
| B1 | A | B | Cross | 2 | K1 |
| B2 | B | A | Circle | 3 | K2 |
| B3 | X | Y | Square | 1 | P1 |
| B4 | Y | X | Triangle | 4 | P2 |
| L1 | LB | L | L1 | 5 | P4 |
| R1 | RB | R | R1 | 6 | P3 |
| L2 | LT | ZL | L2 | 7 | K4 |
| R2 | RT | ZR | R2 | 8 | K3 |
| S1 | Back | Minus | Select | 9 | Coin |
| S2 | Start | Plus | Start | 10 | Start |
| L3 | LS | LS | L3 | 11 | LS |
| R3 | RS | RS | R3 | 12 | RS |
| A1 | Guide | Home | - | 13 | - |
| A2 | - | Capture | - | 14 | - |

Any button references in this documentation will use the `XInput` labels for clarity.
> NOTE: Any button references in this documentation will use the `XInput` labels for clarity.
### Home Button

Expand Down
3 changes: 0 additions & 3 deletions configs/DebugBoard/BoardConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
#define DEFAULT_SOCD_MODE SOCD_MODE_NEUTRAL

#define BOARD_LEDS_PIN 22
#define BOARD_LEDS_COUNT 16

#define LEDS_BASE_ANIMATION CHASE

const static std::vector<Pixel> pixels =
{
Expand Down
1 change: 0 additions & 1 deletion configs/OSFRD/BoardConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#define PIN_BUTTON_A2 18

#define BOARD_LEDS_PIN 14
#define BOARD_LEDS_COUNT 16

#define LEDS_DPAD_UP 3
#define LEDS_DPAD_DOWN 1
Expand Down
12 changes: 0 additions & 12 deletions include/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@
#define LEDS_BRIGHTNESS 75
#endif

#ifndef LEDS_BASE_ANIMATION
#define LEDS_BASE_ANIMATION RAINBOW
#endif

#ifndef LEDS_BASE_ANIMATION_FIRST_PIXEL
#define LEDS_BASE_ANIMATION_FIRST_PIXEL 0
#endif

#ifndef LEDS_BASE_ANIMATION_LAST_PIXEL
#define LEDS_BASE_ANIMATION_LAST_PIXEL 11
#endif

#ifndef LEDS_RAINBOW_CYCLE_TIME
#define LEDS_RAINBOW_CYCLE_TIME 40
#endif
Expand Down
7 changes: 0 additions & 7 deletions lib/AnimationStation/src/AnimationStation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ StaticColor *staticColor;

AnimationStation::AnimationStation(std::vector<Pixel> pixels) : pixels(pixels) {
AnimationStation::SetBrightness(1);

staticColor = new StaticColor(pixels, ColorWhite);

animations.push_back(new StaticColor(pixels, ColorBlack));
animations.push_back(staticColor);
animations.push_back(new Rainbow(pixels));
animations.push_back(new Chase(pixels));
}

void AnimationStation::AddAnimation(Animation *animation) {
Expand Down
4 changes: 1 addition & 3 deletions lib/AnimationStation/src/Effects/Chase.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "Chase.hpp"

int Chase::defaultCycleTime = 85;
Chase::Chase(std::vector<Pixel> pixels, uint16_t cycleTime) : Animation(pixels), cycleTime(cycleTime) {

Chase::Chase(std::vector<Pixel> pixels) : Animation(pixels) {
this->cycleTime = Chase::defaultCycleTime;
}

void Chase::Animate(RGB (&frame)[100]) {
Expand Down
8 changes: 2 additions & 6 deletions lib/AnimationStation/src/Effects/Chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@

class Chase : public Animation {
public:
Chase(std::vector<Pixel> pixels);
Chase(std::vector<Pixel> pixels, uint16_t cycleTime = 85);

void Animate(RGB (&frame)[100]);

static void SetDefaultCycleTime(int cycleTime) {
Chase::defaultCycleTime = cycleTime;
}
protected:
static int defaultCycleTime;
bool IsChasePixel(int i);
int WheelFrame(int i);

int cycleTime;
int16_t cycleTime;
int currentFrame = 0;
int currentPixel = 0;
bool reverse = false;
Expand Down
4 changes: 1 addition & 3 deletions lib/AnimationStation/src/Effects/Rainbow.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "Rainbow.hpp"

int Rainbow::defaultCycleTime = 40;
Rainbow::Rainbow(std::vector<Pixel> pixels, uint16_t cycleTime) : Animation(pixels), cycleTime(cycleTime) {

Rainbow::Rainbow(std::vector<Pixel> pixels) : Animation(pixels) {
this->cycleTime = Rainbow::defaultCycleTime;
}

void Rainbow::Animate(RGB (&frame)[100]) {
Expand Down
9 changes: 2 additions & 7 deletions lib/AnimationStation/src/Effects/Rainbow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@

class Rainbow : public Animation {
public:
Rainbow(std::vector<Pixel> pixels);
Rainbow(std::vector<Pixel> pixels, uint16_t cycleTime = 40);

void Animate(RGB (&frame)[100]);

static void SetDefaultCycleTime(int cycleTime) {
Rainbow::defaultCycleTime = cycleTime;
}
protected:
static int defaultCycleTime;
int cycleTime;
uint16_t cycleTime;
int currentFrame = 0;
bool reverse = false;
absolute_time_t nextRunTime = 0;
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ build_flags =
${env.build_flags}
-I configs/Pico/

[env:pimironi-pico-lipo]
[env:pimoroni-pico-lipo]
build_flags =
${env.build_flags}
-D BOARD_PIMORONI_PICO_LIPO
-I configs/Pico/
upload_port = .pio/build/pimironi-pico-lipo/
upload_port = .pio/build/pimoroni-pico-lipo/

[env:osfrd]
build_flags =
Expand Down
6 changes: 5 additions & 1 deletion src/leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

void configureAnimations(AnimationStation *as)
{
as->SetStaticColor(LEDS_STATIC_COLOR_COLOR);
as->AddAnimation(new StaticColor(pixels, ColorBlack));
as->AddAnimation(new StaticColor(pixels, LEDS_STATIC_COLOR_COLOR));
as->AddAnimation(new Rainbow(pixels, LEDS_RAINBOW_CYCLE_TIME));
as->AddAnimation(new Chase(pixels, LEDS_CHASE_CYCLE_TIME));

for (size_t i = 0; i < customThemes.size(); i++)
as->AddAnimation(&customThemes[i]);
}
Expand Down
8 changes: 4 additions & 4 deletions src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ void GamepadStorage::set(int index, void *data, uint16_t size)
#include "AnimationStorage.hpp"
#include "AnimationStation/src/Effects/StaticColor.hpp"

#define STORAGE_LEDS_BRIGHTNESS_INDEX (STORAGE_FIRST_AVAILBLE_INDEX) // 1 byte
#define STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX (STORAGE_FIRST_AVAILBLE_INDEX + 1) // 1 byte
#define STORAGE_LEDS_BRIGHTNESS_INDEX (STORAGE_FIRST_AVAILBLE_INDEX) // 1 byte
#define STORAGE_LEDS_ANIMATION_MODE_INDEX (STORAGE_FIRST_AVAILBLE_INDEX + 1) // 1 byte

uint8_t AnimationStorage::getMode()
{
uint8_t mode = 0;
getStorageValue(STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX, &mode, sizeof(uint8_t));
getStorageValue(STORAGE_LEDS_ANIMATION_MODE_INDEX, &mode, sizeof(uint8_t));
return mode;
}

void AnimationStorage::setMode(uint8_t mode)
{
setStorageValue(STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX, &mode, sizeof(uint8_t));
setStorageValue(STORAGE_LEDS_ANIMATION_MODE_INDEX, &mode, sizeof(uint8_t));
}

uint8_t AnimationStorage::getBrightness()
Expand Down

0 comments on commit 80cc204

Please sign in to comment.