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

Commit

Permalink
Add RGB PLED support
Browse files Browse the repository at this point in the history
  • Loading branch information
FeralAI committed Nov 2, 2021
1 parent 7847e84 commit 3045102
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
5 changes: 5 additions & 0 deletions configs/OSFRD/BoardConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@
#define LEDS_BUTTON_R2 10
#define LEDS_BUTTON_L2 11

#define PLED_TYPE PLED_TYPE_RGB
#define PLED1_PIN 12
#define PLED2_PIN 13
#define PLED3_PIN 14
#define PLED4_PIN 15

#endif
12 changes: 12 additions & 0 deletions include/pleds.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "BoardConfig.h"
#include "pico/util/queue.h"
#include <stdint.h>
#include "AnimationStation.hpp"
#include "PlayerLEDs.h"
#include "NeoPico.hpp"
#include "gp2040.h"

#define PLED_REPORT_SIZE 32
Expand All @@ -29,13 +31,23 @@

#define PLED_MASK_ALL ((1U << PLED1_PIN) | (1U << PLED2_PIN) | (1U << PLED3_PIN) | (1U << PLED4_PIN))

extern NeoPico neopico;
extern AnimationStation as;

class PWMPlayerLEDs : public PlayerLEDs
{
public:
void setup();
void display();
};

class RGBPlayerLEDs : public PlayerLEDs
{
public:
void setup();
void display();
};

class PLEDModule : public GPModule
{
public:
Expand Down
4 changes: 4 additions & 0 deletions lib/NeoPico/src/NeoPico.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "hardware/clocks.h"
#include "NeoPico.hpp"

LEDFormat NeoPico::GetFormat() {
return format;
}

void NeoPico::PutPixel(uint32_t pixelData) {
switch (format) {
case LED_FORMAT_GRB:
Expand Down
1 change: 1 addition & 0 deletions lib/NeoPico/src/NeoPico.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class NeoPico
NeoPico(int ledPin, int numPixels, LEDFormat format = LED_FORMAT_GRB);
void Show();
void Clear();
LEDFormat GetFormat();
// void SetPixel(int pixel, uint32_t color);
void SetFrame(uint32_t newFrame[100]);
private:
Expand Down
4 changes: 2 additions & 2 deletions lib/PlayerLEDs/include/PlayerLEDs.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ struct PLEDAnimationState
class PlayerLEDs
{
public:
virtual void setup();
virtual void display();
virtual void setup() = 0;
virtual void display() = 0;
void animate(PLEDAnimationState animationState);

protected:
Expand Down
15 changes: 13 additions & 2 deletions src/leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "AnimationStorage.hpp"
#include "NeoPico.hpp"
#include "Pixel.hpp"
#include "PlayerLEDs.h"
#include "leds.h"
#include "themes.h"

Expand All @@ -22,10 +23,17 @@ PixelMatrix matrix = createLedButtonLayout(LED_LAYOUT, LEDS_PER_PIXEL);
PixelMatrix matrix = createLedButtonLayout(LED_LAYOUT, ledPositions);
#endif

#if PLED_TYPE == PLED_TYPE_RGB
extern void setRGBPLEDs(uint32_t *frame);
const uint8_t ledCount = matrix.getLedCount() + PLED_COUNT;
#else
const uint8_t ledCount = matrix.getLedCount();
#endif

#ifdef LED_FORMAT
NeoPico neopico(BOARD_LEDS_PIN, matrix.getLedCount(), LED_FORMAT);
NeoPico neopico(BOARD_LEDS_PIN, ledCount, LED_FORMAT);
#else
NeoPico neopico(BOARD_LEDS_PIN, matrix.getLedCount());
NeoPico neopico(BOARD_LEDS_PIN, ledCount);
#endif

AnimationStation as(matrix);
Expand Down Expand Up @@ -109,6 +117,9 @@ void LEDModule::loop()

as.Animate();
as.ApplyBrightness(frame);
#if PLED_TYPE == PLED_TYPE_RGB
setRGBPLEDs(frame);
#endif
neopico.SetFrame(frame);
neopico.Show();

Expand Down
35 changes: 34 additions & 1 deletion src/pleds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
#include <vector>
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "GamepadEnums.h"
#include "Animation.hpp"
#include "pleds.h"
#include "xinput_driver.h"

const uint8_t PLED_PINS[] = {PLED1_PIN, PLED2_PIN, PLED3_PIN, PLED4_PIN};
InputMode inputMode;
uint32_t rgbPLEDValues[4];

void setRGBPLEDs(uint32_t *frame)
{
for (int i = 0; i < PLED_COUNT; i++)
frame[PLED_PINS[i]] = rgbPLEDValues[i];
}

PLEDAnimationState getXInputAnimation(uint8_t *data)
{
Expand Down Expand Up @@ -101,6 +111,25 @@ void PWMPlayerLEDs::display()
pwm_set_gpio_level(PLED_PINS[i], ledLevels[i]);
}

void RGBPlayerLEDs::setup()
{

}

void RGBPlayerLEDs::display()
{
switch (inputMode)
{
case INPUT_MODE_XINPUT:
for (int i = 0; i < PLED_COUNT; i++) {
float level = (static_cast<float>(PLED_MAX_LEVEL - ledLevels[i]) / static_cast<float>(PLED_MAX_LEVEL));
float brightness = as.GetBrightnessX() * level;
rgbPLEDValues[i] = ((RGB)ColorGreen).value(neopico.GetFormat(), brightness);
}
break;
}
}

void PLEDModule::setup()
{
queue_init(&featureQueue, PLED_REPORT_SIZE, 20);
Expand All @@ -110,6 +139,9 @@ void PLEDModule::setup()
case PLED_TYPE_PWM:
pleds = new PWMPlayerLEDs();
break;
case PLED_TYPE_RGB:
pleds = new RGBPlayerLEDs();
break;
}

pleds->setup();
Expand All @@ -126,7 +158,8 @@ void PLEDModule::process(Gamepad *gamepad)

if (queue_try_remove(&featureQueue, featureData))
{
switch (gamepad->options.inputMode)
inputMode = gamepad->options.inputMode;
switch (inputMode)
{
case INPUT_MODE_XINPUT:
animationState = getXInputAnimation(featureData);
Expand Down

0 comments on commit 3045102

Please sign in to comment.