Refactoring how board control works

This commit is contained in:
2024-08-22 18:17:35 -04:00
parent a29ccbd2c8
commit 48f83eee38
7 changed files with 278 additions and 49 deletions

109
lib/Board/Board.h Normal file
View File

@@ -0,0 +1,109 @@
#pragma once
#include <cstdint>
#include <array>
#include "Cube.h"
#include "Color.h"
template <uint32_t X_SIZE, uint32_t Y_SIZE, uint32_t Z_SIZE>
class Board{
public:
Board();
~Board() = default;
constexpr uint32_t GetXSize() const{return X_SIZE;}
constexpr uint32_t GetYSize() const{return Y_SIZE;}
constexpr uint32_t GetZSize() const {return Z_SIZE;}
constexpr uint32_t GetNumberCubes() const{return X_SIZE * Y_SIZE * Z_SIZE;}
/**
* @brief Returns a string in the format:
* !a,b,c,d,e,f,g,h,i;
* Where each letter is the number of cubes in a given position's stack
* @param stringBuffer the buffer to write the string into
*/
void ToStackString(String& stringBuffer) const;
/**
* @brief Returns a bool array representing the board
*/
std::array<uint32_t, X_SIZE * Y_SIZE> &LinearizeBoard() const;
void Update();
void FillColor(const Color &color);
void SetCubeColor(uint32_t x_coord, uint32_t y_cord, uint32_t z_cord, const Color &color);
private:
// this is a 3d array of cubes to represent the board. Good luck visualizing it
/* _____________
/ /|
/ / |
/ / |
/____________/ |
| | |
| | /
| | /
|____________|/
*/
std::array<std::array<std::array<Cube, Z_SIZE>, Y_SIZE>, X_SIZE> cubes;
bool boardStateHasChanged;
};
template <uint32_t X_SIZE, uint32_t Y_SIZE, uint32_t Z_SIZE>
Board<X_SIZE, Y_SIZE, Z_SIZE>::Board(){
this->FillColor(Color(0,0,0));
}
template <uint32_t X_SIZE, uint32_t Y_SIZE, uint32_t Z_SIZE>
void Board<X_SIZE, Y_SIZE, Z_SIZE>::ToStackString(String &stringBuffer) const{
std::array<uint32_t, X_SIZE*Y_SIZE> linearizedBoard = this->LinearizeBoard();
stringBuffer += "!" + String(linearizedBoard[0]);
for(uint32_t i = 0; i < X_SIZE * Y_SIZE; i++){
stringBuffer += "," + String(linearizedBoard[i]);
}
stringBuffer += ";";
}
template <uint32_t X_SIZE, uint32_t Y_SIZE, uint32_t Z_SIZE>
std::array<uint32_t, X_SIZE * Y_SIZE> & Board<X_SIZE, Y_SIZE, Z_SIZE>::LinearizeBoard() const{
// convert the board into one array where each entry represents the height of one stack
std::array<uint32_t, X_SIZE*Y_SIZE> linearizedBoard;
for(uint32_t x{0}; x < X_SIZE; x++){
for(uint32_t y{0}; y < Y_SIZE; y++){
for(uint32_t z{0}; z < Z_SIZE; z++){
bool isOccupied{this->cubes[x][y][z].isOccupied};
linearizedBoard[x + y*3] += static_cast<uint32_t>(isOccupied);
}
}
}
return linearizedBoard;
}
template <uint32_t X_SIZE, uint32_t Y_SIZE, uint32_t Z_SIZE>
void Board<X_SIZE, Y_SIZE, Z_SIZE>::Update(){
}
template <uint32_t X_SIZE, uint32_t Y_SIZE, uint32_t Z_SIZE>
void Board<X_SIZE, Y_SIZE, Z_SIZE>::FillColor(const Color &color){
for(uint32_t x{0}; x < X_SIZE; x++){
for(uint32_t y{0}; y < Y_SIZE; y++){
for(uint32_t z{0}; z < Z_SIZE; z++){
this->cubes[x][y][z].SetColor(color);
}
}
}
}
template <uint32_t X_SIZE, uint32_t Y_SIZE, uint32_t Z_SIZE>
void Board<X_SIZE, Y_SIZE, Z_SIZE>::SetCubeColor(uint32_t x_coord, uint32_t y_cord, uint32_t z_cord, const Color &color){
this->cubes[x][y][z].SetColor(color);
}

View File

@@ -8,6 +8,7 @@
// store a color
struct Color{
public:
// create a constructor for this struct
Color(uint8_t red, uint8_t green, uint8_t blue) :
red(red),

View File

@@ -12,7 +12,7 @@ CubeStack::CubeStack(uint16_t ADCPin, uint16_t ledPin, uint8_t numLEDs){
}
};
uint8_t CubeStack::GetNumberCubes(){
uint32_t CubeStack::GetNumberCubes(Cube &cube){
// read the ADC and return the number of cubes
/*
0 cubes: 1 : 4095-3400
@@ -20,11 +20,8 @@ uint8_t CubeStack::GetNumberCubes(){
2 cubes: 1/3 2500-1850
3 cubes: 1/4 1850-0
*/
uint16_t value = analogRead(this->ADCPin);
this->lowPassADCRead = static_cast<uint16_t>((static_cast<float>(this->lowPassADCRead) * 0.9) + (static_cast<float>(value) * 0.1));
if(this->lowPassADCRead < 2500 && false){
Serial.println("ADC Pin:" + String(this->ADCPin) + " Value: " + String(value) + " Low Pass: " + String(this->lowPassADCRead));
}
uint16_t value = analogRead(cube.ADCPin);
uint16_t lowPassADCRead = static_cast<uint16_t>((static_cast<float>(cube.lastADCReading) * 0.9) + (static_cast<float>(value) * 0.1));
// temporary definitions to define value ranges:
uint16_t zeroCubesHigh = 4095;
@@ -35,37 +32,22 @@ uint8_t CubeStack::GetNumberCubes(){
uint8_t stackHeight = 0;
if(this->lowPassADCRead >= zeroCubesLow && this->lowPassADCRead <= zeroCubesHigh){
if(lowPassADCRead >= zeroCubesLow && lowPassADCRead <= zeroCubesHigh){
stackHeight = 0;
}
else if(this->lowPassADCRead >= oneCubeLow){
else if(lowPassADCRead >= oneCubeLow){
stackHeight = 1;
}
else if(this->lowPassADCRead >= twoCubesLow){
else if(lowPassADCRead >= twoCubesLow){
stackHeight = 2;
}
else if(this->lowPassADCRead >= threeCubesLow){
else if(lowPassADCRead >= threeCubesLow){
stackHeight = 3;
}
if(this->lastStackHeight != stackHeight){
this->lastStackHeight = stackHeight;
this->SendLEDData();
}
return stackHeight;
}
void CubeStack::SetLEDColors(Color * colors, uint8_t numColors){
// copy the colors into the ledColors array
for(int i = 0; i < numColors; i++){
this->ledColors[i].red = colors[i].red;
this->ledColors[i].green = colors[i].green;
this->ledColors[i].blue = colors[i].blue;
}
this->SendLEDData();
}
void CubeStack::SendLEDData(){
// we always initialize before we do anything because other CubeStacks could be hogging the hardware
// between our writes

View File

@@ -7,10 +7,10 @@
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include "Color.h"
#include "Cube.h"
class CubeStack{
public:
namespace CubeStack{
/**
* @brief Construct a new Cube Stack object
* @param ADCPin the pin that the ADC is connected to
@@ -22,31 +22,10 @@ class CubeStack{
* @brief Returns the number of cubes in the stack
* @return the number of cubes in the stack
*/
uint8_t GetNumberCubes();
/**
* @brief Set the led color array and then send the data to the LED strip
* @param colors the array of colors to set the LEDs to
* @param numColors the number of colors in the array
*/
void SetLEDColors(Color * colors, uint8_t numColors);
uint32_t GetNumberCubes(Cube &cube);
/**
* @brief sends the LED data to the LED strip
*/
void SendLEDData();
private:
uint8_t ADCPin;
// we will probably need a pointer to a fastled object here
Adafruit_NeoPixel blockLights;
uint16_t lowPassADCRead{0};
// store the Color of each LED
Color * ledColors;
uint8_t numLEDs;
uint8_t lastStackHeight{0};
};