updated to large led

This commit is contained in:
nimeissn 2022-02-09 19:29:40 +01:00
parent a8a7222e03
commit a88eab9ee5
4 changed files with 117 additions and 14 deletions

View file

@ -41,7 +41,7 @@ uint8_t clockPin = 3; // Green wire on Adafruit Pixels
// Set the first variable to the NUMBER of pixels in a row and // Set the first variable to the NUMBER of pixels in a row and
// the second value to number of pixels in a column. // the second value to number of pixels in a column.
Adafruit_WS2801 strip = Adafruit_WS2801((uint16_t)200, dataPin, clockPin); Adafruit_WS2801 strip = Adafruit_WS2801((uint16_t)10, (uint16_t)20,dataPin, clockPin);
/* Helper functions */ /* Helper functions */
@ -141,7 +141,18 @@ void loop() {
// Some example procedures showing how to display to the pixels // Some example procedures showing how to display to the pixels
//drawX(10, 20, 100); //drawX(10, 20, 100);
// bounce(10, 20, 50); // bounce(10, 20, 50);
for(uint8_t i; i<200; i++)
for( int i = 0; i<200; i++)
{
strip.setPixelColor(i,250,0,0);
strip.show();
}
strip.show();
/*for(uint8_t i; i<200; i++)
{ {
strip.setPixelColor(i,255,0,0); strip.setPixelColor(i,255,0,0);
@ -162,6 +173,6 @@ void loop() {
strip.setPixelColor(i,0,0,255); strip.setPixelColor(i,0,0,255);
} }
strip.show(); strip.show();
delay(5000); delay(5000);*/
} }

View file

@ -10,6 +10,8 @@
[env:uno] [env:uno]
platform = atmelavr platform = atmelavr
board = uno board = nanoatmega328
framework = arduino framework = arduino
lib_deps = adafruit/Adafruit NeoPixel@^1.10.4 lib_deps =
adafruit/Adafruit NeoPixel@^1.10.4
adafruit/Adafruit WS2801 Library@^1.1.1

33
VSCode/Pong/src/Pong.cpp Normal file
View file

@ -0,0 +1,33 @@
#include <Arduino.h>
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
/*!
@brief Convert separate red, green, blue and white values into a
single "packed" 32-bit WRGB color.
@param r Red brightness, 0 to 255.
@param g Green brightness, 0 to 255.
@param b Blue brightness, 0 to 255.
@param w White brightness, 0 to 255.
@return 32-bit packed WRGB value, which can then be assigned to a
variable for later use or passed to the setPixelColor()
function. Packed WRGB format is predictable, regardless of
LED strand color order.
*/
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
static uint32_t ColorHSV(uint16_t hue, uint8_t sat = 255, uint8_t val = 255);
/*!
@brief A gamma-correction function for 32-bit packed RGB or WRGB
colors. Makes color transitions appear more perceptially
correct.
@param x 32-bit packed RGB or WRGB color.
@return Gamma-adjusted packed color, can then be passed in one of the
setPixelColor() functions. Like gamma8(), this uses a fixed
gamma correction exponent of 2.6, which seems reasonably okay
for average NeoPixels in average tasks. If you need finer
control you'll need to provide your own gamma-correction
function instead.
*/

View file

@ -1,4 +1,5 @@
#include <Arduino.h> #include <Arduino.h>
#include <Adafruit_WS2801.h>
/** /**
* Player 1: x = 0 * Player 1: x = 0
@ -47,15 +48,17 @@
#define GAME_STATE_RUNNING 1 #define GAME_STATE_RUNNING 1
#define GAME_STATE_END 2 #define GAME_STATE_END 2
#define GAME_STATE_INIT 3 #define GAME_STATE_INIT 3
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
uint8_t dataPin = 6;
uint8_t clockPin = 3;
struct Coords { struct Coords {
byte x; byte x;
byte y; byte y;
}; };
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(X_MAX * Y_MAX, PIN_LED_MATRIX, NEO_GRB + NEO_KHZ800); Adafruit_WS2801 pixels = Adafruit_WS2801((uint16_t)10 ,(uint16_t)20, dataPin, clockPin );
bool buttonPressed = false; bool buttonPressed = false;
byte gameState; byte gameState;
byte joystickPins[PLAYER_AMOUNT] = {PIN_JOYSTICK_1_Y, PIN_JOYSTICK_2_Y}; byte joystickPins[PLAYER_AMOUNT] = {PIN_JOYSTICK_1_Y, PIN_JOYSTICK_2_Y};
@ -67,6 +70,51 @@ unsigned long lastDrawUpdate = 0;
unsigned long lastBallUpdate = 0; unsigned long lastBallUpdate = 0;
unsigned long lastButtonClick = 0; unsigned long lastButtonClick = 0;
void resetLEDs();
void initGame();
void updateBall();
void updateGame();
boolean isButtonPressed();
void playTone(byte type);
void toggleLed(byte x, byte y, byte type);
byte getPlayerMovement(byte playerId);
void endGame();
#include <Arduino.h>
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
/*!
@brief Convert separate red, green, blue and white values into a
single "packed" 32-bit WRGB color.
@param r Red brightness, 0 to 255.
@param g Green brightness, 0 to 255.
@param b Blue brightness, 0 to 255.
@param w White brightness, 0 to 255.
@return 32-bit packed WRGB value, which can then be assigned to a
variable for later use or passed to the setPixelColor()
function. Packed WRGB format is predictable, regardless of
LED strand color order.
*/
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
static uint32_t ColorHSV(uint16_t hue, uint8_t sat = 255, uint8_t val = 255);
/*!
@brief A gamma-correction function for 32-bit packed RGB or WRGB
colors. Makes color transitions appear more perceptially
correct.
@param x 32-bit packed RGB or WRGB color.
@return Gamma-adjusted packed color, can then be passed in one of the
setPixelColor() functions. Like gamma8(), this uses a fixed
gamma correction exponent of 2.6, which seems reasonably okay
for average NeoPixels in average tasks. If you need finer
control you'll need to provide your own gamma-correction
function instead.
*/
void setup() void setup()
{ {
pinMode(PIN_JOYSTICK_1_Y, INPUT); pinMode(PIN_JOYSTICK_1_Y, INPUT);
@ -80,6 +128,7 @@ void setup()
void loop() void loop()
{ {
switch(gameState) { switch(gameState) {
case GAME_STATE_INIT: case GAME_STATE_INIT:
initGame(); initGame();
@ -255,28 +304,36 @@ bool isButtonPressed()
void resetLEDs() void resetLEDs()
{ {
for(byte i=0; i<X_MAX*Y_MAX; i++) { for(byte i=0; i<X_MAX*Y_MAX; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); pixels.setPixelColor(i, 0, 0, 0);
} }
pixels.show(); pixels.show();
} }
void toggleLed(byte x, byte y, byte type) void toggleLed(byte x, byte y, byte type)
{ {
byte ledIndex = y * X_MAX + x; byte ledIndex ;
if(y%2 == 0)
{
ledIndex = y * X_MAX + x;
}
else
{
ledIndex = y * X_MAX + (X_MAX -x -1);
}
uint32_t color; uint32_t color;
switch(type) { switch(type) {
case LED_TYPE_PADDLE: case LED_TYPE_PADDLE:
color = pixels.Color(0, 8, 8); color = Color(0, 8, 8);
break; break;
case LED_TYPE_BALL_RED: case LED_TYPE_BALL_RED:
color = pixels.Color(12, 0, 0); color = Color(12, 0, 0);
break; break;
case LED_TYPE_BALL: case LED_TYPE_BALL:
color = pixels.Color(0, 10, 0); color = Color(0, 10, 0);
break; break;
case LED_TYPE_OFF: case LED_TYPE_OFF:
color = pixels.Color(0, 0, 0); color = Color(0, 0, 0);
break; break;
} }