Add the WS2812 code
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#include "Freenove_WS2812_Lib_for_ESP32.h"
|
||||
|
||||
/*********************************************************************************/
|
||||
// Constants
|
||||
|
||||
@@ -29,7 +31,7 @@
|
||||
//#define PIN_JOY_Z 12
|
||||
|
||||
#define BUTTON_STABLETIME 100
|
||||
#define JOYSTICK_STABLETIME 1000
|
||||
#define JOYSTICK_STABLETIME 3000
|
||||
|
||||
// Button states
|
||||
#define BUTTON_STATE_BOUNCING (1 << 0)
|
||||
@@ -44,14 +46,14 @@
|
||||
#define JOYSTICK_STATE_READY (1 << 3)
|
||||
|
||||
// These aren't pins, they're just defines that help us identify the lights on the 2812 unit
|
||||
#define LED_2812_LEFT 1 // 2812 LEDs are ordered from connection point at left
|
||||
#define LED_2812_TOPLEFT 2 // and go clockwise
|
||||
#define LED_2812_TOP 3
|
||||
#define LED_2812_TOPRIGHT 4
|
||||
#define LED_2812_RIGHT 5
|
||||
#define LED_2812_BOTTOMRIGHT 6
|
||||
#define LED_2812_BOTTOM 7
|
||||
#define LED_2812_BOTTOMLEFT 8
|
||||
#define LED_2812_LEFT 0 // 2812 LEDs are ordered from connection point at left
|
||||
#define LED_2812_TOPLEFT 1 // and go clockwise
|
||||
#define LED_2812_TOP 2
|
||||
#define LED_2812_TOPRIGHT 3
|
||||
#define LED_2812_RIGHT 4
|
||||
#define LED_2812_BOTTOMRIGHT 5
|
||||
#define LED_2812_BOTTOM 6
|
||||
#define LED_2812_BOTTOMLEFT 7
|
||||
|
||||
// Error flags
|
||||
|
||||
@@ -97,7 +99,7 @@ typedef struct JoystickAxis {
|
||||
uint8_t pin;
|
||||
// Calibration data
|
||||
Calibration calibration;
|
||||
// Position as of the last read time
|
||||
// Position as of the last read time.
|
||||
uint32_t position;
|
||||
} JoystickAxis;
|
||||
|
||||
@@ -118,6 +120,7 @@ typedef struct Joystick {
|
||||
int errno;
|
||||
Button calibration_button;
|
||||
Joystick js;
|
||||
Freenove_ESP32_WS2812 *strip;
|
||||
|
||||
/******************************************************************************/
|
||||
// Initialization Functions
|
||||
@@ -126,8 +129,15 @@ void initSerial()
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
int init2812()
|
||||
int init2812(Freenove_ESP32_WS2812 **strip)
|
||||
{
|
||||
Freenove_ESP32_WS2812 *obj = new Freenove_ESP32_WS2812(8, PIN_2812, 0, TYPE_GRB);
|
||||
if ( obj == NULL ) {
|
||||
ERROR(ERRNO_NULLPOINTER);
|
||||
}
|
||||
obj->setBrightness(10);
|
||||
obj->begin();
|
||||
*strip = obj;
|
||||
return ERRNO_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -273,10 +283,16 @@ int readJoystick(Joystick *js)
|
||||
adcvalue = analogRead(js->x.pin);
|
||||
if ( adcvalue < js->x.calibration.low || adcvalue > js->x.calibration.high ) {
|
||||
js->x.position = adcvalue;
|
||||
} else {
|
||||
// Snap to center
|
||||
js->x.position = 2048;
|
||||
}
|
||||
adcvalue = analogRead(js->y.pin);
|
||||
if ( adcvalue < js->y.calibration.low || adcvalue > js->y.calibration.high ) {
|
||||
js->y.position = adcvalue;
|
||||
} else {
|
||||
// Snap to center
|
||||
js->y.position = 2048;
|
||||
}
|
||||
//Serial.printf("Joystick x %d y %d\n", js->x.position, js->y.position);
|
||||
|
||||
@@ -317,15 +333,43 @@ int displayLEDs(Joystick *js)
|
||||
return ERRNO_SUCCESS;
|
||||
}
|
||||
|
||||
int display2812(Joystick *js)
|
||||
int display2812(Joystick *js, Freenove_ESP32_WS2812 *strip)
|
||||
{
|
||||
if ( js == NULL ) {
|
||||
ERROR(ERRNO_NULLPOINTER);
|
||||
}
|
||||
strip->setLedColorData(LED_2812_LEFT, 0, 0, 0);
|
||||
strip->setLedColorData(LED_2812_TOPLEFT, 0, 0, 0);
|
||||
strip->setLedColorData(LED_2812_TOP, 0, 0, 0);
|
||||
strip->setLedColorData(LED_2812_TOPRIGHT, 0, 0, 0);
|
||||
strip->setLedColorData(LED_2812_RIGHT, 0, 0, 0);
|
||||
strip->setLedColorData(LED_2812_BOTTOMRIGHT, 0, 0, 0);
|
||||
strip->setLedColorData(LED_2812_BOTTOM, 0, 0, 0);
|
||||
strip->setLedColorData(LED_2812_BOTTOMLEFT, 0, 0, 0);
|
||||
|
||||
// Is the joystick fully ready? Display direction on the 2812
|
||||
if ( ( js->state & JOYSTICK_STATE_READY ) == JOYSTICK_STATE_READY ) {
|
||||
// Light up the correct LED_2812_* depending on the value of the X/Y axes on the joystick
|
||||
if ( js->x.position < 1024 && (js->y.position < 3072 && js->y.position > 1024) ) {
|
||||
strip->setLedColorData(LED_2812_LEFT, 128, 128, 128);
|
||||
} else if ( js->x.position < 1024 && js->y.position < 1024 ) {
|
||||
strip->setLedColorData(LED_2812_TOPLEFT, 128, 128, 128);
|
||||
} else if ( ( js->x.position > 1024 && js->x.position < 3072 ) && js->y.position < 1024 ) {
|
||||
strip->setLedColorData(LED_2812_TOP, 128, 128, 128);
|
||||
} else if ( js->x.position > 3072 && js->y.position < 1024 ) {
|
||||
strip->setLedColorData(LED_2812_TOPRIGHT, 128, 128, 128);
|
||||
} else if ( js->x.position > 3072 && ( js->y.position < 3072 && js->y.position > 1024) ) {
|
||||
strip->setLedColorData(LED_2812_RIGHT, 128, 128, 128);
|
||||
} else if ( js->x.position > 3072 && js->y.position > 3072 ) {
|
||||
strip->setLedColorData(LED_2812_BOTTOMRIGHT, 128, 128, 128);
|
||||
} else if ( ( js->x.position > 1024 && js->x.position < 3072 ) && js->y.position > 3072 ) {
|
||||
strip->setLedColorData(LED_2812_BOTTOM, 128, 128, 128);
|
||||
} else if ( js->x.position < 1024 && js->y.position > 3072 ) {
|
||||
strip->setLedColorData(LED_2812_BOTTOMLEFT, 128, 128, 128);
|
||||
}
|
||||
}
|
||||
strip->show();
|
||||
|
||||
return ERRNO_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -342,7 +386,7 @@ void setup() {
|
||||
calibration_button.pressedvalue = LOW;
|
||||
|
||||
initSerial();
|
||||
if ( init2812() != ERRNO_SUCCESS ) Serial.printf("Failed to initialize 2812 LED array : %d\n", errno);
|
||||
if ( init2812(&strip) != ERRNO_SUCCESS ) Serial.printf("Failed to initialize 2812 LED array : %d\n", errno);
|
||||
if ( initLEDs() != ERRNO_SUCCESS ) Serial.printf("Failed to initialize LED GPIO pins : %d\n", errno);
|
||||
if ( initJoystick(&js) != ERRNO_SUCCESS ) Serial.printf("Failed to initialized Joystick datastructure : %d\n", errno);
|
||||
if ( initButton(&calibration_button) != ERRNO_SUCCESS ) Serial.printf("Failed to initialize Calibration button and GPIO pin : %d\n", errno);
|
||||
@@ -369,7 +413,7 @@ void loop() {
|
||||
if ( displayLEDs(&js) != ERRNO_SUCCESS ) {
|
||||
Serial.printf("Failed to display LEDs : %d\n", errno);
|
||||
}
|
||||
if ( display2812(&js) != ERRNO_SUCCESS ) {
|
||||
if ( display2812(&js, strip) != ERRNO_SUCCESS ) {
|
||||
Serial.printf("Failed to display 2812 LEDs : %d\n", errno);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user